Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

FS_Win32.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.util.ArrayList;
  49. import java.util.Arrays;
  50. import java.util.List;
  51. /**
  52. * FS implementation for Windows
  53. *
  54. * @since 3.0
  55. */
  56. public class FS_Win32 extends FS {
  57. private volatile Boolean supportSymlinks;
  58. /**
  59. * Constructor
  60. */
  61. public FS_Win32() {
  62. super();
  63. }
  64. /**
  65. * Constructor
  66. *
  67. * @param src
  68. * instance whose attributes to copy
  69. */
  70. protected FS_Win32(FS src) {
  71. super(src);
  72. }
  73. public FS newInstance() {
  74. return new FS_Win32(this);
  75. }
  76. public boolean supportsExecute() {
  77. return false;
  78. }
  79. public boolean canExecute(final File f) {
  80. return false;
  81. }
  82. public boolean setExecute(final File f, final boolean canExec) {
  83. return false;
  84. }
  85. @Override
  86. public boolean isCaseSensitive() {
  87. return false;
  88. }
  89. @Override
  90. public boolean retryFailedLockFileCommit() {
  91. return true;
  92. }
  93. @Override
  94. protected File discoverGitExe() {
  95. String path = SystemReader.getInstance().getenv("PATH"); //$NON-NLS-1$
  96. File gitExe = searchPath(path, "git.exe", "git.cmd"); //$NON-NLS-1$ //$NON-NLS-2$
  97. if (gitExe == null) {
  98. if (searchPath(path, "bash.exe") != null) { //$NON-NLS-1$
  99. // This isn't likely to work, but its worth trying:
  100. // If bash is in $PATH, git should also be in $PATH.
  101. String w = readPipe(userHome(),
  102. new String[]{"bash", "--login", "-c", "which git"}, // //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
  103. Charset.defaultCharset().name());
  104. if (!StringUtils.isEmptyOrNull(w))
  105. // The path may be in cygwin/msys notation so resolve it right away
  106. gitExe = resolve(null, w);
  107. }
  108. }
  109. return gitExe;
  110. }
  111. @Override
  112. protected File userHomeImpl() {
  113. String home = SystemReader.getInstance().getenv("HOME"); //$NON-NLS-1$
  114. if (home != null)
  115. return resolve(null, home);
  116. String homeDrive = SystemReader.getInstance().getenv("HOMEDRIVE"); //$NON-NLS-1$
  117. if (homeDrive != null) {
  118. String homePath = SystemReader.getInstance().getenv("HOMEPATH"); //$NON-NLS-1$
  119. if (homePath != null)
  120. return new File(homeDrive, homePath);
  121. }
  122. String homeShare = SystemReader.getInstance().getenv("HOMESHARE"); //$NON-NLS-1$
  123. if (homeShare != null)
  124. return new File(homeShare);
  125. return super.userHomeImpl();
  126. }
  127. @Override
  128. public ProcessBuilder runInShell(String cmd, String[] args) {
  129. List<String> argv = new ArrayList<String>(3 + args.length);
  130. argv.add("cmd.exe"); //$NON-NLS-1$
  131. argv.add("/c"); //$NON-NLS-1$
  132. argv.add(cmd);
  133. argv.addAll(Arrays.asList(args));
  134. ProcessBuilder proc = new ProcessBuilder();
  135. proc.command(argv);
  136. return proc;
  137. }
  138. @Override
  139. public boolean supportsSymlinks() {
  140. if (supportSymlinks == null)
  141. detectSymlinkSupport();
  142. return Boolean.TRUE.equals(supportSymlinks);
  143. }
  144. private void detectSymlinkSupport() {
  145. File tempFile = null;
  146. try {
  147. tempFile = File.createTempFile("tempsymlinktarget", ""); //$NON-NLS-1$ //$NON-NLS-2$
  148. File linkName = new File(tempFile.getParentFile(), "tempsymlink"); //$NON-NLS-1$
  149. FileUtil.createSymLink(linkName, tempFile.getPath());
  150. supportSymlinks = Boolean.TRUE;
  151. linkName.delete();
  152. } catch (IOException | UnsupportedOperationException e) {
  153. supportSymlinks = Boolean.FALSE;
  154. } finally {
  155. if (tempFile != null)
  156. try {
  157. FileUtils.delete(tempFile);
  158. } catch (IOException e) {
  159. throw new RuntimeException(e); // panic
  160. }
  161. }
  162. }
  163. @Override
  164. public boolean isSymLink(File path) throws IOException {
  165. return FileUtil.isSymlink(path);
  166. }
  167. @Override
  168. public long lastModified(File path) throws IOException {
  169. return FileUtil.lastModified(path);
  170. }
  171. @Override
  172. public void setLastModified(File path, long time) throws IOException {
  173. FileUtil.setLastModified(path, time);
  174. }
  175. @Override
  176. public void delete(File path) throws IOException {
  177. FileUtil.delete(path);
  178. }
  179. @Override
  180. public long length(File f) throws IOException {
  181. return FileUtil.getLength(f);
  182. }
  183. @Override
  184. public boolean exists(File path) {
  185. return FileUtil.exists(path);
  186. }
  187. @Override
  188. public boolean isDirectory(File path) {
  189. return FileUtil.isDirectory(path);
  190. }
  191. @Override
  192. public boolean isFile(File path) {
  193. return FileUtil.isFile(path);
  194. }
  195. @Override
  196. public boolean isHidden(File path) throws IOException {
  197. return FileUtil.isHidden(path);
  198. }
  199. @Override
  200. public void setHidden(File path, boolean hidden) throws IOException {
  201. FileUtil.setHidden(path, hidden);
  202. }
  203. @Override
  204. public String readSymLink(File path) throws IOException {
  205. return FileUtil.readSymlink(path);
  206. }
  207. @Override
  208. public void createSymLink(File path, String target) throws IOException {
  209. FileUtil.createSymLink(path, target);
  210. }
  211. /**
  212. * @since 3.3
  213. */
  214. @Override
  215. public Attributes getAttributes(File path) {
  216. return FileUtil.getFileAttributesBasic(this, path);
  217. }
  218. }