hierynomus / smbj

Server Message Block (SMB2, SMB3) implementation in Java
Other
719 stars 180 forks source link

STATUS_OBJECT_PATH_NOT_FOUND (0xc000003a) when trying to rename a file #843

Open akatsudragneel opened 1 month ago

akatsudragneel commented 1 month ago

I am trying to rename a file. So, I have a file named abc.txt in the server: server.com, shareName= share, directory path=dir/1/2/3/4

I am using the below code. I have the session declared as a common variable and I am able to get the file content, upload a file, remove a file, list the files in the above directory. But for some reason gives the error STATUS_OBJECT_PATH_NOT_FOUND although it is available.

public class renameFile { private static final Logger logger = LoggerFactory.getLogger(renameFile.class); public static String urlFix(String s) { return s.replace('/', '\'); } public static void renameTo(String sessionId, String oldFileName, String newFileName, String path, String shareName) {

    Session session = smbLogin.getSession();
    if (session == null || !Long.toString(session.getSessionId()).equals(sessionId)) {
        logger.error("Invalid session ID");
        return;
    }
    String oldFilePath = path + "/" + oldFileName;
    String newFilePath = path + "/" + newFileName;

    try (DiskShare share = (DiskShare) session.connectShare(shareName)) {
        FileAllInformation fileInfo = share.getFileInformation(oldFilePath);

        // Access various attributes
        System.out.println("File Name: " + fileInfo.getNameInformation());
        System.out.println("Creation Time: " + fileInfo.getBasicInformation().getCreationTime());
        System.out.println("Last Access Time: " + fileInfo.getBasicInformation().getLastAccessTime());
        System.out.println("Last Write Time: " + fileInfo.getBasicInformation().getLastWriteTime());
        System.out.println("Change Time: " + fileInfo.getBasicInformation().getChangeTime());
        System.out.println("File Attributes: " + fileInfo.getBasicInformation().getFileAttributes());
        System.out.println("File Size: " + fileInfo.getStandardInformation().getEndOfFile());
        System.out.println("File Access Flag: " + fileInfo.getAccessInformation().getAccessFlags());

        oldFilePath = urlFix(oldFilePath);
        newFilePath = urlFix(newFilePath);

        try (File renameFile = share.openFile(oldFilePath, EnumSet.of(AccessMask.DELETE, AccessMask.GENERIC_WRITE), null, null, SMB2CreateDisposition.FILE_OPEN, null)) {
            renameFile.rename(newFilePath);
        }

    } catch (Exception e) {
        logger.error("An error occurred while renaming the file", e);
    }
}

}

The path is same, just changing the filename. Please help here.

akatsudragneel commented 1 month ago

Hi All,

I found that the reason is because the actual path is on a DFS, although the path I use above is correct and works for get File, download, upload, it doesn't work for rename. But when I used the same for the underlying dfs path, it worked. So could you please investigate why it is not working?

Thanks!