Search in sources :

Example 46 with AtomicFile

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

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 47 with AtomicFile

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

the class ShortcutService method loadBaseStateLocked.

private void loadBaseStateLocked() {
    mRawLastResetTime = 0;
    final AtomicFile file = getBaseStateFile();
    if (DEBUG) {
        Slog.d(TAG, "Loading from " + file.getBaseFile());
    }
    try (FileInputStream in = file.openRead()) {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, StandardCharsets.UTF_8.name());
        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) {
            if (type != XmlPullParser.START_TAG) {
                continue;
            }
            final int depth = parser.getDepth();
            // Check the root tag
            final String tag = parser.getName();
            if (depth == 1) {
                if (!TAG_ROOT.equals(tag)) {
                    Slog.e(TAG, "Invalid root tag: " + tag);
                    return;
                }
                continue;
            }
            // Assume depth == 2
            switch(tag) {
                case TAG_LAST_RESET_TIME:
                    mRawLastResetTime = parseLongAttribute(parser, ATTR_VALUE);
                    break;
                default:
                    Slog.e(TAG, "Invalid tag: " + tag);
                    break;
            }
        }
    } catch (FileNotFoundException e) {
    // Use the default
    } catch (IOException | XmlPullParserException e) {
        Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e);
        mRawLastResetTime = 0;
    }
    // Adjust the last reset time.
    getLastResetTimeLocked();
}
Also used : AtomicFile(android.util.AtomicFile) XmlPullParser(org.xmlpull.v1.XmlPullParser) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 48 with AtomicFile

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

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 49 with AtomicFile

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

the class UserManagerService method readUserLP.

private UserData readUserLP(int id) {
    int flags = 0;
    int serialNumber = id;
    String name = null;
    String account = null;
    String iconPath = null;
    long creationTime = 0L;
    long lastLoggedInTime = 0L;
    String lastLoggedInFingerprint = null;
    int profileGroupId = UserInfo.NO_PROFILE_GROUP_ID;
    int restrictedProfileParentId = UserInfo.NO_PROFILE_GROUP_ID;
    boolean partial = false;
    boolean guestToRemove = false;
    boolean persistSeedData = false;
    String seedAccountName = null;
    String seedAccountType = null;
    PersistableBundle seedAccountOptions = null;
    Bundle baseRestrictions = new Bundle();
    Bundle localRestrictions = new Bundle();
    FileInputStream fis = null;
    try {
        AtomicFile userFile = new AtomicFile(new File(mUsersDir, Integer.toString(id) + XML_SUFFIX));
        fis = userFile.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 " + id);
            return null;
        }
        if (type == XmlPullParser.START_TAG && parser.getName().equals(TAG_USER)) {
            int storedId = readIntAttribute(parser, ATTR_ID, -1);
            if (storedId != id) {
                Slog.e(LOG_TAG, "User id does not match the file name");
                return null;
            }
            serialNumber = readIntAttribute(parser, ATTR_SERIAL_NO, id);
            flags = readIntAttribute(parser, ATTR_FLAGS, 0);
            iconPath = parser.getAttributeValue(null, ATTR_ICON_PATH);
            creationTime = readLongAttribute(parser, ATTR_CREATION_TIME, 0);
            lastLoggedInTime = readLongAttribute(parser, ATTR_LAST_LOGGED_IN_TIME, 0);
            lastLoggedInFingerprint = parser.getAttributeValue(null, ATTR_LAST_LOGGED_IN_FINGERPRINT);
            profileGroupId = readIntAttribute(parser, ATTR_PROFILE_GROUP_ID, UserInfo.NO_PROFILE_GROUP_ID);
            restrictedProfileParentId = readIntAttribute(parser, ATTR_RESTRICTED_PROFILE_PARENT_ID, UserInfo.NO_PROFILE_GROUP_ID);
            String valueString = parser.getAttributeValue(null, ATTR_PARTIAL);
            if ("true".equals(valueString)) {
                partial = true;
            }
            valueString = parser.getAttributeValue(null, ATTR_GUEST_TO_REMOVE);
            if ("true".equals(valueString)) {
                guestToRemove = true;
            }
            seedAccountName = parser.getAttributeValue(null, ATTR_SEED_ACCOUNT_NAME);
            seedAccountType = parser.getAttributeValue(null, ATTR_SEED_ACCOUNT_TYPE);
            if (seedAccountName != null || seedAccountType != null) {
                persistSeedData = true;
            }
            int outerDepth = parser.getDepth();
            while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
                if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
                    continue;
                }
                String tag = parser.getName();
                if (TAG_NAME.equals(tag)) {
                    type = parser.next();
                    if (type == XmlPullParser.TEXT) {
                        name = parser.getText();
                    }
                } else if (TAG_RESTRICTIONS.equals(tag)) {
                    UserRestrictionsUtils.readRestrictions(parser, baseRestrictions);
                } else if (TAG_DEVICE_POLICY_RESTRICTIONS.equals(tag)) {
                    UserRestrictionsUtils.readRestrictions(parser, localRestrictions);
                } else if (TAG_ACCOUNT.equals(tag)) {
                    type = parser.next();
                    if (type == XmlPullParser.TEXT) {
                        account = parser.getText();
                    }
                } else if (TAG_SEED_ACCOUNT_OPTIONS.equals(tag)) {
                    seedAccountOptions = PersistableBundle.restoreFromXml(parser);
                    persistSeedData = true;
                }
            }
        }
        // Create the UserInfo object that gets passed around
        UserInfo userInfo = new UserInfo(id, name, iconPath, flags);
        userInfo.serialNumber = serialNumber;
        userInfo.creationTime = creationTime;
        userInfo.lastLoggedInTime = lastLoggedInTime;
        userInfo.lastLoggedInFingerprint = lastLoggedInFingerprint;
        userInfo.partial = partial;
        userInfo.guestToRemove = guestToRemove;
        userInfo.profileGroupId = profileGroupId;
        userInfo.restrictedProfileParentId = restrictedProfileParentId;
        // Create the UserData object that's internal to this class
        UserData userData = new UserData();
        userData.info = userInfo;
        userData.account = account;
        userData.seedAccountName = seedAccountName;
        userData.seedAccountType = seedAccountType;
        userData.persistSeedData = persistSeedData;
        userData.seedAccountOptions = seedAccountOptions;
        synchronized (mRestrictionsLock) {
            mBaseUserRestrictions.put(id, baseRestrictions);
            mDevicePolicyLocalUserRestrictions.put(id, localRestrictions);
        }
        return userData;
    } catch (IOException ioe) {
    } catch (XmlPullParserException pe) {
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
            }
        }
    }
    return null;
}
Also used : Bundle(android.os.Bundle) PersistableBundle(android.os.PersistableBundle) XmlPullParser(org.xmlpull.v1.XmlPullParser) UserInfo(android.content.pm.UserInfo) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) PersistableBundle(android.os.PersistableBundle) AtomicFile(android.util.AtomicFile) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 50 with AtomicFile

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

