Home | History | Annotate | Download | only in include
      1 /*
      2  * Copyright 2012, The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef _STAT_PORTABLE_H_
     18 #define _STAT_PORTABLE_H_
     19 
     20 #include <sys/stat.h>
     21 #include <string.h>
     22 
     23 #ifdef __LP64__
     24 
     25 #define __STAT64_BODY_PORTABLE \
     26   unsigned long st_dev; \
     27   unsigned long st_ino; \
     28   unsigned int st_mode; \
     29   unsigned int st_nlink; \
     30   uid_t st_uid; \
     31   gid_t st_gid; \
     32   unsigned long st_rdev; \
     33   unsigned long __pad1; \
     34   long st_size; \
     35   int st_blksize; \
     36   int __pad2; \
     37   long st_blocks; \
     38   long st_atime; \
     39   unsigned long st_atime_nsec; \
     40   long st_mtime; \
     41   unsigned long st_mtime_nsec; \
     42   long st_ctime; \
     43   unsigned long st_ctime_nsec; \
     44   unsigned int __unused4; \
     45   unsigned int __unused5; \
     46   unsigned long __unused_for_largest_size; \
     47 
     48 
     49 struct stat_portable { __STAT64_BODY_PORTABLE };
     50 struct stat64_portable { __STAT64_BODY_PORTABLE };
     51 
     52 static inline
     53 void stat_ntop(struct stat *n_stat, struct stat_portable *p_stat)
     54 {
     55     memset(p_stat, '\0', sizeof(struct stat_portable));
     56     p_stat->st_dev        = n_stat->st_dev;
     57     p_stat->st_ino        = n_stat->st_ino;
     58     p_stat->st_mode       = n_stat->st_mode;
     59     p_stat->st_nlink      = n_stat->st_nlink;
     60     p_stat->st_uid        = n_stat->st_uid;
     61     p_stat->st_gid        = n_stat->st_gid;
     62     p_stat->st_rdev       = n_stat->st_rdev;
     63     p_stat->st_size       = n_stat->st_size;
     64     p_stat->st_blksize    = n_stat->st_blksize;
     65     p_stat->st_blocks     = n_stat->st_blocks;
     66     p_stat->st_atime      = n_stat->st_atime;
     67     p_stat->st_atime_nsec = n_stat->st_atime_nsec;
     68     p_stat->st_mtime      = n_stat->st_mtime;
     69     p_stat->st_mtime_nsec = n_stat->st_mtime_nsec;
     70     p_stat->st_ctime      = n_stat->st_ctime;
     71     p_stat->st_ctime_nsec = n_stat->st_ctime_nsec;
     72 }
     73 
     74 #else // ! __LP64__
     75 
     76 /* It's easy to change kernel to support stat */
     77 struct stat_portable {
     78     unsigned long long  st_dev;
     79     unsigned char       __pad0[4];
     80 
     81     unsigned long       __st_ino;
     82     unsigned int        st_mode;
     83     unsigned int        st_nlink;
     84 
     85     unsigned long       st_uid;
     86     unsigned long       st_gid;
     87 
     88     unsigned long long  st_rdev;
     89     unsigned char       __pad3[4];
     90 
     91     unsigned char       __pad4[4];
     92     long long           st_size;
     93     unsigned long       st_blksize;
     94     unsigned char       __pad5[4];
     95     unsigned long long  st_blocks;
     96 
     97     unsigned long       st_atime;
     98     unsigned long       st_atime_nsec;
     99 
    100     unsigned long       st_mtime;
    101     unsigned long       st_mtime_nsec;
    102 
    103     unsigned long       st_ctime;
    104     unsigned long       st_ctime_nsec;
    105 
    106     unsigned long long  st_ino;
    107 };
    108 
    109 /*
    110 The X86 Version is
    111 struct stat {
    112     unsigned long long  st_dev;
    113     unsigned char       __pad0[4];
    114 
    115     unsigned long       __st_ino;
    116     unsigned int        st_mode;
    117     unsigned int        st_nlink;
    118 
    119     unsigned long       st_uid;
    120     unsigned long       st_gid;
    121 
    122     unsigned long long  st_rdev;
    123     unsigned char       __pad3[4];
    124 
    125     long long           st_size;
    126     unsigned long       st_blksize;
    127     unsigned long long  st_blocks;
    128 
    129     unsigned long       st_atime;
    130     unsigned long       st_atime_nsec;
    131 
    132     unsigned long       st_mtime;
    133     unsigned long       st_mtime_nsec;
    134 
    135     unsigned long       st_ctime;
    136     unsigned long       st_ctime_nsec;
    137 
    138     unsigned long long  st_ino;
    139 };
    140 */
    141 
    142 /*
    143 The MIPS Version is
    144 struct stat {
    145     unsigned long       st_dev;
    146     unsigned long       __pad0[3];
    147 
    148     unsigned long long  st_ino;
    149 
    150     unsigned int        st_mode;
    151     unsigned int        st_nlink;
    152 
    153     unsigned long       st_uid;
    154     unsigned long       st_gid;
    155 
    156     unsigned long       st_rdev;
    157     unsigned long       __pad1[3];
    158 
    159     long long           st_size;
    160 
    161     unsigned long       st_atime;
    162     unsigned long       st_atime_nsec;
    163 
    164     unsigned long       st_mtime;
    165     unsigned long       st_mtime_nsec;
    166 
    167     unsigned long       st_ctime;
    168     unsigned long       st_ctime_nsec;
    169 
    170     unsigned long       st_blksize;
    171     unsigned long       __pad2;
    172 
    173     unsigned long long  st_blocks;
    174 };
    175 */
    176 
    177 static inline void stat_ntop(struct stat *n_stat, struct stat_portable *p_stat)
    178 {
    179     memset(p_stat, '\0', sizeof(struct stat_portable));
    180     p_stat->st_dev        = n_stat->st_dev;
    181 #if defined(__mips__)
    182     /* MIPS doesn't have __st_ino */
    183     p_stat->__st_ino      = 0;
    184 #else
    185     p_stat->__st_ino      = n_stat->__st_ino;
    186 #endif
    187     p_stat->st_mode       = n_stat->st_mode;
    188     p_stat->st_nlink      = n_stat->st_nlink;
    189     p_stat->st_uid        = n_stat->st_uid;
    190     p_stat->st_gid        = n_stat->st_gid;
    191     p_stat->st_rdev       = n_stat->st_rdev;
    192     p_stat->st_size       = n_stat->st_size;
    193     p_stat->st_blksize    = n_stat->st_blksize;
    194     p_stat->st_blocks     = n_stat->st_blocks;
    195     p_stat->st_atime      = n_stat->st_atime;
    196     p_stat->st_atime_nsec = n_stat->st_atime_nsec;
    197     p_stat->st_mtime      = n_stat->st_mtime;
    198     p_stat->st_mtime_nsec = n_stat->st_mtime_nsec;
    199     p_stat->st_ctime      = n_stat->st_ctime;
    200     p_stat->st_ctime_nsec = n_stat->st_ctime_nsec;
    201     p_stat->st_ino        = n_stat->st_ino;
    202 }
    203 
    204 #endif // __LP64__
    205 
    206 #endif /* _STAT_PORTABLE_H */
    207