Boost logo

Boost :

Subject: Re: [boost] [filesystem] scoped_file feature request?
From: Jorge Lodos Vigil (lodos_at_[hidden])
Date: 2011-10-12 07:38:11


Robert Stewart wrote:

> Jorge Lodos Vigil wrote:
> > Beman Dawes wrote:
> >
> > > There is a long outstanding feature request, #1090 from
> > > Marcus Lindblom, requesting the following be added to
> > > Boost.Filesystem:
> >
> > > class scoped_file {
> > > public:
> > > explicit scoped_file(const boost::filesystem::path& file)
> > > : m_path(file) {}
> > >
> > > ~scoped_file() { boost::filesystem::remove(m_path); }
> > >
> > > private:
> > > boost::filesystem::path m_path;
> > > };
> >
> > > My weakly held opinion is that this request should be
> > > rejected.
> > > Rationale: Easy enough for users to do themselves, so it
> > > doesn't justify the additional surface area it would add to
> > > of the library.

> > If the constructor would create the file I find this useful,
> > even more if there is an option to create a unique file (using
> > tmpfile, for instance). Temporary unique files are common and
> > it would be nice to have a portable implementation. The name
> > could be temporary_file instead of scoped_file. Please note
> > that the current unique_path function is not very useful
> > because the path is unique when it is generated but not
> > necessarily when it is used.

> I created just such a temporary file class for use in our unit test library. It uses OS-specific APIs to create a temporary file which it destroys on destruction. Obviously, such a class must provide normal file operations to be useful, whereas scoped_file is merely concerned with removing the file upon scope exit.

I don't believe a temporary file class _must_ provide any file operation, it would be very useful by just ensuring that the created temporary file is unique and exposing its name. This is the problem that currently cannot be solved with boost::filesystem. If temporary files are not created together with the temporary path there is always a risk that the path is used between the time it was found and the file was created. To ensure _file_ uniqueness you must use functions like tmpfile.

Best regards
Jorge


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk