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