Search in sources :

Example 66 with RandomAccessFile

use of java.io.RandomAccessFile in project android_frameworks_base by ResurrectionRemix.

the class BackupManagerService method parseLeftoverJournals.

private void parseLeftoverJournals() {
    for (File f : mJournalDir.listFiles()) {
        if (mJournal == null || f.compareTo(mJournal) != 0) {
            // This isn't the current journal, so it must be a leftover.  Read
            // out the package names mentioned there and schedule them for
            // backup.
            RandomAccessFile in = null;
            try {
                Slog.i(TAG, "Found stale backup journal, scheduling");
                in = new RandomAccessFile(f, "r");
                while (true) {
                    String packageName = in.readUTF();
                    if (MORE_DEBUG)
                        Slog.i(TAG, "  " + packageName);
                    dataChangedImpl(packageName);
                }
            } catch (EOFException e) {
            // no more data; we're done
            } catch (Exception e) {
                Slog.e(TAG, "Can't read " + f, e);
            } finally {
                // close/delete the file
                try {
                    if (in != null)
                        in.close();
                } catch (IOException e) {
                }
                f.delete();
            }
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) EOFException(java.io.EOFException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) AtomicFile(android.util.AtomicFile) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) RemoteException(android.os.RemoteException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) ErrnoException(android.system.ErrnoException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) ActivityNotFoundException(android.content.ActivityNotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) BadPaddingException(javax.crypto.BadPaddingException)

Example 67 with RandomAccessFile

use of java.io.RandomAccessFile in project android_frameworks_base by ResurrectionRemix.

the class BackupManagerService method logBackupComplete.

// Called from the backup tasks: record that the given app has been successfully
// backed up at least once.  This includes both key/value and full-data backups
// through the transport.
void logBackupComplete(String packageName) {
    if (packageName.equals(PACKAGE_MANAGER_SENTINEL))
        return;
    synchronized (mEverStoredApps) {
        if (!mEverStoredApps.add(packageName))
            return;
        RandomAccessFile out = null;
        try {
            out = new RandomAccessFile(mEverStored, "rws");
            out.seek(out.length());
            out.writeUTF(packageName);
        } catch (IOException e) {
            Slog.e(TAG, "Can't log backup of " + packageName + " to " + mEverStored);
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException)

Example 68 with RandomAccessFile

use of java.io.RandomAccessFile in project android_frameworks_base by ResurrectionRemix.

the class BackupManagerService method removeEverBackedUp.

// Remove our awareness of having ever backed up the given package
void removeEverBackedUp(String packageName) {
    if (DEBUG)
        Slog.v(TAG, "Removing backed-up knowledge of " + packageName);
    if (MORE_DEBUG)
        Slog.v(TAG, "New set:");
    synchronized (mEverStoredApps) {
        // Rewrite the file and rename to overwrite.  If we reboot in the middle,
        // we'll recognize on initialization time that the package no longer
        // exists and fix it up then.
        File tempKnownFile = new File(mBaseStateDir, "processed.new");
        RandomAccessFile known = null;
        try {
            known = new RandomAccessFile(tempKnownFile, "rws");
            mEverStoredApps.remove(packageName);
            for (String s : mEverStoredApps) {
                known.writeUTF(s);
                if (MORE_DEBUG)
                    Slog.v(TAG, "    " + s);
            }
            known.close();
            known = null;
            if (!tempKnownFile.renameTo(mEverStored)) {
                throw new IOException("Can't rename " + tempKnownFile + " to " + mEverStored);
            }
        } catch (IOException e) {
            // Bad: we couldn't create the new copy.  For safety's sake we
            // abandon the whole process and remove all what's-backed-up
            // state entirely, meaning we'll force a backup pass for every
            // participant on the next boot or [re]install.
            Slog.w(TAG, "Error rewriting " + mEverStored, e);
            mEverStoredApps.clear();
            tempKnownFile.delete();
            mEverStored.delete();
        } finally {
            try {
                if (known != null)
                    known.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) AtomicFile(android.util.AtomicFile)

Example 69 with RandomAccessFile

use of java.io.RandomAccessFile in project android_frameworks_base by ResurrectionRemix.

the class BackupManagerService method initPackageTracking.

private void initPackageTracking() {
    if (MORE_DEBUG)
        Slog.v(TAG, "` tracking");
    // Remember our ancestral dataset
    mTokenFile = new File(mBaseStateDir, "ancestral");
    try {
        RandomAccessFile tf = new RandomAccessFile(mTokenFile, "r");
        int version = tf.readInt();
        if (version == CURRENT_ANCESTRAL_RECORD_VERSION) {
            mAncestralToken = tf.readLong();
            mCurrentToken = tf.readLong();
            int numPackages = tf.readInt();
            if (numPackages >= 0) {
                mAncestralPackages = new HashSet<String>();
                for (int i = 0; i < numPackages; i++) {
                    String pkgName = tf.readUTF();
                    mAncestralPackages.add(pkgName);
                }
            }
        }
        tf.close();
    } catch (FileNotFoundException fnf) {
        // Probably innocuous
        Slog.v(TAG, "No ancestral data");
    } catch (IOException e) {
        Slog.w(TAG, "Unable to read token file", e);
    }
    // Keep a log of what apps we've ever backed up.  Because we might have
    // rebooted in the middle of an operation that was removing something from
    // this log, we sanity-check its contents here and reconstruct it.
    mEverStored = new File(mBaseStateDir, "processed");
    File tempProcessedFile = new File(mBaseStateDir, "processed.new");
    // Ignore it -- we'll validate "processed" against the current package set.
    if (tempProcessedFile.exists()) {
        tempProcessedFile.delete();
    }
    // file to continue the recordkeeping.
    if (mEverStored.exists()) {
        RandomAccessFile temp = null;
        RandomAccessFile in = null;
        try {
            temp = new RandomAccessFile(tempProcessedFile, "rws");
            in = new RandomAccessFile(mEverStored, "r");
            // Loop until we hit EOF
            while (true) {
                String pkg = in.readUTF();
                try {
                    // is this package still present?
                    mPackageManager.getPackageInfo(pkg, 0);
                    // if we get here then yes it is; remember it
                    mEverStoredApps.add(pkg);
                    temp.writeUTF(pkg);
                    if (MORE_DEBUG)
                        Slog.v(TAG, "   + " + pkg);
                } catch (NameNotFoundException e) {
                    // nope, this package was uninstalled; don't include it
                    if (MORE_DEBUG)
                        Slog.v(TAG, "   - " + pkg);
                }
            }
        } catch (EOFException e) {
            // old one with the new one then reopen the file for continuing use.
            if (!tempProcessedFile.renameTo(mEverStored)) {
                Slog.e(TAG, "Error renaming " + tempProcessedFile + " to " + mEverStored);
            }
        } catch (IOException e) {
            Slog.e(TAG, "Error in processed file", e);
        } finally {
            try {
                if (temp != null)
                    temp.close();
            } catch (IOException e) {
            }
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
    }
    synchronized (mQueueLock) {
        // Resume the full-data backup queue
        mFullBackupQueue = readFullBackupSchedule();
    }
    // Register for broadcasts about package install, etc., so we can
    // update the provider list.
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_PACKAGE_ADDED);
    filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    filter.addDataScheme("package");
    mContext.registerReceiver(mBroadcastReceiver, filter);
    // Register for events related to sdcard installation.
    IntentFilter sdFilter = new IntentFilter();
    sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
    sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
    mContext.registerReceiver(mBroadcastReceiver, sdFilter);
}
Also used : IntentFilter(android.content.IntentFilter) RandomAccessFile(java.io.RandomAccessFile) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) FileNotFoundException(java.io.FileNotFoundException) EOFException(java.io.EOFException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) AtomicFile(android.util.AtomicFile)

Example 70 with RandomAccessFile

use of java.io.RandomAccessFile in project android_frameworks_base by ResurrectionRemix.

the class BackupManagerService method writeRestoreTokens.

// Persistently record the current and ancestral backup tokens as well
// as the set of packages with data [supposedly] available in the
// ancestral dataset.
void writeRestoreTokens() {
    try {
        RandomAccessFile af = new RandomAccessFile(mTokenFile, "rwd");
        // First, the version number of this record, for futureproofing
        af.writeInt(CURRENT_ANCESTRAL_RECORD_VERSION);
        // Write the ancestral and current tokens
        af.writeLong(mAncestralToken);
        af.writeLong(mCurrentToken);
        // Now write the set of ancestral packages
        if (mAncestralPackages == null) {
            af.writeInt(-1);
        } else {
            af.writeInt(mAncestralPackages.size());
            if (DEBUG)
                Slog.v(TAG, "Ancestral packages:  " + mAncestralPackages.size());
            for (String pkgName : mAncestralPackages) {
                af.writeUTF(pkgName);
                if (MORE_DEBUG)
                    Slog.v(TAG, "   " + pkgName);
            }
        }
        af.close();
    } catch (IOException e) {
        Slog.w(TAG, "Unable to write token file:", e);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException)

Aggregations

RandomAccessFile (java.io.RandomAccessFile)1607 IOException (java.io.IOException)761 File (java.io.File)691 FileChannel (java.nio.channels.FileChannel)284 ByteBuffer (java.nio.ByteBuffer)174 FileNotFoundException (java.io.FileNotFoundException)167 Test (org.junit.Test)152 FileOutputStream (java.io.FileOutputStream)91 FileLock (java.nio.channels.FileLock)91 MappedByteBuffer (java.nio.MappedByteBuffer)83 InputStream (java.io.InputStream)68 FileInputStream (java.io.FileInputStream)63 EOFException (java.io.EOFException)55 ArrayList (java.util.ArrayList)45 ByteArrayOutputStream (java.io.ByteArrayOutputStream)39 BufferedInputStream (java.io.BufferedInputStream)35 ByteArrayInputStream (java.io.ByteArrayInputStream)35 Random (java.util.Random)33 HashMap (java.util.HashMap)24 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)23