Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entry unique name not freed when entry is moved #14

Closed
tenode opened this issue Oct 30, 2013 · 1 comment
Closed

Entry unique name not freed when entry is moved #14

tenode opened this issue Oct 30, 2013 · 1 comment
Labels

Comments

@tenode
Copy link

tenode commented Oct 30, 2013

Great library - thanks for creating and maintaining it. I used this a couple of months ago and ran into what I think is a bug. The test case is below but basically if you move a file then it's unique name is not freed from its original parent directory, so you can't add a new file of the same name or move the old file back.

I fixed this in FatLfnDirectory.java by moving the call to "freeUniqueName()" from the remove() method (line 380) to the bottom of the the unlinkEntry() (now line 412).

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

import de.waldheinz.fs.fat.FatFileSystem;
import de.waldheinz.fs.fat.FatLfnDirectory;
import de.waldheinz.fs.fat.FatLfnDirectoryEntry;
import de.waldheinz.fs.fat.SuperFloppyFormatter;
import de.waldheinz.fs.util.RamDisk;


public class MoveToReplaceTest {

    private static final String FILE_NAME = "file"; 
    private static final String DIRECTORY_NAME = "dir"; 

    private FatLfnDirectoryEntry file;
    private FatLfnDirectoryEntry dir;
    private FatLfnDirectory root;
    private FatFileSystem fs;


    @Before
    public void setUp() throws IOException {
        RamDisk dev = new RamDisk(1024 * 1024);
        fs = SuperFloppyFormatter.get(dev).format();

        root = fs.getRoot();
        file = root.addFile(FILE_NAME);
        dir = root.addDirectory(DIRECTORY_NAME);

    }

    @Test
    public void moveToAndReplaceTest() throws IOException {
        // Move file form root to dir
        file.moveTo(dir.getDirectory(), DIRECTORY_NAME);

        // Check that the file has been moved and isn't in the original dir
        assertEquals(file.getParent(), dir.getDirectory());
        assertNull( root.getEntry(FILE_NAME) );

        // Create a new file with a SAME filename in the original dir
        try {
            root.addFile(FILE_NAME);
        } catch (IOException e) {
            // Code will fail here
            //  java.io.IOException: an entry named file already exists
            //  at de.waldheinz.fs.fat.FatLfnDirectory.checkUniqueName(FatLfnDirectory.java:162)
            //  at de.waldheinz.fs.fat.FatLfnDirectory.addFile(FatLfnDirectory.java:135)
            e.printStackTrace();
            fail("IO Exception");
        }
    }

}
@waldheinz
Copy link
Owner

Thanks for pointing this out, and especially for providing a working test case. I fixed this issue with commit db0516e.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant