Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

FS_Win32.java 6.0KB

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