public SharedPreferencesImpl(File file, int mode, Map initialContents, Handler mainHandler) { mFile = file; this.mainHandler = mainHandler; mBackupFile = makeBackupFile(file); mMode = mode; mLoaded = initialContents != null; mMap = initialContents != null ? initialContents : new HashMap<String, Object>(); FileUtils.FileStatus stat = new FileUtils.FileStatus(); if(FileUtils.getFileStatus(file.getPath(), stat)) { mStatTimestamp = stat.mtime; } mListeners = new WeakHashMap<OnSharedPreferenceChangeListener, Object>(); }
public boolean hasFileChangedUnexpectedly() { synchronized (this) { if (mDiskWritesInFlight > 0) { // If we know we caused it, it's not unexpected. if (DEBUG) Log.d(TAG, "disk write in flight, not unexpected."); return false; } } FileUtils.FileStatus stat = new FileUtils.FileStatus(); if (!FileUtils.getFileStatus(mFile.getPath(), stat)) { return true; } synchronized (this) { return mStatTimestamp != stat.mtime || mStatSize != stat.size; } }
private static FileOutputStream createFileOutputStream(File file) { FileOutputStream str = null; try { str = new FileOutputStream(file); } catch (FileNotFoundException e) { File parent = file.getParentFile(); if (!parent.mkdir()) { Log.e(TAG, "Couldn't create directory for SharedPreferences file " + file); return null; } FileUtils.setPermissions( parent.getPath(), FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, -1, -1); try { str = new FileOutputStream(file); } catch (FileNotFoundException e2) { Log.e(TAG, "Couldn't create SharedPreferences file " + file, e2); } } return str; }
private static void setFilePermissionsFromMode(String name, int mode, int extraPermissions) { int perms = FileUtils.S_IRUSR|FileUtils.S_IWUSR |FileUtils.S_IRGRP|FileUtils.S_IWGRP |extraPermissions; if ((mode& Context.MODE_WORLD_READABLE) != 0) { perms |= FileUtils.S_IROTH; } if ((mode& Context.MODE_WORLD_WRITEABLE) != 0) { perms |= FileUtils.S_IWOTH; } if (DEBUG) { Log.i(TAG, "File " + name + ": mode=0x" + Integer.toHexString(mode) + ", perms=0x" + Integer.toHexString(perms)); } FileUtils.setPermissions(name, perms, - 1, - 1); }
public void replace(Map newContents, FileUtils.FileStatus stat) { synchronized (this) { mLoaded = true; if (newContents != null) { mMap = newContents; } if (stat != null) { mStatTimestamp = stat.mtime; mStatSize = stat.size; } } }
/** * Start a new write operation on the file. This returns a FileOutputStream * to which you can write the new file data. The existing file is replaced * with the new data. You <em>must not</em> directly close the given * FileOutputStream; instead call either {@link #finishWrite(FileOutputStream)} * or {@link #failWrite(FileOutputStream)}. * * <p>Note that if another thread is currently performing * a write, this will simply replace whatever that thread is writing * with the new file being written by this thread, and when the other * thread finishes the write the new write operation will no longer be * safe (or will be lost). You must do your own threading protection for * access to AtomicFile. */ public FileOutputStream startWrite() throws IOException { // Rename the current file so it may be used as a backup during the next read if (mBaseName.exists()) { if (!mBackupName.exists()) { if (!mBaseName.renameTo(mBackupName)) { Log.w("AtomicFile", "Couldn't rename file " + mBaseName + " to backup file " + mBackupName); } } else { mBaseName.delete(); } } FileOutputStream str = null; try { str = new FileOutputStream(mBaseName); } catch (FileNotFoundException e) { File parent = mBaseName.getParentFile(); if (!parent.mkdir()) { throw new IOException("Couldn't create directory " + mBaseName); } FileUtils.setPermissions( parent.getPath(), FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH, -1, -1); try { str = new FileOutputStream(mBaseName); } catch (FileNotFoundException e2) { throw new IOException("Couldn't create " + mBaseName); } } return str; }
/** * Call when you have successfully finished writing to the stream * returned by {@link #startWrite()}. This will close, sync, and * commit the new data. The next attempt to read the atomic file * will return the new file stream. */ public void finishWrite(FileOutputStream str) { if (str != null) { FileUtils.sync(str); try { str.close(); mBackupName.delete(); } catch (IOException e) { Log.w("AtomicFile", "finishWrite: Got exception:", e); } } }
/** * Call when you have failed for some reason at writing to the stream * returned by {@link #startWrite()}. This will close the current * write stream, and roll back to the previous state of the file. */ public void failWrite(FileOutputStream str) { if (str != null) { FileUtils.sync(str); try { str.close(); mBaseName.delete(); mBackupName.renameTo(mBaseName); } catch (IOException e) { Log.w("AtomicFile", "failWrite: Got exception:", e); } } }
/** * Called after a successful completion to take any necessary action on the transfered file. */ private void finalizeDestinationFile(State state) { if (state.mFilename != null) { // make sure the file is readable FileUtils.setPermissions(state.mFilename, 0644, -1, -1); } }
/** * Called after a successful completion to take any necessary action on the * downloaded file. */ private void finalizeDestinationFile(State state) throws StopRequest { // make sure the file is readable FileUtils.setPermissions(state.mFilename, 0644, -1, -1); syncDestination(state); }