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.

FileUtil.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (C) 2012, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.nio.file.Files;
  47. import java.nio.file.LinkOption;
  48. import java.nio.file.NoSuchFileException;
  49. import java.nio.file.Path;
  50. import java.nio.file.attribute.BasicFileAttributeView;
  51. import java.nio.file.attribute.BasicFileAttributes;
  52. import java.nio.file.attribute.FileTime;
  53. import java.nio.file.attribute.PosixFileAttributeView;
  54. import java.nio.file.attribute.PosixFileAttributes;
  55. import java.nio.file.attribute.PosixFilePermission;
  56. import java.text.Normalizer;
  57. import java.text.Normalizer.Form;
  58. import org.eclipse.jgit.lib.Constants;
  59. import org.eclipse.jgit.util.FS.Attributes;
  60. /**
  61. * File utilities using Java 7 NIO2
  62. */
  63. public class FileUtil {
  64. static class Java7BasicAttributes extends Attributes {
  65. Java7BasicAttributes(FS fs, File fPath, boolean exists,
  66. boolean isDirectory, boolean isExecutable,
  67. boolean isSymbolicLink, boolean isRegularFile,
  68. long creationTime, long lastModifiedTime, long length) {
  69. super(fs, fPath, exists, isDirectory, isExecutable, isSymbolicLink,
  70. isRegularFile, creationTime, lastModifiedTime, length);
  71. }
  72. }
  73. /**
  74. * @param path
  75. * @return target path of the symlink
  76. * @throws IOException
  77. */
  78. public static String readSymlink(File path) throws IOException {
  79. Path nioPath = path.toPath();
  80. Path target = Files.readSymbolicLink(nioPath);
  81. String targetString = target.toString();
  82. if (SystemReader.getInstance().isWindows())
  83. targetString = targetString.replace('\\', '/');
  84. else if (SystemReader.getInstance().isMacOS())
  85. targetString = Normalizer.normalize(targetString, Form.NFC);
  86. return targetString;
  87. }
  88. /**
  89. * @param path
  90. * path of the symlink to be created
  91. * @param target
  92. * target of the symlink to be created
  93. * @throws IOException
  94. */
  95. public static void createSymLink(File path, String target)
  96. throws IOException {
  97. Path nioPath = path.toPath();
  98. if (Files.exists(nioPath, LinkOption.NOFOLLOW_LINKS))
  99. Files.delete(nioPath);
  100. if (SystemReader.getInstance().isWindows())
  101. target = target.replace('/', '\\');
  102. Path nioTarget = new File(target).toPath();
  103. Files.createSymbolicLink(nioPath, nioTarget);
  104. }
  105. /**
  106. * @param path
  107. * @return {@code true} if the passed path is a symlink
  108. */
  109. public static boolean isSymlink(File path) {
  110. Path nioPath = path.toPath();
  111. return Files.isSymbolicLink(nioPath);
  112. }
  113. /**
  114. * @param path
  115. * @return lastModified attribute for given path
  116. * @throws IOException
  117. */
  118. public static long lastModified(File path) throws IOException {
  119. Path nioPath = path.toPath();
  120. return Files.getLastModifiedTime(nioPath, LinkOption.NOFOLLOW_LINKS)
  121. .toMillis();
  122. }
  123. /**
  124. * @param path
  125. * @param time
  126. * @throws IOException
  127. */
  128. public static void setLastModified(File path, long time) throws IOException {
  129. Path nioPath = path.toPath();
  130. Files.setLastModifiedTime(nioPath, FileTime.fromMillis(time));
  131. }
  132. /**
  133. * @param path
  134. * @return {@code true} if the given path exists
  135. */
  136. public static boolean exists(File path) {
  137. Path nioPath = path.toPath();
  138. return Files.exists(nioPath, LinkOption.NOFOLLOW_LINKS);
  139. }
  140. /**
  141. * @param path
  142. * @return {@code true} if the given path is hidden
  143. * @throws IOException
  144. */
  145. public static boolean isHidden(File path) throws IOException {
  146. Path nioPath = path.toPath();
  147. return Files.isHidden(nioPath);
  148. }
  149. /**
  150. * @param path
  151. * @param hidden
  152. * @throws IOException
  153. */
  154. public static void setHidden(File path, boolean hidden) throws IOException {
  155. Path nioPath = path.toPath();
  156. Files.setAttribute(nioPath, "dos:hidden", Boolean.valueOf(hidden), //$NON-NLS-1$
  157. LinkOption.NOFOLLOW_LINKS);
  158. }
  159. /**
  160. * @param path
  161. * @return length of the given file
  162. * @throws IOException
  163. */
  164. public static long getLength(File path) throws IOException {
  165. Path nioPath = path.toPath();
  166. if (Files.isSymbolicLink(nioPath))
  167. return Files.readSymbolicLink(nioPath).toString()
  168. .getBytes(Constants.CHARSET).length;
  169. return Files.size(nioPath);
  170. }
  171. /**
  172. * @param path
  173. * @return {@code true} if the given file a directory
  174. */
  175. public static boolean isDirectory(File path) {
  176. Path nioPath = path.toPath();
  177. return Files.isDirectory(nioPath, LinkOption.NOFOLLOW_LINKS);
  178. }
  179. /**
  180. * @param path
  181. * @return {@code true} if the given file is a file
  182. */
  183. public static boolean isFile(File path) {
  184. Path nioPath = path.toPath();
  185. return Files.isRegularFile(nioPath, LinkOption.NOFOLLOW_LINKS);
  186. }
  187. /**
  188. * @param path
  189. * @return {@code true} if the given file can be executed
  190. */
  191. public static boolean canExecute(File path) {
  192. if (!isFile(path))
  193. return false;
  194. return path.canExecute();
  195. }
  196. /**
  197. * @param path
  198. * @param executable
  199. * @return true if succeeded, false if not supported or failed
  200. * @deprecated the implementation is highly platform dependent, consider
  201. * using {@link FS#setExecute(File, boolean)} instead
  202. */
  203. @Deprecated
  204. public static boolean setExecute(File path, boolean executable) {
  205. if (!isFile(path))
  206. return false;
  207. return path.setExecutable(executable);
  208. }
  209. /**
  210. * @param path
  211. * @throws IOException
  212. */
  213. public static void delete(File path) throws IOException {
  214. Path nioPath = path.toPath();
  215. Files.delete(nioPath);
  216. }
  217. static Attributes getFileAttributesBasic(FS fs, File path) {
  218. try {
  219. Path nioPath = path.toPath();
  220. BasicFileAttributes readAttributes = nioPath
  221. .getFileSystem()
  222. .provider()
  223. .getFileAttributeView(nioPath,
  224. BasicFileAttributeView.class,
  225. LinkOption.NOFOLLOW_LINKS).readAttributes();
  226. Attributes attributes = new FileUtil.Java7BasicAttributes(fs, path,
  227. true,
  228. readAttributes.isDirectory(),
  229. fs.supportsExecute() ? path.canExecute() : false,
  230. readAttributes.isSymbolicLink(),
  231. readAttributes.isRegularFile(), //
  232. readAttributes.creationTime().toMillis(), //
  233. readAttributes.lastModifiedTime().toMillis(),
  234. readAttributes.isSymbolicLink() ? Constants
  235. .encode(FileUtils.readSymLink(path)).length
  236. : readAttributes.size());
  237. return attributes;
  238. } catch (NoSuchFileException e) {
  239. return new FileUtil.Java7BasicAttributes(fs, path, false, false,
  240. false, false, false, 0L, 0L, 0L);
  241. } catch (IOException e) {
  242. return new Attributes(path, fs);
  243. }
  244. }
  245. /**
  246. * @param fs
  247. * @param path
  248. * @return file system attributes for the given file
  249. */
  250. public static Attributes getFileAttributesPosix(FS fs, File path) {
  251. try {
  252. Path nioPath = path.toPath();
  253. PosixFileAttributes readAttributes = nioPath
  254. .getFileSystem()
  255. .provider()
  256. .getFileAttributeView(nioPath,
  257. PosixFileAttributeView.class,
  258. LinkOption.NOFOLLOW_LINKS).readAttributes();
  259. Attributes attributes = new FileUtil.Java7BasicAttributes(
  260. fs,
  261. path,
  262. true, //
  263. readAttributes.isDirectory(), //
  264. readAttributes.permissions().contains(
  265. PosixFilePermission.OWNER_EXECUTE),
  266. readAttributes.isSymbolicLink(),
  267. readAttributes.isRegularFile(), //
  268. readAttributes.creationTime().toMillis(), //
  269. readAttributes.lastModifiedTime().toMillis(),
  270. readAttributes.size());
  271. return attributes;
  272. } catch (NoSuchFileException e) {
  273. return new FileUtil.Java7BasicAttributes(fs, path, false, false,
  274. false, false, false, 0L, 0L, 0L);
  275. } catch (IOException e) {
  276. return new Attributes(path, fs);
  277. }
  278. }
  279. /**
  280. * @param file
  281. * @return on Mac: NFC normalized {@link File}, otherwise the passed file
  282. */
  283. public static File normalize(File file) {
  284. if (SystemReader.getInstance().isMacOS()) {
  285. // TODO: Would it be faster to check with isNormalized first
  286. // assuming normalized paths are much more common
  287. String normalized = Normalizer.normalize(file.getPath(),
  288. Normalizer.Form.NFC);
  289. return new File(normalized);
  290. }
  291. return file;
  292. }
  293. /**
  294. * @param name
  295. * @return on Mac: NFC normalized form of given name
  296. */
  297. public static String normalize(String name) {
  298. if (SystemReader.getInstance().isMacOS()) {
  299. if (name == null)
  300. return null;
  301. return Normalizer.normalize(name, Normalizer.Form.NFC);
  302. }
  303. return name;
  304. }
  305. }