View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.util;
45  
46  import java.io.File;
47  import java.io.PrintStream;
48  import java.nio.file.Files;
49  import java.nio.file.Path;
50  import java.security.AccessController;
51  import java.security.PrivilegedAction;
52  import java.util.ArrayList;
53  import java.util.Arrays;
54  import java.util.List;
55  
56  import org.eclipse.jgit.api.errors.JGitInternalException;
57  import org.eclipse.jgit.errors.CommandFailedException;
58  import org.eclipse.jgit.lib.Constants;
59  import org.eclipse.jgit.lib.Repository;
60  import org.slf4j.Logger;
61  import org.slf4j.LoggerFactory;
62  
63  /**
64   * FS implementation for Cygwin on Windows
65   *
66   * @since 3.0
67   */
68  public class FS_Win32_Cygwin extends FS_Win32 {
69  	private final static Logger LOG = LoggerFactory
70  			.getLogger(FS_Win32_Cygwin.class);
71  
72  	private static String cygpath;
73  
74  	/**
75  	 * Whether cygwin is found
76  	 *
77  	 * @return true if cygwin is found
78  	 */
79  	public static boolean isCygwin() {
80  		final String path = AccessController
81  				.doPrivileged(new PrivilegedAction<String>() {
82  					@Override
83  					public String run() {
84  						return System.getProperty("java.library.path"); //$NON-NLS-1$
85  					}
86  				});
87  		if (path == null)
88  			return false;
89  		File found = FS.searchPath(path, "cygpath.exe"); //$NON-NLS-1$
90  		if (found != null)
91  			cygpath = found.getPath();
92  		return cygpath != null;
93  	}
94  
95  	/**
96  	 * Constructor
97  	 */
98  	public FS_Win32_Cygwin() {
99  		super();
100 	}
101 
102 	/**
103 	 * Constructor
104 	 *
105 	 * @param src
106 	 *            instance whose attributes to copy
107 	 */
108 	protected FS_Win32_Cygwin(FS src) {
109 		super(src);
110 	}
111 
112 	/** {@inheritDoc} */
113 	@Override
114 	public FS newInstance() {
115 		return new FS_Win32_Cygwin(this);
116 	}
117 
118 	/** {@inheritDoc} */
119 	@Override
120 	public File resolve(final File dir, final String pn) {
121 		String useCygPath = System.getProperty("jgit.usecygpath"); //$NON-NLS-1$
122 		if (useCygPath != null && useCygPath.equals("true")) { //$NON-NLS-1$
123 			String w;
124 			try {
125 				w = readPipe(dir, //
126 					new String[] { cygpath, "--windows", "--absolute", pn }, // //$NON-NLS-1$ //$NON-NLS-2$
127 					"UTF-8"); //$NON-NLS-1$
128 			} catch (CommandFailedException e) {
129 				LOG.warn(e.getMessage());
130 				return null;
131 			}
132 			if (!StringUtils.isEmptyOrNull(w)) {
133 				return new File(w);
134 			}
135 		}
136 		return super.resolve(dir, pn);
137 	}
138 
139 	/** {@inheritDoc} */
140 	@Override
141 	protected File userHomeImpl() {
142 		final String home = AccessController
143 				.doPrivileged(new PrivilegedAction<String>() {
144 					@Override
145 					public String run() {
146 						return System.getenv("HOME"); //$NON-NLS-1$
147 					}
148 				});
149 		if (home == null || home.length() == 0)
150 			return super.userHomeImpl();
151 		return resolve(new File("."), home); //$NON-NLS-1$
152 	}
153 
154 	/** {@inheritDoc} */
155 	@Override
156 	public ProcessBuilder runInShell(String cmd, String[] args) {
157 		List<String> argv = new ArrayList<>(4 + args.length);
158 		argv.add("sh.exe"); //$NON-NLS-1$
159 		argv.add("-c"); //$NON-NLS-1$
160 		argv.add(cmd + " \"$@\""); //$NON-NLS-1$
161 		argv.add(cmd);
162 		argv.addAll(Arrays.asList(args));
163 		ProcessBuilder proc = new ProcessBuilder();
164 		proc.command(argv);
165 		return proc;
166 	}
167 
168 	/** {@inheritDoc} */
169 	@Override
170 	public String relativize(String base, String other) {
171 		final String relativized = super.relativize(base, other);
172 		return relativized.replace(File.separatorChar, '/');
173 	}
174 
175 	/** {@inheritDoc} */
176 	@Override
177 	public ProcessResult runHookIfPresent(Repository repository, String hookName,
178 			String[] args, PrintStream outRedirect, PrintStream errRedirect,
179 			String stdinArgs) throws JGitInternalException {
180 		return internalRunHookIfPresent(repository, hookName, args, outRedirect,
181 				errRedirect, stdinArgs);
182 	}
183 
184 	/** {@inheritDoc} */
185 	@Override
186 	public File findHook(Repository repository, String hookName) {
187 		final File gitdir = repository.getDirectory();
188 		if (gitdir == null) {
189 			return null;
190 		}
191 		final Path hookPath = gitdir.toPath().resolve(Constants.HOOKS)
192 				.resolve(hookName);
193 		if (Files.isExecutable(hookPath))
194 			return hookPath.toFile();
195 		return null;
196 	}
197 }