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  	 * @return true if cygwin is found
76  	 */
77  	public static boolean isCygwin() {
78  		final String path = AccessController
79  				.doPrivileged(new PrivilegedAction<String>() {
80  					@Override
81  					public String run() {
82  						return System.getProperty("java.library.path"); //$NON-NLS-1$
83  					}
84  				});
85  		if (path == null)
86  			return false;
87  		File found = FS.searchPath(path, "cygpath.exe"); //$NON-NLS-1$
88  		if (found != null)
89  			cygpath = found.getPath();
90  		return cygpath != null;
91  	}
92  
93  	/**
94  	 * Constructor
95  	 */
96  	public FS_Win32_Cygwin() {
97  		super();
98  	}
99  
100 	/**
101 	 * Constructor
102 	 *
103 	 * @param src
104 	 *            instance whose attributes to copy
105 	 */
106 	protected FS_Win32_Cygwin(FS src) {
107 		super(src);
108 	}
109 
110 	@Override
111 	public FS newInstance() {
112 		return new FS_Win32_Cygwin(this);
113 	}
114 
115 	@Override
116 	public File resolve(final File dir, final String pn) {
117 		String useCygPath = System.getProperty("jgit.usecygpath"); //$NON-NLS-1$
118 		if (useCygPath != null && useCygPath.equals("true")) { //$NON-NLS-1$
119 			String w;
120 			try {
121 				w = readPipe(dir, //
122 					new String[] { cygpath, "--windows", "--absolute", pn }, // //$NON-NLS-1$ //$NON-NLS-2$
123 					"UTF-8"); //$NON-NLS-1$
124 			} catch (CommandFailedException e) {
125 				LOG.warn(e.getMessage());
126 				return null;
127 			}
128 			if (!StringUtils.isEmptyOrNull(w)) {
129 				return new File(w);
130 			}
131 		}
132 		return super.resolve(dir, pn);
133 	}
134 
135 	@Override
136 	protected File userHomeImpl() {
137 		final String home = AccessController
138 				.doPrivileged(new PrivilegedAction<String>() {
139 					@Override
140 					public String run() {
141 						return System.getenv("HOME"); //$NON-NLS-1$
142 					}
143 				});
144 		if (home == null || home.length() == 0)
145 			return super.userHomeImpl();
146 		return resolve(new File("."), home); //$NON-NLS-1$
147 	}
148 
149 	@Override
150 	public ProcessBuilder runInShell(String cmd, String[] args) {
151 		List<String> argv = new ArrayList<>(4 + args.length);
152 		argv.add("sh.exe"); //$NON-NLS-1$
153 		argv.add("-c"); //$NON-NLS-1$
154 		argv.add(cmd + " \"$@\""); //$NON-NLS-1$
155 		argv.add(cmd);
156 		argv.addAll(Arrays.asList(args));
157 		ProcessBuilder proc = new ProcessBuilder();
158 		proc.command(argv);
159 		return proc;
160 	}
161 
162 	/**
163 	 * @since 3.7
164 	 */
165 	@Override
166 	public String relativize(String base, String other) {
167 		final String relativized = super.relativize(base, other);
168 		return relativized.replace(File.separatorChar, '/');
169 	}
170 
171 	/**
172 	 * @since 4.0
173 	 */
174 	@Override
175 	public ProcessResult runHookIfPresent(Repository repository, String hookName,
176 			String[] args, PrintStream outRedirect, PrintStream errRedirect,
177 			String stdinArgs) throws JGitInternalException {
178 		return internalRunHookIfPresent(repository, hookName, args, outRedirect,
179 				errRedirect, stdinArgs);
180 	}
181 
182 	/**
183 	 * @since 3.7
184 	 */
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 }