@Override public void error(Errno error, String methodName, String extraData) { throw new PosixException("native error calling " + methodName + ": " + error.description() + " " + extraData, convert(error)); } private org.jruby.ext.posix.POSIX.ERRORS convert(Errno error) {
private org.jruby.ext.posix.POSIX.ERRORS convert(Errno error) { try { return org.jruby.ext.posix.POSIX.ERRORS.valueOf(error.name()); } catch (IllegalArgumentException x) { return org.jruby.ext.posix.POSIX.ERRORS.EIO; // PosixException.message has real error anyway } } }, true);
long procSpawnSh(Ruby runtime, String str, ExecArg eargp) { long status; String shell = dlnFindExeR(runtime, "sh", eargp.path_env); // System.out.println("before: " + shell + ", fa=" + eargp.fileActions + ", a=" + eargp.attributes + ", argv=" + Arrays.asList("sh", "-c", str)); status = runtime.getPosix().posix_spawnp( shell != null ? shell : "/bin/sh", eargp.fileActions, eargp.attributes, Arrays.asList("sh", "-c", str), eargp.envp_str == null ? Collections.EMPTY_LIST : Arrays.asList(eargp.envp_str)); if (status == -1) errno = Errno.valueOf(runtime.getPosix().errno()); return status; }
/** * Helper for handling common POSIX situations where a negative return value * from a function call indicates an error, and errno must be consulted to * determine how exactly the function failed. * @param runtime Ruby runtime * @param result return value of a POSIX call */ public static void checkErrno(Ruby runtime, int result) { if (result < 0) { // FIXME: The error message is a bit off. // e.g., No such process - No such process (Errno::ESRCH) // Note the repetition of 'No such process'. Errno errno = Errno.valueOf(runtime.getPosix().errno()); String name = errno.name(); String msg = errno.toString(); RubyClass errnoClass = runtime.getErrno().getClass(name); if (errnoClass != null) { throw RaiseException.from(runtime, errnoClass, msg); } } }
stat_loc[0] = 0; int result = runtime.getPosix().waitpid((int)finalPid, stat_loc, 0); if (result == -1) { Errno errno = Errno.valueOf(runtime.getPosix().errno()); switch (errno) { case EINTR: runtime.getCurrentContext().pollThreadEvents(); continue retry; case ECHILD: return -1; default: throw new RuntimeException("unexpected waitpid errno: " + Errno.valueOf(runtime.getPosix().errno()));
/** * Create module Errno's Variables. We have this method since Errno does not have its * own java class. */ private void initErrno() { if (profile.allowModule("Errno")) { errnoModule = defineModule("Errno"); try { // define EAGAIN now, so that future EWOULDBLOCK will alias to it // see MRI's error.c and its explicit ordering of Errno definitions. createSysErr(Errno.EAGAIN.intValue(), Errno.EAGAIN.name()); for (Errno e : Errno.values()) { Constant c = (Constant) e; if (Character.isUpperCase(c.name().charAt(0))) { createSysErr(c.intValue(), c.name()); } } // map ENOSYS to NotImplementedError errnos.put(Errno.ENOSYS.intValue(), notImplementedError); } catch (Exception e) { // dump the trace and continue // this is currently only here for Android, which seems to have // bugs in its enumeration logic // http://code.google.com/p/android/issues/detail?id=2812 LOG.error(e); } } }
private static long transfer(ThreadContext context, ReadableByteChannel from, FileChannel to, long length, long position) throws IOException { // handle large files on 32-bit JVMs long chunkSize = 128 * 1024 * 1024; long transferred = 0; long bytes; long startPosition = to.position(); if (position != -1) { if (from instanceof NativeSelectableChannel) { int ret = context.runtime.getPosix().lseek(((NativeSelectableChannel)from).getFD(), position, PosixShim.SEEK_SET); if (ret == -1) { throw context.runtime.newErrnoFromErrno(Errno.valueOf(context.runtime.getPosix().errno()), from.toString()); } } } if (length > 0) { while ((bytes = to.transferFrom(from, startPosition+transferred, Math.min(chunkSize, length))) > 0) { transferred += bytes; length -= bytes; } } else { while ((bytes = to.transferFrom(from, startPosition+transferred, chunkSize)) > 0) { transferred += bytes; } } // transforFrom does not change position of target to.position(startPosition + transferred); return transferred; }
public RaiseException newErrnoFromInt(int errno) { Errno errnoObj = Errno.valueOf(errno); if (errnoObj == null) { return newSystemCallError("Unknown Error (" + errno + ")"); } String message = errnoObj.description(); return newErrnoFromInt(errno, message); }
POSIX posix = runtime.getPosix(); if (posix.isNative() && !Platform.IS_WINDOWS) { IRubyObject oldExc = context.runtime.getGlobalVariables().get("$!"); // Save $! try { RubyFile.unlink(context, this); if (errno != Errno.ENOENT.intValue() && errno != Errno.EACCES.intValue()) { throw re; context.runtime.getGlobalVariables().set("$!", oldExc); // Restore $!
public long size(ChannelFD fd) { if (fd.chNative != null) { // native fd, use fstat FileStat stat = posix.allocateStat(); int ret = posix.fstat(fd.chNative.getFD(), stat); if (ret == -1) { errno = Errno.valueOf(posix.errno()); return -1; } return stat.st_size(); } else if (fd.chSeek != null) { // if it is seekable, get size directly try { return fd.chSeek.size(); } catch (IOException ioe) { errno = Helpers.errnoFromException(ioe); return -1; } } else { // otherwise just return -1 (should be rare, since size is only defined on File errno = Errno.EINVAL; return -1; } }
/** * Address family not supported by protocol */ public static int EAFNOSUPPORT() { return Errno.EAFNOSUPPORT.intValue(); }
public int setCloexec(int fd, boolean cloexec) { int ret = posix.fcntl(fd, Fcntl.F_GETFD); if (ret == -1) { errno = Errno.valueOf(posix.errno()); return -1; } if ( (cloexec && (ret & FcntlLibrary.FD_CLOEXEC) == FcntlLibrary.FD_CLOEXEC) || (!cloexec && (ret & FcntlLibrary.FD_CLOEXEC) == 0)) { return 0; } ret = cloexec ? ret | FcntlLibrary.FD_CLOEXEC : ret & ~FcntlLibrary.FD_CLOEXEC; ret = posix.fcntlInt(fd, Fcntl.F_SETFD, ret); if (ret == -1) errno = Errno.valueOf(posix.errno()); return ret; }
public void error(Errno error, String extraData) { throw runtime.newErrnoFromInt(error.intValue(), extraData); }
public RaiseException newErrnoFromInt(int errno, String methodName, String message) { if (Platform.IS_WINDOWS && ("stat".equals(methodName) || "lstat".equals(methodName))) { if (errno == 20047) return newErrnoENOENTError(message); // boo:bar UNC stat failure if (errno == Errno.ESRCH.intValue()) return newErrnoENOENTError(message); // ESRCH on stating "" } return newErrnoFromInt(errno, message); }
context.setLastExitStatus(new RubyProcess.RubyStatus(runtime, runtime.getProcStatus(), 0x7f << 8, 0)); if (errno == null || errno == Errno.__UNKNOWN_CONSTANT__) { errno = Errno.valueOf(runtime.getPosix().errno());
public int ftruncate(ChannelFD fd, long pos) { if (fd.chNative != null) { int ret = posix.ftruncate(fd.chNative.getFD(), pos); if (ret == -1) errno = Errno.valueOf(posix.errno()); return ret; } else if (fd.chFile != null) { try { fd.chFile.truncate(pos); } catch (IOException ioe) { errno = Helpers.errnoFromException(ioe); return -1; } } else { errno = Errno.EINVAL; return -1; } return 0; }
@Override public FileStat fstat(int fd) { FileStat stat = allocateStat(); int ret = fstat(fd, stat); if (ret < 0) handler.error(Errno.valueOf(errno()), "fstat", Integer.toString(fd)); return stat; }
public long posix_spawnp(String path, Collection<? extends SpawnFileAction> fileActions, CharSequence[] argv, CharSequence[] envp) { AbstractNumberReference<? extends Number> pid = Library.getRuntime(libc()).findType(TypeAlias.pid_t).size() == 4 ? new IntByReference(-1) : new LongLongByReference(-1); Pointer nativeFileActions = nativeFileActions(fileActions); try { if (((UnixLibC) libc()).posix_spawnp(pid, path, nativeFileActions, null, argv, envp) < 0) { Errno e = Errno.valueOf(errno()); handler.error(e, "posix_spawnp", e.description()); } } finally { ((UnixLibC) libc()).posix_spawn_file_actions_destroy(nativeFileActions); } return pid.longValue(); }