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