You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FSTest.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2012-2013, 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 static java.time.Instant.EPOCH;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assume.assumeTrue;
  49. import java.io.File;
  50. import java.io.IOException;
  51. import java.nio.charset.Charset;
  52. import java.nio.file.Files;
  53. import java.nio.file.Path;
  54. import java.nio.file.attribute.FileTime;
  55. import java.nio.file.attribute.PosixFileAttributeView;
  56. import java.nio.file.attribute.PosixFilePermission;
  57. import java.time.Duration;
  58. import java.time.ZoneId;
  59. import java.time.format.DateTimeFormatter;
  60. import java.util.Locale;
  61. import java.util.Set;
  62. import java.util.concurrent.TimeUnit;
  63. import org.eclipse.jgit.errors.CommandFailedException;
  64. import org.eclipse.jgit.junit.RepositoryTestCase;
  65. import org.eclipse.jgit.lib.RepositoryCache;
  66. import org.junit.After;
  67. import org.junit.Assume;
  68. import org.junit.Before;
  69. import org.junit.Test;
  70. public class FSTest {
  71. private File trash;
  72. @Before
  73. public void setUp() throws Exception {
  74. trash = File.createTempFile("tmp_", "");
  75. trash.delete();
  76. assertTrue("mkdir " + trash, trash.mkdir());
  77. }
  78. @After
  79. public void tearDown() throws Exception {
  80. FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.RETRY);
  81. }
  82. /**
  83. * The old File methods traverse symbolic links and look at the targets.
  84. * With symbolic links we usually want to modify/look at the link. For some
  85. * reason the executable attribute seems to always look at the target, but
  86. * for the other attributes like lastModified, hidden and exists we must
  87. * differ between the link and the target.
  88. *
  89. * @throws IOException
  90. * @throws InterruptedException
  91. */
  92. @Test
  93. public void testSymlinkAttributes() throws IOException, InterruptedException {
  94. Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  95. FS fs = FS.DETECTED;
  96. File link = new File(trash, "ä");
  97. File target = new File(trash, "å");
  98. fs.createSymLink(link, "å");
  99. assertTrue(fs.exists(link));
  100. String targetName = fs.readSymLink(link);
  101. assertEquals("å", targetName);
  102. assertTrue(fs.lastModifiedInstant(link).compareTo(EPOCH) > 0);
  103. assertTrue(fs.exists(link));
  104. assertFalse(fs.canExecute(link));
  105. assertEquals(2, fs.length(link));
  106. assertFalse(fs.exists(target));
  107. assertFalse(fs.isFile(target));
  108. assertFalse(fs.isDirectory(target));
  109. assertFalse(fs.canExecute(target));
  110. RepositoryTestCase.fsTick(link);
  111. // Now create the link target
  112. FileUtils.createNewFile(target);
  113. assertTrue(fs.exists(link));
  114. assertTrue(fs.lastModifiedInstant(link).compareTo(EPOCH) > 0);
  115. assertTrue(fs.lastModifiedInstant(target)
  116. .compareTo(fs.lastModifiedInstant(link)) > 0);
  117. assertFalse(fs.canExecute(link));
  118. fs.setExecute(target, true);
  119. assertFalse(fs.canExecute(link));
  120. assumeTrue(fs.supportsExecute());
  121. assertTrue(fs.canExecute(target));
  122. }
  123. @Test
  124. public void testExecutableAttributes() throws Exception {
  125. FS fs = FS.DETECTED.newInstance();
  126. // If this assumption fails the test is halted and ignored.
  127. assumeTrue(fs instanceof FS_POSIX);
  128. ((FS_POSIX) fs).setUmask(0022);
  129. File f = new File(trash, "bla");
  130. assertTrue(f.createNewFile());
  131. assertFalse(fs.canExecute(f));
  132. Set<PosixFilePermission> permissions = readPermissions(f);
  133. assertTrue(!permissions.contains(PosixFilePermission.OTHERS_EXECUTE));
  134. assertTrue(!permissions.contains(PosixFilePermission.GROUP_EXECUTE));
  135. assertTrue(!permissions.contains(PosixFilePermission.OWNER_EXECUTE));
  136. fs.setExecute(f, true);
  137. permissions = readPermissions(f);
  138. assertTrue("'owner' execute permission not set",
  139. permissions.contains(PosixFilePermission.OWNER_EXECUTE));
  140. assertTrue("'group' execute permission not set",
  141. permissions.contains(PosixFilePermission.GROUP_EXECUTE));
  142. assertTrue("'others' execute permission not set",
  143. permissions.contains(PosixFilePermission.OTHERS_EXECUTE));
  144. ((FS_POSIX) fs).setUmask(0033);
  145. fs.setExecute(f, false);
  146. assertFalse(fs.canExecute(f));
  147. fs.setExecute(f, true);
  148. permissions = readPermissions(f);
  149. assertTrue("'owner' execute permission not set",
  150. permissions.contains(PosixFilePermission.OWNER_EXECUTE));
  151. assertFalse("'group' execute permission set",
  152. permissions.contains(PosixFilePermission.GROUP_EXECUTE));
  153. assertFalse("'others' execute permission set",
  154. permissions.contains(PosixFilePermission.OTHERS_EXECUTE));
  155. }
  156. private Set<PosixFilePermission> readPermissions(File f) throws IOException {
  157. return Files
  158. .getFileAttributeView(f.toPath(), PosixFileAttributeView.class)
  159. .readAttributes().permissions();
  160. }
  161. @Test(expected = CommandFailedException.class)
  162. public void testReadPipePosixCommandFailure()
  163. throws CommandFailedException {
  164. FS fs = FS.DETECTED.newInstance();
  165. assumeTrue(fs instanceof FS_POSIX);
  166. FS.readPipe(fs.userHome(),
  167. new String[] { "/bin/sh", "-c", "exit 1" },
  168. Charset.defaultCharset().name());
  169. }
  170. @Test(expected = CommandFailedException.class)
  171. public void testReadPipeCommandStartFailure()
  172. throws CommandFailedException {
  173. FS fs = FS.DETECTED.newInstance();
  174. FS.readPipe(fs.userHome(),
  175. new String[] { "this-command-does-not-exist" },
  176. Charset.defaultCharset().name());
  177. }
  178. @Test
  179. public void testFsTimestampResolution() throws Exception {
  180. DateTimeFormatter formatter = DateTimeFormatter
  181. .ofPattern("uuuu-MMM-dd HH:mm:ss.nnnnnnnnn", Locale.ENGLISH)
  182. .withZone(ZoneId.systemDefault());
  183. Path dir = Files.createTempDirectory("probe-filesystem");
  184. Duration resolution = FS.getFileStoreAttributes(dir)
  185. .getFsTimestampResolution();
  186. long resolutionNs = resolution.toNanos();
  187. assertTrue(resolutionNs > 0);
  188. for (int i = 0; i < 10; i++) {
  189. Path f = null;
  190. try {
  191. f = dir.resolve("testTimestampResolution" + i);
  192. Files.createFile(f);
  193. FileUtils.touch(f);
  194. FileTime t1 = Files.getLastModifiedTime(f);
  195. TimeUnit.NANOSECONDS.sleep(resolutionNs);
  196. FileUtils.touch(f);
  197. FileTime t2 = Files.getLastModifiedTime(f);
  198. assertTrue(String.format(
  199. "expected t2=%s to be larger than t1=%s\nsince file timestamp resolution was measured to be %,d ns",
  200. formatter.format(t2.toInstant()),
  201. formatter.format(t1.toInstant()),
  202. Long.valueOf(resolutionNs)), t2.compareTo(t1) > 0);
  203. } finally {
  204. Files.delete(f);
  205. }
  206. }
  207. }
  208. // bug 548682
  209. @Test
  210. public void testRepoCacheRelativePathUnbornRepo() {
  211. assertFalse(RepositoryCache.FileKey
  212. .isGitRepository(new File("repo.git"), FS.DETECTED));
  213. }
  214. }