use of android.system.StructStat in project android_frameworks_base by DirtyUnicorns.
the class PinnerService method pinFile.
/** mlock length bytes of fileToPin in memory, starting at offset
* length == 0 means pin from offset to end of file
* maxSize == 0 means infinite
*/
private static PinnedFile pinFile(String fileToPin, long offset, long length, long maxSize) {
FileDescriptor fd = new FileDescriptor();
try {
fd = Os.open(fileToPin, OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW, OsConstants.O_RDONLY);
StructStat sb = Os.fstat(fd);
if (offset + length > sb.st_size) {
Os.close(fd);
Slog.e(TAG, "Failed to pin file " + fileToPin + ", request extends beyond end of file. offset + length = " + (offset + length) + ", file length = " + sb.st_size);
return null;
}
if (length == 0) {
length = sb.st_size - offset;
}
if (maxSize > 0 && length > maxSize) {
Slog.e(TAG, "Could not pin file " + fileToPin + ", size = " + length + ", maxSize = " + maxSize);
Os.close(fd);
return null;
}
long address = Os.mmap(0, length, OsConstants.PROT_READ, OsConstants.MAP_PRIVATE, fd, offset);
Os.close(fd);
Os.mlock(address, length);
return new PinnedFile(address, length, fileToPin);
} catch (ErrnoException e) {
Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage());
if (fd.valid()) {
try {
Os.close(fd);
} catch (ErrnoException eClose) {
Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());
}
}
return null;
}
}
use of android.system.StructStat in project android_frameworks_base by DirtyUnicorns.
the class BackupAgent method fullBackupFileTree.
/**
* Scan the dir tree (if it actually exists) and process each entry we find. If the
* 'excludes' parameters are non-null, they are 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.
*
* @param systemExcludes An optional list of excludes.
* @hide
*/
protected final void fullBackupFileTree(String packageName, String domain, String startingPath, ArraySet<String> manifestExcludes, ArraySet<String> systemExcludes, FullBackupDataOutput output) {
// Pull out the domain and set it aside to use when making the tarball.
String domainPath = FullBackup.getBackupScheme(this).tokenToDirectoryPath(domain);
if (domainPath == null) {
// Should never happen.
return;
}
File rootFile = new File(startingPath);
if (rootFile.exists()) {
LinkedList<File> scanQueue = new LinkedList<File>();
scanQueue.add(rootFile);
while (scanQueue.size() > 0) {
File file = scanQueue.remove(0);
String filePath;
try {
// Ignore things that aren't "real" files or dirs
StructStat stat = Os.lstat(file.getPath());
if (!OsConstants.S_ISREG(stat.st_mode) && !OsConstants.S_ISDIR(stat.st_mode)) {
if (DEBUG)
Log.i(TAG, "Not a file/dir (skipping)!: " + file);
continue;
}
// For all other verification, look at the canonicalized path
filePath = file.getCanonicalPath();
// prune this subtree?
if (manifestExcludes != null && manifestExcludes.contains(filePath)) {
continue;
}
if (systemExcludes != null && systemExcludes.contains(filePath)) {
continue;
}
// If it's a directory, enqueue its contents for scanning.
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);
if (Log.isLoggable(FullBackup.TAG_XML_PARSER, Log.VERBOSE)) {
Log.v(FullBackup.TAG_XML_PARSER, "Error canonicalizing path of " + file);
}
continue;
} catch (ErrnoException e) {
if (DEBUG)
Log.w(TAG, "Error scanning file " + file + " : " + e);
if (Log.isLoggable(FullBackup.TAG_XML_PARSER, Log.VERBOSE)) {
Log.v(FullBackup.TAG_XML_PARSER, "Error scanning file " + file + " : " + e);
}
continue;
}
// Finally, back this file up (or measure it) before proceeding
FullBackup.backupToTar(packageName, domain, null, domainPath, filePath, output);
}
}
}
use of android.system.StructStat in project android_frameworks_base by DirtyUnicorns.
the class SharedPreferencesImpl method loadFromDisk.
private void loadFromDisk() {
synchronized (SharedPreferencesImpl.this) {
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 = 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 | IOException e) {
Log.w(TAG, "getSharedPreferences", e);
} finally {
IoUtils.closeQuietly(str);
}
}
} catch (ErrnoException e) {
/* ignore */
}
synchronized (SharedPreferencesImpl.this) {
mLoaded = true;
if (map != null) {
mMap = map;
mStatTimestamp = stat.st_mtime;
mStatSize = stat.st_size;
} else {
mMap = new HashMap<>();
}
notifyAll();
}
}
use of android.system.StructStat in project android_frameworks_base by AOSPA.
the class FileUtils method copyPermissions.
public static void copyPermissions(File from, File to) throws IOException {
try {
final StructStat stat = Os.stat(from.getAbsolutePath());
Os.chmod(to.getAbsolutePath(), stat.st_mode);
Os.chown(to.getAbsolutePath(), stat.st_uid, stat.st_gid);
} catch (ErrnoException e) {
throw e.rethrowAsIOException();
}
}
use of android.system.StructStat in project android_frameworks_base by AOSPA.
the class PinnerService method pinFile.
/** mlock length bytes of fileToPin in memory, starting at offset
* length == 0 means pin from offset to end of file
* maxSize == 0 means infinite
*/
private static PinnedFile pinFile(String fileToPin, long offset, long length, long maxSize) {
FileDescriptor fd = new FileDescriptor();
try {
fd = Os.open(fileToPin, OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW, OsConstants.O_RDONLY);
StructStat sb = Os.fstat(fd);
if (offset + length > sb.st_size) {
Os.close(fd);
Slog.e(TAG, "Failed to pin file " + fileToPin + ", request extends beyond end of file. offset + length = " + (offset + length) + ", file length = " + sb.st_size);
return null;
}
if (length == 0) {
length = sb.st_size - offset;
}
if (maxSize > 0 && length > maxSize) {
Slog.e(TAG, "Could not pin file " + fileToPin + ", size = " + length + ", maxSize = " + maxSize);
Os.close(fd);
return null;
}
long address = Os.mmap(0, length, OsConstants.PROT_READ, OsConstants.MAP_PRIVATE, fd, offset);
Os.close(fd);
Os.mlock(address, length);
return new PinnedFile(address, length, fileToPin);
} catch (ErrnoException e) {
Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage());
if (fd.valid()) {
try {
Os.close(fd);
} catch (ErrnoException eClose) {
Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());
}
}
return null;
}
}
Aggregations