Search in sources :

Example 66 with AtomicFile

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

the class ShortcutService method saveUserLocked.

private void saveUserLocked(@UserIdInt int userId) {
    final File path = getUserFile(userId);
    if (DEBUG) {
        Slog.d(TAG, "Saving to " + path);
    }
    path.getParentFile().mkdirs();
    final AtomicFile file = new AtomicFile(path);
    FileOutputStream os = null;
    try {
        os = file.startWrite();
        saveUserInternalLocked(userId, os, /* forBackup= */
        false);
        file.finishWrite(os);
        // Remove all dangling bitmap files.
        cleanupDanglingBitmapDirectoriesLocked(userId);
    } catch (XmlPullParserException | IOException e) {
        Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e);
        file.failWrite(os);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) File(java.io.File) AtomicFile(android.util.AtomicFile)

Example 67 with AtomicFile

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

the class ShortcutService method getBaseStateFile.

/** Return the base state file name */
private AtomicFile getBaseStateFile() {
    final File path = new File(injectSystemDataPath(), FILENAME_BASE_STATE);
    path.mkdirs();
    return new AtomicFile(path);
}
Also used : AtomicFile(android.util.AtomicFile) File(java.io.File) AtomicFile(android.util.AtomicFile)

Example 68 with AtomicFile

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

the class UserManagerService method writeUserListLP.

/*
     * Writes the user list file in this format:
     *
     * <users nextSerialNumber="3">
     *   <user id="0"></user>
     *   <user id="2"></user>
     * </users>
     */
