Switch jgit library to the EDL (3-clause BSD)
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / util / FS_Win32_Cygwin.java
blobe7ab4274580769b7742f7b66c28d256921555b8b
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.util;
40 import java.io.BufferedReader;
41 import java.io.File;
42 import java.io.IOException;
43 import java.io.InputStreamReader;
44 import java.security.AccessController;
45 import java.security.PrivilegedAction;
47 class FS_Win32_Cygwin extends FS_Win32 {
48 private static String cygpath;
50 static boolean detect() {
51 final String path = AccessController
52 .doPrivileged(new PrivilegedAction<String>() {
53 public String run() {
54 return System.getProperty("java.library.path");
56 });
57 if (path == null)
58 return false;
59 for (final String p : path.split(";")) {
60 final File e = new File(p, "cygpath.exe");
61 if (e.isFile()) {
62 cygpath = e.getAbsolutePath();
63 return true;
66 return false;
69 protected File resolveImpl(final File dir, final String pn) {
70 try {
71 final Process p;
73 p = Runtime.getRuntime().exec(
74 new String[] { cygpath, "--windows", "--absolute", pn },
75 null, dir);
76 p.getOutputStream().close();
78 final BufferedReader lineRead = new BufferedReader(
79 new InputStreamReader(p.getInputStream(), "UTF-8"));
80 String r = null;
81 try {
82 r = lineRead.readLine();
83 } finally {
84 lineRead.close();
87 for (;;) {
88 try {
89 if (p.waitFor() == 0 && r != null && r.length() > 0)
90 return new File(r);
91 break;
92 } catch (InterruptedException ie) {
93 // Stop bothering me, I have a zombie to reap.
96 } catch (IOException ioe) {
97 // Fall through and use the default return.
100 return super.resolveImpl(dir, pn);
103 @Override
104 protected File userHomeImpl() {
105 final String home = AccessController
106 .doPrivileged(new PrivilegedAction<String>() {
107 public String run() {
108 return System.getenv("HOME");
111 if (home == null || home.length() == 0)
112 return super.userHomeImpl();
113 return resolveImpl(new File("."), home);