Search in sources :

Example 96 with ErrnoException

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

the class LocalSocketImpl method getOption.

public Object getOption(int optID) throws IOException {
    if (fd == null) {
        throw new IOException("socket not created");
    }
    try {
        Object toReturn;
        switch(optID) {
            case SocketOptions.SO_TIMEOUT:
                StructTimeval timeval = Os.getsockoptTimeval(fd, OsConstants.SOL_SOCKET, OsConstants.SO_SNDTIMEO);
                toReturn = (int) timeval.toMillis();
                break;
            case SocketOptions.SO_RCVBUF:
            case SocketOptions.SO_SNDBUF:
            case SocketOptions.SO_REUSEADDR:
                int osOpt = javaSoToOsOpt(optID);
                toReturn = Os.getsockoptInt(fd, OsConstants.SOL_SOCKET, osOpt);
                break;
            case SocketOptions.SO_LINGER:
                StructLinger linger = Os.getsockoptLinger(fd, OsConstants.SOL_SOCKET, OsConstants.SO_LINGER);
                if (!linger.isOn()) {
                    toReturn = -1;
                } else {
                    toReturn = linger.l_linger;
                }
                break;
            case SocketOptions.TCP_NODELAY:
                toReturn = Os.getsockoptInt(fd, OsConstants.IPPROTO_TCP, OsConstants.TCP_NODELAY);
                break;
            default:
                throw new IOException("Unknown option: " + optID);
        }
        return toReturn;
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
Also used : ErrnoException(android.system.ErrnoException) StructTimeval(android.system.StructTimeval) IOException(java.io.IOException) StructLinger(android.system.StructLinger)

Example 97 with ErrnoException

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

the class LocalSocketImpl method setOption.

public void setOption(int optID, Object value) throws IOException {
    if (fd == null) {
        throw new IOException("socket not created");
    }
    /*
         * Boolean.FALSE is used to disable some options, so it
         * is important to distinguish between FALSE and unset.
         * We define it here that -1 is unset, 0 is FALSE, and 1
         * is TRUE.
         */
    int boolValue = -1;
    int intValue = 0;
    if (value instanceof Integer) {
        intValue = (Integer) value;
    } else if (value instanceof Boolean) {
        boolValue = ((Boolean) value) ? 1 : 0;
    } else {
        throw new IOException("bad value: " + value);
    }
    try {
        switch(optID) {
            case SocketOptions.SO_LINGER:
                StructLinger linger = new StructLinger(boolValue, intValue);
                Os.setsockoptLinger(fd, OsConstants.SOL_SOCKET, OsConstants.SO_LINGER, linger);
                break;
            case SocketOptions.SO_TIMEOUT:
                // The option must set both send and receive timeouts.
                // Note: The incoming timeout value is in milliseconds.
                StructTimeval timeval = StructTimeval.fromMillis(intValue);
                Os.setsockoptTimeval(fd, OsConstants.SOL_SOCKET, OsConstants.SO_RCVTIMEO, timeval);
                Os.setsockoptTimeval(fd, OsConstants.SOL_SOCKET, OsConstants.SO_SNDTIMEO, timeval);
                break;
            case SocketOptions.SO_RCVBUF:
            case SocketOptions.SO_SNDBUF:
            case SocketOptions.SO_REUSEADDR:
                int osOpt = javaSoToOsOpt(optID);
                Os.setsockoptInt(fd, OsConstants.SOL_SOCKET, osOpt, intValue);
                break;
            case SocketOptions.TCP_NODELAY:
                Os.setsockoptInt(fd, OsConstants.IPPROTO_TCP, OsConstants.TCP_NODELAY, intValue);
                break;
            default:
                throw new IOException("Unknown option: " + optID);
        }
    } catch (ErrnoException e) {
        throw e.rethrowAsIOException();
    }
}
Also used : ErrnoException(android.system.ErrnoException) StructTimeval(android.system.StructTimeval) IOException(java.io.IOException) StructLinger(android.system.StructLinger)

Example 98 with ErrnoException

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

the class SharedPreferencesImpl method writeToFile.

// Note: must hold mWritingToDiskLock
private void writeToFile(MemoryCommitResult mcr, boolean isFromSyncCommit) {
    // Rename the current file so it may be used as a backup during the next read
    if (mFile.exists()) {
        boolean needsWrite = false;
        // Only need to write if the disk state is older than this commit
        if (mDiskStateGeneration < mcr.memoryStateGeneration) {
            if (isFromSyncCommit) {
                needsWrite = true;
            } else {
                synchronized (this) {
                    // be persisted.
                    if (mCurrentMemoryStateGeneration == mcr.memoryStateGeneration) {
                        needsWrite = true;
                    }
                }
            }
        }
        if (!needsWrite) {
            if (DEBUG) {
                Log.d(TAG, "skipped " + mcr.memoryStateGeneration + " -> " + mFile.getName());
            }
            mcr.setDiskWriteResult(true);
            return;
        }
        if (!mBackupFile.exists()) {
            if (!mFile.renameTo(mBackupFile)) {
                Log.e(TAG, "Couldn't rename file " + mFile + " to backup file " + mBackupFile);
                mcr.setDiskWriteResult(false);
                return;
            }
        } else {
            mFile.delete();
        }
    }
    // from the backup.
    try {
        FileOutputStream str = createFileOutputStream(mFile);
        if (str == null) {
            mcr.setDiskWriteResult(false);
            return;
        }
        XmlUtils.writeMapXml(mcr.mapToWriteToDisk, str);
        FileUtils.sync(str);
        if (DEBUG) {
            Log.d(TAG, "wrote " + mcr.memoryStateGeneration + " -> " + mFile.getName());
        }
        str.close();
        ContextImpl.setFilePermissionsFromMode(mFile.getPath(), mMode, 0);
        try {
            final StructStat stat = Os.stat(mFile.getPath());
            synchronized (this) {
                mStatTimestamp = stat.st_mtime;
                mStatSize = stat.st_size;
            }
        } catch (ErrnoException e) {
        // Do nothing
        }
        // Writing was successful, delete the backup file if there is one.
        mBackupFile.delete();
        mDiskStateGeneration = mcr.memoryStateGeneration;
        mcr.setDiskWriteResult(true);
        return;
    } catch (XmlPullParserException e) {
        Log.w(TAG, "writeToFile: Got exception:", e);
    } catch (IOException e) {
        Log.w(TAG, "writeToFile: Got exception:", e);
    }
    // Clean up an unsuccessfully written file
    if (mFile.exists()) {
        if (!mFile.delete()) {
            Log.e(TAG, "Couldn't clean up partially-written file " + mFile);
        }
    }
    mcr.setDiskWriteResult(false);
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) FileOutputStream(java.io.FileOutputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 99 with ErrnoException

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

the class MemoryMappedFile_Delegate method mmapRO.

@LayoutlibDelegate
static MemoryMappedFile mmapRO(String path) throws ErrnoException {
    if (!path.startsWith(TARGET_PATH)) {
        throw new ErrnoException("Custom timezone data files are not supported.", 1);
    }
    if (sRootPath == null) {
        throw new ErrnoException("Bridge has not been initialized properly.", 1);
    }
    path = path.substring(TARGET_PATH.length());
    try {
        File f = new File(sRootPath, path);
        if (!f.exists()) {
            throw new ErrnoException("File not found: " + f.getPath(), 1);
        }
        RandomAccessFile file = new RandomAccessFile(f, "r");
        try {
            long size = file.length();
            MemoryMappedFile_Delegate newDelegate = new MemoryMappedFile_Delegate(file);
            long filePointer = file.getFilePointer();
            MemoryMappedFile mmFile = new MemoryMappedFile(filePointer, size);
            long delegateIndex = sManager.addNewDelegate(newDelegate);
            sMemoryMappedFileMap.put(mmFile, delegateIndex);
            return mmFile;
        } finally {
            file.close();
        }
    } catch (IOException e) {
        throw new ErrnoException("mmapRO", 1, e);
    }
}
Also used : ErrnoException(android.system.ErrnoException) RandomAccessFile(java.io.RandomAccessFile) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 100 with ErrnoException

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

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)

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