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.nio.file.FileVisitOption;
51  import java.nio.file.FileVisitResult;
52  import java.nio.file.Files;
53  import java.nio.file.Path;
54  import java.nio.file.SimpleFileVisitor;
55  import java.nio.file.attribute.BasicFileAttributes;
56  import java.util.ArrayList;
57  import java.util.Arrays;
58  import java.util.EnumSet;
59  import java.util.List;
60  
61  import org.eclipse.jgit.errors.CommandFailedException;
62  import org.eclipse.jgit.treewalk.FileTreeIterator.FileEntry;
63  import org.eclipse.jgit.treewalk.FileTreeIterator.FileModeStrategy;
64  import org.eclipse.jgit.treewalk.WorkingTreeIterator.Entry;
65  import org.slf4j.Logger;
66  import org.slf4j.LoggerFactory;
67  
68  
69  /**
70   * FS implementation for Windows
71   *
72   * @since 3.0
73   */
74  public class FS_Win32 extends FS {
75  	private final static Logger LOG = LoggerFactory.getLogger(FS_Win32.class);
76  
77  	private volatile Boolean supportSymlinks;
78  
79  	/**
80  	 * Constructor
81  	 */
82  	public FS_Win32() {
83  		super();
84  	}
85  
86  	/**
87  	 * Constructor
88  	 *
89  	 * @param src
90  	 *            instance whose attributes to copy
91  	 */
92  	protected FS_Win32(FS src) {
93  		super(src);
94  	}
95  
96  	/** {@inheritDoc} */
97  	@Override
98  	public FS newInstance() {
99  		return new FS_Win32(this);
100 	}
101 
102 	/** {@inheritDoc} */
103 	@Override
104 	public boolean supportsExecute() {
105 		return false;
106 	}
107 
108 	/** {@inheritDoc} */
109 	@Override
110 	public boolean canExecute(File f) {
111 		return false;
112 	}
113 
114 	/** {@inheritDoc} */
115 	@Override
116 	public boolean setExecute(File f, boolean canExec) {
117 		return false;
118 	}
119 
120 	/** {@inheritDoc} */
121 	@Override
122 	public boolean isCaseSensitive() {
123 		return false;
124 	}
125 
126 	/** {@inheritDoc} */
127 	@Override
128 	public boolean retryFailedLockFileCommit() {
129 		return true;
130 	}
131 
132 	/** {@inheritDoc} */
133 	@Override
134 	public Entry[] list(File directory, FileModeStrategy fileModeStrategy) {
135 		List<Entry> result = new ArrayList<>();
136 		FS fs = this;
137 		boolean checkExecutable = fs.supportsExecute();
138 		try {
139 			Files.walkFileTree(directory.toPath(),
140 					EnumSet.noneOf(FileVisitOption.class), 1,
141 					new SimpleFileVisitor<Path>() {
142 						@Override
143 						public FileVisitResult visitFile(Path file,
144 								BasicFileAttributes attrs) throws IOException {
145 							File f = file.toFile();
146 							FS.Attributes attributes = new FS.Attributes(fs, f,
147 									true, attrs.isDirectory(),
148 									checkExecutable && f.canExecute(),
149 									attrs.isSymbolicLink(),
150 									attrs.isRegularFile(),
151 									attrs.creationTime().toMillis(),
152 									attrs.lastModifiedTime().toMillis(),
153 									attrs.size());
154 							result.add(new FileEntry(f, fs, attributes,
155 									fileModeStrategy));
156 							return FileVisitResult.CONTINUE;
157 						}
158 
159 						@Override
160 						public FileVisitResult visitFileFailed(Path file,
161 								IOException exc) throws IOException {
162 							// Just ignore it
163 							return FileVisitResult.CONTINUE;
164 						}
165 					});
166 		} catch (IOException e) {
167 			// Ignore
168 		}
169 		if (result.isEmpty()) {
170 			return NO_ENTRIES;
171 		}
172 		return result.toArray(new Entry[result.size()]);
173 	}
174 
175 	/** {@inheritDoc} */
176 	@Override
177 	protected File discoverGitExe() {
178 		String path = SystemReader.getInstance().getenv("PATH"); //$NON-NLS-1$
179 		File gitExe = searchPath(path, "git.exe", "git.cmd"); //$NON-NLS-1$ //$NON-NLS-2$
180 
181 		if (gitExe == null) {
182 			if (searchPath(path, "bash.exe") != null) { //$NON-NLS-1$
183 				// This isn't likely to work, but its worth trying:
184 				// If bash is in $PATH, git should also be in $PATH.
185 				String w;
186 				try {
187 					w = readPipe(userHome(),
188 						new String[]{"bash", "--login", "-c", "which git"}, // //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
189 						Charset.defaultCharset().name());
190 				} catch (CommandFailedException e) {
191 					LOG.warn(e.getMessage());
192 					return null;
193 				}
194 				if (!StringUtils.isEmptyOrNull(w)) {
195 					// The path may be in cygwin/msys notation so resolve it right away
196 					gitExe = resolve(null, w);
197 				}
198 			}
199 		}
200 
201 		return gitExe;
202 	}
203 
204 	/** {@inheritDoc} */
205 	@Override
206 	protected File userHomeImpl() {
207 		String home = SystemReader.getInstance().getenv("HOME"); //$NON-NLS-1$
208 		if (home != null)
209 			return resolve(null, home);
210 		String homeDrive = SystemReader.getInstance().getenv("HOMEDRIVE"); //$NON-NLS-1$
211 		if (homeDrive != null) {
212 			String homePath = SystemReader.getInstance().getenv("HOMEPATH"); //$NON-NLS-1$
213 			if (homePath != null)
214 				return new File(homeDrive, homePath);
215 		}
216 
217 		String homeShare = SystemReader.getInstance().getenv("HOMESHARE"); //$NON-NLS-1$
218 		if (homeShare != null)
219 			return new File(homeShare);
220 
221 		return super.userHomeImpl();
222 	}
223 
224 	/** {@inheritDoc} */
225 	@Override
226 	public ProcessBuilder runInShell(String cmd, String[] args) {
227 		List<String> argv = new ArrayList<>(3 + args.length);
228 		argv.add("cmd.exe"); //$NON-NLS-1$
229 		argv.add("/c"); //$NON-NLS-1$
230 		argv.add(cmd);
231 		argv.addAll(Arrays.asList(args));
232 		ProcessBuilder proc = new ProcessBuilder();
233 		proc.command(argv);
234 		return proc;
235 	}
236 
237 	/** {@inheritDoc} */
238 	@Override
239 	public boolean supportsSymlinks() {
240 		if (supportSymlinks == null)
241 			detectSymlinkSupport();
242 		return Boolean.TRUE.equals(supportSymlinks);
243 	}
244 
245 	private void detectSymlinkSupport() {
246 		File tempFile = null;
247 		try {
248 			tempFile = File.createTempFile("tempsymlinktarget", ""); //$NON-NLS-1$ //$NON-NLS-2$
249 			File linkName = new File(tempFile.getParentFile(), "tempsymlink"); //$NON-NLS-1$
250 			createSymLink(linkName, tempFile.getPath());
251 			supportSymlinks = Boolean.TRUE;
252 			linkName.delete();
253 		} catch (IOException | UnsupportedOperationException
254 				| InternalError e) {
255 			supportSymlinks = Boolean.FALSE;
256 		} finally {
257 			if (tempFile != null)
258 				try {
259 					FileUtils.delete(tempFile);
260 				} catch (IOException e) {
261 					throw new RuntimeException(e); // panic
262 				}
263 		}
264 	}
265 
266 	/** {@inheritDoc} */
267 	@Override
268 	public Attributes getAttributes(File path) {
269 		return FileUtils.getFileAttributesBasic(this, path);
270 	}
271 }