the class UserManagerService method removeUserState.

private void removeUserState(final int userHandle) {
    try {
        mContext.getSystemService(StorageManager.class).destroyUserKey(userHandle);
    } catch (IllegalStateException e) {
        // This may be simply because the user was partially created.
        Slog.i(LOG_TAG, "Destroying key for user " + userHandle + " failed, continuing anyway", e);
    }
    // Cleanup gatekeeper secure user id
    try {
        final IGateKeeperService gk = GateKeeper.getService();
        if (gk != null) {
            gk.clearSecureUserId(userHandle);
        }
    } catch (Exception ex) {
        Slog.w(LOG_TAG, "unable to clear GK secure user id");
    }
    // Cleanup package manager settings
    mPm.cleanUpUser(this, userHandle);
    // Clean up all data before removing metadata
    mPm.destroyUserData(userHandle, StorageManager.FLAG_STORAGE_DE | StorageManager.FLAG_STORAGE_CE);
    // Remove this user from the list
    synchronized (mUsersLock) {
        mUsers.remove(userHandle);
        mIsUserManaged.delete(userHandle);
    }
    synchronized (mUserStates) {
        mUserStates.delete(userHandle);
    }
    synchronized (mRestrictionsLock) {
        mBaseUserRestrictions.remove(userHandle);
        mAppliedUserRestrictions.remove(userHandle);
        mCachedEffectiveUserRestrictions.remove(userHandle);
        mDevicePolicyLocalUserRestrictions.remove(userHandle);
    }
    // Update the user list
    synchronized (mPackagesLock) {
        writeUserListLP();
    }
    // Remove user file
    AtomicFile userFile = new AtomicFile(new File(mUsersDir, userHandle + XML_SUFFIX));
    userFile.delete();
    updateUserIds();
}
Also used : IGateKeeperService(android.service.gatekeeper.IGateKeeperService) AtomicFile(android.util.AtomicFile) StorageManager(android.os.storage.StorageManager) AtomicFile(android.util.AtomicFile) File(java.io.File) 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)

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