Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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