Author: bleny Date: 2010-06-17 15:37:59 +0200 (Thu, 17 Jun 2010) New Revision: 83 Url: http://nuiton.org/repositories/revision/diswork/83 Log: bugfix move, overwrite Modified: trunk/diswork-fs/src/main/java/org/nuiton/diswork/fs/DisworkFileSystem.java trunk/diswork-fs/src/test/java/org/nuiton/diswork/fs/AbstractDisworkFileSystemTest.java trunk/diswork-fs/src/test/java/org/nuiton/diswork/fs/DisworkFileSystemInMemoryTest.java Modified: trunk/diswork-fs/src/main/java/org/nuiton/diswork/fs/DisworkFileSystem.java =================================================================== --- trunk/diswork-fs/src/main/java/org/nuiton/diswork/fs/DisworkFileSystem.java 2010-06-17 13:33:48 UTC (rev 82) +++ trunk/diswork-fs/src/main/java/org/nuiton/diswork/fs/DisworkFileSystem.java 2010-06-17 13:37:59 UTC (rev 83) @@ -49,6 +49,11 @@ * <li>{@link #exists(String)} and {@link #delete(String)} can be used * on directories, files and symlinks</li> * </ul> + * + * Those methods are not thread-safe, so manipulating the content of + * same directory in two different thread using the same instance may + * lead to errors. + * */ public class DisworkFileSystem implements Closeable { @@ -164,14 +169,6 @@ if (EntryUtil.isDirectory(entryParent)) { String parentId = EntryUtil.getIdFromEntry(entryParent); - // checking that file do not already exists in this directory - String content = storage.getDirectory(parentId); - String findResult = EntryUtil.findEntryInDirectory - (content, fileName); - if (findResult != null) { - throw new IOException - (parent + " already contains an element named " + fileName); - } // file do not exists, write file on the FS String newFileId = EntryUtil.generateId(); @@ -184,11 +181,26 @@ lockAcquired = storage.tryToLock(parentId); if (lockAcquired) { // parent dir is locked, do the update - content = storage.getDirectory(parentId); - String newContent = EntryUtil.addEntryToDirectoryContent( + + // checking that file do not already exists in this directory + String content = storage.getDirectory(parentId); + String oldEntry = EntryUtil.findEntryInDirectory + (content, fileName); + + if (oldEntry != null) { + content = EntryUtil.removeEntryFromEntries(content, fileName); + } + + content = EntryUtil.addEntryToDirectoryContent( content, EntryUtil.TYPE.F, fileName, newFileId); - storage.putDirectory(parentId, newContent); + storage.putDirectory(parentId, content); storage.unLock(parentId); + + // removing old data + if (oldEntry != null) { + storage.removeFile(EntryUtil.getIdFromEntry(oldEntry)); + } + } else { log.info(parent + " is locked and can't be written"); try { @@ -410,13 +422,16 @@ /** * remove a file, directory, or link. Non-empty directories can't be - * removed + * removed. * @param path the complete path to the entity to remove * @throws IOException if path is incorrect or directory not empty */ public void delete(String path) throws IOException, ConcurrentModificationException { checkPathSyntax(path); + if (!exists(path)) { + throw new IOException(path + " doesn't exists"); + } String parent = EntryUtil.getParentFromPath(path); String name = EntryUtil.getNameFromPath(path); log.info("trying to remove " + path); @@ -703,6 +718,10 @@ checkPathSyntax(path); checkPathSyntax(destination); + if (exists(destination)) { + throw new IOException(destination + " already exists"); + } + String pathParent = EntryUtil.getParentFromPath(path); String pathName = EntryUtil.getNameFromPath(path); String destinationParent = EntryUtil.getParentFromPath(destination); @@ -750,6 +769,7 @@ while (numberOfTry <= LOCK_MAX_NUMBER_OF_TRY && !lockAcquired) { lockAcquired = storage.tryToLock(parentId) && storage.tryToLock(destinationParentId); + if (lockAcquired) { // we have locked, do the update parentContent = storage.getDirectory(parentId); Modified: trunk/diswork-fs/src/test/java/org/nuiton/diswork/fs/AbstractDisworkFileSystemTest.java =================================================================== --- trunk/diswork-fs/src/test/java/org/nuiton/diswork/fs/AbstractDisworkFileSystemTest.java 2010-06-17 13:33:48 UTC (rev 82) +++ trunk/diswork-fs/src/test/java/org/nuiton/diswork/fs/AbstractDisworkFileSystemTest.java 2010-06-17 13:37:59 UTC (rev 83) @@ -21,6 +21,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.junit.experimental.categories.Categories.ExcludeCategory; import org.nuiton.diswork.fs.DisworkFileSystem; import org.nuiton.util.FileUtil; @@ -184,21 +185,17 @@ } /** - * this use case should raise an exception because writing to a file - * that already exists + * this writing a file that already exists should not be a problem */ - @Test(expected = IOException.class) + @Test public void testFailAtDoubleWrite() throws Exception { InputStream source = new FileInputStream(randomFilePath); fileSystem.write("/my_file", source); source.close(); source = new FileInputStream(randomFilePath); - try { - fileSystem.write("/my_file", source); - } finally { - source.close(); - } + fileSystem.write("/my_file", source); + source.close(); } /** @@ -395,6 +392,13 @@ */ } + @Test(expected = IOException.class) + public void failAtMove() throws Exception { + fileSystem.createDirectory("/dir"); + fileSystem.createDirectory("/dir2"); + fileSystem.move("/dir", "/dir2"); + } + @Test public void testCreateDirectories() throws Exception { try { Modified: trunk/diswork-fs/src/test/java/org/nuiton/diswork/fs/DisworkFileSystemInMemoryTest.java =================================================================== --- trunk/diswork-fs/src/test/java/org/nuiton/diswork/fs/DisworkFileSystemInMemoryTest.java 2010-06-17 13:33:48 UTC (rev 82) +++ trunk/diswork-fs/src/test/java/org/nuiton/diswork/fs/DisworkFileSystemInMemoryTest.java 2010-06-17 13:37:59 UTC (rev 83) @@ -29,6 +29,7 @@ fileSystem.createDirectory("/dir"); fileSystem.write("/dir/file", new FileInputStream(randomFilePath)); + fileSystem.write("/dir/file", new FileInputStream(randomFilePath)); fileSystem.createSymbolicLink("/dir/link", "/dir/file"); fileSystem.delete("/dir/link");