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.AccessDeniedException;
52  import java.nio.file.Files;
53  import java.nio.file.Path;
54  import java.nio.file.Paths;
55  import java.nio.file.attribute.PosixFilePermission;
56  import java.text.MessageFormat;
57  import java.util.ArrayList;
58  import java.util.Arrays;
59  import java.util.List;
60  import java.util.Optional;
61  import java.util.Set;
62  import java.util.UUID;
63  
64  import org.eclipse.jgit.annotations.Nullable;
65  import org.eclipse.jgit.api.errors.JGitInternalException;
66  import org.eclipse.jgit.errors.CommandFailedException;
67  import org.eclipse.jgit.errors.ConfigInvalidException;
68  import org.eclipse.jgit.internal.JGitText;
69  import org.eclipse.jgit.lib.ConfigConstants;
70  import org.eclipse.jgit.lib.Constants;
71  import org.eclipse.jgit.lib.Repository;
72  import org.eclipse.jgit.storage.file.FileBasedConfig;
73  import org.slf4j.Logger;
74  import org.slf4j.LoggerFactory;
75  
76  /**
77   * Base FS for POSIX based systems
78   *
79   * @since 3.0
80   */
81  public class FS_POSIX extends FS {
82  	private final static Logger LOG = LoggerFactory.getLogger(FS_POSIX.class);
83  
84  	private static final int DEFAULT_UMASK = 0022;
85  	private volatile int umask = -1;
86  
87  	private volatile boolean supportsUnixNLink = true;
88  
89  	private volatile AtomicFileCreation supportsAtomicCreateNewFile = AtomicFileCreation.UNDEFINED;
90  
91  	private enum AtomicFileCreation {
92  		SUPPORTED, NOT_SUPPORTED, UNDEFINED
93  	}
94  
95  	/**
96  	 * Default constructor.
97  	 */
98  	protected FS_POSIX() {
99  	}
100 
101 	/**
102 	 * Constructor
103 	 *
104 	 * @param src
105 	 *            FS to copy some settings from
106 	 */
107 	protected FS_POSIX(FS src) {
108 		super(src);
109 		if (src instanceof FS_POSIX) {
110 			umask = ((FS_POSIX) src).umask;
111 		}
112 	}
113 
114 	private void determineAtomicFileCreationSupport() {
115 		// @TODO: enhance SystemReader to support this without copying code
116 		AtomicFileCreation ret = getAtomicFileCreationSupportOption(
117 				SystemReader.getInstance().openUserConfig(null, this));
118 		if (ret == AtomicFileCreation.UNDEFINED
119 				&& StringUtils.isEmptyOrNull(SystemReader.getInstance()
120 						.getenv(Constants.GIT_CONFIG_NOSYSTEM_KEY))) {
121 			ret = getAtomicFileCreationSupportOption(
122 					SystemReader.getInstance().openSystemConfig(null, this));
123 		}
124 		supportsAtomicCreateNewFile = ret;
125 	}
126 
127 	private AtomicFileCreation getAtomicFileCreationSupportOption(
128 			FileBasedConfig config) {
129 		try {
130 			config.load();
131 			String value = config.getString(ConfigConstants.CONFIG_CORE_SECTION,
132 					null,
133 					ConfigConstants.CONFIG_KEY_SUPPORTSATOMICFILECREATION);
134 			if (value == null) {
135 				return AtomicFileCreation.UNDEFINED;
136 			}
137 			return StringUtils.toBoolean(value)
138 					? AtomicFileCreation.SUPPORTED
139 					: AtomicFileCreation.NOT_SUPPORTED;
140 		} catch (IOException | ConfigInvalidException e) {
141 			return AtomicFileCreation.SUPPORTED;
142 		}
143 	}
144 
145 	/** {@inheritDoc} */
146 	@Override
147 	public FS newInstance() {
148 		return new FS_POSIX(this);
149 	}
150 
151 	/**
152 	 * Set the umask, overriding any value observed from the shell.
153 	 *
154 	 * @param umask
155 	 *            mask to apply when creating files.
156 	 * @since 4.0
157 	 */
158 	public void setUmask(int umask) {
159 		this.umask = umask;
160 	}
161 
162 	private int umask() {
163 		int u = umask;
164 		if (u == -1) {
165 			u = readUmask();
166 			umask = u;
167 		}
168 		return u;
169 	}
170 
171 	/** @return mask returned from running {@code umask} command in shell. */
172 	private static int readUmask() {
173 		try {
174 			Process p = Runtime.getRuntime().exec(
175 					new String[] { "sh", "-c", "umask" }, //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
176 					null, null);
177 			try (BufferedReader lineRead = new BufferedReader(
178 					new InputStreamReader(p.getInputStream(), Charset
179 							.defaultCharset().name()))) {
180 				if (p.waitFor() == 0) {
181 					String s = lineRead.readLine();
182 					if (s != null && s.matches("0?\\d{3}")) { //$NON-NLS-1$
183 						return Integer.parseInt(s, 8);
184 					}
185 				}
186 				return DEFAULT_UMASK;
187 			}
188 		} catch (Exception e) {
189 			return DEFAULT_UMASK;
190 		}
191 	}
192 
193 	/** {@inheritDoc} */
194 	@Override
195 	protected File discoverGitExe() {
196 		String path = SystemReader.getInstance().getenv("PATH"); //$NON-NLS-1$
197 		File gitExe = searchPath(path, "git"); //$NON-NLS-1$
198 
199 		if (gitExe == null) {
200 			if (SystemReader.getInstance().isMacOS()) {
201 				if (searchPath(path, "bash") != null) { //$NON-NLS-1$
202 					// On MacOSX, PATH is shorter when Eclipse is launched from the
203 					// Finder than from a terminal. Therefore try to launch bash as a
204 					// login shell and search using that.
205 					String w;
206 					try {
207 						w = readPipe(userHome(),
208 							new String[]{"bash", "--login", "-c", "which git"}, // //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
209 							Charset.defaultCharset().name());
210 					} catch (CommandFailedException e) {
211 						LOG.warn(e.getMessage());
212 						return null;
213 					}
214 					if (!StringUtils.isEmptyOrNull(w)) {
215 						gitExe = new File(w);
216 					}
217 				}
218 			}
219 		}
220 
221 		return gitExe;
222 	}
223 
224 	/** {@inheritDoc} */
225 	@Override
226 	public boolean isCaseSensitive() {
227 		return !SystemReader.getInstance().isMacOS();
228 	}
229 
230 	/** {@inheritDoc} */
231 	@Override
232 	public boolean supportsExecute() {
233 		return true;
234 	}
235 
236 	/** {@inheritDoc} */
237 	@Override
238 	public boolean canExecute(File f) {
239 		return FileUtils.canExecute(f);
240 	}
241 
242 	/** {@inheritDoc} */
243 	@Override
244 	public boolean setExecute(File f, boolean canExecute) {
245 		if (!isFile(f))
246 			return false;
247 		if (!canExecute)
248 			return f.setExecutable(false, false);
249 
250 		try {
251 			Path path = FileUtils.toPath(f);
252 			Set<PosixFilePermission> pset = Files.getPosixFilePermissions(path);
253 
254 			// owner (user) is always allowed to execute.
255 			pset.add(PosixFilePermission.OWNER_EXECUTE);
256 
257 			int mask = umask();
258 			apply(pset, mask, PosixFilePermission.GROUP_EXECUTE, 1 << 3);
259 			apply(pset, mask, PosixFilePermission.OTHERS_EXECUTE, 1);
260 			Files.setPosixFilePermissions(path, pset);
261 			return true;
262 		} catch (IOException e) {
263 			// The interface doesn't allow to throw IOException
264 			final boolean debug = Boolean.parseBoolean(SystemReader
265 					.getInstance().getProperty("jgit.fs.debug")); //$NON-NLS-1$
266 			if (debug)
267 				System.err.println(e);
268 			return false;
269 		}
270 	}
271 
272 	private static void apply(Set<PosixFilePermission> set,
273 			int umask, PosixFilePermission perm, int test) {
274 		if ((umask & test) == 0) {
275 			// If bit is clear in umask, permission is allowed.
276 			set.add(perm);
277 		} else {
278 			// If bit is set in umask, permission is denied.
279 			set.remove(perm);
280 		}
281 	}
282 
283 	/** {@inheritDoc} */
284 	@Override
285 	public ProcessBuilder runInShell(String cmd, String[] args) {
286 		List<String> argv = new ArrayList<>(4 + args.length);
287 		argv.add("sh"); //$NON-NLS-1$
288 		argv.add("-c"); //$NON-NLS-1$
289 		argv.add(cmd + " \"$@\""); //$NON-NLS-1$
290 		argv.add(cmd);
291 		argv.addAll(Arrays.asList(args));
292 		ProcessBuilder proc = new ProcessBuilder();
293 		proc.command(argv);
294 		return proc;
295 	}
296 
297 	/** {@inheritDoc} */
298 	@Override
299 	public ProcessResult runHookIfPresent(Repository repository, String hookName,
300 			String[] args, PrintStream outRedirect, PrintStream errRedirect,
301 			String stdinArgs) throws JGitInternalException {
302 		return internalRunHookIfPresent(repository, hookName, args, outRedirect,
303 				errRedirect, stdinArgs);
304 	}
305 
306 	/** {@inheritDoc} */
307 	@Override
308 	public boolean retryFailedLockFileCommit() {
309 		return false;
310 	}
311 
312 	/** {@inheritDoc} */
313 	@Override
314 	public boolean supportsSymlinks() {
315 		return true;
316 	}
317 
318 	/** {@inheritDoc} */
319 	@Override
320 	public void setHidden(File path, boolean hidden) throws IOException {
321 		// no action on POSIX
322 	}
323 
324 	/** {@inheritDoc} */
325 	@Override
326 	public Attributes getAttributes(File path) {
327 		return FileUtils.getFileAttributesPosix(this, path);
328 	}
329 
330 	/** {@inheritDoc} */
331 	@Override
332 	public File normalize(File file) {
333 		return FileUtils.normalize(file);
334 	}
335 
336 	/** {@inheritDoc} */
337 	@Override
338 	public String normalize(String name) {
339 		return FileUtils.normalize(name);
340 	}
341 
342 	/** {@inheritDoc} */
343 	@Override
344 	public File findHook(Repository repository, String hookName) {
345 		final File gitdir = repository.getDirectory();
346 		if (gitdir == null) {
347 			return null;
348 		}
349 		final Path hookPath = gitdir.toPath().resolve(Constants.HOOKS)
350 				.resolve(hookName);
351 		if (Files.isExecutable(hookPath))
352 			return hookPath.toFile();
353 		return null;
354 	}
355 
356 	/** {@inheritDoc} */
357 	@Override
358 	public boolean supportsAtomicCreateNewFile() {
359 		if (supportsAtomicCreateNewFile == AtomicFileCreation.UNDEFINED) {
360 			determineAtomicFileCreationSupport();
361 		}
362 		return supportsAtomicCreateNewFile == AtomicFileCreation.SUPPORTED;
363 	}
364 
365 	@Override
366 	@SuppressWarnings("boxing")
367 	/**
368 	 * {@inheritDoc}
369 	 * <p>
370 	 * An implementation of the File#createNewFile() semantics which works also
371 	 * on NFS. If the config option
372 	 * {@code core.supportsAtomicCreateNewFile = true} (which is the default)
373 	 * then simply File#createNewFile() is called.
374 	 *
375 	 * But if {@code core.supportsAtomicCreateNewFile = false} then after
376 	 * successful creation of the lock file a hard link to that lock file is
377 	 * created and the attribute nlink of the lock file is checked to be 2. If
378 	 * multiple clients manage to create the same lock file nlink would be
379 	 * greater than 2 showing the error.
380 	 *
381 	 * @see "https://www.time-travellers.org/shane/papers/NFS_considered_harmful.html"
382 	 *
383 	 * @deprecated use {@link FS_POSIX#createNewFileAtomic(File)} instead
384 	 * @since 4.5
385 	 */
386 	@Deprecated
387 	public boolean createNewFile(File lock) throws IOException {
388 		if (!lock.createNewFile()) {
389 			return false;
390 		}
391 		if (supportsAtomicCreateNewFile() || !supportsUnixNLink) {
392 			return true;
393 		}
394 		Path lockPath = lock.toPath();
395 		Path link = null;
396 		try {
397 			link = Files.createLink(
398 					Paths.get(lock.getAbsolutePath() + ".lnk"), //$NON-NLS-1$
399 					lockPath);
400 			Integer nlink = (Integer) (Files.getAttribute(lockPath,
401 					"unix:nlink")); //$NON-NLS-1$
402 			if (nlink > 2) {
403 				LOG.warn(MessageFormat.format(
404 						JGitText.get().failedAtomicFileCreation, lockPath,
405 						nlink));
406 				return false;
407 			} else if (nlink < 2) {
408 				supportsUnixNLink = false;
409 			}
410 			return true;
411 		} catch (UnsupportedOperationException | IllegalArgumentException e) {
412 			supportsUnixNLink = false;
413 			return true;
414 		} finally {
415 			if (link != null) {
416 				Files.delete(link);
417 			}
418 		}
419 	}
420 
421 	/**
422 	 * {@inheritDoc}
423 	 * <p>
424 	 * An implementation of the File#createNewFile() semantics which can create
425 	 * a unique file atomically also on NFS. If the config option
426 	 * {@code core.supportsAtomicCreateNewFile = true} (which is the default)
427 	 * then simply File#createNewFile() is called.
428 	 *
429 	 * But if {@code core.supportsAtomicCreateNewFile = false} then after
430 	 * successful creation of the lock file a hard link to that lock file is
431 	 * created and the attribute nlink of the lock file is checked to be 2. If
432 	 * multiple clients manage to create the same lock file nlink would be
433 	 * greater than 2 showing the error. The hard link needs to be retained
434 	 * until the corresponding file is no longer needed in order to prevent that
435 	 * another process can create the same file concurrently using another NFS
436 	 * client which might not yet see the file due to caching.
437 	 *
438 	 * @see "https://www.time-travellers.org/shane/papers/NFS_considered_harmful.html"
439 	 * @param file
440 	 *            the unique file to be created atomically
441 	 * @return LockToken this lock token must be held until the file is no
442 	 *         longer needed
443 	 * @throws IOException
444 	 * @since 5.0
445 	 */
446 	@Override
447 	public LockToken createNewFileAtomic(File file) throws IOException {
448 		if (!file.createNewFile()) {
449 			return token(false, null);
450 		}
451 		if (supportsAtomicCreateNewFile() || !supportsUnixNLink) {
452 			return token(true, null);
453 		}
454 		Path link = null;
455 		Path path = file.toPath();
456 		try {
457 			link = Files.createLink(Paths.get(uniqueLinkPath(file)), path);
458 			Integer nlink = (Integer) (Files.getAttribute(path,
459 					"unix:nlink")); //$NON-NLS-1$
460 			if (nlink.intValue() > 2) {
461 				LOG.warn(MessageFormat.format(
462 						JGitText.get().failedAtomicFileCreation, path, nlink));
463 				return token(false, link);
464 			} else if (nlink.intValue() < 2) {
465 				supportsUnixNLink = false;
466 			}
467 			return token(true, link);
468 		} catch (UnsupportedOperationException | IllegalArgumentException
469 				| AccessDeniedException | SecurityException e) {
470 			supportsUnixNLink = false;
471 			return token(true, link);
472 		}
473 	}
474 
475 	private static LockToken token(boolean created, @Nullable Path p) {
476 		return ((p != null) && Files.exists(p))
477 				? new LockToken(created, Optional.of(p))
478 				: new LockToken(created, Optional.empty());
479 	}
480 
481 	private static String uniqueLinkPath(File file) {
482 		UUID id = UUID.randomUUID();
483 		return file.getAbsolutePath() + "." //$NON-NLS-1$
484 				+ Long.toHexString(id.getMostSignificantBits())
485 				+ Long.toHexString(id.getLeastSignificantBits());
486 	}
487 }