blob: 29a50ed0975a5bbbd5b115c5a3ed1f9a6eefc706 [file] [log] [blame]
Raghu Gandham86d2fee2015-01-15 11:12:22 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <sys/stat.h>
30#include <sys/types.h>
31#include <sys/syscall.h>
Nikola Veljkovicdaf89112015-02-23 16:14:56 +010032#include <unistd.h>
Raghu Gandham86d2fee2015-01-15 11:12:22 -080033
34struct kernel_stat {
Elliott Hughes469b4182015-06-15 17:49:54 -070035 unsigned int st_dev;
36 unsigned int st_pad0[3];
37 unsigned long st_ino;
38 mode_t st_mode;
39 __u32 st_nlink;
40 uid_t st_uid;
41 gid_t st_gid;
42 unsigned int st_rdev;
43 unsigned int st_pad1[3];
44 __kernel_off_t st_size;
45 unsigned int _st_atime;
46 unsigned int st_atime_nsec;
47 unsigned int _st_mtime;
48 unsigned int st_mtime_nsec;
49 unsigned int _st_ctime;
50 unsigned int st_ctime_nsec;
51 unsigned int st_blksize;
52 unsigned int st_pad2;
53 unsigned long st_blocks;
Raghu Gandham86d2fee2015-01-15 11:12:22 -080054};
55
Elliott Hughes469b4182015-06-15 17:49:54 -070056static void copy_stat(struct stat* st, struct kernel_stat* s) {
Raghu Gandham86d2fee2015-01-15 11:12:22 -080057 st->st_dev = static_cast<dev_t>(s->st_dev);
58 st->st_ino = static_cast<ino_t>(s->st_ino);
59 st->st_mode = static_cast<mode_t>(s->st_mode);
60 st->st_nlink = static_cast<nlink_t>(s->st_nlink);
61 st->st_uid = static_cast<uid_t>(s->st_uid);
62 st->st_gid = static_cast<gid_t>(s->st_gid);
63 st->st_rdev = static_cast<dev_t>(s->st_rdev);
64 st->st_size = static_cast<off_t>(s->st_size);
65 st->st_blksize = static_cast<int>(s->st_blksize);
66 st->st_blocks = static_cast<long>(s->st_blocks);
67 st->st_atim.tv_sec = static_cast<time_t>(s->_st_atime);
68 st->st_atim.tv_nsec = static_cast<long>(s->st_atime_nsec);
69 st->st_mtim.tv_sec = static_cast<time_t>(s->_st_mtime);
70 st->st_mtim.tv_nsec = static_cast<long>(s->st_mtime_nsec);
71 st->st_ctim.tv_sec = static_cast<time_t>(s->_st_ctime);
72 st->st_ctim.tv_nsec = static_cast<long>(s->st_ctime_nsec);
73}
74
Elliott Hughes469b4182015-06-15 17:49:54 -070075int fstat(int fp, struct stat* st) {
Raghu Gandham86d2fee2015-01-15 11:12:22 -080076 kernel_stat s;
Elliott Hughes469b4182015-06-15 17:49:54 -070077 int ret = syscall(__NR_fstat, fp, &s);
78 copy_stat(st, &s);
Raghu Gandham86d2fee2015-01-15 11:12:22 -080079 return ret;
80}
81__strong_alias(fstat64, fstat);
82
Elliott Hughes469b4182015-06-15 17:49:54 -070083int fstatat(int dirfd, const char* pathname, struct stat* buf, int flags) {
Raghu Gandham86d2fee2015-01-15 11:12:22 -080084 kernel_stat s;
Elliott Hughes469b4182015-06-15 17:49:54 -070085 int ret = syscall(__NR_newfstatat, dirfd, pathname, &s, flags);
Raghu Gandham86d2fee2015-01-15 11:12:22 -080086 copy_stat(buf, &s);
87 return ret;
88}
89__strong_alias(fstatat64, fstatat);