Recent

Author Topic: DeleteFile() fails on Win10  (Read 3120 times)

MoCityMM

  • Jr. Member
  • **
  • Posts: 72
Re: DeleteFile() fails on Win10
« Reply #15 on: March 09, 2020, 03:33:45 pm »
Was looking for something else but, ran into this post.

If I am reading this correctly...

Seen similar 'symptoms' with other applications on shared folder (network shares) and local folders.

Could be ACL (access control list) or UAC (which is a pain), if an application writes the file - it's the owner of the file in some circumstances (especially if there is a system account associated with the application) and the user may not have access to delete the file.

I would check to see what 'group' the user belongs to: 'Users', 'Power Users', etc...

If they are part of the Administrators group and it still happens, run iCACLS on file and see if you can delete the file then.

Try the link below, it's worth a shot:

https://theitbros.com/using-icacls-to-list-folder-permissions-and-manage-files/

-Mo




dbannon

  • Hero Member
  • *****
  • Posts: 1167
    • tomboy-ng, a rewrite of the classic Tomboy
Re: DeleteFile() fails on Win10
« Reply #16 on: March 09, 2020, 11:38:50 pm »
Because I cannot, myself replicate this problem, its quite hard to work on.

I have had two users affected, the first reported the issue, I assume he has stopped using my app and does not respond.  The second person was a lot more helpful, but is now convinced that the problem relates to his use of drive encryption. Between us, we spent a lot of time on this problem, I have to assume he, too, has got to the point where he does not want to put in any more.

I have much of my diagnostic code still in there, if it happens to someone else then I can track it much quicker but I suspect it is no longer something that is going to trouble us.

So, thanks for your input MoCityMM but its looking like a cold case.

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

avra

  • Hero Member
  • *****
  • Posts: 2016
    • Additional info
Re: DeleteFile() fails on Win10
« Reply #17 on: March 10, 2020, 09:20:44 am »
The problem really does appear to be that DeleteFileUTF8() can return false even when it correctly deleted the file.
So at the end this could be fixed with just additional check for FileExists()?
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

dbannon

  • Hero Member
  • *****
  • Posts: 1167
    • tomboy-ng, a rewrite of the classic Tomboy
Re: DeleteFile() fails on Win10
« Reply #18 on: March 10, 2020, 11:18:26 pm »
Yes avra, thats what I have done in my code (for windows only). So far, with this users experience, we see only the first log error.
 
Code: Pascal  [Select][+][-]
  1.     if not DeleteFile(FullFileName) then begin
  2.         ErrorMsg := SysErrorMessage(GetLastOSError);
  3.         Debugln('Failed using DeleteFile - file name is :' + FullFilename);
  4.         Debugln('OS Error Msg : ' + ErrorMsg);
  5.         if not FileExistsUTF8(FullFileName) then
  6.             debugln('But, FileExists says its gone, proceed !')
  7.         else begin
  8.                 debugln('I can confirm its still there .');
  9.                 Debugln('Trying a little sleep...');
  10.                 sleep(10);
  11.                 if not DeleteFileUTF8(FullFileName) then begin
  12.                      if not FileExistsUTF8(FullFileName) then
  13.                      debugln('DeleteFileUTF8 says it failed but FileExists says its gone, proceed !')
  14.                 else exit(false);
  15.             end;
  16.         end;
  17.     end;

I'd be a lot happier if I could see it myself ...

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

winni

  • Hero Member
  • *****
  • Posts: 1908
Re: DeleteFile() fails on Win10
« Reply #19 on: March 10, 2020, 11:50:36 pm »
Hello!

What says the OS Error Msg??

Before  that I would insert:

Io := IoResult;

and then write it with DebugLn


Might be at the edge of "Off Topic" but

Reminds me to Win 3.1 and our first Netware Server.
( I know, grandpa talks about the war .....)

You thought all disk operations were executed, but they were not:

It gave the new IOerror 32, but no software new about it.
The error said something about "File Sharing Violation".
You needed some new addon for the Netware Driver.

Maybe it's something similar.

Winni

dbannon

  • Hero Member
  • *****
  • Posts: 1167
    • tomboy-ng, a rewrite of the classic Tomboy
Re: DeleteFile() fails on Win10
« Reply #20 on: March 11, 2020, 02:04:49 am »
Hello!

What says the OS Error Msg??

Before  that I would insert:

