private int childResult(WindowsChildRecord child, boolean overlay) { if (child == null) return -1; if (overlay) { IntByReference exitCode = new IntByReference(); WindowsLibC libc = (WindowsLibC) libc(); HANDLE handle = child.getProcess(); libc.WaitForSingleObject(handle, WindowsLibC.INFINITE); libc.GetExitCodeProcess(handle, exitCode); libc.CloseHandle(handle); System.exit(exitCode.getValue()); } return child.getPid(); }
private int setFileTime(String path, FileTime aTime, FileTime mTime) { byte[] wpath = WindowsHelpers.toWPath(path); HANDLE handle = wlibc().CreateFileW(wpath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, null, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); if (!handle.isValid()) { return -1; // TODO proper error handling } boolean timeSet = wlibc().SetFileTime(handle, null, aTime, mTime); wlibc().CloseHandle(handle); return timeSet ? 0 : -1; }
/** * The logic here is a bit strange and this copies MRI (Ruby) which may not be language * agnostic, but windows (win7 and others) automatically mark folders as read-only when * it contains other files and folders within it. This document explains more: * http://support.microsoft.com/kb/326549 * I think the logic is based around idea that if you removed all other files it would * be empty but will stay marked as read-only. */ @Override public int rmdir(String path) { WString pathW = WString.path(path); int attr = wlibc().GetFileAttributesW(pathW); boolean isReadOnly = attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_READONLY) != 0; if (isReadOnly) wlibc().SetFileAttributesW(pathW, attr & ~FILE_ATTRIBUTE_READONLY); if (!wlibc().RemoveDirectoryW(pathW)) { int errno = errno(); if (isReadOnly) wlibc().SetFileAttributesW(pathW, attr & FILE_ATTRIBUTE_READONLY); handler.error(mapErrorToErrno(errno), "rmdir", path); return -1; } return 0; }
wlibc().GetStdHandle(WindowsLibC.STD_INPUT_HANDLE)); startupInfo.setStandardOutput(output != null ? output : wlibc().GetStdHandle(WindowsLibC.STD_OUTPUT_HANDLE)); startupInfo.setStandardError(error != null ? input : wlibc().GetStdHandle(WindowsLibC.STD_ERROR_HANDLE)); byte[] cwd = WindowsHelpers.toWString(WindowsHelpers.escapePath(handler.getCurrentWorkingDirectory().toString()) +"\\"); ByteBuffer commandW = ByteBuffer.wrap(WindowsHelpers.toWString(command)); boolean returnValue = wlibc().CreateProcessW(programW, commandW, securityAttributes, securityAttributes, securityAttributes.getInheritHandle() ? 1: 0, creationFlags, wideEnv, cwd, wlibc().CloseHandle(processInformation.getThread());
@Override public int waitpid(int pid, int[] status, int flags) { if (pid <= 0) { handler.unimplementedError("waitpid"); } HANDLE h = wlibc().OpenProcess(WindowsLibC.PROCESS_QUERY_INFORMATION, 0, pid); if (h == null) { return -1; // TODO: Throw exception } // Block if ((flags & WaitFlags.WNOHANG.intValue()) != 0) { wlibc().WaitForSingleObject(h, WindowsLibC.INFINITE); } IntByReference exitCode = new IntByReference(); wlibc().GetExitCodeProcess(h, exitCode); wlibc().CloseHandle(h); int code = exitCode.getValue(); if (code == 259) { return 0; } else { status[0] = code; return pid; } }
@Override public int unsetenv(String envName) { if (!wlibc().SetEnvironmentVariableW(new WString(envName), null)) { handler.error(EINVAL, "unsetenv", envName); return -1; } return 0; }
@Override public boolean isatty(FileDescriptor fd) { HANDLE handle = JavaLibCHelper.gethandle(fd); int type = wlibc().GetFileType(handle); return type == FILE_TYPE_CHAR; }
@Override public int link(String oldpath, String newpath) { boolean linkCreated = wlibc().CreateHardLinkW(WString.path(newpath), WString.path(oldpath), null); if (!linkCreated) { int error = errno(); handler.error(mapErrorToErrno(error), "link", oldpath + " or " + newpath); return error; } else { return 0; } }
wlibc().GetStdHandle(WindowsLibC.STD_INPUT_HANDLE)); startupInfo.setStandardOutput(output != null ? output : wlibc().GetStdHandle(WindowsLibC.STD_OUTPUT_HANDLE)); startupInfo.setStandardError(error != null ? input : wlibc().GetStdHandle(WindowsLibC.STD_ERROR_HANDLE)); byte[] cwd = WindowsHelpers.toWString(WindowsHelpers.escapePath(handler.getCurrentWorkingDirectory().toString()) +"\\"); ByteBuffer commandW = ByteBuffer.wrap(WindowsHelpers.toWString(command)); boolean returnValue = wlibc().CreateProcessW(programW, commandW, securityAttributes, securityAttributes, securityAttributes.getInheritHandle() ? 1: 0, creationFlags, wideEnv, cwd, wlibc().CloseHandle(processInformation.getThread());
@Override public int unsetenv(String envName) { if (!wlibc().SetEnvironmentVariableW(new WString(envName), null)) { handler.error(EINVAL, "unsetenv", envName); return -1; } return 0; }
@Override public int isatty(int fd) { HANDLE handle = JavaLibCHelper.gethandle(fd); int type = wlibc().GetFileType(handle); return type == FILE_TYPE_CHAR ? 1 : 0; }
@Override public int link(String oldpath, String newpath) { boolean linkCreated = wlibc().CreateHardLinkW(WString.path(newpath), WString.path(oldpath), null); if (!linkCreated) { int error = errno(); handler.error(mapErrorToErrno(error), "link", oldpath + " or " + newpath); return error; } else { return 0; } }
private int childResult(WindowsChildRecord child, boolean overlay) { if (child == null) return -1; if (overlay) { IntByReference exitCode = new IntByReference(); WindowsLibC libc = (WindowsLibC) libc(); HANDLE handle = child.getProcess(); libc.WaitForSingleObject(handle, WindowsLibC.INFINITE); libc.GetExitCodeProcess(handle, exitCode); libc.CloseHandle(handle); System.exit(exitCode.getValue()); } return child.getPid(); }
@Override public int utimes(String path, long[] atimeval, long[] mtimeval) { byte[] wpath = WindowsHelpers.toWPath(path); FileTime aTime = atimeval == null ? null : unixTimeToFileTime(atimeval[0]); FileTime mTime = mtimeval == null ? null : unixTimeToFileTime(mtimeval[0]); if (aTime == null || mTime == null) { FileTime nowFile = unixTimeToFileTime(System.currentTimeMillis() / 1000L); if (aTime == null) aTime = nowFile; if (mTime == null) mTime = nowFile; } HANDLE handle = wlibc().CreateFileW(wpath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, null, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0); if (!handle.isValid()) { return -1; // TODO proper error handling } boolean timeSet = wlibc().SetFileTime(handle, null, aTime, mTime); wlibc().CloseHandle(handle); return timeSet ? 0 : -1; }
/** * The logic here is a bit strange and this copies MRI (Ruby) which may not be language * agnostic, but windows (win7 and others) automatically mark folders as read-only when * it contains other files and folders within it. This document explains more: * http://support.microsoft.com/kb/326549 * I think the logic is based around idea that if you removed all other files it would * be empty but will stay marked as read-only. */ @Override public int rmdir(String path) { WString pathW = WString.path(path); int attr = wlibc().GetFileAttributesW(pathW); boolean isReadOnly = attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_READONLY) != 0; if (isReadOnly) wlibc().SetFileAttributesW(pathW, attr & ~FILE_ATTRIBUTE_READONLY); if (!wlibc().RemoveDirectoryW(pathW)) { int errno = errno(); if (isReadOnly) wlibc().SetFileAttributesW(pathW, attr & FILE_ATTRIBUTE_READONLY); handler.error(mapErrorToErrno(errno), "rmdir", path); return -1; } return 0; }
wlibc().GetStdHandle(WindowsLibC.STD_INPUT_HANDLE)); startupInfo.setStandardOutput(output != null ? output : wlibc().GetStdHandle(WindowsLibC.STD_OUTPUT_HANDLE)); startupInfo.setStandardError(error != null ? input : wlibc().GetStdHandle(WindowsLibC.STD_ERROR_HANDLE)); byte[] cwd = WindowsHelpers.toWString(WindowsHelpers.escapePath(handler.getCurrentWorkingDirectory().toString()) +"\\"); ByteBuffer commandW = ByteBuffer.wrap(WindowsHelpers.toWString(command)); boolean returnValue = wlibc().CreateProcessW(programW, commandW, securityAttributes, securityAttributes, securityAttributes.getInheritHandle() ? 1: 0, creationFlags, wideEnv, cwd, wlibc().CloseHandle(processInformation.getThread());
@Override public int unsetenv(String envName) { if (!wlibc().SetEnvironmentVariableW(new WString(envName), null)) { handler.error(EINVAL, "unsetenv", envName); return -1; } return 0; }
@Override public boolean isatty(FileDescriptor fd) { HANDLE handle = JavaLibCHelper.gethandle(fd); int type = wlibc().GetFileType(handle); return type == FILE_TYPE_CHAR; }
@Override public int link(String oldpath, String newpath) { boolean linkCreated = wlibc().CreateHardLinkW(WString.path(newpath), WString.path(oldpath), null); if (!linkCreated) { int error = errno(); handler.error(mapErrorToErrno(error), "link", oldpath + " or " + newpath); return error; } else { return 0; } }
private int childResult(WindowsChildRecord child, boolean overlay) { if (child == null) return -1; if (overlay) { IntByReference exitCode = new IntByReference(); WindowsLibC libc = (WindowsLibC) libc(); HANDLE handle = child.getProcess(); libc.WaitForSingleObject(handle, WindowsLibC.INFINITE); libc.GetExitCodeProcess(handle, exitCode); libc.CloseHandle(handle); System.exit(exitCode.getValue()); } return child.getPid(); }