Search in sources :

Example 41 with StructStat

use of android.system.StructStat in project android_frameworks_base by ResurrectionRemix.

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();
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) BufferedInputStream(java.io.BufferedInputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) FileInputStream(java.io.FileInputStream)

Example 42 with StructStat

use of android.system.StructStat in project android_frameworks_base by ResurrectionRemix.

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 = Os.stat(mFile.getPath());
    } catch (ErrnoException e) {
        return true;
    }
    synchronized (this) {
        return mStatTimestamp != stat.st_mtime || mStatSize != stat.st_size;
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException)

Example 43 with StructStat

use of android.system.StructStat in project android_frameworks_base by ResurrectionRemix.

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) {
        if (startingPath == null) {
            return;
        } else {
            domainPath = startingPath;
        }
    }
    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);
        }
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) IOException(java.io.IOException) File(java.io.File) LinkedList(java.util.LinkedList)

Example 44 with StructStat

use of android.system.StructStat in project android_frameworks_base by crdroidandroid.

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();
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException)

Example 45 with StructStat

use of android.system.StructStat in project android_frameworks_base by crdroidandroid.

the class PackageManagerTests method assertDirOwnerGroupPermsIfExists.

private void assertDirOwnerGroupPermsIfExists(String reason, int uid, int gid, int perms, String path) {
    if (!new File(path).exists()) {
        return;
    }
    final StructStat stat;
    try {
        stat = 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());
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) File(java.io.File)

Aggregations

StructStat (android.system.StructStat)46 ErrnoException (android.system.ErrnoException)45 File (java.io.File)21 IOException (java.io.IOException)21 FileDescriptor (java.io.FileDescriptor)11 FileOutputStream (java.io.FileOutputStream)10 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)10 ParcelFileDescriptor (android.os.ParcelFileDescriptor)6 BackupDataInput (android.app.backup.BackupDataInput)5 FileBridge (android.os.FileBridge)5 BufferedInputStream (java.io.BufferedInputStream)5 FileInputStream (java.io.FileInputStream)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 Map (java.util.Map)5 WeakHashMap (java.util.WeakHashMap)5 TargetApi (android.annotation.TargetApi)1 DownloadableFile (eu.siacs.conversations.entities.DownloadableFile)1 FileWriterException (eu.siacs.conversations.utils.FileWriterException)1 FileNotFoundException (java.io.FileNotFoundException)1