DeleteFile () or unlink () succeeds but does not delete the file

I am facing this strange problem. To remove the file the unlink () API is called in my code. This call deletes the file and succeeds on non-Windows platforms. On windows this succeeds (returns 0) but does not delete the file.

To experiment, I added a loop to re-call the same API. On the second iteration, I got a permission denied error, error code = 13. Although read / write attributes are set in the file, and the program has full permission to access the file.

Then I called DeleteFile () instead of the unlink () API. To my surprise, I see the same result, the call succeeded, i.e. 1 returned, but the file is not physically deleted.

I checked the unlocker utility, no other program accesses the file other than the program that tries to delete the file.

Does anyone know what else might be wrong?

Edit1: Just to prevent the file from being opened while deleting it. I saved the descriptor when the file was created and tried to close it before deleting it, but I got the error "UNOPENED" (Errcode: 9 - Bad file descriptor) "So I conclude that the file was not open at the time it was deleted ...

Edit2 As requested, here is a simplified version of the code used to create and delete a file.

// Code to create the file
int create_file(const char* path)
{
  HANDLE osfh;                            /* OS handle of opened file */
  DWORD fileaccess;                       /* OS file access (requested) */
  DWORD fileshare;                        /* OS file sharing mode */
  DWORD filecreate;                       /* OS method of opening/creating */
  DWORD fileattrib;                       /* OS file attribute flags */
  SECURITY_ATTRIBUTES SecurityAttributes;


  SecurityAttributes.nLength= sizeof(SecurityAttributes);
  SecurityAttributes.lpSecurityDescriptor= NULL;
  SecurityAttributes.bInheritHandle= !(oflag & _O_NOINHERIT);



 fileaccess= GENERIC_WRITE;
 fileshare= FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
 filecreate= CREATE_NEW; 


 if ((osfh= CreateFile(path, fileaccess, fileshare, &SecurityAttributes, 
 filecreate, fileattrib, NULL)) == INVALID_HANDLE_VALUE)
 {
   // error handling
 }

}

//Code to delete the file - 
int remove_file (const char* name)
{
  if ((err = unlink(name)) == -1)
  { //Error handling }
}

      

Edit3 As pointed out by Joachim Pileborg and icabod DeleteFile()

does not delete the file if it is still open. As suggested by Remy Lebeau, use a process handler. I found that one file descriptor was actually open when I closed it from the process explorer file deleted like a charm :)

I also mentioned in Edit1 , when I tried to close the file, I got an error. This happened because the file descriptor I am getting from createfile()

is not the actual descriptor returned by the CreateFile () API, and not a boolean mapped descriptor due to the complexity of the code supported by other non-windows platforms. Anyway, now I understand the root cause of the problem, but I was expecting if a file with an open descriptor is passed to the DeleteFile()

API, then it should fail on the first try, rather than wait for the open descriptors to close.

+3


source to share


1 answer


Assuming you call your function Createfile

and then call the function remove_file

... you still have a handle open to the file. The WinAPI function Createfile

, if it succeeds, keeps the handle open in the file. In your provided code, you are not closing that descriptor.

From the documentation on DeleteFile :

The DeleteFile function marks a file for deletion on close. Therefore, file deletion does not occur until the last file descriptor is closed. Subsequent calls to CreateFile to open the file fail with ERROR_ACCESS_DENIED.



I am assuming that you are still opening a handle and when you close that handle the file will be deleted.

However, your code sample is incomplete, so it's hard to tell.

+5


source







All Articles