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_Win32.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.util;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.nio.charset.Charset;
  48. import java.nio.file.FileVisitOption;
  49. import java.nio.file.FileVisitResult;
  50. import java.nio.file.Files;
  51. import java.nio.file.Path;
  52. import java.nio.file.SimpleFileVisitor;
  53. import java.nio.file.attribute.BasicFileAttributes;
  54. import java.util.ArrayList;
  55. import java.util.Arrays;
  56. import java.util.EnumSet;
  57. import java.util.List;
  58. import org.eclipse.jgit.errors.CommandFailedException;
  59. import org.eclipse.jgit.treewalk.FileTreeIterator.FileEntry;
  60. import org.eclipse.jgit.treewalk.FileTreeIterator.FileModeStrategy;
  61. import org.eclipse.jgit.treewalk.WorkingTreeIterator.Entry;
  62. import org.slf4j.Logger;
  63. import org.slf4j.LoggerFactory;
  64. /**
  65. * FS implementation for Windows
  66. *
  67. * @since 3.0
  68. */
  69. public class FS_Win32 extends FS {
  70. private final static Logger LOG = LoggerFactory.getLogger(FS_Win32.class);
  71. private volatile Boolean supportSymlinks;
  72. /**
  73. * Constructor
  74. */
  75. public FS_Win32() {
  76. super();
  77. }
  78. /**
  79. * Constructor
  80. *
  81. * @param src
  82. * instance whose attributes to copy
  83. */
  84. protected FS_Win32(FS src) {
  85. super(src);
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. public FS newInstance() {
  90. return new FS_Win32(this);
  91. }
  92. /** {@inheritDoc} */
  93. @Override
  94. public boolean supportsExecute() {
  95. return false;
  96. }
  97. /** {@inheritDoc} */
  98. @Override
  99. public boolean canExecute(File f) {
  100. return false;
  101. }
  102. /** {@inheritDoc} */
  103. @Override
  104. public boolean setExecute(File f, boolean canExec) {
  105. return false;
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. public boolean isCaseSensitive() {
  110. return false;
  111. }
  112. /** {@inheritDoc} */
  113. @Override
  114. public boolean retryFailedLockFileCommit() {
  115. return true;
  116. }
  117. /** {@inheritDoc} */
  118. @Override
  119. public Entry[] list(File directory, FileModeStrategy fileModeStrategy) {
  120. List<Entry> result = new ArrayList<>();
  121. FS fs = this;
  122. boolean checkExecutable = fs.supportsExecute();
  123. try {
  124. Files.walkFileTree(directory.toPath(),
  125. EnumSet.noneOf(FileVisitOption.class), 1,
  126. new SimpleFileVisitor<Path>() {
  127. @Override
  128. public FileVisitResult visitFile(Path file,
  129. BasicFileAttributes attrs) throws IOException {
  130. File f = file.toFile();
  131. FS.Attributes attributes = new FS.Attributes(fs, f,
  132. true, attrs.isDirectory(),
  133. checkExecutable && f.canExecute(),
  134. attrs.isSymbolicLink(),
  135. attrs.isRegularFile(),
  136. attrs.creationTime().toMillis(),
  137. attrs.lastModifiedTime().toInstant(),
  138. attrs.size());
  139. result.add(new FileEntry(f, fs, attributes,
  140. fileModeStrategy));
  141. return FileVisitResult.CONTINUE;
  142. }
  143. @Override
  144. public FileVisitResult visitFileFailed(Path file,
  145. IOException exc) throws IOException {
  146. // Just ignore it
  147. return FileVisitResult.CONTINUE;
  148. }
  149. });
  150. } catch (IOException e) {
  151. // Ignore
  152. }
  153. if (result.isEmpty()) {
  154. return NO_ENTRIES;
  155. }
  156. return result.toArray(new Entry[0]);
  157. }
  158. /** {@inheritDoc} */
  159. @Override
  160. protected File discoverGitExe() {
  161. String path = SystemReader.getInstance().getenv("PATH"); //$NON-NLS-1$
  162. File gitExe = searchPath(path, "git.exe", "git.cmd"); //$NON-NLS-1$ //$NON-NLS-2$
  163. if (gitExe == null) {
  164. if (searchPath(path, "bash.exe") != null) { //$NON-NLS-1$
  165. // This isn't likely to work, but its worth trying:
  166. // If bash is in $PATH, git should also be in $PATH.
  167. String w;
  168. try {
  169. w = readPipe(userHome(),
  170. new String[]{"bash", "--login", "-c", "which git"}, // //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  171. Charset.defaultCharset().name());
  172. } catch (CommandFailedException e) {
  173. LOG.warn(e.getMessage());
  174. return null;
  175. }
  176. if (!StringUtils.isEmptyOrNull(w)) {
  177. // The path may be in cygwin/msys notation so resolve it right away
  178. gitExe = resolve(null, w);
  179. }
  180. }
  181. }
  182. return gitExe;
  183. }
  184. /** {@inheritDoc} */
  185. @Override
  186. protected File userHomeImpl() {
  187. String home = SystemReader.getInstance().getenv("HOME"); //$NON-NLS-1$
  188. if (home != null)
  189. return resolve(null, home);
  190. String homeDrive = SystemReader.getInstance().getenv("HOMEDRIVE"); //$NON-NLS-1$
  191. if (homeDrive != null) {
  192. String homePath = SystemReader.getInstance().getenv("HOMEPATH"); //$NON-NLS-1$
  193. if (homePath != null)
  194. return new File(homeDrive, homePath);
  195. }
  196. String homeShare = SystemReader.getInstance().getenv("HOMESHARE"); //$NON-NLS-1$
  197. if (homeShare != null)
  198. return new File(homeShare);
  199. return super.userHomeImpl();
  200. }
  201. /** {@inheritDoc} */
  202. @Override
  203. public ProcessBuilder runInShell(String cmd, String[] args) {
  204. List<String> argv = new ArrayList<>(3 + args.length);
  205. argv.add("cmd.exe"); //$NON-NLS-1$
  206. argv.add("/c"); //$NON-NLS-1$
  207. argv.add(cmd);
  208. argv.addAll(Arrays.asList(args));
  209. ProcessBuilder proc = new ProcessBuilder();
  210. proc.command(argv);
  211. return proc;
  212. }
  213. /** {@inheritDoc} */
  214. @Override
  215. public boolean supportsSymlinks() {
  216. if (supportSymlinks == null)
  217. detectSymlinkSupport();
  218. return Boolean.TRUE.equals(supportSymlinks);
  219. }
  220. private void detectSymlinkSupport() {
  221. File tempFile = null;
  222. try {
  223. tempFile = File.createTempFile("tempsymlinktarget", ""); //$NON-NLS-1$ //$NON-NLS-2$
  224. File linkName = new File(tempFile.getParentFile(), "tempsymlink"); //$NON-NLS-1$
  225. createSymLink(linkName, tempFile.getPath());
  226. supportSymlinks = Boolean.TRUE;
  227. linkName.delete();
  228. } catch (IOException | UnsupportedOperationException
  229. | InternalError e) {
  230. supportSymlinks = Boolean.FALSE;
  231. } finally {
  232. if (tempFile != null)
  233. try {
  234. FileUtils.delete(tempFile);
  235. } catch (IOException e) {
  236. throw new RuntimeException(e); // panic
  237. }
  238. }
  239. }
  240. /** {@inheritDoc} */
  241. @Override
  242. public Attributes getAttributes(File path) {
  243. return FileUtils.getFileAttributesBasic(this, path);
  244. }
  245. }