You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FS_POSIX.java 8.0KB

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