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