Menu ▾ ▴

#105 TFileSystem.DelDirTree empties a drive root for 'c:' or 'c:\', and DeleteFile with Force changes a symlink's target

closed
nobody
2026-11-05
2026-11-05
No

From a code review of TFileSystem.DelDirTree and the routines it calls. The code is older than the recent link changes ([bugs:#101]).

  1. DelDirTree('c:') deletes the contents of the root of drive C: rather than the current directory on C:. IsDirLinkName exits early because of the colon, DirectoryExists('c:') checks the current directory on C: and is True, and the enumeration then runs on itpd('c:') = c:\. This is the same trap as [bugs:#98].
  2. There is no guard against a root (c:\, \\server\share\): the whole volume or share is emptied before the final RemoveDir fails. TUniqueTempDir.Destroy calls DelDirTree with RaiseException = False, so this would go unnoticed.
  3. DelDirTree('\') raises EDirNotFound for an existing directory, because etpd('\') is ''.
  4. TFileSystem.DeleteFile with Force uses FileGetAttr/FileSetAttr, which from Delphi XE2 on follow a symbolic link. For a read-only file symlink they clear the read-only flag of the target, possibly outside the tree, and the link itself still cannot be deleted. RemoveDir had the same problem and was fixed in [bugs:#101].
  5. DeleteFile with Force does not check FileGetAttr for -1 and then calls FileSetAttr with almost every attribute bit set.
  6. When the second attempt with Force fails too, DeleteFile and RemoveDir leave the read-only flag cleared.
  7. An error from FindFirst/FindNext (for example access denied on a subdirectory) counts as "no more files", so DelDirTree reports a misleading "directory is not empty" from RemoveDirectory instead of the real cause.
  8. Entries are deleted while the same directory is still being enumerated. Some SMB servers skip entries then, so RemoveDir fails although everything could be deleted. Not measured.

Decided: DelDirTree will refuse a root, '' and a bare drive such as c: with an exception instead of deleting anything.

Related

Bug Reports: #101
Bug Reports: #109
Bug Reports: #98

Discussion

  • Thomas Mueller

    Thomas Mueller - 2026-11-05

    Fixed in revision [r2533].

     

    Related

    Commit: [r2533]

  • Thomas Mueller

    Thomas Mueller - 2026-11-07

    Follow-up in revision [r2542]: more spellings of a root are refused (all of \\?\GLOBALROOT, Global\ after the prefix, the NT prefix \??\, . or empty segments). A check by what the directory is, instead of its name, is in [bugs:#109].

     

    Related

    Bug Reports: #109
    Commit: [r2542]


Log in to post a comment.