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

smb2_rename move files to a non-root directory failed. #218

Closed
anyeloveqiuf opened this issue Jan 15, 2022 · 4 comments
Closed

smb2_rename move files to a non-root directory failed. #218

anyeloveqiuf opened this issue Jan 15, 2022 · 4 comments

Comments

@anyeloveqiuf
Copy link

anyeloveqiuf commented Jan 15, 2022

If "smb moves a file from a non-root directory tmp to dst" is needed, what should I do?
The api is:
int smb2_rename(struct smb2_context *smb2, const char *oldpath, const char *newpath)
Environment: directories are in Windows10, project running in Redhat 8.
Problem:
I created 2 directories -- "tmp" and "dst" -- in the root directory, and transfered a file "test.txt" to the "tmp" successfully. Then I mv "test.txt" to directory "dst" ended with failure. What confused me is, when I tried to mv "test.txt" to root, it worked.
This shows how I use the api(smb2_connect_share was established with the smb2_context *smb2).

  1. mv "test.txt" to directory "dst":
char *oldpath = "tmp/test.txt";
char *newpath = "dst/test.txt";
int ret = 0;
ret = smb2_rename(smb2, oldpath, newpath);
**It failed with STATUS_OBJECT_NAME_NOT_FOUND.**

2.mv "test.txt" to root:

char *oldpath = "tmp/test.txt";
char *newpath = "test.txt";
int ret = 0;
ret = smb2_rename(smb2, oldpath, newpath);
**Now in the root directory, there are 2 directories and 1 text file.**

After I learned the definition of smb2_rename_async, I found the code fragment:
/* CREATE command */
(other codes)
cr_req.name = olapath;
(other codes)
/* SET INFO command */
rn_info.replace_if_exist = 0;
rn_info.file_name = discard_const(newpath);

``
I have limited knowledge of protocol smb, so it may be silly that I conjecture that in smb2_rename_async, the parameter newpath meant the file's name and it always mv the file to the root directory, rather than the full path, which differed from oldpath. In this case, when I tried to mv test.txt from directory tmp to dst, the system sought to mv test.txt to root and rename the file with dst/test.txt actually.
Therefore, if "smb moves a file from a non-root directory tmp to dst" is needed, what should I do?

@sahlberg
Copy link
Owner

All the paths should be relative to the root of the share if I recall correctly.

Please try current master, it has an example utility that uses smb2_rename()

On my server, I created two directories src and dst and in src I created a file called hello.
I then run : ./examples/smb2-rename-sync smb://sahlberg@10.10.10.11/PS2SMB/ src/hello dst/hello
and it renames / moves the file to dst/hello

Can you try that?

@anyeloveqiuf
Copy link
Author

All the paths should be relative to the root of the share if I recall correctly.

Please try current master, it has an example utility that uses smb2_rename()

On my server, I created two directories src and dst and in src I created a file called hello. I then run : ./examples/smb2-rename-sync smb://sahlberg@10.10.10.11/PS2SMB/ src/hello dst/hello and it renames / moves the file to dst/hello

Can you try that?

Thank you. I've tried and it still failed. Possibly it conflicts with my security engine(cannot be removed). For example, using smb2_mkdir() to create a directory that already exists should return STATUS_OBJECT_NAME_COLLISION or what, but smb2_get_error() got NULL. In fact in my project it got nothing except STATUS_INVALID_PARAMETER while the wireshark could always capture them.

@cy33hc
Copy link
Contributor

cy33hc commented Dec 21, 2022

@sahlberg I'm also facing an issue with rename. I rename a file in the same directory.

example: Rename from "testfolder/testfile.txt" to "testfolder/rename.txt" and is fails. smb2_get_error returns an empty string. Return code = -5

Edit: After more testing, this is only a problem with Windows shares in Win 7/10 and 11. Samba on Linux doesn't have this problem.
Edit1: Also using the samba "smbclient" tool, it's able to rename files in subfolders in Windows Shares.
Edit2: I compiled the "smb2-rename-sync.c program in Ubuntu and it also fails to rename files in subdirectories for Windows Shares.

@sahlberg
Copy link
Owner

I have pushed a fix for this to the master branch now.
Thanks for the report.

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

No branches or pull requests

3 participants