use of libcore.io.ErrnoException in project robovm by robovm.
the class File method doChmod.
private boolean doChmod(int mask, boolean set) {
try {
StructStat sb = Libcore.os.stat(path);
int newMode = set ? (sb.st_mode | mask) : (sb.st_mode & ~mask);
Libcore.os.chmod(path, newMode);
return true;
} catch (ErrnoException errnoException) {
return false;
}
}
use of libcore.io.ErrnoException in project robovm by robovm.
the class FileChannelImpl method basicLock.
private FileLock basicLock(long position, long size, boolean shared, boolean wait) throws IOException {
int accessMode = (mode & O_ACCMODE);
if (accessMode == O_RDONLY) {
if (!shared) {
throw new NonWritableChannelException();
}
} else if (accessMode == O_WRONLY) {
if (shared) {
throw new NonReadableChannelException();
}
}
if (position < 0 || size < 0) {
throw new IllegalArgumentException("position=" + position + " size=" + size);
}
FileLock pendingLock = new FileLockImpl(this, position, size, shared);
addLock(pendingLock);
StructFlock flock = new StructFlock();
flock.l_type = (short) (shared ? F_RDLCK : F_WRLCK);
flock.l_whence = (short) SEEK_SET;
flock.l_start = position;
flock.l_len = translateLockLength(size);
boolean success = false;
try {
success = (Libcore.os.fcntlFlock(fd, wait ? F_SETLKW64 : F_SETLK64, flock) != -1);
} catch (ErrnoException errnoException) {
throw errnoException.rethrowAsIOException();
} finally {
if (!success) {
removeLock(pendingLock);
}
}
return success ? pendingLock : null;
}
use of libcore.io.ErrnoException in project robovm by robovm.
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);
}
}
use of libcore.io.ErrnoException in project robovm by robovm.
the class FileChannelImpl method release.
/**
* Non-API method to release a given lock on a file channel. Assumes that
* the lock will mark itself invalid after successful unlocking.
*/
public void release(FileLock lock) throws IOException {
checkOpen();
StructFlock flock = new StructFlock();
flock.l_type = (short) F_UNLCK;
flock.l_whence = (short) SEEK_SET;
flock.l_start = lock.position();
flock.l_len = translateLockLength(lock.size());
try {
Libcore.os.fcntlFlock(fd, F_SETLKW64, flock);
} catch (ErrnoException errnoException) {
throw errnoException.rethrowAsIOException();
}
removeLock(lock);
}
use of libcore.io.ErrnoException in project robovm by robovm.
the class FileChannelImpl method map.
public final MappedByteBuffer map(MapMode mapMode, long position, long size) throws IOException {
checkOpen();
if (mapMode == null) {
throw new NullPointerException("mapMode == null");
}
if (position < 0 || size < 0 || size > Integer.MAX_VALUE) {
throw new IllegalArgumentException("position=" + position + " size=" + size);
}
int accessMode = (mode & O_ACCMODE);
if (accessMode == O_RDONLY) {
if (mapMode != MapMode.READ_ONLY) {
throw new NonWritableChannelException();
}
} else if (accessMode == O_WRONLY) {
throw new NonReadableChannelException();
}
if (position + size > size()) {
// and we only care about making our backing file longer here.
try {
Libcore.os.ftruncate(fd, position + size);
} catch (ErrnoException ftruncateException) {
// continue on.
try {
if (S_ISREG(Libcore.os.fstat(fd).st_mode) || ftruncateException.errno != EINVAL) {
throw ftruncateException.rethrowAsIOException();
}
} catch (ErrnoException fstatException) {
throw fstatException.rethrowAsIOException();
}
}
}
long alignment = position - position % Libcore.os.sysconf(_SC_PAGE_SIZE);
int offset = (int) (position - alignment);
MemoryBlock block = MemoryBlock.mmap(fd, alignment, size + offset, mapMode);
return new DirectByteBuffer(block, (int) size, offset, (mapMode == MapMode.READ_ONLY), mapMode);
}
Aggregations