1 /*
2  * Copyright (C) 2010 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 package com.android.cts.verifier.os;
18 
19 /** Bits and pieces copied from hidden API of android.os.FileUtils. */
20 public class FileUtils {
21 
22     private static final int S_IFSOCK = 0140000;
23     private static final int S_IFLNK = 0120000;
24     private static final int S_IFREG = 0100000;
25     private static final int S_IFBLK = 0060000;
26     private static final int S_IFDIR = 0040000;
27     private static final int S_IFCHR = 0020000;
28     private static final int S_IFIFO = 0010000;
29 
30     private static final int S_ISUID = 0004000;
31     private static final int S_ISGID = 0002000;
32     private static final int S_ISVTX = 0001000;
33 
34     private static final int S_IRUSR = 00400;
35     private static final int S_IWUSR = 00200;
36     private static final int S_IXUSR = 00100;
37 
38     private static final int S_IRGRP = 00040;
39     private static final int S_IWGRP = 00020;
40     private static final int S_IXGRP = 00010;
41 
42     private static final int S_IROTH = 00004;
43     private static final int S_IWOTH = 00002;
44     private static final int S_IXOTH = 00001;
45 
46     static {
47         System.loadLibrary("ctsverifier_jni");
48     }
49 
50     public static class FileStatus {
51 
52         private int dev;
53         private int ino;
54         private int mode;
55         private int nlink;
56         private int uid;
57         private int gid;
58         private int rdev;
59         private long size;
60         private int blksize;
61         private long blocks;
62         private long atime;
63         private long mtime;
64         private long ctime;
65         private boolean executable;
66 
getUid()67         public int getUid() {
68             return uid;
69         }
70 
getGid()71         public int getGid() {
72             return gid;
73         }
74 
getMode()75         public int getMode() {
76             return mode;
77         }
78 
isDirectory()79         public boolean isDirectory() {
80             return hasModeFlag(mode, S_IFDIR);
81         }
82 
isSymbolicLink()83         public boolean isSymbolicLink() {
84             return hasModeFlag(mode, S_IFLNK);
85         }
86 
isSetUid()87         public boolean isSetUid() {
88             return hasModeFlag(mode, S_ISUID);
89         }
90 
isSetGid()91         public boolean isSetGid() {
92             return hasModeFlag(mode, S_ISGID);
93         }
94 
isExecutableByCTS()95         public boolean isExecutableByCTS() {
96             return executable;
97         }
98     }
99 
100     /**
101      * @param path of the file to stat
102      * @param status object to set the fields on
103      * @param statLinks or don't stat links (lstat vs stat)
104      * @return whether or not we were able to stat the file
105      */
getFileStatus(String path, FileStatus status, boolean statLinks)106     public native static boolean getFileStatus(String path, FileStatus status, boolean statLinks);
107 
getUserName(int uid)108     public native static String getUserName(int uid);
109 
getGroupName(int gid)110     public native static String getGroupName(int gid);
111 
112     /** Display the file's mode like "ls -l" does. */
getFormattedPermissions(int mode)113     public static String getFormattedPermissions(int mode) {
114         StringBuilder permissions = new StringBuilder("-rwxrwxrwx");
115 
116         int[] typeMasks = {S_IFSOCK, S_IFLNK, S_IFREG, S_IFBLK, S_IFDIR, S_IFCHR, S_IFIFO};
117         char[] typeSymbols = {'s', 'l', '-', 'b', 'd', 'c', 'p'};
118         for (int i = 0; i < typeMasks.length; i++) {
119             if (hasModeFlag(mode, typeMasks[i])) {
120                 permissions.setCharAt(0, typeSymbols[i]);
121                 break;
122             }
123         }
124 
125         int[] masks = {S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP,
126                 S_IROTH, S_IWOTH, S_IXOTH};
127         for (int i = 0; i < masks.length; i++) {
128             if (!hasModeFlag(mode, masks[i])) {
129                 permissions.setCharAt(1 + i, '-');
130             }
131         }
132 
133 
134         if (hasModeFlag(mode, S_ISUID)) {
135             permissions.setCharAt(3, hasModeFlag(mode, S_IXUSR) ? 's' : 'S');
136         }
137 
138         if (hasModeFlag(mode, S_ISGID)) {
139             permissions.setCharAt(6, hasModeFlag(mode, S_IXGRP) ? 's' : 'S');
140         }
141 
142         if (hasModeFlag(mode, S_ISVTX)) {
143             permissions.setCharAt(9, hasModeFlag(mode, S_IXOTH) ? 't' : 'T');
144         }
145 
146         return permissions.toString();
147     }
148 
hasModeFlag(int mode, int flag)149     private static boolean hasModeFlag(int mode, int flag) {
150         return (mode & flag) == flag;
151     }
152 }
153