Search in sources :

Example 91 with AtomicFile

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

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

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

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

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

the class UserManagerService method writeApplicationRestrictionsLP.

private void writeApplicationRestrictionsLP(String packageName, Bundle restrictions, int userId) {
    AtomicFile restrictionsFile = new AtomicFile(new File(Environment.getUserSystemDirectory(userId), packageToRestrictionsFileName(packageName)));
    writeApplicationRestrictionsLP(restrictions, restrictionsFile);
}
Also used : AtomicFile(android.util.AtomicFile) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 94 with AtomicFile

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

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)

Example 95 with AtomicFile

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

the class UserManagerServiceTest method testWriteReadApplicationRestrictions.

public void testWriteReadApplicationRestrictions() throws IOException {
    AtomicFile atomicFile = new AtomicFile(restrictionsFile);
    Bundle bundle = createBundle();
    UserManagerService.writeApplicationRestrictionsLP(bundle, atomicFile);
    assertTrue(atomicFile.getBaseFile().exists());
    String s = FileUtils.readTextFile(restrictionsFile, 10000, "");
    System.out.println("restrictionsFile: " + s);
    bundle = UserManagerService.readApplicationRestrictionsLP(atomicFile);
    System.out.println("readApplicationRestrictionsLocked bundle: " + bundle);
    assertBundle(bundle);
}
Also used : AtomicFile(android.util.AtomicFile) Bundle(android.os.Bundle)

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