Search in sources :

Example 6 with ErrnoException

use of libcore.io.ErrnoException in project android_frameworks_base by ParanoidAndroid.

the class MediaScanner method prescan.

private void prescan(String filePath, boolean prescanFiles) throws RemoteException {
    Cursor c = null;
    String where = null;
    String[] selectionArgs = null;
    if (mPlayLists == null) {
        mPlayLists = new ArrayList<FileEntry>();
    } else {
        mPlayLists.clear();
    }
    if (filePath != null) {
        // query for only one file
        where = MediaStore.Files.FileColumns._ID + ">?" + " AND " + Files.FileColumns.DATA + "=?";
        selectionArgs = new String[] { "", filePath };
    } else {
        where = MediaStore.Files.FileColumns._ID + ">?";
        selectionArgs = new String[] { "" };
    }
    // Tell the provider to not delete the file.
    // If the file is truly gone the delete is unnecessary, and we want to avoid
    // accidentally deleting files that are really there (this may happen if the
    // filesystem is mounted and unmounted while the scanner is running).
    Uri.Builder builder = mFilesUri.buildUpon();
    builder.appendQueryParameter(MediaStore.PARAM_DELETE_DATA, "false");
    MediaBulkDeleter deleter = new MediaBulkDeleter(mMediaProvider, mPackageName, builder.build());
    // Build the list of files from the content provider
    try {
        if (prescanFiles) {
            // First read existing files from the files table.
            // Because we'll be deleting entries for missing files as we go,
            // we need to query the database in small batches, to avoid problems
            // with CursorWindow positioning.
            long lastId = Long.MIN_VALUE;
            Uri limitUri = mFilesUri.buildUpon().appendQueryParameter("limit", "1000").build();
            mWasEmptyPriorToScan = true;
            while (true) {
                selectionArgs[0] = "" + lastId;
                if (c != null) {
                    c.close();
                    c = null;
                }
                c = mMediaProvider.query(mPackageName, limitUri, FILES_PRESCAN_PROJECTION, where, selectionArgs, MediaStore.Files.FileColumns._ID, null);
                if (c == null) {
                    break;
                }
                int num = c.getCount();
                if (num == 0) {
                    break;
                }
                mWasEmptyPriorToScan = false;
                while (c.moveToNext()) {
                    long rowId = c.getLong(FILES_PRESCAN_ID_COLUMN_INDEX);
                    String path = c.getString(FILES_PRESCAN_PATH_COLUMN_INDEX);
                    int format = c.getInt(FILES_PRESCAN_FORMAT_COLUMN_INDEX);
                    long lastModified = c.getLong(FILES_PRESCAN_DATE_MODIFIED_COLUMN_INDEX);
                    lastId = rowId;
                    // media scanner removing them.
                    if (path != null && path.startsWith("/")) {
                        boolean exists = false;
                        try {
                            exists = Libcore.os.access(path, libcore.io.OsConstants.F_OK);
                        } catch (ErrnoException e1) {
                        }
                        if (!exists && !MtpConstants.isAbstractObject(format)) {
                            // do not delete missing playlists, since they may have been
                            // modified by the user.
                            // The user can delete them in the media player instead.
                            // instead, clear the path and lastModified fields in the row
                            MediaFile.MediaFileType mediaFileType = MediaFile.getFileType(path);
                            int fileType = (mediaFileType == null ? 0 : mediaFileType.fileType);
                            if (!MediaFile.isPlayListFileType(fileType)) {
                                deleter.delete(rowId);
                                if (path.toLowerCase(Locale.US).endsWith("/.nomedia")) {
                                    deleter.flush();
                                    String parent = new File(path).getParent();
                                    mMediaProvider.call(mPackageName, MediaStore.UNHIDE_CALL, parent, null);
                                }
                            }
                        }
                    }
                }
            }
        }
    } finally {
        if (c != null) {
            c.close();
        }
        deleter.flush();
    }
    // compute original size of images
    mOriginalCount = 0;
    c = mMediaProvider.query(mPackageName, mImagesUri, ID_PROJECTION, null, null, null, null);
    if (c != null) {
        mOriginalCount = c.getCount();
        c.close();
    }
}
Also used : Cursor(android.database.Cursor) Uri(android.net.Uri) ErrnoException(libcore.io.ErrnoException) File(java.io.File)

