Search in sources :

Example 11 with AtomicFile

use of android.util.AtomicFile in project platform_frameworks_base by android.

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

use of android.util.AtomicFile in project platform_frameworks_base by android.

the class NetworkStatsCollection method readLegacyNetwork.

@Deprecated
public void readLegacyNetwork(File file) 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_NETWORK_INIT:
                {
                    // network := size *(NetworkIdentitySet NetworkStatsHistory)
                    final int size = in.readInt();
                    for (int i = 0; i < size; i++) {
                        final NetworkIdentitySet ident = new NetworkIdentitySet(in);
                        final NetworkStatsHistory history = new NetworkStatsHistory(in);
                        final Key key = new Key(ident, UID_ALL, SET_ALL, TAG_NONE);
                        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 13 with AtomicFile

use of android.util.AtomicFile in project platform_frameworks_base by android.

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

use of android.util.AtomicFile in project platform_frameworks_base by android.

the class PackageDexUsage 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);
        }
        Slog.e(TAG, "Failed to write usage for dex files", e);
    }
}
Also used : AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException)

Example 15 with AtomicFile

use of android.util.AtomicFile in project platform_frameworks_base by android.

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)

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