Skip to content

Bug in renameTo. #30

@isabsent

Description

@isabsent

Your method

public SmbFile renameTo(String newFileName, boolean replaceIfExist) {
    try (File file = getDiskShare().openFile(getPath(), EnumSet.of(AccessMask.GENERIC_ALL), null, SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, null)) {
        String newFilePath = buildProperItemPath(getParentPath().getPath(), newFileName);
        file.rename(newFilePath, replaceIfExist);
        return new SmbFile(getSmbConnection(), newFilePath);
    }
}

doesn't work for me with com.hierynomus:smbj:0.14.0. The only working code is:

public SmbFile renameTo(String newFileName, boolean replaceIfExist) {
    try (File file = getDiskShare().openFile(getPath(),
            EnumSet.of(AccessMask.MAXIMUM_ALLOWED), //Переименование на Windows
            null,
            EnumSet.of(SMB2ShareAccess.FILE_SHARE_DELETE, SMB2ShareAccess.FILE_SHARE_READ, SMB2ShareAccess.FILE_SHARE_WRITE),
            SMB2CreateDisposition.FILE_OPEN_IF,
            EnumSet.of(SMB2CreateOptions.FILE_NON_DIRECTORY_FILE, SMB2CreateOptions.FILE_WRITE_THROUGH)
    )) {
        String newFilePath = buildProperItemPath(getParentPath().getPath(), newFileName);
        file.rename(newFilePath, replaceIfExist);
        return new SmbFile(getSmbConnection(), newFilePath);
    }
}

with the method

protected static String buildProperItemPath(String path, String name) {
    if (!path.isEmpty()) { // Example: Dir + / + File
        return path + "\\"+ name; //  PATH_SEPARATOR -> "\\"
    } else { // Example: File
        return name;
    }
}

and

public SmbItem(SmbConnection smbConnection, String pathName) {
    this.smbConnection = smbConnection;
    if (SmbUtils.isValidSmbItemName(pathName)) {
        this.pathName = pathName.startsWith("/") ? pathName.substring(1) : pathName; // "/" in the beginning should be removed
    } else {
        throw new RuntimeException("The given path name is not a valid SMB path");
    }
}

The same is for

public SmbDirectory renameTo(String newDirectoryName, boolean replaceIfExist) {
    try (Directory directory = getDiskShare().openDirectory(getPath(),
            EnumSet.of(AccessMask.DELETE, AccessMask.FILE_WRITE_ATTRIBUTES, AccessMask.FILE_READ_ATTRIBUTES),
            null, 
            EnumSet.of(SMB2ShareAccess.FILE_SHARE_DELETE, SMB2ShareAccess.FILE_SHARE_READ, SMB2ShareAccess.FILE_SHARE_WRITE),
            SMB2CreateDisposition.FILE_OPEN_IF, 
            EnumSet.of(SMB2CreateOptions.FILE_DIRECTORY_FILE))
    ) {
        String newDirectoryPath = buildProperItemPath(getParentPath().getPath(), newDirectoryName);
        directory.rename(newDirectoryPath, replaceIfExist);
        return new SmbDirectory(getSmbConnection(), newDirectoryPath);
    }
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions