Use the user's Cygwin home directory if invoked by a Cygwin shell
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / util / FS_Win32_Cygwin.java
blob6f42e786364501a5d25bb201189c441fbaf68961
1 /*
2 * Copyright (C) 2008 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.jgit.util;
19 import java.io.BufferedReader;
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.security.AccessController;
24 import java.security.PrivilegedAction;
26 class FS_Win32_Cygwin extends FS_Win32 {
27 private static String cygpath;
29 static boolean detect() {
30 final String path = AccessController
31 .doPrivileged(new PrivilegedAction<String>() {
32 public String run() {
33 return System.getProperty("java.library.path");
35 });
36 if (path == null)
37 return false;
38 for (final String p : path.split(";")) {
39 final File e = new File(p, "cygpath.exe");
40 if (e.isFile()) {
41 cygpath = e.getAbsolutePath();
42 return true;
45 return false;
48 protected File resolveImpl(final File dir, final String pn) {
49 try {
50 final Process p;
52 p = Runtime.getRuntime().exec(
53 new String[] { cygpath, "--windows", "--absolute", pn },
54 null, dir);
55 p.getOutputStream().close();
57 final BufferedReader lineRead = new BufferedReader(
58 new InputStreamReader(p.getInputStream(), "UTF-8"));
59 String r = null;
60 try {
61 r = lineRead.readLine();
62 } finally {
63 lineRead.close();
66 for (;;) {
67 try {
68 if (p.waitFor() == 0 && r != null && r.length() > 0)
69 return new File(r);
70 break;
71 } catch (InterruptedException ie) {
72 // Stop bothering me, I have a zombie to reap.
75 } catch (IOException ioe) {
76 // Fall through and use the default return.
79 return super.resolveImpl(dir, pn);
82 @Override
83 protected File userHomeImpl() {
84 final String home = AccessController
85 .doPrivileged(new PrivilegedAction<String>() {
86 public String run() {
87 return System.getenv("HOME");
89 });
90 if (home == null || home.length() == 0)
91 return super.userHomeImpl();
92 return resolveImpl(new File("."), home);