File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\jna\Kernel32Utils.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util.jna;
25
26 import java.io.*;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
30 import com.sun.jna.Native;
31 import com.sun.jna.Pointer;
32 import com.sun.jna.ptr.IntByReference;
33 import com.sun.jna.WString;
34
35 /**
36 *
37 * @author Kohsuke Kawaguchi
38 */
39 public class Kernel32Utils {
40 /**
41 * Given the process handle, waits for its completion and returns the exit code.
42 */
43 public static int waitForExitProcess(Pointer hProcess) throws InterruptedException {
44 while (true) {
45 if (Thread.interrupted())
46 throw new InterruptedException();
47
48 Kernel32.INSTANCE.WaitForSingleObject(hProcess,1000);
49 IntByReference exitCode = new IntByReference();
50 exitCode.setValue(-1);
51 Kernel32.INSTANCE.GetExitCodeProcess(hProcess,exitCode);
52
53 int v = exitCode.getValue();
54 if (v !=Kernel32.STILL_ACTIVE) {
55 return v;
56 }
57 }
58 }
59
60 public static int getWin32FileAttributes(File file) throws IOException {
61 // allow lookup of paths longer than MAX_PATH
62 // http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx
63 String canonicalPath = file.getCanonicalPath();
64 String path;
65 if(canonicalPath.length() < 260) {
66 // path is short, use as-is
67 path = canonicalPath;
68 } else if(canonicalPath.startsWith("\\\\")) {
69 // network share
70 // \\server\share --> \\?\UNC\server\share
71 path = "\\\\?\\UNC\\" + canonicalPath.substring(2);
72 } else {
73 // prefix, canonical path should be normalized and absolute so this should work.
74 path = "\\\\?\\" + canonicalPath;
75 }
76 return Kernel32.INSTANCE.GetFileAttributesW(new WString(path));
77 }
78
79 public static boolean isJunctionOrSymlink(File file) throws IOException {
80 try {
81 return (file.exists() && (Kernel32.FILE_ATTRIBUTE_REPARSE_POINT & getWin32FileAttributes(file)) != 0);
82 } catch (UnsupportedOperationException e) {
83 return false;
84 } catch (LinkageError e) {
85 return false;
86 }
87 }
88
89 /*package*/ static Kernel32 load() {
90 try {
91 return (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
92 } catch (Throwable e) {
93 LOGGER.log(Level.SEVERE, "Failed to load Kernel32", e);
94 return InitializationErrorInvocationHandler.create(Kernel32.class,e);
95 }
96 }
97
98 private static final Logger LOGGER = Logger.getLogger(Kernel32Utils.class.getName());
99
100 }
101