[Development] QTemporaryDir API

David Faure david.faure at kdab.com
Thu Nov 17 17:11:43 CET 2011


On Thursday 17 November 2011 13:52:55 lars.knoll at nokia.com wrote:
> Hi David,
> 
> looks good in general.
> 
> I am however concerned about the static removeDir() method. This one is
> pretty much the equivalent of a 'rm -rf'. Since it's not limited to
> temporary directories, so it doesn't match the class.
> 
> It's also a rather dangerous operation, and I'm worried we'll see bugs in
> it's usage leading to users home directories being blown away.

Well this is actually one of the reasons for having it in QTemporaryDir rather 
than in QDir : to make it clear that this should only be used for 
programmatically created dirs (mostly temp dirs, but there are a few other use 
cases like apps saving stuff into dirs). Not as a general QDir method to be 
used .

The method already asserts if the path is empty or is ".", we could make that 
stricter to catch more bugs (error instead of debug-mode-only assert, and 
refuse more paths, including the home dir).

But I fail to see why "this API could be misused" is a reason for not having 
it. I mean, one could make the same argument against having QFile::remove in 
the API -- it could be used to delete the document that a user spent days to 
write!

In any case: see http://developer.qt.nokia.com/forums/viewthread/9860/ for the 
"popular demand" for such a method. I could be convinced to put it into QDir, 
I would also be very happy to leave it in QTemporaryDir, for "almost-like a 
temp dir but not created by QTemporaryDir because a random naming isn't 
wanted". But I think it would be a mistake to not have any recursive deletion 
method, available, still, in Qt5.

Note that KTemporaryDir had a removeDir method for many many years and nobody 
ever blamed the kdelibs developers for "bugs in its usage leading to users 
home dirs being blown away"...
If the app developers implement their own version and call it wrongly, then 
the users still lose data, and worse, we make this more likely by not 
providing a well-tested method to app developers.
E.g. it's not only about the starting directory. There's the issue of not 
following symlinks to directories. Any naive implementation based on QFileInfo 
will get this wrong, and will follow symlinks. Ouch!

-- 
David Faure | david.faure at kdab.com | KDE/Qt Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, Sweden (HQ) +46-563-540090
KDAB - Qt Experts - Platform-independent software solutions




More information about the Development mailing list