Home | History | Annotate | Download | only in bionic
      1 /*
      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>
     32 #include <unistd.h>
     33 
     34 struct kernel_stat {
     35   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;
     54 };
     55 
     56 static void copy_stat(struct stat* st, struct kernel_stat* s) {
     57   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 
     75 int fstat(int fp, struct stat* st) {
     76   kernel_stat s;
     77   int ret = syscall(__NR_fstat, fp, &s);
     78   copy_stat(st, &s);
     79   return ret;
     80 }
     81 __strong_alias(fstat64, fstat);
     82 
     83 int fstatat(int dirfd, const char* pathname, struct stat* buf, int flags) {
     84    kernel_stat s;
     85    int ret = syscall(__NR_newfstatat, dirfd, pathname, &s, flags);
     86    copy_stat(buf, &s);
     87    return ret;
     88 }
     89 __strong_alias(fstatat64, fstatat);
     90