Search in sources :

Example 46 with ErrnoException

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

the class UserManagerService method getSerialNumber.

/**
     * Return serial number stored in user directory inode.
     *
     * @return parsed serial number, or -1 if not set
     */
private static int getSerialNumber(File file) throws IOException {
    try {
        final byte[] buf = new byte[256];
        final int len = Os.getxattr(file.getAbsolutePath(), XATTR_SERIAL, buf);
        final String serial = new String(buf, 0, len);
        try {
            return Integer.parseInt(serial);
        } catch (NumberFormatException e) {
            throw new IOException("Bad serial number: " + serial);
        }
    } catch (ErrnoException e) {
        if (e.errno == OsConstants.ENODATA) {
            return -1;
        } else {
            throw e.rethrowAsIOException();
        }
    }
}
Also used : ErrnoException(android.system.ErrnoException) IOException(java.io.IOException)

Example 47 with ErrnoException

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

the class SELinuxPolicyInstallReceiver method applyUpdate.

private void applyUpdate() throws IOException, ErrnoException {
    Slog.i(TAG, "Applying SELinux policy");
    File backup = new File(updateDir.getParentFile(), "backup");
    File current = new File(updateDir.getParentFile(), "current");
    File tmp = new File(updateDir.getParentFile(), "tmp");
    if (current.exists()) {
        deleteRecursive(backup);
        Os.rename(current.getPath(), backup.getPath());
    }
    try {
        Os.rename(tmp.getPath(), current.getPath());
        SystemProperties.set("selinux.reload_policy", "1");
    } catch (ErrnoException e) {
        Slog.e(TAG, "Could not update selinux policy: ", e);
        if (backup.exists()) {
            Os.rename(backup.getPath(), current.getPath());
        }
    }
}
Also used : ErrnoException(android.system.ErrnoException) File(java.io.File)

Example 48 with ErrnoException

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

the class ExifInterface method saveAttributes.

/**
     * Save the tag data into the original image file. This is expensive because it involves
     * copying all the data from one file to another and deleting the old file and renaming the
     * other. It's best to use {@link #setAttribute(String,String)} to set all attributes to write
     * and make a single call rather than multiple calls for each attribute.
     * <p>
     * This method is only supported for JPEG files.
     * </p>
     */
public void saveAttributes() throws IOException {
    if (!mIsSupportedFile || mIsRaw) {
        throw new IOException("ExifInterface only supports saving attributes on JPEG formats.");
    }
    if (mIsInputStream || (mSeekableFileDescriptor == null && mFilename == null)) {
        throw new IOException("ExifInterface does not support saving attributes for the current input.");
    }
    // Keep the thumbnail in memory
    mThumbnailBytes = getThumbnail();
    FileInputStream in = null;
    FileOutputStream out = null;
    File tempFile = null;
    try {
        // Move the original file to temporary file.
        if (mFilename != null) {
            tempFile = new File(mFilename + ".tmp");
            File originalFile = new File(mFilename);
            if (!originalFile.renameTo(tempFile)) {
                throw new IOException("Could'nt rename to " + tempFile.getAbsolutePath());
            }
        } else if (mSeekableFileDescriptor != null) {
            tempFile = File.createTempFile("temp", "jpg");
            Os.lseek(mSeekableFileDescriptor, 0, OsConstants.SEEK_SET);
            in = new FileInputStream(mSeekableFileDescriptor);
            out = new FileOutputStream(tempFile);
            Streams.copy(in, out);
        }
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    } finally {
        IoUtils.closeQuietly(in);
        IoUtils.closeQuietly(out);
    }
    in = null;
    out = null;
    try {
        // Save the new file.
        in = new FileInputStream(tempFile);
        if (mFilename != null) {
            out = new FileOutputStream(mFilename);
        } else if (mSeekableFileDescriptor != null) {
            Os.lseek(mSeekableFileDescriptor, 0, OsConstants.SEEK_SET);
            out = new FileOutputStream(mSeekableFileDescriptor);
        }
        saveJpegAttributes(in, out);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    } finally {
        IoUtils.closeQuietly(in);
        IoUtils.closeQuietly(out);
        tempFile.delete();
    }
    // Discard the thumbnail in memory
    mThumbnailBytes = null;
}
Also used : ErrnoException(android.system.ErrnoException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 49 with ErrnoException

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

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)

Example 50 with ErrnoException

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

the class ExifInterfaceTest method testSaveAttributes_withFileDescriptor.

private void testSaveAttributes_withFileDescriptor(File imageFile, ExpectedValue expectedValue) throws IOException {
    String verboseTag = imageFile.getName();
    FileDescriptor fd = null;
    try {
        fd = Os.open(imageFile.getAbsolutePath(), OsConstants.O_RDWR, 0600);
        ExifInterface exifInterface = new ExifInterface(fd);
        exifInterface.saveAttributes();
        Os.lseek(fd, 0, OsConstants.SEEK_SET);
        exifInterface = new ExifInterface(fd);
        compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
        // Test for modifying one attribute.
        String backupValue = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
        exifInterface.setAttribute(ExifInterface.TAG_MAKE, "abc");
        exifInterface.saveAttributes();
        Os.lseek(fd, 0, OsConstants.SEEK_SET);
        exifInterface = new ExifInterface(fd);
        assertEquals("abc", exifInterface.getAttribute(ExifInterface.TAG_MAKE));
        // Restore the backup value.
        exifInterface.setAttribute(ExifInterface.TAG_MAKE, backupValue);
        exifInterface.saveAttributes();
        Os.lseek(fd, 0, OsConstants.SEEK_SET);
        exifInterface = new ExifInterface(fd);
        compareWithExpectedValue(exifInterface, expectedValue, verboseTag);
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    } finally {
        IoUtils.closeQuietly(fd);
    }
}
Also used : ErrnoException(android.system.ErrnoException) ExifInterface(android.media.ExifInterface) 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