Search in sources :

Example 91 with ErrnoException

use of android.system.ErrnoException in project android_frameworks_base by crdroidandroid.

the class PackageInstallerSession method openWriteInternal.

private ParcelFileDescriptor openWriteInternal(String name, long offsetBytes, long lengthBytes) throws IOException {
    // Quick sanity check of state, and allocate a pipe for ourselves. We
    // then do heavy disk allocation outside the lock, but this open pipe
    // will block any attempted install transitions.
    final FileBridge bridge;
    synchronized (mLock) {
        assertPreparedAndNotSealed("openWrite");
        bridge = new FileBridge();
        mBridges.add(bridge);
    }
    try {
        // Use installer provided name for now; we always rename later
        if (!FileUtils.isValidExtFilename(name)) {
            throw new IllegalArgumentException("Invalid name: " + name);
        }
        final File target;
        final long identity = Binder.clearCallingIdentity();
        try {
            target = new File(resolveStageDir(), name);
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
        // TODO: this should delegate to DCS so the system process avoids
        // holding open FDs into containers.
        final FileDescriptor targetFd = Libcore.os.open(target.getAbsolutePath(), O_CREAT | O_WRONLY, 0644);
        Os.chmod(target.getAbsolutePath(), 0644);
        // cache space to grow, if needed.
        if (lengthBytes > 0) {
            final StructStat stat = Libcore.os.fstat(targetFd);
            final long deltaBytes = lengthBytes - stat.st_size;
            // Only need to free up space when writing to internal stage
            if (stageDir != null && deltaBytes > 0) {
                mPm.freeStorage(params.volumeUuid, deltaBytes);
            }
            try {
                Libcore.os.posix_fallocate(targetFd, 0, lengthBytes);
            } catch (ErrnoException e) {
                if (e.errno == OsConstants.ENOTSUP) {
                    Libcore.os.ftruncate(targetFd, lengthBytes);
                } else {
                    throw e.rethrowAsIOException();
                }
            }
        }
        if (offsetBytes > 0) {
            Libcore.os.lseek(targetFd, offsetBytes, OsConstants.SEEK_SET);
        }
        bridge.setTargetFile(targetFd);
        bridge.start();
        return new ParcelFileDescriptor(bridge.getClientSocket());
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) FileBridge(android.os.FileBridge) ParcelFileDescriptor(android.os.ParcelFileDescriptor) File(java.io.File) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Example 92 with ErrnoException

use of android.system.ErrnoException in project android_frameworks_base by crdroidandroid.

the class PackageInstallerSession method openReadInternal.

private ParcelFileDescriptor openReadInternal(String name) throws IOException {
    assertPreparedAndNotSealed("openRead");
    try {
        if (!FileUtils.isValidExtFilename(name)) {
            throw new IllegalArgumentException("Invalid name: " + name);
        }
        final File target = new File(resolveStageDir(), name);
        final FileDescriptor targetFd = Libcore.os.open(target.getAbsolutePath(), O_RDONLY, 0);
        return new ParcelFileDescriptor(targetFd);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
Also used : ErrnoException(android.system.ErrnoException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) File(java.io.File) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Example 93 with ErrnoException

use of android.system.ErrnoException in project android_frameworks_base by crdroidandroid.

the class PackageInstallerSession method createRemoveSplitMarker.

private void createRemoveSplitMarker(String splitName) throws IOException {
    try {
        final String markerName = splitName + REMOVE_SPLIT_MARKER_EXTENSION;
        if (!FileUtils.isValidExtFilename(markerName)) {
            throw new IllegalArgumentException("Invalid marker: " + markerName);
        }
        final File target = new File(resolveStageDir(), markerName);
        target.createNewFile();
        Os.chmod(target.getAbsolutePath(), 0);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
Also used : ErrnoException(android.system.ErrnoException) File(java.io.File)

Example 94 with ErrnoException

use of android.system.ErrnoException in project android_frameworks_base by crdroidandroid.

the class PackageInstallerService method prepareStageDir.

static void prepareStageDir(File stageDir) throws IOException {
    if (stageDir.exists()) {
        throw new IOException("Session dir already exists: " + stageDir);
    }
    try {
        Os.mkdir(stageDir.getAbsolutePath(), 0755);
        Os.chmod(stageDir.getAbsolutePath(), 0755);
    } catch (ErrnoException e) {
        // This purposefully throws if directory already exists
        throw new IOException("Failed to prepare session dir: " + stageDir, e);
    }
    if (!SELinux.restorecon(stageDir)) {
        throw new IOException("Failed to restorecon session dir: " + stageDir);
    }
}
Also used : ErrnoException(android.system.ErrnoException) IOException(java.io.IOException)

Example 95 with ErrnoException

use of android.system.ErrnoException in project android_frameworks_base by crdroidandroid.

the class ExifInterfaceTest method testExifInterfaceCommon.

private void testExifInterfaceCommon(File imageFile, ExpectedValue expectedValue) throws IOException {
    String verboseTag = imageFile.getName();
    // Creates via path.
    ExifInterface exifInterface = new ExifInterface(imageFile.getAbsolutePath());
    compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
    // Creates from an asset file.
    InputStream in = null;
    try {
        in = mContext.getAssets().open(imageFile.getName());
        exifInterface = new ExifInterface(in);
        compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
    } finally {
        IoUtils.closeQuietly(in);
    }
    // Creates via InputStream.
    in = null;
    try {
        in = new BufferedInputStream(new FileInputStream(imageFile.getAbsolutePath()));
        exifInterface = new ExifInterface(in);
        compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
    } finally {
        IoUtils.closeQuietly(in);
    }
    // Creates via FileDescriptor.
    FileDescriptor fd = null;
    try {
        fd = Os.open(imageFile.getAbsolutePath(), OsConstants.O_RDONLY, 0600);
        exifInterface = new ExifInterface(fd);
        compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    } finally {
        IoUtils.closeQuietly(fd);
    }
}
Also used : ErrnoException(android.system.ErrnoException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ExifInterface(android.media.ExifInterface) FileInputStream(java.io.FileInputStream) FileDescriptor(java.io.FileDescriptor)

Aggregations

ErrnoException (android.system.ErrnoException)215 IOException (java.io.IOException)99 FileDescriptor (java.io.FileDescriptor)95 File (java.io.File)74 StructStat (android.system.StructStat)45 FileInputStream (java.io.FileInputStream)40 FileOutputStream (java.io.FileOutputStream)29 ParcelFileDescriptor (android.os.ParcelFileDescriptor)17 SocketException (java.net.SocketException)17 BufferedInputStream (java.io.BufferedInputStream)15 InputStream (java.io.InputStream)11 AssetFileDescriptor (android.content.res.AssetFileDescriptor)10 ExifInterface (android.media.ExifInterface)10 StructLinger (android.system.StructLinger)10 StructTimeval (android.system.StructTimeval)10 FileNotFoundException (java.io.FileNotFoundException)10 StructPollfd (android.system.StructPollfd)9 InterruptedIOException (java.io.InterruptedIOException)9 PacketSocketAddress (android.system.PacketSocketAddress)8 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)8