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.

FileUtilsTest.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * Copyright (C) 2010, 2013 Matthias Sohn <matthias.sohn@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.util;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertTrue;
  14. import static org.junit.Assert.fail;
  15. import java.io.File;
  16. import java.io.IOException;
  17. import java.io.UnsupportedEncodingException;
  18. import java.nio.file.Files;
  19. import java.nio.file.StandardCopyOption;
  20. import java.rmi.RemoteException;
  21. import java.util.regex.Matcher;
  22. import javax.management.remote.JMXProviderException;
  23. import org.eclipse.jgit.junit.JGitTestUtil;
  24. import org.junit.After;
  25. import org.junit.Assume;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. public class FileUtilsTest {
  29. private static final String MSG = "Stale file handle";
  30. private static final String SOME_ERROR_MSG = "some error message";
  31. private static final IOException IO_EXCEPTION = new UnsupportedEncodingException(
  32. MSG);
  33. private static final IOException IO_EXCEPTION_WITH_CAUSE = new RemoteException(
  34. SOME_ERROR_MSG,
  35. new JMXProviderException(SOME_ERROR_MSG, IO_EXCEPTION));
  36. private File trash;
  37. @Before
  38. public void setUp() throws Exception {
  39. trash = File.createTempFile("tmp_", "");
  40. trash.delete();
  41. assertTrue("mkdir " + trash, trash.mkdir());
  42. }
  43. @After
  44. public void tearDown() throws Exception {
  45. FileUtils.delete(trash, FileUtils.RECURSIVE | FileUtils.RETRY);
  46. }
  47. @Test
  48. public void testDeleteFile() throws IOException {
  49. File f = new File(trash, "test");
  50. FileUtils.createNewFile(f);
  51. FileUtils.delete(f);
  52. assertFalse(f.exists());
  53. try {
  54. FileUtils.delete(f);
  55. fail("deletion of non-existing file must fail");
  56. } catch (IOException e) {
  57. // expected
  58. }
  59. try {
  60. FileUtils.delete(f, FileUtils.SKIP_MISSING);
  61. } catch (IOException e) {
  62. fail("deletion of non-existing file must not fail with option SKIP_MISSING");
  63. }
  64. }
  65. @Test
  66. public void testDeleteReadOnlyFile() throws IOException {
  67. File f = new File(trash, "f");
  68. FileUtils.createNewFile(f);
  69. assertTrue(f.setReadOnly());
  70. FileUtils.delete(f);
  71. assertFalse(f.exists());
  72. }
  73. @Test
  74. public void testDeleteRecursive() throws IOException {
  75. File f1 = new File(trash, "test/test/a");
  76. FileUtils.mkdirs(f1.getParentFile());
  77. FileUtils.createNewFile(f1);
  78. File f2 = new File(trash, "test/test/b");
  79. FileUtils.createNewFile(f2);
  80. File d = new File(trash, "test");
  81. FileUtils.delete(d, FileUtils.RECURSIVE);
  82. assertFalse(d.exists());
  83. try {
  84. FileUtils.delete(d, FileUtils.RECURSIVE);
  85. fail("recursive deletion of non-existing directory must fail");
  86. } catch (IOException e) {
  87. // expected
  88. }
  89. try {
  90. FileUtils.delete(d, FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
  91. } catch (IOException e) {
  92. fail("recursive deletion of non-existing directory must not fail with option SKIP_MISSING");
  93. }
  94. }
  95. @Test
  96. public void testDeleteRecursiveEmpty() throws IOException {
  97. File f1 = new File(trash, "test/test/a");
  98. File f2 = new File(trash, "test/a");
  99. File d1 = new File(trash, "test");
  100. File d2 = new File(trash, "test/test");
  101. File d3 = new File(trash, "test/b");
  102. FileUtils.mkdirs(f1.getParentFile());
  103. FileUtils.createNewFile(f2);
  104. FileUtils.createNewFile(f1);
  105. FileUtils.mkdirs(d3);
  106. // Cannot delete hierarchy since files exist
  107. try {
  108. FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY);
  109. fail("delete should fail");
  110. } catch (IOException e1) {
  111. try {
  112. FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY|FileUtils.RECURSIVE);
  113. fail("delete should fail");
  114. } catch (IOException e2) {
  115. // Everything still there
  116. assertTrue(f1.exists());
  117. assertTrue(f2.exists());
  118. assertTrue(d1.exists());
  119. assertTrue(d2.exists());
  120. assertTrue(d3.exists());
  121. }
  122. }
  123. // setup: delete files, only directories left
  124. assertTrue(f1.delete());
  125. assertTrue(f2.delete());
  126. // Shall not delete hierarchy without recursive
  127. try {
  128. FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY);
  129. fail("delete should fail");
  130. } catch (IOException e2) {
  131. // Everything still there
  132. assertTrue(d1.exists());
  133. assertTrue(d2.exists());
  134. assertTrue(d3.exists());
  135. }
  136. // Now delete the empty hierarchy
  137. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  138. | FileUtils.RECURSIVE);
  139. assertFalse(d2.exists());
  140. // Will fail to delete non-existing without SKIP_MISSING
  141. try {
  142. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY);
  143. fail("Cannot delete non-existent entity");
  144. } catch (IOException e) {
  145. // ok
  146. }
  147. // ..with SKIP_MISSING there is no exception
  148. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  149. | FileUtils.SKIP_MISSING);
  150. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  151. | FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
  152. // essentially the same, using IGNORE_ERRORS
  153. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  154. | FileUtils.IGNORE_ERRORS);
  155. FileUtils.delete(d2, FileUtils.EMPTY_DIRECTORIES_ONLY
  156. | FileUtils.RECURSIVE | FileUtils.IGNORE_ERRORS);
  157. }
  158. @Test
  159. public void testDeleteRecursiveEmptyNeedsToCheckFilesFirst()
  160. throws IOException {
  161. File d1 = new File(trash, "test");
  162. File d2 = new File(trash, "test/a");
  163. File d3 = new File(trash, "test/b");
  164. File f1 = new File(trash, "test/c");
  165. File d4 = new File(trash, "test/d");
  166. FileUtils.mkdirs(d1);
  167. FileUtils.mkdirs(d2);
  168. FileUtils.mkdirs(d3);
  169. FileUtils.mkdirs(d4);
  170. FileUtils.createNewFile(f1);
  171. // Cannot delete hierarchy since file exists
  172. try {
  173. FileUtils.delete(d1, FileUtils.EMPTY_DIRECTORIES_ONLY
  174. | FileUtils.RECURSIVE);
  175. fail("delete should fail");
  176. } catch (IOException e) {
  177. // Everything still there
  178. assertTrue(f1.exists());
  179. assertTrue(d1.exists());
  180. assertTrue(d2.exists());
  181. assertTrue(d3.exists());
  182. assertTrue(d4.exists());
  183. }
  184. }
  185. @Test
  186. public void testDeleteRecursiveEmptyDirectoriesOnlyButIsFile()
  187. throws IOException {
  188. File f1 = new File(trash, "test/test/a");
  189. FileUtils.mkdirs(f1.getParentFile());
  190. FileUtils.createNewFile(f1);
  191. try {
  192. FileUtils.delete(f1, FileUtils.EMPTY_DIRECTORIES_ONLY);
  193. fail("delete should fail");
  194. } catch (IOException e) {
  195. assertTrue(f1.exists());
  196. }
  197. }
  198. @Test
  199. public void testMkdir() throws IOException {
  200. File d = new File(trash, "test");
  201. FileUtils.mkdir(d);
  202. assertTrue(d.exists() && d.isDirectory());
  203. try {
  204. FileUtils.mkdir(d);
  205. fail("creation of existing directory must fail");
  206. } catch (IOException e) {
  207. // expected
  208. }
  209. FileUtils.mkdir(d, true);
  210. assertTrue(d.exists() && d.isDirectory());
  211. assertTrue(d.delete());
  212. File f = new File(trash, "test");
  213. FileUtils.createNewFile(f);
  214. try {
  215. FileUtils.mkdir(d);
  216. fail("creation of directory having same path as existing file must"
  217. + " fail");
  218. } catch (IOException e) {
  219. // expected
  220. }
  221. assertTrue(f.delete());
  222. }
  223. @Test
  224. public void testMkdirs() throws IOException {
  225. File root = new File(trash, "test");
  226. assertTrue(root.mkdir());
  227. File d = new File(root, "test/test");
  228. FileUtils.mkdirs(d);
  229. assertTrue(d.exists() && d.isDirectory());
  230. try {
  231. FileUtils.mkdirs(d);
  232. fail("creation of existing directory hierarchy must fail");
  233. } catch (IOException e) {
  234. // expected
  235. }
  236. FileUtils.mkdirs(d, true);
  237. assertTrue(d.exists() && d.isDirectory());
  238. FileUtils.delete(root, FileUtils.RECURSIVE);
  239. File f = new File(trash, "test");
  240. FileUtils.createNewFile(f);
  241. try {
  242. FileUtils.mkdirs(d);
  243. fail("creation of directory having path conflicting with existing"
  244. + " file must fail");
  245. } catch (IOException e) {
  246. // expected
  247. }
  248. assertTrue(f.delete());
  249. }
  250. @Test
  251. public void testCreateNewFile() throws IOException {
  252. File f = new File(trash, "x");
  253. FileUtils.createNewFile(f);
  254. assertTrue(f.exists());
  255. try {
  256. FileUtils.createNewFile(f);
  257. fail("creation of already existing file must fail");
  258. } catch (IOException e) {
  259. // expected
  260. }
  261. FileUtils.delete(f);
  262. }
  263. @Test
  264. public void testDeleteEmptyTreeOk() throws IOException {
  265. File t = new File(trash, "t");
  266. FileUtils.mkdir(t);
  267. FileUtils.mkdir(new File(t, "d"));
  268. FileUtils.mkdir(new File(new File(t, "d"), "e"));
  269. FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE);
  270. assertFalse(t.exists());
  271. }
  272. @Test
  273. public void testDeleteNotEmptyTreeNotOk() throws IOException {
  274. File t = new File(trash, "t");
  275. FileUtils.mkdir(t);
  276. FileUtils.mkdir(new File(t, "d"));
  277. File f = new File(new File(t, "d"), "f");
  278. FileUtils.createNewFile(f);
  279. FileUtils.mkdir(new File(new File(t, "d"), "e"));
  280. try {
  281. FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE);
  282. fail("expected failure to delete f");
  283. } catch (IOException e) {
  284. assertTrue(e.getMessage().endsWith(f.getAbsolutePath()));
  285. }
  286. assertTrue(t.exists());
  287. }
  288. @Test
  289. public void testDeleteNotEmptyTreeNotOkButIgnoreFail() throws IOException {
  290. File t = new File(trash, "t");
  291. FileUtils.mkdir(t);
  292. FileUtils.mkdir(new File(t, "d"));
  293. File f = new File(new File(t, "d"), "f");
  294. FileUtils.createNewFile(f);
  295. File e = new File(new File(t, "d"), "e");
  296. FileUtils.mkdir(e);
  297. FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.RECURSIVE
  298. | FileUtils.IGNORE_ERRORS);
  299. // Should have deleted as much as possible, but not all
  300. assertTrue(t.exists());
  301. assertTrue(f.exists());
  302. assertFalse(e.exists());
  303. }
  304. @Test
  305. public void testDeleteNonRecursiveTreeNotOk() throws IOException {
  306. File t = new File(trash, "t");
  307. FileUtils.mkdir(t);
  308. File f = new File(t, "f");
  309. FileUtils.createNewFile(f);
  310. try {
  311. FileUtils.delete(t, FileUtils.EMPTY_DIRECTORIES_ONLY);
  312. fail("expected failure to delete f");
  313. } catch (IOException e) {
  314. assertTrue(e.getMessage().endsWith(t.getAbsolutePath()));
  315. }
  316. assertTrue(f.exists());
  317. assertTrue(t.exists());
  318. }
  319. @Test
  320. public void testDeleteNonRecursiveTreeIgnoreError() throws IOException {
  321. File t = new File(trash, "t");
  322. FileUtils.mkdir(t);
  323. File f = new File(t, "f");
  324. FileUtils.createNewFile(f);
  325. FileUtils.delete(t,
  326. FileUtils.EMPTY_DIRECTORIES_ONLY | FileUtils.IGNORE_ERRORS);
  327. assertTrue(f.exists());
  328. assertTrue(t.exists());
  329. }
  330. @Test
  331. public void testRenameOverNonExistingFile() throws IOException {
  332. File d = new File(trash, "d");
  333. FileUtils.mkdirs(d);
  334. File f1 = new File(trash, "d/f");
  335. File f2 = new File(trash, "d/g");
  336. JGitTestUtil.write(f1, "f1");
  337. // test
  338. FileUtils.rename(f1, f2);
  339. assertFalse(f1.exists());
  340. assertTrue(f2.exists());
  341. assertEquals("f1", JGitTestUtil.read(f2));
  342. }
  343. @Test
  344. public void testRenameOverExistingFile() throws IOException {
  345. File d = new File(trash, "d");
  346. FileUtils.mkdirs(d);
  347. File f1 = new File(trash, "d/f");
  348. File f2 = new File(trash, "d/g");
  349. JGitTestUtil.write(f1, "f1");
  350. JGitTestUtil.write(f2, "f2");
  351. // test
  352. FileUtils.rename(f1, f2);
  353. assertFalse(f1.exists());
  354. assertTrue(f2.exists());
  355. assertEquals("f1", JGitTestUtil.read(f2));
  356. }
  357. @Test
  358. public void testRenameOverExistingNonEmptyDirectory() throws IOException {
  359. File d = new File(trash, "d");
  360. FileUtils.mkdirs(d);
  361. File f1 = new File(trash, "d/f");
  362. File f2 = new File(trash, "d/g");
  363. File d1 = new File(trash, "d/g/h/i");
  364. File f3 = new File(trash, "d/g/h/f");
  365. FileUtils.mkdirs(d1);
  366. JGitTestUtil.write(f1, "f1");
  367. JGitTestUtil.write(f3, "f3");
  368. // test
  369. try {
  370. FileUtils.rename(f1, f2);
  371. fail("rename to non-empty directory should fail");
  372. } catch (IOException e) {
  373. assertEquals("f1", JGitTestUtil.read(f1)); // untouched source
  374. assertEquals("f3", JGitTestUtil.read(f3)); // untouched
  375. // empty directories within f2 may or may not have been deleted
  376. }
  377. }
  378. @Test
  379. public void testRenameOverExistingEmptyDirectory() throws IOException {
  380. File d = new File(trash, "d");
  381. FileUtils.mkdirs(d);
  382. File f1 = new File(trash, "d/f");
  383. File f2 = new File(trash, "d/g");
  384. File d1 = new File(trash, "d/g/h/i");
  385. FileUtils.mkdirs(d1);
  386. JGitTestUtil.write(f1, "f1");
  387. // test
  388. FileUtils.rename(f1, f2);
  389. assertFalse(f1.exists());
  390. assertTrue(f2.exists());
  391. assertEquals("f1", JGitTestUtil.read(f2));
  392. }
  393. @Test
  394. public void testCreateSymlink() throws IOException {
  395. FS fs = FS.DETECTED;
  396. // show test as ignored if the FS doesn't support symlinks
  397. Assume.assumeTrue(fs.supportsSymlinks());
  398. fs.createSymLink(new File(trash, "x"), "y");
  399. String target = fs.readSymLink(new File(trash, "x"));
  400. assertEquals("y", target);
  401. }
  402. @Test
  403. public void testCreateSymlinkOverrideExisting() throws IOException {
  404. FS fs = FS.DETECTED;
  405. // show test as ignored if the FS doesn't support symlinks
  406. Assume.assumeTrue(fs.supportsSymlinks());
  407. File file = new File(trash, "x");
  408. fs.createSymLink(file, "y");
  409. String target = fs.readSymLink(file);
  410. assertEquals("y", target);
  411. fs.createSymLink(file, "z");
  412. target = fs.readSymLink(file);
  413. assertEquals("z", target);
  414. }
  415. @Test
  416. public void testRelativize_doc() {
  417. // This is the example from the javadoc
  418. String base = toOSPathString("c:\\Users\\jdoe\\eclipse\\git\\project");
  419. String other = toOSPathString("c:\\Users\\jdoe\\eclipse\\git\\another_project\\pom.xml");
  420. String expected = toOSPathString("..\\another_project\\pom.xml");
  421. String actual = FileUtils.relativizeNativePath(base, other);
  422. assertEquals(expected, actual);
  423. }
  424. @Test
  425. public void testRelativize_mixedCase() {
  426. SystemReader systemReader = SystemReader.getInstance();
  427. String base = toOSPathString("C:\\git\\jgit");
  428. String other = toOSPathString("C:\\Git\\test\\d\\f.txt");
  429. String expectedCaseInsensitive = toOSPathString("..\\test\\d\\f.txt");
  430. String expectedCaseSensitive = toOSPathString("..\\..\\Git\\test\\d\\f.txt");
  431. if (systemReader.isWindows()) {
  432. String actual = FileUtils.relativizeNativePath(base, other);
  433. assertEquals(expectedCaseInsensitive, actual);
  434. } else if (systemReader.isMacOS()) {
  435. String actual = FileUtils.relativizeNativePath(base, other);
  436. assertEquals(expectedCaseInsensitive, actual);
  437. } else {
  438. String actual = FileUtils.relativizeNativePath(base, other);
  439. assertEquals(expectedCaseSensitive, actual);
  440. }
  441. }
  442. @Test
  443. public void testRelativize_scheme() {
  444. String base = toOSPathString("file:/home/eclipse/runtime-New_configuration/project_1/file.java");
  445. String other = toOSPathString("file:/home/eclipse/runtime-New_configuration/project");
  446. // 'file.java' is treated as a folder
  447. String expected = toOSPathString("../../project");
  448. String actual = FileUtils.relativizeNativePath(base, other);
  449. assertEquals(expected, actual);
  450. }
  451. @Test
  452. public void testRelativize_equalPaths() {
  453. String base = toOSPathString("file:/home/eclipse/runtime-New_configuration/project_1");
  454. String other = toOSPathString("file:/home/eclipse/runtime-New_configuration/project_1");
  455. String expected = "";
  456. String actual = FileUtils.relativizeNativePath(base, other);
  457. assertEquals(expected, actual);
  458. }
  459. @Test
  460. public void testRelativize_whitespaces() {
  461. String base = toOSPathString("/home/eclipse 3.4/runtime New_configuration/project_1");
  462. String other = toOSPathString("/home/eclipse 3.4/runtime New_configuration/project_1/file");
  463. String expected = "file";
  464. String actual = FileUtils.relativizeNativePath(base, other);
  465. assertEquals(expected, actual);
  466. }
  467. @Test
  468. public void testDeleteSymlinkToDirectoryDoesNotDeleteTarget()
  469. throws IOException {
  470. org.junit.Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
  471. FS fs = FS.DETECTED;
  472. File dir = new File(trash, "dir");
  473. File file = new File(dir, "file");
  474. File link = new File(trash, "link");
  475. FileUtils.mkdirs(dir);
  476. FileUtils.createNewFile(file);
  477. fs.createSymLink(link, "dir");
  478. FileUtils.delete(link, FileUtils.RECURSIVE);
  479. assertFalse(link.exists());
  480. assertTrue(dir.exists());
  481. assertTrue(file.exists());
  482. }
  483. @Test
  484. public void testAtomicMove() throws IOException {
  485. File src = new File(trash, "src");
  486. Files.createFile(src.toPath());
  487. File dst = new File(trash, "dst");
  488. FileUtils.rename(src, dst, StandardCopyOption.ATOMIC_MOVE);
  489. assertFalse(Files.exists(src.toPath()));
  490. assertTrue(Files.exists(dst.toPath()));
  491. }
  492. private String toOSPathString(String path) {
  493. return path.replaceAll("/|\\\\",
  494. Matcher.quoteReplacement(File.separator));
  495. }
  496. @Test
  497. public void testIsStaleFileHandleWithDirectCause() throws Exception {
  498. assertTrue(FileUtils.isStaleFileHandle(IO_EXCEPTION));
  499. }
  500. @Test
  501. public void testIsStaleFileHandleWithIndirectCause() throws Exception {
  502. assertFalse(
  503. FileUtils.isStaleFileHandle(IO_EXCEPTION_WITH_CAUSE));
  504. }
  505. @Test
  506. public void testIsStaleFileHandleInCausalChainWithDirectCause()
  507. throws Exception {
  508. assertTrue(
  509. FileUtils.isStaleFileHandleInCausalChain(IO_EXCEPTION));
  510. }
  511. @Test
  512. public void testIsStaleFileHandleInCausalChainWithIndirectCause()
  513. throws Exception {
  514. assertTrue(FileUtils
  515. .isStaleFileHandleInCausalChain(IO_EXCEPTION_WITH_CAUSE));
  516. }
  517. }