Search in sources :

Example 1 with StructStat

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());
        }
    }
}
Also used : StructStat(libcore.io.StructStat) ErrnoException(libcore.io.ErrnoException) IOException(java.io.IOException) File(java.io.File) LinkedList(java.util.LinkedList)

Example 2 with StructStat

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();
}
Also used : StructStat(libcore.io.StructStat) ErrnoException(libcore.io.ErrnoException) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) 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 3 with StructStat

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

Example 4 with StructStat

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

Example 5 with StructStat

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);
}
Also used : StructStat(libcore.io.StructStat) ErrnoException(libcore.io.ErrnoException) FileOutputStream(java.io.FileOutputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Aggregations

ErrnoException (libcore.io.ErrnoException)9 StructStat (libcore.io.StructStat)9 IOException (java.io.IOException)5 File (java.io.File)3 LinkedList (java.util.LinkedList)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 ComponentName (android.content.ComponentName)1 PackageCleanItem (android.content.pm.PackageCleanItem)1 PackageParser (android.content.pm.PackageParser)1 Signature (android.content.pm.Signature)1 ResolverActivity (com.android.internal.app.ResolverActivity)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 WeakHashMap (java.util.WeakHashMap)1 ZipFile (java.util.zip.ZipFile)1