Search in sources :

Example 36 with StructStat

use of android.system.StructStat in project android_frameworks_base by ResurrectionRemix.

the class FileUtils method copyPermissions.

public static void copyPermissions(File from, File to) throws IOException {
    try {
        final StructStat stat = Os.stat(from.getAbsolutePath());
        Os.chmod(to.getAbsolutePath(), stat.st_mode);
        Os.chown(to.getAbsolutePath(), stat.st_uid, stat.st_gid);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException)

Example 37 with StructStat

use of android.system.StructStat in project android_frameworks_base by ResurrectionRemix.

the class PackageManagerTests method assertDirOwnerGroupPermsIfExists.

private void assertDirOwnerGroupPermsIfExists(String reason, int uid, int gid, int perms, String path) {
    if (!new File(path).exists()) {
        return;
    }
    final StructStat stat;
    try {
        stat = Os.lstat(path);
    } catch (ErrnoException e) {
        throw new AssertionError(reason + "\n" + "Got: " + path + " does not exist");
    }
    StringBuilder sb = new StringBuilder();
    if (!S_ISDIR(stat.st_mode)) {
        sb.append("\nExpected type: ");
        sb.append(S_IFDIR);
        sb.append("\ngot type: ");
        sb.append((stat.st_mode & S_IFMT));
    }
    if (stat.st_uid != uid) {
        sb.append("\nExpected owner: ");
        sb.append(uid);
        sb.append("\nGot owner: ");
        sb.append(stat.st_uid);
    }
    if (stat.st_gid != gid) {
        sb.append("\nExpected group: ");
        sb.append(gid);
        sb.append("\nGot group: ");
        sb.append(stat.st_gid);
    }
    if ((stat.st_mode & ~S_IFMT) != perms) {
        sb.append("\nExpected permissions: ");
        sb.append(Integer.toOctalString(perms));
        sb.append("\nGot permissions: ");
        sb.append(Integer.toOctalString(stat.st_mode & ~S_IFMT));
    }
    if (sb.length() > 0) {
        throw new AssertionError(reason + sb.toString());
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) File(java.io.File)

Example 38 with StructStat

use of android.system.StructStat in project android_frameworks_base by ResurrectionRemix.

the class PinnerService method pinFile.

/** mlock length bytes of fileToPin in memory, starting at offset
     *  length == 0 means pin from offset to end of file
     *  maxSize == 0 means infinite
     */
private static PinnedFile pinFile(String fileToPin, long offset, long length, long maxSize) {
    FileDescriptor fd = new FileDescriptor();
    try {
        fd = Os.open(fileToPin, OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW, OsConstants.O_RDONLY);
        StructStat sb = Os.fstat(fd);
        if (offset + length > sb.st_size) {
            Os.close(fd);
            Slog.e(TAG, "Failed to pin file " + fileToPin + ", request extends beyond end of file.  offset + length =  " + (offset + length) + ", file length = " + sb.st_size);
            return null;
        }
        if (length == 0) {
            length = sb.st_size - offset;
        }
        if (maxSize > 0 && length > maxSize) {
            Slog.e(TAG, "Could not pin file " + fileToPin + ", size = " + length + ", maxSize = " + maxSize);
            Os.close(fd);
            return null;
        }
        long address = Os.mmap(0, length, OsConstants.PROT_READ, OsConstants.MAP_PRIVATE, fd, offset);
        Os.close(fd);
        Os.mlock(address, length);
        return new PinnedFile(address, length, fileToPin);
    } catch (ErrnoException e) {
        Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage());
        if (fd.valid()) {
            try {
                Os.close(fd);
            } catch (ErrnoException eClose) {
                Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());
            }
        }
        return null;
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) FileDescriptor(java.io.FileDescriptor)

Example 39 with StructStat

use of android.system.StructStat in project android_frameworks_base by ResurrectionRemix.

the class LocalTransport method performBackup.

@Override
public int performBackup(PackageInfo packageInfo, ParcelFileDescriptor data) {
    if (DEBUG) {
        try {
            StructStat ss = Os.fstat(data.getFileDescriptor());
            Log.v(TAG, "performBackup() pkg=" + packageInfo.packageName + " size=" + ss.st_size);
        } catch (ErrnoException e) {
            Log.w(TAG, "Unable to stat input file in performBackup() on " + packageInfo.packageName);
        }
    }
    File packageDir = new File(mCurrentSetIncrementalDir, packageInfo.packageName);
    packageDir.mkdirs();
    // Each 'record' in the restore set is kept in its own file, named by
    // the record key.  Wind through the data file, extracting individual
    // record operations and building a set of all the updates to apply
    // in this update.
    BackupDataInput changeSet = new BackupDataInput(data.getFileDescriptor());
    try {
        int bufSize = 512;
        byte[] buf = new byte[bufSize];
        while (changeSet.readNextHeader()) {
            String key = changeSet.getKey();
            String base64Key = new String(Base64.encode(key.getBytes()));
            File entityFile = new File(packageDir, base64Key);
            int dataSize = changeSet.getDataSize();
            if (DEBUG)
                Log.v(TAG, "Got change set key=" + key + " size=" + dataSize + " key64=" + base64Key);
            if (dataSize >= 0) {
                if (entityFile.exists()) {
                    entityFile.delete();
                }
                FileOutputStream entity = new FileOutputStream(entityFile);
                if (dataSize > bufSize) {
                    bufSize = dataSize;
                    buf = new byte[bufSize];
                }
                changeSet.readEntityData(buf, 0, dataSize);
                if (DEBUG) {
                    try {
                        long cur = Os.lseek(data.getFileDescriptor(), 0, SEEK_CUR);
                        Log.v(TAG, "  read entity data; new pos=" + cur);
                    } catch (ErrnoException e) {
                        Log.w(TAG, "Unable to stat input file in performBackup() on " + packageInfo.packageName);
                    }
                }
                try {
                    entity.write(buf, 0, dataSize);
                } catch (IOException e) {
                    Log.e(TAG, "Unable to update key file " + entityFile.getAbsolutePath());
                    return TRANSPORT_ERROR;
                } finally {
                    entity.close();
                }
            } else {
                entityFile.delete();
            }
        }
        return TRANSPORT_OK;
    } catch (IOException e) {
        // oops, something went wrong.  abort the operation and return error.
        Log.v(TAG, "Exception reading backup input:", e);
        return TRANSPORT_ERROR;
    }
}
Also used : BackupDataInput(android.app.backup.BackupDataInput) StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 40 with StructStat

use of android.system.StructStat in project android_frameworks_base by DirtyUnicorns.

the class PackageManagerTests method assertDirOwnerGroupPermsIfExists.

private void assertDirOwnerGroupPermsIfExists(String reason, int uid, int gid, int perms, String path) {
    if (!new File(path).exists()) {
        return;
    }
    final StructStat stat;
    try {
        stat = Os.lstat(path);
    } catch (ErrnoException e) {
        throw new AssertionError(reason + "\n" + "Got: " + path + " does not exist");
    }
    StringBuilder sb = new StringBuilder();
    if (!S_ISDIR(stat.st_mode)) {
        sb.append("\nExpected type: ");
        sb.append(S_IFDIR);
        sb.append("\ngot type: ");
        sb.append((stat.st_mode & S_IFMT));
    }
    if (stat.st_uid != uid) {
        sb.append("\nExpected owner: ");
        sb.append(uid);
        sb.append("\nGot owner: ");
        sb.append(stat.st_uid);
    }
    if (stat.st_gid != gid) {
        sb.append("\nExpected group: ");
        sb.append(gid);
        sb.append("\nGot group: ");
        sb.append(stat.st_gid);
    }
    if ((stat.st_mode & ~S_IFMT) != perms) {
        sb.append("\nExpected permissions: ");
        sb.append(Integer.toOctalString(perms));
        sb.append("\nGot permissions: ");
        sb.append(Integer.toOctalString(stat.st_mode & ~S_IFMT));
    }
    if (sb.length() > 0) {
        throw new AssertionError(reason + sb.toString());
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) File(java.io.File)

Aggregations

StructStat (android.system.StructStat)46 ErrnoException (android.system.ErrnoException)45 File (java.io.File)21 IOException (java.io.IOException)21 FileDescriptor (java.io.FileDescriptor)11 FileOutputStream (java.io.FileOutputStream)10 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)10 ParcelFileDescriptor (android.os.ParcelFileDescriptor)6 BackupDataInput (android.app.backup.BackupDataInput)5 FileBridge (android.os.FileBridge)5 BufferedInputStream (java.io.BufferedInputStream)5 FileInputStream (java.io.FileInputStream)5 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 Map (java.util.Map)5 WeakHashMap (java.util.WeakHashMap)5 TargetApi (android.annotation.TargetApi)1 DownloadableFile (eu.siacs.conversations.entities.DownloadableFile)1 FileWriterException (eu.siacs.conversations.utils.FileWriterException)1 FileNotFoundException (java.io.FileNotFoundException)1