Example 7 with ErrnoException

use of libcore.io.ErrnoException in project robovm by robovm.

the class InetAddress method isReachable.

private boolean isReachable(InetAddress destination, InetAddress source, int timeout) throws IOException {
    // TODO: try ICMP first (http://code.google.com/p/android/issues/detail?id=20106)
    FileDescriptor fd = IoBridge.socket(true);
    boolean reached = false;
    try {
        if (source != null) {
            IoBridge.bind(fd, source, 0);
        }
        IoBridge.connect(fd, destination, 7, timeout);
        reached = true;
    } catch (IOException e) {
        if (e.getCause() instanceof ErrnoException) {
            // "Connection refused" means the IP address was reachable.
            reached = (((ErrnoException) e.getCause()).errno == ECONNREFUSED);
        }
    }
    IoBridge.closeSocket(fd);
    return reached;
}
Also used : ErrnoException(libcore.io.ErrnoException) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor)

Example 8 with ErrnoException

use of libcore.io.ErrnoException in project robovm by robovm.

the class PlainSocketImpl method accept.

@Override
protected void accept(SocketImpl newImpl) throws IOException {
    if (usingSocks()) {
        ((PlainSocketImpl) newImpl).socksBind();
        ((PlainSocketImpl) newImpl).socksAccept();
        return;
    }
    try {
        // RovmVM note: accept() on Darwin does not honor the SO_RCVTIMEO
        // set using setSoTimeout() on blocking sockets. As a work around we 
        // do poll() if a timeout has been set followed by an accept().
        int timeout = (Integer) getOption(SO_TIMEOUT);
        int flags = Libcore.os.fcntlVoid(fd, F_GETFL);
        if (timeout > 0 && (flags & O_NONBLOCK) == 0) {
            StructPollfd pfd = new StructPollfd();
            pfd.fd = fd;
            pfd.events = (short) (POLLIN | POLLERR);
            StructPollfd[] pfds = new StructPollfd[] { pfd };
            long start = System.currentTimeMillis();
            long deadline = start + timeout;
            while (true) {
                try {
                    if (timeout <= 0 || Libcore.os.poll(pfds, timeout) == 0) {
                        throw new SocketTimeoutException("accept() timed out");
                    }
                    break;
                } catch (ErrnoException e) {
                    if (e.errno == EINTR) {
                        long now = System.currentTimeMillis();
                        timeout = (int) (deadline - now);
                    } else {
                        throw e;
                    }
                }
            }
        }
        InetSocketAddress peerAddress = new InetSocketAddress();
        FileDescriptor clientFd = Libcore.os.accept(fd, peerAddress);
        // TODO: we can't just set newImpl.fd to clientFd because a nio SocketChannel may
        // be sharing the FileDescriptor. http://b//4452981.
        newImpl.fd.setInt$(clientFd.getInt$());
        newImpl.address = peerAddress.getAddress();
        newImpl.port = peerAddress.getPort();
    } catch (ErrnoException errnoException) {
        if (errnoException.errno == EAGAIN) {
            throw new SocketTimeoutException(errnoException);
        }
        throw errnoException.rethrowAsSocketException();
    }
    // Reset the client's inherited read timeout to the Java-specified default of 0.
    newImpl.setOption(SocketOptions.SO_TIMEOUT, Integer.valueOf(0));
    newImpl.localport = IoBridge.getSocketLocalPort(newImpl.fd);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) ErrnoException(libcore.io.ErrnoException) InetSocketAddress(java.net.InetSocketAddress) StructPollfd(libcore.io.StructPollfd) FileDescriptor(java.io.FileDescriptor)

Example 9 with ErrnoException

use of libcore.io.ErrnoException in project robovm by robovm.

the class ProcessManager method watchChildren.

