use of libcore.io.StructStat in project android_frameworks_base by ParanoidAndroid.
the class BackupAgent method fullBackupFileTree.
/**
* Scan the dir tree (if it actually exists) and process each entry we find. If the
* 'excludes' parameter is non-null, it is consulted each time a new file system entity
* is visited to see whether that entity (and its subtree, if appropriate) should be
* omitted from the backup process.
*
* @hide
*/
protected final void fullBackupFileTree(String packageName, String domain, String rootPath, HashSet<String> excludes, FullBackupDataOutput output) {
File rootFile = new File(rootPath);
if (rootFile.exists()) {
LinkedList<File> scanQueue = new LinkedList<File>();
scanQueue.add(rootFile);
while (scanQueue.size() > 0) {
File file = scanQueue.remove(0);
String filePath;
try {
filePath = file.getCanonicalPath();
// prune this subtree?
if (excludes != null && excludes.contains(filePath)) {
continue;
}
// If it's a directory, enqueue its contents for scanning.
StructStat stat = Libcore.os.lstat(filePath);
if (OsConstants.S_ISLNK(stat.st_mode)) {
if (DEBUG)
Log.i(TAG, "Symlink (skipping)!: " + file);
continue;
} else if (OsConstants.S_ISDIR(stat.st_mode)) {
File[] contents = file.listFiles();
if (contents != null) {
for (File entry : contents) {
scanQueue.add(0, entry);
}
}
}
} catch (IOException e) {
if (DEBUG)
Log.w(TAG, "Error canonicalizing path of " + file);
continue;
} catch (ErrnoException e) {
if (DEBUG)
Log.w(TAG, "Error scanning file " + file + " : " + e);
continue;
}
// Finally, back this file up before proceeding
FullBackup.backupToTar(packageName, domain, null, rootPath, filePath, output.getData());
}
}
}
use of libcore.io.StructStat in project android_frameworks_base by ParanoidAndroid.
the class SharedPreferencesImpl method loadFromDiskLocked.
private void loadFromDiskLocked() {
if (mLoaded) {
return;
}
if (mBackupFile.exists()) {
mFile.delete();
mBackupFile.renameTo(mFile);
}
// Debugging
if (mFile.exists() && !mFile.canRead()) {
Log.w(TAG, "Attempt to read preferences file " + mFile + " without permission");
}
Map map = null;
StructStat stat = null;
try {
stat = Libcore.os.stat(mFile.getPath());
if (mFile.canRead()) {
BufferedInputStream str = null;
try {
str = new BufferedInputStream(new FileInputStream(mFile), 16 * 1024);
map = XmlUtils.readMapXml(str);
} catch (XmlPullParserException e) {
Log.w(TAG, "getSharedPreferences", e);
} catch (FileNotFoundException e) {
Log.w(TAG, "getSharedPreferences", e);
} catch (IOException e) {
Log.w(TAG, "getSharedPreferences", e);
} finally {
IoUtils.closeQuietly(str);
}
}
} catch (ErrnoException e) {
}
mLoaded = true;
if (map != null) {
mMap = map;
mStatTimestamp = stat.st_mtime;
mStatSize = stat.st_size;
} else {
mMap = new HashMap<String, Object>();
}
notifyAll();
}
use of libcore.io.StructStat in project android_frameworks_base by ParanoidAndroid.
the class SharedPreferencesImpl method hasFileChangedUnexpectedly.
// Has the file changed out from under us? i.e. writes that
// we didn't instigate.
private 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;
}
}
final StructStat stat;
try {
/*
* Metadata operations don't usually count as a block guard
* violation, but we explicitly want this one.
*/
BlockGuard.getThreadPolicy().onReadFromDisk();
stat = Libcore.os.stat(mFile.getPath());
} catch (ErrnoException e) {
return true;
}
synchronized (this) {
return mStatTimestamp != stat.st_mtime || mStatSize != stat.st_size;
}
}
use of libcore.io.StructStat in project android_frameworks_base by ParanoidAndroid.
the class PackageManagerTests method assertDirOwnerGroupPerms.
private void assertDirOwnerGroupPerms(String reason, int uid, int gid, int perms, String path) {
final StructStat stat;
try {
stat = Libcore.os.lstat(path);
} catch (ErrnoException e) {
throw new AssertionError(reason + "\n" + "Got: " + path + " does not exist");
}
StringBuilder sb = new StringBuilder();
if (!S_ISDIR(stat.st_mode)) {
sb.append("\nExpected type: ");
sb.append(S_IFDIR);
sb.append("\ngot type: ");
sb.append((stat.st_mode & S_IFMT));
}
if (stat.st_uid != uid) {
sb.append("\nExpected owner: ");
sb.append(uid);
sb.append("\nGot owner: ");
sb.append(stat.st_uid);
}
if (stat.st_gid != gid) {
sb.append("\nExpected group: ");
sb.append(gid);
sb.append("\nGot group: ");
sb.append(stat.st_gid);
}
if ((stat.st_mode & ~S_IFMT) != perms) {
sb.append("\nExpected permissions: ");
sb.append(Integer.toOctalString(perms));
sb.append("\nGot permissions: ");
sb.append(Integer.toOctalString(stat.st_mode & ~S_IFMT));
}
if (sb.length() > 0) {
throw new AssertionError(reason + sb.toString());
}
}
use of libcore.io.StructStat in project android_frameworks_base by ParanoidAndroid.
the class SharedPreferencesImpl method writeToFile.
// Note: must hold mWritingToDiskLock
private void writeToFile(MemoryCommitResult mcr) {
// Rename the current file so it may be used as a backup during the next read
if (mFile.exists()) {
if (!mcr.changesMade) {
// If the file already exists, but no changes were
// made to the underlying map, it's wasteful to
// re-write the file. Return as if we wrote it
// out.
mcr.setDiskWriteResult(true);
return;
}
if (!mBackupFile.exists()) {
if (!mFile.renameTo(mBackupFile)) {
Log.e(TAG, "Couldn't rename file " + mFile + " to backup file " + mBackupFile);
mcr.setDiskWriteResult(false);
return;
}
} else {
mFile.delete();
}
}
// from the backup.
try {
FileOutputStream str = createFileOutputStream(mFile);
if (str == null) {
mcr.setDiskWriteResult(false);
return;
}
XmlUtils.writeMapXml(mcr.mapToWriteToDisk, str);
FileUtils.sync(str);
str.close();
ContextImpl.setFilePermissionsFromMode(mFile.getPath(), mMode, 0);
try {
final StructStat stat = Libcore.os.stat(mFile.getPath());
synchronized (this) {
mStatTimestamp = stat.st_mtime;
mStatSize = stat.st_size;
}
} catch (ErrnoException e) {
// Do nothing
}
// Writing was successful, delete the backup file if there is one.
mBackupFile.delete();
mcr.setDiskWriteResult(true);
return;
} catch (XmlPullParserException e) {
Log.w(TAG, "writeToFile: Got exception:", e);
} catch (IOException e) {
Log.w(TAG, "writeToFile: Got exception:", e);
}
// Clean up an unsuccessfully written file
if (mFile.exists()) {
if (!mFile.delete()) {
Log.e(TAG, "Couldn't clean up partially-written file " + mFile);
}
}
mcr.setDiskWriteResult(false);
}
Aggregations