Io := IoResult;

and then write it with DebugLn
....

Winni, I do grab SysErrorMessage(GetLastOSError) and drop that out in the debug line. From memory (at end users request, I have closed the issue ticket) it reported that the file could not be found to delete. But, as noted, it appeared it does try to delete it and successes.

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

jmnj

  • Newbie
  • Posts: 4
Re: DeleteFile() fails on Win10
« Reply #21 on: June 17, 2020, 05:33:31 pm »
This same issue drove me NUTS for some time.  Here's a snippet what I use to reliably delete files in Windows 10...

// Requires Units: LazFileUtils, LAZUTF8

FileName:='String with full path of file to delete.';
DeleteFileUTF8(UTF8ToWinCP(FileName));

Hope this helps!  :D
« Last Edit: June 18, 2020, 04:01:19 pm by jmnj »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 825
    • Lebeau Software
Re: DeleteFile() fails on Win10
« Reply #22 on: June 17, 2020, 09:59:04 pm »
Here's a snippet what I use to reliably delete files in Windows 10...

One, the FileExists() check is unnecessary, as DeleteFileW() will do the same check and set GetLastError() accordingly.

Two, why would you pass a CP-encoded string to a UTF-8 function?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

jmnj

  • Newbie
  • Posts: 4
Re: DeleteFile() fails on Win10
« Reply #23 on: June 18, 2020, 03:05:29 pm »
Hi Remy,

Thanks for your comments.  I've removed the FileExists().  Explanations below:

Honestly, I haven't delved into why this works.  I did initially pass UTF8 without success.  Tried racking my head on this for weeks before I was willing to try "anything".

For some unknown reason this does work on Windows 10, post update KB4556799 using Lazarus 2.0.8 r62944.  I just don't know exactly -why- it does. 

I would love to know why this works and I'd love to know if the "normal" way of deleting files gets a permanent fix so that I'm not resorting to a clear hack.  I'm kinda between a rock and a hard place using this hack on a critical project.
« Last Edit: June 18, 2020, 04:07:38 pm by jmnj »

dbannon

  • Hero Member
  • *****
  • Posts: 1167
    • tomboy-ng, a rewrite of the classic Tomboy
Re: DeleteFile() fails on Win10
« Reply #24 on: June 20, 2020, 02:48:37 am »
....
One, the FileExists() check is unnecessary, as DeleteFileW() will do the same check and set GetLastError() accordingly.
.....

Just for the record, several variations that I sent the end user apparently received no error message from DeleteFile() and DeleteFileUTF8() but a subsequent call to FileExists() revealed that the file was, in fact still present.  And, on one occasion, I saw a log that indicated, perhaps, the file was deleted but DeleteFile() said it failed !   However, as I later found out, the user was using a disk encryption system and a portable application handler to run it, collectively, we think that was somehow related to the issue. An update to VeraCrypt seems to have helped.

I could not duplicate the end user's problem myself so was dependent on sending him/her a test binary. Fortunately, this was a very cooperative user. One other person did have what might have been a similar problem but with hind sight I don't think that is the case. 

Early stages of this problem was complicated by the fact that Linux/Mac are quite happy to overwrite an existing file with RenameFileUTF8() but Windows is not.  But I recently discovered that Linux is also upset about using RenameFileUTF8() on an existing file if that existing file is on an SMB share. And smb is, of course a Windows protocol ......

I am convinced that this is one very specific, isolated case.  I have left the obsessively careful code in there for Windows in my app, it does no harm and may, perhaps do some good ?

Thanks for input folks !

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 825
    • Lebeau Software
Re: DeleteFile() fails on Win10
« Reply #25 on: June 21, 2020, 12:08:40 am »
Just for the record, several variations that I sent the end user apparently received no error message from DeleteFile() and DeleteFileUTF8() but a subsequent call to FileExists() revealed that the file was, in fact still present.

That is certainly a possibility, if the file is in use at the time it is "deleted".  If the file is open, it will be marked for deletion, and won't actually be physically deleted until all open handles to the file are closed.

And, on one occasion, I saw a log that indicated, perhaps, the file was deleted but DeleteFile() said it failed !

That, I don't see being possible, unless something else deleted the file after DeleteFile() was called.  Unless the log was wrong/misleading, for instance if the log showed the file was *about to be* deleted, and then afterwards showed the *actual delete* failing.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018