Search in sources :

Example 76 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by AOSPA.

the class SettingsState method doWriteState.

private void doWriteState() {
    if (DEBUG_PERSISTENCE) {
        Slog.i(LOG_TAG, "[PERSIST START]");
    }
    AtomicFile destination = new AtomicFile(mStatePersistFile);
    final int version;
    final ArrayMap<String, Setting> settings;
    synchronized (mLock) {
        version = mVersion;
        settings = new ArrayMap<>(mSettings);
        mDirty = false;
        mWriteScheduled = false;
    }
    FileOutputStream out = null;
    try {
        out = destination.startWrite();
        XmlSerializer serializer = Xml.newSerializer();
        serializer.setOutput(out, StandardCharsets.UTF_8.name());
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startDocument(null, true);
        serializer.startTag(null, TAG_SETTINGS);
        serializer.attribute(null, ATTR_VERSION, String.valueOf(version));
        final int settingCount = settings.size();
        for (int i = 0; i < settingCount; i++) {
            Setting setting = settings.valueAt(i);
            writeSingleSetting(mVersion, serializer, setting.getId(), setting.getName(), setting.getValue(), setting.getPackageName());
            if (DEBUG_PERSISTENCE) {
                Slog.i(LOG_TAG, "[PERSISTED]" + setting.getName() + "=" + setting.getValue());
            }
        }
        serializer.endTag(null, TAG_SETTINGS);
        serializer.endDocument();
        destination.finishWrite(out);
        synchronized (mLock) {
            addHistoricalOperationLocked(HISTORICAL_OPERATION_PERSIST, null);
        }
        if (DEBUG_PERSISTENCE) {
            Slog.i(LOG_TAG, "[PERSIST END]");
        }
    } catch (Throwable t) {
        Slog.wtf(LOG_TAG, "Failed to write settings, restoring backup", t);
        destination.failWrite(out);
    } finally {
        IoUtils.closeQuietly(out);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) XmlSerializer(org.xmlpull.v1.XmlSerializer)

Example 77 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by AOSPA.

the class NetworkStatsCollection method readLegacyUid.

@Deprecated
public void readLegacyUid(File file, boolean onlyTags) throws IOException {
    final AtomicFile inputFile = new AtomicFile(file);
    DataInputStream in = null;
    try {
        in = new DataInputStream(new BufferedInputStream(inputFile.openRead()));
        // verify file magic header intact
        final int magic = in.readInt();
        if (magic != FILE_MAGIC) {
            throw new ProtocolException("unexpected magic: " + magic);
        }
        final int version = in.readInt();
        switch(version) {
            case VERSION_UID_INIT:
                {
                    // mapping into NetworkIdentitySet.
                    break;
                }
            case VERSION_UID_WITH_IDENT:
                {
                    // for a short time.
                    break;
                }
            case VERSION_UID_WITH_TAG:
            case VERSION_UID_WITH_SET:
                {
                    // uid := size *(NetworkIdentitySet size *(uid set tag NetworkStatsHistory))
                    final int identSize = in.readInt();
                    for (int i = 0; i < identSize; i++) {
                        final NetworkIdentitySet ident = new NetworkIdentitySet(in);
                        final int size = in.readInt();
                        for (int j = 0; j < size; j++) {
                            final int uid = in.readInt();
                            final int set = (version >= VERSION_UID_WITH_SET) ? in.readInt() : SET_DEFAULT;
                            final int tag = in.readInt();
                            final Key key = new Key(ident, uid, set, tag);
                            final NetworkStatsHistory history = new NetworkStatsHistory(in);
                            if ((tag == TAG_NONE) != onlyTags) {
                                recordHistory(key, history);
                            }
                        }
                    }
                    break;
                }
            default:
                {
                    throw new ProtocolException("unexpected version: " + version);
                }
        }
    } catch (FileNotFoundException e) {
    // missing stats is okay, probably first boot
    } finally {
        IoUtils.closeQuietly(in);
    }
}
Also used : ProtocolException(java.net.ProtocolException) AtomicFile(android.util.AtomicFile) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) NetworkStatsHistory(android.net.NetworkStatsHistory) DataInputStream(java.io.DataInputStream)

Example 78 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by AOSPA.

the class CompilerStats method writeInternal.

@Override
protected void writeInternal(Void data) {
    AtomicFile file = getFile();
    FileOutputStream f = null;
    try {
        f = file.startWrite();
        OutputStreamWriter osw = new OutputStreamWriter(f);
        write(osw);
        osw.flush();
        file.finishWrite(f);
    } catch (IOException e) {
        if (f != null) {
            file.failWrite(f);
        }
        Log.e(PackageManagerService.TAG, "Failed to write compiler stats", e);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Example 79 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by AOSPA.

the class EphemeralApplicationRegistry method parseMetadataFile.

private static UninstalledEphemeralAppState parseMetadataFile(File metadataFile) {
    if (!metadataFile.exists()) {
        return null;
    }
    FileInputStream in;
    try {
        in = new AtomicFile(metadataFile).openRead();
    } catch (FileNotFoundException fnfe) {
        Slog.i(LOG_TAG, "No ephemeral metadata file");
        return null;
    }
    final File ephemeralDir = metadataFile.getParentFile();
    final long timestamp = metadataFile.lastModified();
    final String packageName = ephemeralDir.getName();
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, StandardCharsets.UTF_8.name());
        return new UninstalledEphemeralAppState(parseMetadata(parser, packageName), timestamp);
    } catch (XmlPullParserException | IOException e) {
        throw new IllegalStateException("Failed parsing ephemeral" + " metadata file: " + metadataFile, e);
    } finally {
        IoUtils.closeQuietly(in);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileNotFoundException(java.io.FileNotFoundException) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) AtomicFile(android.util.AtomicFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 80 with AtomicFile

use of android.util.AtomicFile in project android_frameworks_base by crdroidandroid.

the class RegisteredServicesCache method createFileForUser.

private AtomicFile createFileForUser(int userId) {
    File userDir = getUserSystemDirectory(userId);
    File userFile = new File(userDir, REGISTERED_SERVICES_DIR + "/" + mInterfaceName + ".xml");
    return new AtomicFile(userFile);
}
Also used : AtomicFile(android.util.AtomicFile) AtomicFile(android.util.AtomicFile) File(java.io.File)

Aggregations

AtomicFile (android.util.AtomicFile)231 IOException (java.io.IOException)135 File (java.io.File)106 FileOutputStream (java.io.FileOutputStream)73 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)70 FileNotFoundException (java.io.FileNotFoundException)61 FileInputStream (java.io.FileInputStream)39 XmlSerializer (org.xmlpull.v1.XmlSerializer)35 XmlPullParser (org.xmlpull.v1.XmlPullParser)33 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)25 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)24 BufferedOutputStream (java.io.BufferedOutputStream)19 BufferedInputStream (java.io.BufferedInputStream)16 UserInfo (android.content.pm.UserInfo)15 Bundle (android.os.Bundle)15 RemoteException (android.os.RemoteException)15 ArrayList (java.util.ArrayList)15 InputStream (java.io.InputStream)14 NetworkStatsHistory (android.net.NetworkStatsHistory)12 ErrnoException (android.system.ErrnoException)12