1 package android.permission.cts; 2 3 /* 4 * Copyright (C) 2010 The Android Open Source Project 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 import com.google.common.primitives.Ints; 20 21 import android.system.OsConstants; 22 23 import java.util.HashSet; 24 import java.util.Set; 25 26 /** Bits and pieces copied from hidden API of android.os.FileUtils. */ 27 public class FileUtils { 28 29 public static final int S_IFMT = 0170000; 30 public static final int S_IFSOCK = 0140000; 31 public static final int S_IFLNK = 0120000; 32 public static final int S_IFREG = 0100000; 33 public static final int S_IFBLK = 0060000; 34 public static final int S_IFDIR = 0040000; 35 public static final int S_IFCHR = 0020000; 36 public static final int S_IFIFO = 0010000; 37 38 public static final int S_ISUID = 0004000; 39 public static final int S_ISGID = 0002000; 40 public static final int S_ISVTX = 0001000; 41 42 public static final int S_IRWXU = 00700; 43 public static final int S_IRUSR = 00400; 44 public static final int S_IWUSR = 00200; 45 public static final int S_IXUSR = 00100; 46 47 public static final int S_IRWXG = 00070; 48 public static final int S_IRGRP = 00040; 49 public static final int S_IWGRP = 00020; 50 public static final int S_IXGRP = 00010; 51 52 public static final int S_IRWXO = 00007; 53 public static final int S_IROTH = 00004; 54 public static final int S_IWOTH = 00002; 55 public static final int S_IXOTH = 00001; 56 57 static { 58 System.loadLibrary("ctspermission_jni"); 59 } 60 61 public static class FileStatus { 62 63 public int dev; 64 public int ino; 65 public int mode; 66 public int nlink; 67 public int uid; 68 public int gid; 69 public int rdev; 70 public long size; 71 public int blksize; 72 public long blocks; 73 public long atime; 74 public long mtime; 75 public long ctime; 76 hasModeFlag(int flag)77 public boolean hasModeFlag(int flag) { 78 if (((S_IRWXU | S_IRWXG | S_IRWXO) & flag) != flag) { 79 throw new IllegalArgumentException("Inappropriate flag " + flag); 80 } 81 return (mode & flag) == flag; 82 } 83 isOfType(int type)84 public boolean isOfType(int type) { 85 if ((type & S_IFMT) != type) { 86 throw new IllegalArgumentException("Unknown type " + type); 87 } 88 return (mode & S_IFMT) == type; 89 } 90 } 91 92 public static class CapabilitySet { 93 94 private final Set<Integer> mCapabilities = new HashSet<Integer>(); 95 add(int capability)96 public CapabilitySet add(int capability) { 97 if ((capability < 0) || (capability > OsConstants.CAP_LAST_CAP)) { 98 throw new IllegalArgumentException(String.format( 99 "capability id %d out of valid range", capability)); 100 } 101 mCapabilities.add(capability); 102 return this; 103 } 104 fileHasOnly(String path, int[] capabilities)105 private native static boolean fileHasOnly(String path, 106 int[] capabilities); 107 fileHasOnly(String path)108 public boolean fileHasOnly(String path) { 109 return fileHasOnly(path, Ints.toArray(mCapabilities)); 110 } 111 } 112 113 /** 114 * @param path of the file to stat 115 * @param status object to set the fields on 116 * @param statLinks or don't stat links (lstat vs stat) 117 * @return whether or not we were able to stat the file 118 */ getFileStatus(String path, FileStatus status, boolean statLinks)119 public native static boolean getFileStatus(String path, FileStatus status, boolean statLinks); 120 getUserName(int uid)121 public native static String getUserName(int uid); 122 getGroupName(int gid)123 public native static String getGroupName(int gid); 124 hasSetUidCapability(String path)125 public native static boolean hasSetUidCapability(String path); 126 hasSetGidCapability(String path)127 public native static boolean hasSetGidCapability(String path); 128 } 129