Search in sources :

Example 86 with ErrnoException

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

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 87 with ErrnoException

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

the class PackageInstallerSession method copyFiles.

private static void copyFiles(List<File> fromFiles, File toDir) throws IOException {
    // Remove any partial files from previous attempt
    for (File file : toDir.listFiles()) {
        if (file.getName().endsWith(".tmp")) {
            file.delete();
        }
    }
    for (File fromFile : fromFiles) {
        final File tmpFile = File.createTempFile("inherit", ".tmp", toDir);
        if (LOGD)
            Slog.d(TAG, "Copying " + fromFile + " to " + tmpFile);
        if (!FileUtils.copyFile(fromFile, tmpFile)) {
            throw new IOException("Failed to copy " + fromFile + " to " + tmpFile);
        }
        try {
            Os.chmod(tmpFile.getAbsolutePath(), 0644);
        } catch (ErrnoException e) {
            throw new IOException("Failed to chmod " + tmpFile);
        }
        final File toFile = new File(toDir, fromFile.getName());
        if (LOGD)
            Slog.d(TAG, "Renaming " + tmpFile + " to " + toFile);
        if (!tmpFile.renameTo(toFile)) {
            throw new IOException("Failed to rename " + tmpFile + " to " + toFile);
        }
    }
    Slog.d(TAG, "Copied " + fromFiles.size() + " files into " + toDir);
}
Also used : ErrnoException(android.system.ErrnoException) IOException(java.io.IOException) File(java.io.File)

Example 88 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 89 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 90 with ErrnoException

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

the class IpReachabilityMonitor method probeNeighbor.

/**
     * Make the kernel perform neighbor reachability detection (IPv4 ARP or IPv6 ND)
     * for the given IP address on the specified interface index.
     *
     * @return 0 if the request was successfully passed to the kernel; otherwise return
     *         a non-zero error code.
     */
private static int probeNeighbor(int ifIndex, InetAddress ip) {
    final String msgSnippet = "probing ip=" + ip.getHostAddress() + "%" + ifIndex;
    if (DBG) {
        Log.d(TAG, msgSnippet);
    }
    final byte[] msg = RtNetlinkNeighborMessage.newNewNeighborMessage(1, ip, StructNdMsg.NUD_PROBE, ifIndex, null);
    int errno = -OsConstants.EPROTO;
    try (NetlinkSocket nlSocket = new NetlinkSocket(OsConstants.NETLINK_ROUTE)) {
        final long IO_TIMEOUT = 300L;
        nlSocket.connectToKernel();
        nlSocket.sendMessage(msg, 0, msg.length, IO_TIMEOUT);
        final ByteBuffer bytes = nlSocket.recvMessage(IO_TIMEOUT);
        // recvMessage() guaranteed to not return null if it did not throw.
        final NetlinkMessage response = NetlinkMessage.parse(bytes);
        if (response != null && response instanceof NetlinkErrorMessage && (((NetlinkErrorMessage) response).getNlMsgError() != null)) {
            errno = ((NetlinkErrorMessage) response).getNlMsgError().error;
            if (errno != 0) {
                // TODO: consider ignoring EINVAL (-22), which appears to be
                // normal when probing a neighbor for which the kernel does
                // not already have / no longer has a link layer address.
                Log.e(TAG, "Error " + msgSnippet + ", errmsg=" + response.toString());
            }
        } else {
            String errmsg;
            if (response == null) {
                bytes.position(0);
                errmsg = "raw bytes: " + NetlinkConstants.hexify(bytes);
            } else {
                errmsg = response.toString();
            }
            Log.e(TAG, "Error " + msgSnippet + ", errmsg=" + errmsg);
        }
    } catch (ErrnoException e) {
        Log.e(TAG, "Error " + msgSnippet, e);
        errno = -e.errno;
    } catch (InterruptedIOException e) {
        Log.e(TAG, "Error " + msgSnippet, e);
        errno = -OsConstants.ETIMEDOUT;
    } catch (SocketException e) {
        Log.e(TAG, "Error " + msgSnippet, e);
        errno = -OsConstants.EIO;
    }
    return errno;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) SocketException(java.net.SocketException) NetlinkSocket(android.net.netlink.NetlinkSocket) ErrnoException(android.system.ErrnoException) NetlinkMessage(android.net.netlink.NetlinkMessage) NetlinkErrorMessage(android.net.netlink.NetlinkErrorMessage) ByteBuffer(java.nio.ByteBuffer)

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