FileUtils.java revision 21de56a94668e0fda1b8bb4ee4f99a09b40d28fd
1/* 2 * Copyright (C) 2006 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 17package android.os; 18 19import android.util.Log; 20import android.util.Slog; 21 22import libcore.io.ErrnoException; 23import libcore.io.Libcore; 24 25import java.io.BufferedInputStream; 26import java.io.ByteArrayOutputStream; 27import java.io.File; 28import java.io.FileDescriptor; 29import java.io.FileInputStream; 30import java.io.FileNotFoundException; 31import java.io.FileOutputStream; 32import java.io.FileWriter; 33import java.io.IOException; 34import java.io.InputStream; 35import java.util.Arrays; 36import java.util.Comparator; 37import java.util.regex.Pattern; 38import java.util.zip.CRC32; 39import java.util.zip.CheckedInputStream; 40 41/** 42 * Tools for managing files. Not for public consumption. 43 * @hide 44 */ 45public class FileUtils { 46 private static final String TAG = "FileUtils"; 47 48 public static final int S_IRWXU = 00700; 49 public static final int S_IRUSR = 00400; 50 public static final int S_IWUSR = 00200; 51 public static final int S_IXUSR = 00100; 52 53 public static final int S_IRWXG = 00070; 54 public static final int S_IRGRP = 00040; 55 public static final int S_IWGRP = 00020; 56 public static final int S_IXGRP = 00010; 57 58 public static final int S_IRWXO = 00007; 59 public static final int S_IROTH = 00004; 60 public static final int S_IWOTH = 00002; 61 public static final int S_IXOTH = 00001; 62 63 /** Regular expression for safe filenames: no spaces or metacharacters */ 64 private static final Pattern SAFE_FILENAME_PATTERN = Pattern.compile("[\\w%+,./=_-]+"); 65 66 /** 67 * Set owner and mode of of given {@link File}. 68 * 69 * @param mode to apply through {@code chmod} 70 * @param uid to apply through {@code chown}, or -1 to leave unchanged 71 * @param gid to apply through {@code chown}, or -1 to leave unchanged 72 * @return 0 on success, otherwise errno. 73 */ 74 public static int setPermissions(File path, int mode, int uid, int gid) { 75 return setPermissions(path.getAbsolutePath(), mode, uid, gid); 76 } 77 78 /** 79 * Set owner and mode of of given path. 80 * 81 * @param mode to apply through {@code chmod} 82 * @param uid to apply through {@code chown}, or -1 to leave unchanged 83 * @param gid to apply through {@code chown}, or -1 to leave unchanged 84 * @return 0 on success, otherwise errno. 85 */ 86 public static int setPermissions(String path, int mode, int uid, int gid) { 87 try { 88 Libcore.os.chmod(path, mode); 89 } catch (ErrnoException e) { 90 Slog.w(TAG, "Failed to chmod(" + path + "): " + e); 91 return e.errno; 92 } 93 94 if (uid >= 0 || gid >= 0) { 95 try { 96 Libcore.os.chown(path, uid, gid); 97 } catch (ErrnoException e) { 98 Slog.w(TAG, "Failed to chown(" + path + "): " + e); 99 return e.errno; 100 } 101 } 102 103 return 0; 104 } 105 106 /** 107 * Set owner and mode of of given {@link FileDescriptor}. 108 * 109 * @param mode to apply through {@code chmod} 110 * @param uid to apply through {@code chown}, or -1 to leave unchanged 111 * @param gid to apply through {@code chown}, or -1 to leave unchanged 112 * @return 0 on success, otherwise errno. 113 */ 114 public static int setPermissions(FileDescriptor fd, int mode, int uid, int gid) { 115 try { 116 Libcore.os.fchmod(fd, mode); 117 } catch (ErrnoException e) { 118 Slog.w(TAG, "Failed to fchmod(): " + e); 119 return e.errno; 120 } 121 122 if (uid >= 0 || gid >= 0) { 123 try { 124 Libcore.os.fchown(fd, uid, gid); 125 } catch (ErrnoException e) { 126 Slog.w(TAG, "Failed to fchown(): " + e); 127 return e.errno; 128 } 129 } 130 131 return 0; 132 } 133 134 /** 135 * Return owning UID of given path, otherwise -1. 136 */ 137 public static int getUid(String path) { 138 try { 139 return Libcore.os.stat(path).st_uid; 140 } catch (ErrnoException e) { 141 return -1; 142 } 143 } 144 145 /** 146 * Perform an fsync on the given FileOutputStream. The stream at this 147 * point must be flushed but not yet closed. 148 */ 149 public static boolean sync(FileOutputStream stream) { 150 try { 151 if (stream != null) { 152 stream.getFD().sync(); 153 } 154 return true; 155 } catch (IOException e) { 156 } 157 return false; 158 } 159 160 // copy a file from srcFile to destFile, return true if succeed, return 161 // false if fail 162 public static boolean copyFile(File srcFile, File destFile) { 163 boolean result = false; 164 try { 165 InputStream in = new FileInputStream(srcFile); 166 try { 167 result = copyToFile(in, destFile); 168 } finally { 169 in.close(); 170 } 171 } catch (IOException e) { 172 result = false; 173 } 174 return result; 175 } 176 177 /** 178 * Copy data from a source stream to destFile. 179 * Return true if succeed, return false if failed. 180 */ 181 public static boolean copyToFile(InputStream inputStream, File destFile) { 182 try { 183 if (destFile.exists()) { 184 destFile.delete(); 185 } 186 FileOutputStream out = new FileOutputStream(destFile); 187 try { 188 byte[] buffer = new byte[4096]; 189 int bytesRead; 190 while ((bytesRead = inputStream.read(buffer)) >= 0) { 191 out.write(buffer, 0, bytesRead); 192 } 193 } finally { 194 out.flush(); 195 try { 196 out.getFD().sync(); 197 } catch (IOException e) { 198 } 199 out.close(); 200 } 201 return true; 202 } catch (IOException e) { 203 return false; 204 } 205 } 206 207 /** 208 * Check if a filename is "safe" (no metacharacters or spaces). 209 * @param file The file to check 210 */ 211 public static boolean isFilenameSafe(File file) { 212 // Note, we check whether it matches what's known to be safe, 213 // rather than what's known to be unsafe. Non-ASCII, control 214 // characters, etc. are all unsafe by default. 215 return SAFE_FILENAME_PATTERN.matcher(file.getPath()).matches(); 216 } 217 218 /** 219 * Read a text file into a String, optionally limiting the length. 220 * @param file to read (will not seek, so things like /proc files are OK) 221 * @param max length (positive for head, negative of tail, 0 for no limit) 222 * @param ellipsis to add of the file was truncated (can be null) 223 * @return the contents of the file, possibly truncated 224 * @throws IOException if something goes wrong reading the file 225 */ 226 public static String readTextFile(File file, int max, String ellipsis) throws IOException { 227 InputStream input = new FileInputStream(file); 228 // wrapping a BufferedInputStream around it because when reading /proc with unbuffered 229 // input stream, bytes read not equal to buffer size is not necessarily the correct 230 // indication for EOF; but it is true for BufferedInputStream due to its implementation. 231 BufferedInputStream bis = new BufferedInputStream(input); 232 try { 233 long size = file.length(); 234 if (max > 0 || (size > 0 && max == 0)) { // "head" mode: read the first N bytes 235 if (size > 0 && (max == 0 || size < max)) max = (int) size; 236 byte[] data = new byte[max + 1]; 237 int length = bis.read(data); 238 if (length <= 0) return ""; 239 if (length <= max) return new String(data, 0, length); 240 if (ellipsis == null) return new String(data, 0, max); 241 return new String(data, 0, max) + ellipsis; 242 } else if (max < 0) { // "tail" mode: keep the last N 243 int len; 244 boolean rolled = false; 245 byte[] last = null; 246 byte[] data = null; 247 do { 248 if (last != null) rolled = true; 249 byte[] tmp = last; last = data; data = tmp; 250 if (data == null) data = new byte[-max]; 251 len = bis.read(data); 252 } while (len == data.length); 253 254 if (last == null && len <= 0) return ""; 255 if (last == null) return new String(data, 0, len); 256 if (len > 0) { 257 rolled = true; 258 System.arraycopy(last, len, last, 0, last.length - len); 259 System.arraycopy(data, 0, last, last.length - len, len); 260 } 261 if (ellipsis == null || !rolled) return new String(last); 262 return ellipsis + new String(last); 263 } else { // "cat" mode: size unknown, read it all in streaming fashion 264 ByteArrayOutputStream contents = new ByteArrayOutputStream(); 265 int len; 266 byte[] data = new byte[1024]; 267 do { 268 len = bis.read(data); 269 if (len > 0) contents.write(data, 0, len); 270 } while (len == data.length); 271 return contents.toString(); 272 } 273 } finally { 274 bis.close(); 275 input.close(); 276 } 277 } 278 279 /** 280 * Writes string to file. Basically same as "echo -n $string > $filename" 281 * 282 * @param filename 283 * @param string 284 * @throws IOException 285 */ 286 public static void stringToFile(String filename, String string) throws IOException { 287 FileWriter out = new FileWriter(filename); 288 try { 289 out.write(string); 290 } finally { 291 out.close(); 292 } 293 } 294 295 /** 296 * Computes the checksum of a file using the CRC32 checksum routine. 297 * The value of the checksum is returned. 298 * 299 * @param file the file to checksum, must not be null 300 * @return the checksum value or an exception is thrown. 301 */ 302 public static long checksumCrc32(File file) throws FileNotFoundException, IOException { 303 CRC32 checkSummer = new CRC32(); 304 CheckedInputStream cis = null; 305 306 try { 307 cis = new CheckedInputStream( new FileInputStream(file), checkSummer); 308 byte[] buf = new byte[128]; 309 while(cis.read(buf) >= 0) { 310 // Just read for checksum to get calculated. 311 } 312 return checkSummer.getValue(); 313 } finally { 314 if (cis != null) { 315 try { 316 cis.close(); 317 } catch (IOException e) { 318 } 319 } 320 } 321 } 322 323 /** 324 * Delete older files in a directory until only those matching the given 325 * constraints remain. 326 * 327 * @param minCount Always keep at least this many files. 328 * @param minAge Always keep files younger than this age. 329 * @return if any files were deleted. 330 */ 331 public static boolean deleteOlderFiles(File dir, int minCount, long minAge) { 332 if (minCount < 0 || minAge < 0) { 333 throw new IllegalArgumentException("Constraints must be positive or 0"); 334 } 335 336 final File[] files = dir.listFiles(); 337 if (files == null) return false; 338 339 // Sort with newest files first 340 Arrays.sort(files, new Comparator<File>() { 341 @Override 342 public int compare(File lhs, File rhs) { 343 return (int) (rhs.lastModified() - lhs.lastModified()); 344 } 345 }); 346 347 // Keep at least minCount files 348 boolean deleted = false; 349 for (int i = minCount; i < files.length; i++) { 350 final File file = files[i]; 351 352 // Keep files newer than minAge 353 final long age = System.currentTimeMillis() - file.lastModified(); 354 if (age > minAge) { 355 if (file.delete()) { 356 Log.d(TAG, "Deleted old file " + file); 357 deleted = true; 358 } 359 } 360 } 361 return deleted; 362 } 363 364 /** 365 * Test if a file lives under the given directory, either as a direct child 366 * or a distant grandchild. 367 * <p> 368 * Both files <em>must</em> have been resolved using 369 * {@link File#getCanonicalFile()} to avoid symlink or path traversal 370 * attacks. 371 */ 372 public static boolean contains(File dir, File file) { 373 String dirPath = dir.getAbsolutePath(); 374 String filePath = file.getAbsolutePath(); 375 376 if (dirPath.equals(filePath)) { 377 return true; 378 } 379 380 if (!dirPath.endsWith("/")) { 381 dirPath += "/"; 382 } 383 return filePath.startsWith(dirPath); 384 } 385} 386