1 /* 2 * Copyright (C) 2011 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 android.cts.util; 18 19 import java.io.ByteArrayOutputStream; 20 import java.io.File; 21 import java.io.FileOutputStream; 22 import java.io.IOException; 23 import java.io.InputStream; 24 25 /** Bits and pieces copied from hidden API of android.os.FileUtils. */ 26 public class FileUtils { 27 28 public static final int S_IFMT = 0170000; 29 public static final int S_IFSOCK = 0140000; 30 public static final int S_IFLNK = 0120000; 31 public static final int S_IFREG = 0100000; 32 public static final int S_IFBLK = 0060000; 33 public static final int S_IFDIR = 0040000; 34 public static final int S_IFCHR = 0020000; 35 public static final int S_IFIFO = 0010000; 36 37 public static final int S_ISUID = 0004000; 38 public static final int S_ISGID = 0002000; 39 public static final int S_ISVTX = 0001000; 40 41 public static final int S_IRWXU = 00700; 42 public static final int S_IRUSR = 00400; 43 public static final int S_IWUSR = 00200; 44 public static final int S_IXUSR = 00100; 45 46 public static final int S_IRWXG = 00070; 47 public static final int S_IRGRP = 00040; 48 public static final int S_IWGRP = 00020; 49 public static final int S_IXGRP = 00010; 50 51 public static final int S_IRWXO = 00007; 52 public static final int S_IROTH = 00004; 53 public static final int S_IWOTH = 00002; 54 public static final int S_IXOTH = 00001; 55 56 static { 57 System.loadLibrary("cts_jni"); 58 } 59 60 public static class FileStatus { 61 62 public int dev; 63 public int ino; 64 public int mode; 65 public int nlink; 66 public int uid; 67 public int gid; 68 public int rdev; 69 public long size; 70 public int blksize; 71 public long blocks; 72 public long atime; 73 public long mtime; 74 public long ctime; 75 hasModeFlag(int flag)76 public boolean hasModeFlag(int flag) { 77 if (((S_IRWXU | S_IRWXG | S_IRWXO) & flag) != flag) { 78 throw new IllegalArgumentException("Inappropriate flag " + flag); 79 } 80 return (mode & flag) == flag; 81 } 82 isOfType(int type)83 public boolean isOfType(int type) { 84 if ((type & S_IFMT) != type) { 85 throw new IllegalArgumentException("Unknown type " + type); 86 } 87 return (mode & S_IFMT) == type; 88 } 89 } 90 91 /** 92 * @param path of the file to stat 93 * @param status object to set the fields on 94 * @param statLinks or don't stat links (lstat vs stat) 95 * @return whether or not we were able to stat the file 96 */ getFileStatus(String path, FileStatus status, boolean statLinks)97 public native static boolean getFileStatus(String path, FileStatus status, boolean statLinks); 98 getUserName(int uid)99 public native static String getUserName(int uid); 100 getGroupName(int gid)101 public native static String getGroupName(int gid); 102 setPermissions(String file, int mode)103 public native static int setPermissions(String file, int mode); 104 105 /** 106 * Copy data from a source stream to destFile. 107 * Return true if succeed, return false if failed. 108 */ copyToFile(InputStream inputStream, File destFile)109 public static boolean copyToFile(InputStream inputStream, File destFile) { 110 try { 111 if (destFile.exists()) { 112 destFile.delete(); 113 } 114 FileOutputStream out = new FileOutputStream(destFile); 115 try { 116 byte[] buffer = new byte[4096]; 117 int bytesRead; 118 while ((bytesRead = inputStream.read(buffer)) >= 0) { 119 out.write(buffer, 0, bytesRead); 120 } 121 } finally { 122 out.flush(); 123 try { 124 out.getFD().sync(); 125 } catch (IOException e) { 126 } 127 out.close(); 128 } 129 return true; 130 } catch (IOException e) { 131 return false; 132 } 133 } 134 createFile(File file, int numBytes)135 public static void createFile(File file, int numBytes) throws IOException { 136 File parentFile = file.getParentFile(); 137 if (parentFile != null) { 138 parentFile.mkdirs(); 139 } 140 byte[] buffer = new byte[numBytes]; 141 FileOutputStream output = new FileOutputStream(file); 142 try { 143 output.write(buffer); 144 } finally { 145 output.close(); 146 } 147 } 148 readInputStreamFully(InputStream is)149 public static byte[] readInputStreamFully(InputStream is) { 150 ByteArrayOutputStream os = new ByteArrayOutputStream(); 151 byte[] buffer = new byte[32768]; 152 int count; 153 try { 154 while ((count = is.read(buffer)) != -1) { 155 os.write(buffer, 0, count); 156 } 157 is.close(); 158 } catch (IOException e) { 159 throw new RuntimeException(e); 160 } 161 return os.toByteArray(); 162 } 163 } 164