Why does the error occur when renaming the file? Also, what should I do to
prevent from error occurring?
The Following is a sample program to reproduce this problem.
Any help would be appreciated. Thank you.
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
int test() {
unsigned char buf[] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99
};
HANDLE h;
DWORD nbytes;
LPCTSTR fn_tmp = "aaa";
LPCTSTR fn = "bbb";
h = CreateFile(fn_tmp, GENERIC_WRITE, FILE_SHARE_READ |
FILE_SHARE_WRITE, 0, OPEN_ALWAYS, 0, 0);
if (h == INVALID_HANDLE_VALUE) return 0;
if (!WriteFile(h, buf, sizeof buf, &nbytes, 0)) goto error;
if (!FlushFileBuffers(h)) goto error;
if (!CloseHandle(h)) goto error;
if (!MoveFileEx(fn_tmp, fn, MOVEFILE_REPLACE_EXISTING |
MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) {
printf("error=%d\n", GetLastError());
return 0;
}
return 1;
error:
CloseHandle(h);
return 0;
}
int main(int argc, char** argv) {
unsigned int i;
for (i = 0;; ++i) {
printf("*%u\n", i);
if (!test()) return 1;
}
return 0;
}
Are they in the root of the volume or in some directory?
"chikara" <chi...@discussions.microsoft.com> wrote in message
news:1D85312A-259C-4237...@microsoft.com...
i would perform these test before i automate the process
a) Please check if you are able to rename a file
b) if you are able to successfully rename the file , then please add the
required rights using API , i would suggest to provide full rights and test.
Sainath SMB KID
I sometime can rename a file, but sometime cannot. The test program repeats
writing data to a file and renaming the file and renaming usually succeeds it
fails randomly though. When I tested with the test program, renaming failed
in a several tens of seconds while it worked with a different trial.
Could you run the program and see if you also see the failure? Thank you.
Sure ! , i will run the program , can you please provide me the exe , as i
am out of town i dont have visual studio handy.
send it to my email id sain...@live.in.
Sainath
SMB KID.
I have went through the code and also tested here, the results are sporadic
as you said and i have few observations made
Observations
=========
We are trying to rename the destination file if it exists using an API which
inturn calls os.rename function internally.
We can also rename a file using MoveFile() and Move FileEX() , in your case
you have used MoveFileEX(), i understand that MoveFile() had severe problems
in the past , when used this function ,the destination file was not getting
renamed hence a patch was released which addressed the issue with
MoveFileEX() which uses the flag MoveFile_Replace_Existing which causes
destination file to be replaced it exists.
However, this change is subtle and if there is any existing code that
depends on current os.rename behaviour on windows, their code is silently
broken with
(any) destination file being overwritten.
Even though MSDN docs do not say it explicitly, I found some
discussions claiming that MOVEFILE_REPLACE_EXISTING is atomic in nature
which means , you can explicitly use it , but i find that issue still
persists with these API and we need to have a fix from microsoft
Additionaly you can still check with the below options
a) use only MOVEFILE_REPLACE_EXISTING by removing rest of the flags and test
the behavior
b) use MoveFile() without EX
c) Use ReplaceFile Function which is equivalent to the above function
Regards,
Sainath SMB KID