/** * Given the process handle, waits for its completion and returns the exit code. */ public static int waitForExitProcess(Pointer hProcess) throws InterruptedException { while (true) { if (Thread.interrupted()) throw new InterruptedException(); Kernel32.INSTANCE.WaitForSingleObject(hProcess,1000); IntByReference exitCode = new IntByReference(); exitCode.setValue(-1); Kernel32.INSTANCE.GetExitCodeProcess(hProcess,exitCode); int v = exitCode.getValue(); if (v !=Kernel32.STILL_ACTIVE) { return v; } } }
/** * If {@code jenkins.exe} is old compared to our copy, * schedule an overwrite (except that since it's currently running, * we can only do it when Jenkins restarts next time.) */ private void updateJenkinsExeIfNeeded() { try { File baseDir = getBaseDir(); URL exe = getClass().getResource("/windows-service/jenkins.exe"); String ourCopy = Util.getDigestOf(exe.openStream()); for (String name : new String[]{"hudson.exe","jenkins.exe"}) { try { File currentCopy = new File(baseDir,name); if(!currentCopy.exists()) continue; String curCopy = new FilePath(currentCopy).digest(); if(ourCopy.equals(curCopy)) continue; // identical File stage = new File(baseDir,name+".new"); FileUtils.copyURLToFile(exe,stage); Kernel32.INSTANCE.MoveFileExA(stage.getAbsolutePath(),currentCopy.getAbsolutePath(),MOVEFILE_DELAY_UNTIL_REBOOT|MOVEFILE_REPLACE_EXISTING); LOGGER.info("Scheduled a replacement of "+name); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Failed to replace "+name,e); } catch (InterruptedException e) { } } } catch (IOException e) { LOGGER.log(Level.SEVERE, "Failed to replace jenkins.exe",e); } }
/** * @param target * If relative, resolved against the location of the symlink. * If absolute, it's absolute. * @throws UnsatisfiedLinkError * If the function is not exported by kernel32. * See http://msdn.microsoft.com/en-us/library/windows/desktop/aa363866(v=vs.85).aspx * for compatibility info. * @deprecated Use {@link Util#createSymlink} instead. */ @Deprecated public static void createSymbolicLink(File symlink, String target, boolean dirLink) throws IOException { if (!Kernel32.INSTANCE.CreateSymbolicLinkW( new WString(symlink.getPath()), new WString(target), dirLink?Kernel32.SYMBOLIC_LINK_FLAG_DIRECTORY:0)) { throw new WinIOException("Failed to create a symlink "+symlink+" to "+target); } }
public static File getTempDir() { Memory buf = new Memory(1024); if (Kernel32.INSTANCE.GetTempPathW(512,buf)!=0) {// the first arg is number of wchar return new File(buf.getString(0, true)); } else { return null; } }
/** * @deprecated Use {@link java.nio.file.Files#readAttributes} with * {@link java.nio.file.attribute.DosFileAttributes} and reflective calls to * WindowsFileAttributes if necessary. */ @Deprecated public static int getWin32FileAttributes(File file) throws IOException { // allow lookup of paths longer than MAX_PATH // http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx String canonicalPath = file.getCanonicalPath(); String path; if(canonicalPath.length() < 260) { // path is short, use as-is path = canonicalPath; } else if(canonicalPath.startsWith("\\\\")) { // network share // \\server\share --> \\?\UNC\server\share path = "\\\\?\\UNC\\" + canonicalPath.substring(2); } else { // prefix, canonical path should be normalized and absolute so this should work. path = "\\\\?\\" + canonicalPath; } return Kernel32.INSTANCE.GetFileAttributesW(new WString(path)); }
public static File getTempDir() { Memory buf = new Memory(1024); if (Kernel32.INSTANCE.GetTempPathW(512,buf)!=0) {// the first arg is number of wchar return new File(buf.getString(0, true)); } else { return null; } }
public static int getWin32FileAttributes(File file) throws IOException { // allow lookup of paths longer than MAX_PATH // http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx String canonicalPath = file.getCanonicalPath(); String path; if(canonicalPath.length() < 260) { // path is short, use as-is path = canonicalPath; } else if(canonicalPath.startsWith("\\\\")) { // network share // \\server\share --> \\?\UNC\server\share path = "\\\\?\\UNC\\" + canonicalPath.substring(2); } else { // prefix, canonical path should be normalized and absolute so this should work. path = "\\\\?\\" + canonicalPath; } return Kernel32.INSTANCE.GetFileAttributesW(new WString(path)); }
/** * Given the process handle, waits for its completion and returns the exit code. */ public static int waitForExitProcess(Pointer hProcess) throws InterruptedException { while (true) { if (Thread.interrupted()) throw new InterruptedException(); Kernel32.INSTANCE.WaitForSingleObject(hProcess,1000); IntByReference exitCode = new IntByReference(); exitCode.setValue(-1); Kernel32.INSTANCE.GetExitCodeProcess(hProcess,exitCode); int v = exitCode.getValue(); if (v !=Kernel32.STILL_ACTIVE) { return v; } } }
/** * If <tt>jenkins.exe</tt> is old compared to our copy, * schedule an overwrite (except that since it's currently running, * we can only do it when Jenkins restarts next time.) */ private void updateJenkinsExeIfNeeded() { try { File baseDir = getBaseDir(); URL exe = getClass().getResource("/windows-service/jenkins.exe"); String ourCopy = Util.getDigestOf(exe.openStream()); for (String name : new String[]{"hudson.exe","jenkins.exe"}) { try { File currentCopy = new File(baseDir,name); if(!currentCopy.exists()) continue; String curCopy = new FilePath(currentCopy).digest(); if(ourCopy.equals(curCopy)) continue; // identical File stage = new File(baseDir,name+".new"); FileUtils.copyURLToFile(exe,stage); Kernel32.INSTANCE.MoveFileExA(stage.getAbsolutePath(),currentCopy.getAbsolutePath(),MOVEFILE_DELAY_UNTIL_REBOOT|MOVEFILE_REPLACE_EXISTING); LOGGER.info("Scheduled a replacement of "+name); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Failed to replace "+name,e); } catch (InterruptedException e) { } } } catch (IOException e) { LOGGER.log(Level.SEVERE, "Failed to replace jenkins.exe",e); } }
/** * @param target * If relative, resolved against the location of the symlink. * If absolute, it's absolute. * @throws UnsatisfiedLinkError * If the function is not exported by kernel32. * See http://msdn.microsoft.com/en-us/library/windows/desktop/aa363866(v=vs.85).aspx * for compatibility info. */ public static void createSymbolicLink(File symlink, String target, boolean dirLink) throws IOException { if (!Kernel32.INSTANCE.CreateSymbolicLinkW( new WString(symlink.getPath()), new WString(target), dirLink?Kernel32.SYMBOLIC_LINK_FLAG_DIRECTORY:0)) { throw new WinIOException("Failed to create a symlink "+symlink+" to "+target); } }
/** * Given the process handle, waits for its completion and returns the exit code. */ public static int waitForExitProcess(Pointer hProcess) throws InterruptedException { while (true) { if (Thread.interrupted()) throw new InterruptedException(); Kernel32.INSTANCE.WaitForSingleObject(hProcess,1000); IntByReference exitCode = new IntByReference(); exitCode.setValue(-1); Kernel32.INSTANCE.GetExitCodeProcess(hProcess,exitCode); int v = exitCode.getValue(); if (v !=Kernel32.STILL_ACTIVE) { return v; } } } }
/** * If <tt>hudson.exe</tt> is old compared to our copy, * schedule an overwrite (except that since it's currently running, * we can only do it when Hudson restarts next time.) */ private void updateHudsonExeIfNeeded() { try { File rootDir = Hudson.getInstance().getRootDir(); URL exe = getClass().getResource("/windows-service/hudson.exe"); String ourCopy = Util.getDigestOf(exe.openStream()); File currentCopy = new File(rootDir,"hudson.exe"); if(!currentCopy.exists()) return; String curCopy = new FilePath(currentCopy).digest(); if(ourCopy.equals(curCopy)) return; // identical File stage = new File(rootDir,"hudson.exe.new"); FileUtils.copyURLToFile(exe,stage); Kernel32.INSTANCE.MoveFileExA(stage.getAbsolutePath(),currentCopy.getAbsolutePath(),MOVEFILE_DELAY_UNTIL_REBOOT|MOVEFILE_REPLACE_EXISTING); LOGGER.info("Scheduled a replacement of hudson.exe"); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Failed to replace hudson.exe",e); } catch (InterruptedException e) { LOGGER.log(Level.SEVERE, "Failed to replace hudson.exe",e); } }
/** * Given the process handle, waits for its completion and returns the exit code. */ public static int waitForExitProcess(Pointer hProcess) throws InterruptedException { while (true) { if (Thread.interrupted()) throw new InterruptedException(); Kernel32.INSTANCE.WaitForSingleObject(hProcess,1000); IntByReference exitCode = new IntByReference(); exitCode.setValue(-1); Kernel32.INSTANCE.GetExitCodeProcess(hProcess,exitCode); int v = exitCode.getValue(); if (v !=Kernel32.STILL_ACTIVE) { return v; } } } }
/** * If <tt>hudson.exe</tt> is old compared to our copy, * schedule an overwrite (except that since it's currently running, * we can only do it when Hudson restarts next time.) */ private void updateHudsonExeIfNeeded() { try { File rootDir = Hudson.getInstance().getRootDir(); URL exe = getClass().getResource("/windows-service/hudson.exe"); String ourCopy = Util.getDigestOf(exe.openStream()); File currentCopy = new File(rootDir,"hudson.exe"); if(!currentCopy.exists()) return; String curCopy = new FilePath(currentCopy).digest(); if(ourCopy.equals(curCopy)) return; // identical File stage = new File(rootDir,"hudson.exe.new"); FileUtils.copyURLToFile(exe,stage); Kernel32.INSTANCE.MoveFileExA(stage.getAbsolutePath(),currentCopy.getAbsolutePath(),MOVEFILE_DELAY_UNTIL_REBOOT|MOVEFILE_REPLACE_EXISTING); LOGGER.info("Scheduled a replacement of hudson.exe"); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Failed to replace hudson.exe",e); } catch (InterruptedException e) { LOGGER.log(Level.SEVERE, "Failed to replace hudson.exe",e); } }