/**
     * Loops indefinitely and calls ProcessManager.onExit() when children exit.
     */
private void watchChildren() {
    MutableInt status = new MutableInt(-1);
    while (true) {
        try {
            // Wait for children in our process group.
            int pid = Libcore.os.waitpid(0, status, 0);
            // Work out what onExit wants to hear.
            int exitValue;
            if (WIFEXITED(status.value)) {
                exitValue = WEXITSTATUS(status.value);
            } else if (WIFSIGNALED(status.value)) {
                exitValue = WTERMSIG(status.value);
            } else if (WIFSTOPPED(status.value)) {
                exitValue = WSTOPSIG(status.value);
            } else {
                throw new AssertionError("unexpected status from waitpid: " + status.value);
            }
            onExit(pid, exitValue);
        } catch (ErrnoException errnoException) {
            if (errnoException.errno == ECHILD) {
                // Expected errno: there are no children to wait for.
                // onExit will sleep until it is informed of another child coming to life.
                waitForMoreChildren();
                continue;
            } else {
                throw new AssertionError(errnoException);
            }
        }
    }
}
Also used : ErrnoException(libcore.io.ErrnoException) MutableInt(libcore.util.MutableInt)

Example 10 with ErrnoException

use of libcore.io.ErrnoException in project j2objc by google.

the class FileChannelImpl method transferTo.

public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
    checkOpen();
    if (!target.isOpen()) {
        throw new ClosedChannelException();
    }
    checkReadable();
    if (target instanceof FileChannelImpl) {
        ((FileChannelImpl) target).checkWritable();
    }
    if (position < 0 || count < 0) {
        throw new IllegalArgumentException("position=" + position + " count=" + count);
    }
    if (count == 0 || position >= size()) {
        return 0;
    }
    count = Math.min(count, size() - position);
    // Try sendfile(2) first...
    boolean completed = false;
    if (target instanceof SocketChannelImpl) {
        FileDescriptor outFd = ((SocketChannelImpl) target).getFD();
        try {
            begin();
            try {
                MutableLong offset = new MutableLong(position);
                long rc = Libcore.os.sendfile(outFd, fd, offset, count);
                completed = true;
                return rc;
            } catch (ErrnoException errnoException) {
                // try a different approach. If it does support it, but it failed, we're done.
                if (errnoException.errno != ENOSYS && errnoException.errno != EINVAL) {
                    throw errnoException.rethrowAsIOException();
                }
            }
        } finally {
            end(completed);
        }
    }
    // ...fall back to write(2).
    ByteBuffer buffer = null;
    try {
        buffer = map(MapMode.READ_ONLY, position, count);
        return target.write(buffer);
    } finally {
        NioUtils.freeDirectBuffer(buffer);
    }
}
Also used : ClosedChannelException(java.nio.channels.ClosedChannelException) MutableLong(libcore.util.MutableLong) ErrnoException(libcore.io.ErrnoException) FileDescriptor(java.io.FileDescriptor)

Aggregations

ErrnoException (libcore.io.ErrnoException)46 IOException (java.io.IOException)15 FileDescriptor (java.io.FileDescriptor)13 StructStat (libcore.io.StructStat)9 File (java.io.File)7 InetSocketAddress (java.net.InetSocketAddress)7 NonReadableChannelException (java.nio.channels.NonReadableChannelException)6 NonWritableChannelException (java.nio.channels.NonWritableChannelException)6 StructFlock (libcore.io.StructFlock)6 FileOutputStream (java.io.FileOutputStream)5 SocketAddress (java.net.SocketAddress)4 FileInputStream (java.io.FileInputStream)3 SocketTimeoutException (java.net.SocketTimeoutException)3 ClosedChannelException (java.nio.channels.ClosedChannelException)3 FileLock (java.nio.channels.FileLock)3 MutableLong (libcore.util.MutableLong)3 PrintStream (java.io.PrintStream)2 SocketException (java.net.SocketException)2 LinkedList (java.util.LinkedList)2 GaiException (libcore.io.GaiException)2