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

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