TFileSystem.DelDirTree empties a drive root for 'c:' or 'c:\', and...
Brought to you by:
twm
From a code review of TFileSystem.DelDirTree and the routines it calls. The code is older than the recent link changes ([bugs:#101]).
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].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.DelDirTree('\') raises EDirNotFound for an existing directory, because etpd('\') is ''.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].DeleteFile with Force does not check FileGetAttr for -1 and then calls FileSetAttr with almost every attribute bit set.Force fails too, DeleteFile and RemoveDir leave the read-only flag cleared.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.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.
Fixed in revision [r2533].
Related
Commit: [r2533]
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:
#109Commit: [r2542]