Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unable to rename the file

171 views
Skip to first unread message

chikara

unread,
Jul 8, 2008, 12:30:00 AM7/8/08
to
I am using Windows Vista and sometimes fail to rename the file by using
MoveFileEx() when renaming the file after writing data in the file with
WriteFile() and FlushFileBuffers() repeatedly.
The error code "5(ERROR_ACCESS_DENIED)" is returned from GetLastError().

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;
}

Dilip Naik

unread,
Jul 8, 2008, 9:14:57 PM7/8/08
to
Are the source and destination on a local volume or a network volume?

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...

chikara

unread,
Jul 9, 2008, 2:06:01 AM7/9/08
to
The source and destination files are on a local volume.
I executed this sample program on the desktop; therefore these files are
also created on there.

Sainath , SMB KID

unread,
Jul 21, 2008, 8:49:03 AM7/21/08
to

Hi Chikara,

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

chikara

unread,
Jul 29, 2008, 1:46:05 AM7/29/08
to
Thank you for your reply.

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.

Sainath , SMB KID

unread,
Jul 30, 2008, 4:34:01 AM7/30/08
to
Hi chikara,

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.

Sainath , SMB KID

unread,
Aug 5, 2008, 6:18:12 AM8/5/08
to
Hi Chikara,

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

0 new messages