private void writeUserListLP() {
    if (DBG) {
        debug("writeUserList");
    }
    FileOutputStream fos = null;
    AtomicFile userListFile = new AtomicFile(mUserListFile);
    try {
        fos = userListFile.startWrite();
        final BufferedOutputStream bos = new BufferedOutputStream(fos);
        // XmlSerializer serializer = XmlUtils.serializerInstance();
        final XmlSerializer serializer = new FastXmlSerializer();
        serializer.setOutput(bos, StandardCharsets.UTF_8.name());
        serializer.startDocument(null, true);
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startTag(null, TAG_USERS);
        serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
        serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
        serializer.startTag(null, TAG_GUEST_RESTRICTIONS);
        synchronized (mGuestRestrictions) {
            UserRestrictionsUtils.writeRestrictions(serializer, mGuestRestrictions, TAG_RESTRICTIONS);
        }
        serializer.endTag(null, TAG_GUEST_RESTRICTIONS);
        synchronized (mRestrictionsLock) {
            UserRestrictionsUtils.writeRestrictions(serializer, mDevicePolicyGlobalUserRestrictions, TAG_DEVICE_POLICY_RESTRICTIONS);
        }
        serializer.startTag(null, TAG_GLOBAL_RESTRICTION_OWNER_ID);
        serializer.attribute(null, ATTR_ID, Integer.toString(mGlobalRestrictionOwnerUserId));
        serializer.endTag(null, TAG_GLOBAL_RESTRICTION_OWNER_ID);
        int[] userIdsToWrite;
        synchronized (mUsersLock) {
            userIdsToWrite = new int[mUsers.size()];
            for (int i = 0; i < userIdsToWrite.length; i++) {
                UserInfo user = mUsers.valueAt(i).info;
                userIdsToWrite[i] = user.id;
            }
        }
        for (int id : userIdsToWrite) {
            serializer.startTag(null, TAG_USER);
            serializer.attribute(null, ATTR_ID, Integer.toString(id));
            serializer.endTag(null, TAG_USER);
        }
        serializer.endTag(null, TAG_USERS);
        serializer.endDocument();
        userListFile.finishWrite(fos);
    } catch (Exception e) {
        userListFile.failWrite(fos);
        Slog.e(LOG_TAG, "Error writing user list");
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) UserInfo(android.content.pm.UserInfo) BufferedOutputStream(java.io.BufferedOutputStream) ErrnoException(android.system.ErrnoException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer)

Example 69 with AtomicFile

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

the class UserManagerService method readUserListLP.

private void readUserListLP() {
    if (!mUserListFile.exists()) {
        fallbackToSingleUserLP();
        return;
    }
    FileInputStream fis = null;
    AtomicFile userListFile = new AtomicFile(mUserListFile);
    try {
        fis = userListFile.openRead();
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(fis, StandardCharsets.UTF_8.name());
        int type;
        while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT) {
        // Skip
        }
        if (type != XmlPullParser.START_TAG) {
            Slog.e(LOG_TAG, "Unable to read user list");
            fallbackToSingleUserLP();
            return;
        }
        mNextSerialNumber = -1;
        if (parser.getName().equals(TAG_USERS)) {
            String lastSerialNumber = parser.getAttributeValue(null, ATTR_NEXT_SERIAL_NO);
            if (lastSerialNumber != null) {
                mNextSerialNumber = Integer.parseInt(lastSerialNumber);
            }
            String versionNumber = parser.getAttributeValue(null, ATTR_USER_VERSION);
            if (versionNumber != null) {
                mUserVersion = Integer.parseInt(versionNumber);
            }
        }
        final Bundle newDevicePolicyGlobalUserRestrictions = new Bundle();
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
            if (type == XmlPullParser.START_TAG) {
                final String name = parser.getName();
                if (name.equals(TAG_USER)) {
                    String id = parser.getAttributeValue(null, ATTR_ID);
                    UserData userData = readUserLP(Integer.parseInt(id));
                    if (userData != null) {
                        synchronized (mUsersLock) {
                            mUsers.put(userData.info.id, userData);
                            if (mNextSerialNumber < 0 || mNextSerialNumber <= userData.info.id) {
                                mNextSerialNumber = userData.info.id + 1;
                            }
                        }
                    }
                } else if (name.equals(TAG_GUEST_RESTRICTIONS)) {
                    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.END_TAG) {
                        if (type == XmlPullParser.START_TAG) {
                            if (parser.getName().equals(TAG_RESTRICTIONS)) {
                                synchronized (mGuestRestrictions) {
                                    UserRestrictionsUtils.readRestrictions(parser, mGuestRestrictions);
                                }
                            } else if (parser.getName().equals(TAG_DEVICE_POLICY_RESTRICTIONS)) {
                                UserRestrictionsUtils.readRestrictions(parser, newDevicePolicyGlobalUserRestrictions);
                            }
                            break;
                        }
                    }
                } else if (name.equals(TAG_GLOBAL_RESTRICTION_OWNER_ID)) {
                    String ownerUserId = parser.getAttributeValue(null, ATTR_ID);
                    if (ownerUserId != null) {
                        mGlobalRestrictionOwnerUserId = Integer.parseInt(ownerUserId);
                    }
                }
            }
        }
        synchronized (mRestrictionsLock) {
            mDevicePolicyGlobalUserRestrictions = newDevicePolicyGlobalUserRestrictions;
        }
        updateUserIds();
        upgradeIfNecessaryLP();
    } catch (IOException | XmlPullParserException e) {
        fallbackToSingleUserLP();
    } finally {
        IoUtils.closeQuietly(fis);
    }
}
Also used : AtomicFile(android.util.AtomicFile) Bundle(android.os.Bundle) PersistableBundle(android.os.PersistableBundle) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 70 with AtomicFile

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

the class ShortcutService method loadUserLocked.

@Nullable
private ShortcutUser loadUserLocked(@UserIdInt int userId) {
    final File path = getUserFile(userId);
    if (DEBUG) {
        Slog.d(TAG, "Loading from " + path);
    }
    final AtomicFile file = new AtomicFile(path);
    final FileInputStream in;
    try {
        in = file.openRead();
    } catch (FileNotFoundException e) {
        if (DEBUG) {
            Slog.d(TAG, "Not found " + path);
        }
        return null;
    }
    try {
        final ShortcutUser ret = loadUserInternal(userId, in, /* forBackup= */
        false);
        return ret;
    } catch (IOException | XmlPullParserException | InvalidFileFormatException e) {
        Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e);
        return null;
    } finally {
        IoUtils.closeQuietly(in);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) File(java.io.File) AtomicFile(android.util.AtomicFile) FileInputStream(java.io.FileInputStream) Nullable(android.annotation.Nullable)

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