Search in sources :

Example 16 with StructStat

use of android.system.StructStat in project Conversations by siacs.

the class FileBackend method weOwnFileLollipop.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean weOwnFileLollipop(Uri uri) {
    try {
        File file = new File(uri.getPath());
        FileDescriptor fd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY).getFileDescriptor();
        StructStat st = Os.fstat(fd);
        return st.st_uid == android.os.Process.myUid();
    } catch (FileNotFoundException e) {
        return false;
    } catch (Exception e) {
        return true;
    }
}
Also used : StructStat(android.system.StructStat) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) DownloadableFile(eu.siacs.conversations.entities.DownloadableFile) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) FileWriterException(eu.siacs.conversations.utils.FileWriterException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) TargetApi(android.annotation.TargetApi)

Example 17 with StructStat

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

the class SharedPreferencesImpl method writeToFile.

// Note: must hold mWritingToDiskLock
private void writeToFile(MemoryCommitResult mcr, boolean isFromSyncCommit) {
    // Rename the current file so it may be used as a backup during the next read
    if (mFile.exists()) {
        boolean needsWrite = false;
        // Only need to write if the disk state is older than this commit
        if (mDiskStateGeneration < mcr.memoryStateGeneration) {
            if (isFromSyncCommit) {
                needsWrite = true;
            } else {
                synchronized (this) {
                    // be persisted.
                    if (mCurrentMemoryStateGeneration == mcr.memoryStateGeneration) {
                        needsWrite = true;
                    }
                }
            }
        }
        if (!needsWrite) {
            if (DEBUG) {
                Log.d(TAG, "skipped " + mcr.memoryStateGeneration + " -> " + mFile.getName());
            }
            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);
        if (DEBUG) {
            Log.d(TAG, "wrote " + mcr.memoryStateGeneration + " -> " + mFile.getName());
        }
        str.close();
        ContextImpl.setFilePermissionsFromMode(mFile.getPath(), mMode, 0);
        try {
            final StructStat stat = 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();
        mDiskStateGeneration = mcr.memoryStateGeneration;
        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(android.system.StructStat) ErrnoException(android.system.ErrnoException) FileOutputStream(java.io.FileOutputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 18 with StructStat

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

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 19 with StructStat

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

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 20 with StructStat

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

the class LocalTransport method performBackup.

@Override
public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data) {
    if (DEBUG) {
        try {
            StructStat ss = Os.fstat(data.getFileDescriptor());
            Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName + " size=" + ss.st_size);
        } catch (ErrnoException e) {
            Log.w(TAG, "Unable to stat input file in performBackup() on " + packageInfo.packageName);
        }
    }
    File packageDir = new File(mCurrentSetIncrementalDir, packageInfo.packageName);
    packageDir.mkdirs();
    // Each 'record' in the restore set is kept in its own file, named by
    // the record key.  Wind through the data file, extracting individual
    // record operations and building a set of all the updates to apply
    // in this update.
    BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor());
    try {
        int bufSize = 512;
        byte[] buf = new byte[bufSize];
        while (changeSet.readNextHeader()) {
            String key = changeSet.getKey();
            String base64Key = new String(Base64.encode(key.getBytes()));
            File entityFile = new File(packageDir, base64Key);
            int dataSize = changeSet.getDataSize();
            if (DEBUG)
                Log.v(TAG, "Got change set key=" + key + " size=" + dataSize + " key64=" + base64Key);
            if (dataSize >= 0) {
                if (entityFile.exists()) {
                    entityFile.delete();
                }
                FileOutputStream entity = new FileOutputStream(entityFile);
                if (dataSize > bufSize) {
                    bufSize = dataSize;
                    buf = new byte[bufSize];
                }
                changeSet.readEntityData(buf, 0, dataSize);
                if (DEBUG) {
                    try {
                        long cur = Os.lseek(data.getFileDescriptor(), 0, SEEK_CUR);
                        Log.v(TAG, "  read entity data; new pos=" + cur);
                    } catch (ErrnoException e) {
                        Log.w(TAG, "Unable to stat input file in performBackup() on " + packageInfo.packageName);
                    }
                }
                try {
                    entity.write(buf, 0, dataSize);
                } catch (IOException e) {
                    Log.e(TAG, "Unable to update key file " + entityFile.getAbsolutePath());
                    return TRANSPORT_ERROR;
                } finally {
                    entity.close();
                }
            } else {
                entityFile.delete();
            }
        }
        return TRANSPORT_OK;
    } catch (IOException e) {
        // oops, something went wrong.  abort the operation and return error.
        Log.v(TAG, "Exception reading backup input:", e);
        return TRANSPORT_ERROR;
    }
}
Also used : BackupDataInput(android.app.backup.BackupDataInput) StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) 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