View Javadoc
1   /*
2    * Copyright (C) 2010, Robin Rosenberg
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  package org.eclipse.jgit.util;
44  
45  import java.io.BufferedReader;
46  import java.io.File;
47  import java.io.IOException;
48  import java.io.InputStreamReader;
49  import java.io.PrintStream;
50  import java.nio.charset.Charset;
51  import java.nio.file.Files;
52  import java.nio.file.Path;
53  import java.nio.file.attribute.PosixFilePermission;
54  import java.util.ArrayList;
55  import java.util.Arrays;
56  import java.util.List;
57  import java.util.Set;
58  
59  import org.eclipse.jgit.api.errors.JGitInternalException;
60  import org.eclipse.jgit.lib.Constants;
61  import org.eclipse.jgit.lib.Repository;
62  
63  /**
64   * Base FS for POSIX based systems
65   *
66   * @since 3.0
67   */
68  public class FS_POSIX extends FS {
69  	private static final int DEFAULT_UMASK = 0022;
70  	private volatile int umask = -1;
71  
72  	/** Default constructor. */
73  	protected FS_POSIX() {
74  	}
75  
76  	/**
77  	 * Constructor
78  	 *
79  	 * @param src
80  	 *            FS to copy some settings from
81  	 */
82  	protected FS_POSIX(FS src) {
83  		super(src);
84  		if (src instanceof FS_POSIX) {
85  			umask = ((FS_POSIX) src).umask;
86  		}
87  	}
88  
89  	@Override
90  	public FS newInstance() {
91  		return new FS_POSIX(this);
92  	}
93  
94  	/**
95  	 * Set the umask, overriding any value observed from the shell.
96  	 *
97  	 * @param umask
98  	 *            mask to apply when creating files.
99  	 * @since 4.0
100 	 */
101 	public void setUmask(int umask) {
102 		this.umask = umask;
103 	}
104 
105 	private int umask() {
106 		int u = umask;
107 		if (u == -1) {
108 			u = readUmask();
109 			umask = u;
110 		}
111 		return u;
112 	}
113 
114 	/** @return mask returned from running {@code umask} command in shell. */
115 	private static int readUmask() {
116 		try {
117 			Process p = Runtime.getRuntime().exec(
118 					new String[] { "sh", "-c", "umask" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
119 					null, null);
120 			try (BufferedReader lineRead = new BufferedReader(
121 					new InputStreamReader(p.getInputStream(), Charset
122 							.defaultCharset().name()))) {
123 				if (p.waitFor() == 0) {
124 					String s = lineRead.readLine();
125 					if (s.matches("0?\\d{3}")) { //$NON-NLS-1$
126 						return Integer.parseInt(s, 8);
127 					}
128 				}
129 				return DEFAULT_UMASK;
130 			}
131 		} catch (Exception e) {
132 			return DEFAULT_UMASK;
133 		}
134 	}
135 
136 	@Override
137 	protected File discoverGitExe() {
138 		String path = SystemReader.getInstance().getenv("PATH"); //$NON-NLS-1$
139 		File gitExe = searchPath(path, "git"); //$NON-NLS-1$
140 
141 		if (gitExe == null) {
142 			if (SystemReader.getInstance().isMacOS()) {
143 				if (searchPath(path, "bash") != null) { //$NON-NLS-1$
144 					// On MacOSX, PATH is shorter when Eclipse is launched from the
145 					// Finder than from a terminal. Therefore try to launch bash as a
146 					// login shell and search using that.
147 					String w = readPipe(userHome(),
148 							new String[]{"bash", "--login", "-c", "which git"}, // //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
149 							Charset.defaultCharset().name());
150 					if (!StringUtils.isEmptyOrNull(w))
151 						gitExe = new File(w);
152 				}
153 			}
154 		}
155 
156 		return gitExe;
157 	}
158 
159 	@Override
160 	public boolean isCaseSensitive() {
161 		return !SystemReader.getInstance().isMacOS();
162 	}
163 
164 	@Override
165 	public boolean supportsExecute() {
166 		return true;
167 	}
168 
169 	@Override
170 	public boolean canExecute(File f) {
171 		return FileUtils.canExecute(f);
172 	}
173 
174 	@Override
175 	public boolean setExecute(File f, boolean canExecute) {
176 		if (!isFile(f))
177 			return false;
178 		if (!canExecute)
179 			return f.setExecutable(false);
180 
181 		try {
182 			Path path = f.toPath();
183 			Set<PosixFilePermission> pset = Files.getPosixFilePermissions(path);
184 
185 			// owner (user) is always allowed to execute.
186 			pset.add(PosixFilePermission.OWNER_EXECUTE);
187 
188 			int mask = umask();
189 			apply(pset, mask, PosixFilePermission.GROUP_EXECUTE, 1 << 3);
190 			apply(pset, mask, PosixFilePermission.OTHERS_EXECUTE, 1);
191 			Files.setPosixFilePermissions(path, pset);
192 			return true;
193 		} catch (IOException e) {
194 			// The interface doesn't allow to throw IOException
195 			final boolean debug = Boolean.parseBoolean(SystemReader
196 					.getInstance().getProperty("jgit.fs.debug")); //$NON-NLS-1$
197 			if (debug)
198 				System.err.println(e);
199 			return false;
200 		}
201 	}
202 
203 	private static void apply(Set<PosixFilePermission> set,
204 			int umask, PosixFilePermission perm, int test) {
205 		if ((umask & test) == 0) {
206 			// If bit is clear in umask, permission is allowed.
207 			set.add(perm);
208 		} else {
209 			// If bit is set in umask, permission is denied.
210 			set.remove(perm);
211 		}
212 	}
213 
214 	@Override
215 	public ProcessBuilder runInShell(String cmd, String[] args) {
216 		List<String> argv = new ArrayList<String>(4 + args.length);
217 		argv.add("sh"); //$NON-NLS-1$
218 		argv.add("-c"); //$NON-NLS-1$
219 		argv.add(cmd + " \"$@\""); //$NON-NLS-1$
220 		argv.add(cmd);
221 		argv.addAll(Arrays.asList(args));
222 		ProcessBuilder proc = new ProcessBuilder();
223 		proc.command(argv);
224 		return proc;
225 	}
226 
227 	/**
228 	 * @since 4.0
229 	 */
230 	@Override
231 	public ProcessResult runHookIfPresent(Repository repository, String hookName,
232 			String[] args, PrintStream outRedirect, PrintStream errRedirect,
233 			String stdinArgs) throws JGitInternalException {
234 		return internalRunHookIfPresent(repository, hookName, args, outRedirect,
235 				errRedirect, stdinArgs);
236 	}
237 
238 	@Override
239 	public boolean retryFailedLockFileCommit() {
240 		return false;
241 	}
242 
243 	@Override
244 	public boolean supportsSymlinks() {
245 		return true;
246 	}
247 
248 	@Override
249 	public void setHidden(File path, boolean hidden) throws IOException {
250 		// no action on POSIX
251 	}
252 
253 	/**
254 	 * @since 3.3
255 	 */
256 	@Override
257 	public Attributes getAttributes(File path) {
258 		return FileUtils.getFileAttributesPosix(this, path);
259 	}
260 
261 	/**
262 	 * @since 3.3
263 	 */
264 	@Override
265 	public File normalize(File file) {
266 		return FileUtils.normalize(file);
267 	}
268 
269 	/**
270 	 * @since 3.3
271 	 */
272 	@Override
273 	public String normalize(String name) {
274 		return FileUtils.normalize(name);
275 	}
276 
277 	/**
278 	 * @since 3.7
279 	 */
280 	@Override
281 	public File findHook(Repository repository, String hookName) {
282 		final File gitdir = repository.getDirectory();
283 		if (gitdir == null) {
284 			return null;
285 		}
286 		final Path hookPath = gitdir.toPath().resolve(Constants.HOOKS)
287 				.resolve(hookName);
288 		if (Files.isExecutable(hookPath))
289 			return hookPath.toFile();
290 		return null;
291 	}
292 }