Date: Mon, 3 Dec 2001 18:04:35 -0500 From: The Anarcat <anarcat@anarcat.dyndns.org> To: Libh <freebsd-libh@freebsd.org> Subject: Re: removing mktemp call from File Message-ID: <20011203230435.GB2908@shall.anarcat.dyndns.org> In-Reply-To: <20011203224645.GA2908@shall.anarcat.dyndns.org> References: <20011203224645.GA2908@shall.anarcat.dyndns.org>
next in thread | previous in thread | raw e-mail | index | archive | help
--OBd5C1Lgu00Gd/Tn
Content-Type: multipart/mixed; boundary="2B/JsCI69OhZNC5r"
Content-Disposition: inline
--2B/JsCI69OhZNC5r
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Here is the modification. libh still compiles and I expect the general
behavior haven't changed at all, apart from the race condition.
The code is now a bit more complex since all calls that were made to
localName() now have to check if the file has been created.=20
localName() and File, in general, haven't been properly designed, IMHO.
The fact that localName() initializes mLocalName and created a file is
counter-intuitive. mUrl should be accessible only through a setUrl
interface and that should set mLocalName accordingly.
The local file should then be created when asked/needed. Not when
accessing its path.
<mental note>I will put this in the commit log, I think.. </mental note>
a.
--2B/JsCI69OhZNC5r
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=patch
Content-Transfer-Encoding: quoted-printable
Index: include/file/Fetch.hh
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/libh/cvs/libh/include/file/Fetch.hh,v
retrieving revision 1.4
diff -u -r1.4 Fetch.hh
--- include/file/Fetch.hh 2001/09/20 21:08:59 1.4
+++ include/file/Fetch.hh 2001/12/03 22:56:10
@@ -69,7 +69,7 @@
virtual bool isRemote( const string& aUrl );
virtual void stat( const string& aUrl, Stat& aStat );
virtual Pointer<Container<list<string> > > directory( const string& a=
Url );
- virtual void download( const string& aUrl, const string& aLocalName );
+ virtual string download( const string& aUrl );
=20
static Pointer<Fetch> instance();
=20
Index: include/file/File.hh
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/libh/cvs/libh/include/file/File.hh,v
retrieving revision 1.5
diff -u -r1.5 File.hh
--- include/file/File.hh 2001/09/20 21:08:59 1.5
+++ include/file/File.hh 2001/12/03 22:56:10
@@ -92,6 +92,7 @@
bool mRemoveOnClose;
FILE *mFile;
=20
+ int create_tmp_file();
static void read_directory( const string& aDir, bool aRecurseIntoSubd=
irs, bool aIncludeDirs, Pointer<Container<list<string> > > aIgnoreNames, li=
st<Pointer<RealFile> >& files );
static void add_file( const string& aName, const string& aDir, bool a=
RecurseIntoSubdirs, bool aIncludeDirs, Pointer<Container<list<string> > > a=
IgnoreNames, list<Pointer<RealFile> >& files );
=20
Index: lib/file/Fetch.cc
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/libh/cvs/libh/lib/file/Fetch.cc,v
retrieving revision 1.5
diff -u -r1.5 Fetch.cc
--- lib/file/Fetch.cc 2001/09/20 21:14:11 1.5
+++ lib/file/Fetch.cc 2001/12/03 22:56:10
@@ -38,6 +38,10 @@
#include "file/Fetch.hh"
#endif
=20
+#ifndef Configuration_hh
+#include "Configuration.hh"
+#endif
+
#include <stdexcept>
#include <cerrno>
#include <unistd.h>
@@ -118,19 +122,30 @@
=20
//=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=20
-void Fetch::download( const string& aUrl, const string& aLocalName )
+// download the given url in a temporary file
+// returns the temporary file
+// should be given a File to d/l in instead
+string Fetch::download( const string& aUrl )
{
#ifdef FETCH_LIB
FILE* in =3D 0;
FILE* out =3D 0;
+ static char aLocalName[ PATH_MAX ];
=20
try {
in =3D fetchGetURL( const_cast<char*>( addSchemeSpecifier( aUrl ).c_str(=
) ), 0 );
- if ( in =3D=3D 0 )
- throw runtime_error( string( "Cannot open URL " ) + aUrl + ": " + sComE=
rr );
- out =3D fopen( aLocalName.c_str(), "w" );
- if ( out =3D=3D 0 )
- throw runtime_error( string( "Cannot open file " ) + aLocalName + ": " =
+ ::strerror( errno ) );
+ if ( in =3D=3D 0 )
+ throw runtime_error( string( "Cannot open URL " ) + aUrl + ": " + sCom=
Err );
+
+ strncpy( aLocalName, ( Configuration::instance()->tempDir()
+ + "/libhfile-XXXXXX" ).c_str(),
+ PATH_MAX);
+ int s =3D ::mkstemp( aLocalName );
+ if ( s =3D=3D 0 )
+ throw runtime_error( string( "Unable to find unique name for temporary=
file in " ) + Configuration::instance()->tempDir() + ": " + ::strerror( er=
rno ) );
+ out =3D fdopen( s, "w" );
+ if ( out =3D=3D 0 )
+ throw runtime_error( string( "Cannot open file " ) + aLocalName + ": "=
+ ::strerror( errno ) );
=20
int c;
while ( ( c =3D getc( in ) ) !=3D EOF )
@@ -146,6 +161,7 @@
fclose( out );
throw;
}
+ return string(aLocalName);
#else
throw runtime_error( "Libfetch support disabled" );
#endif
Index: lib/file/Fetch.cd.cc
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/libh/cvs/libh/lib/file/Fetch.cd.cc,v
retrieving revision 1.5
diff -u -r1.5 Fetch.cd.cc
--- lib/file/Fetch.cd.cc 2001/09/20 21:14:11 1.5
+++ lib/file/Fetch.cd.cc 2001/12/03 22:56:10
@@ -57,7 +57,7 @@
MethodDescription( "download", "download",
LanguageInterface::Object::MethodDescription::fDynamic | LanguageInter=
face::Object::MethodDescription::fSafeExecution,
"Downloads file",
- MethodDescription::vtVoid, MethodDescription::Value( MethodDescription=
::vtString, 0, 0, "URL" ), MethodDescription::Value( MethodDescription::vtS=
tring, 0, 0, "local_file_name" ) ),
+ MethodDescription::vtVoid, MethodDescription::Value( MethodDescription=
::vtString, 0, 0, "URL" ) ),
MethodDescription()
};
=20
Index: lib/file/File.cc
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
RCS file: /home/libh/cvs/libh/lib/file/File.cc,v
retrieving revision 1.7
diff -u -r1.7 File.cc
--- lib/file/File.cc 2001/09/20 21:14:11 1.7
+++ lib/file/File.cc 2001/12/03 22:56:10
@@ -102,8 +102,7 @@
Fetch::instance()->stat( mUrl, st );
if ( st.type !=3D Fetch::Stat::Regular )
throw runtime_error( string( "Cannot download non-regular file " ) + mU=
rl );
- Fetch::instance()->download( mUrl, localName() );
- return localName();
+ return ( mLocalName =3D Fetch::instance()->download( mUrl ) );
}
#endif
} // File::open_local
@@ -141,7 +140,7 @@
Fetch::instance()->stat( mUrl, st );
if ( st.type !=3D Fetch::Stat::Regular )
throw runtime_error( string( "Cannot open non-regular file " ) + mUrl +=
" for reading" );
- Fetch::instance()->download( mUrl, localName() );
+ mLocalName =3D Fetch::instance()->download( mUrl );
mRemoveOnClose =3D remove_local_copy_on_close;
}
FILE* f =3D mFile =3D ::fopen( localName().c_str(), "r" );
@@ -157,7 +156,15 @@
{
#ifndef LANGUAGE_INTERFACE
if ( isLocal() ) {
- FILE* f =3D mFile =3D ::fopen( localName().c_str(), "w+" );
+ FILE* f;
+ if (localName().length() =3D=3D 0) {
+ int s =3D create_tmp_file();
+ if ( s =3D=3D 0 )
+ throw runtime_error( string( "Unable to create local file " ) + mUrl +=
": " + ::strerror( errno ) );
+ f =3D mFile =3D ::fdopen(s , "w+" );
+ } else {
+ f =3D mFile =3D ::fopen(localName().c_str(), "w+");
+ }
if ( f =3D=3D 0 )
throw runtime_error( string( "Unable to create local file " ) + mUrl + =
": " + ::strerror( errno ) );
return f;
@@ -193,12 +200,7 @@
}
}
else {
- static char templ[ PATH_MAX ];
- strcpy( templ, ( Configuration::instance()->tempDir() + "/libhfile-XXXX=
XX" ).c_str() );
- const char* s =3D ::mktemp( templ );
- if ( s =3D=3D 0 )
- throw runtime_error( string( "Unable to find unique name for temporary=
file in " ) + Configuration::instance()->tempDir() + ": " + ::strerror( er=
rno ) );
- mLocalName =3D s;
+ mLocalName =3D "";
}
}
return(mLocalName);
@@ -301,6 +303,23 @@
return(files);
#endif
} // File::scan
+
+//=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
+
+
+// create a temporary file securly an return its file descriptor
+// ** sets mLocalName **
+int File::create_tmp_file () {
+
+ static char templ[ PATH_MAX ];
+ strcpy( templ, ( Configuration::instance()->tempDir() + "/libhfile-XXXXXX=
" ).c_str() );
+ int s =3D ::mkstemp( templ );
+ if ( s =3D=3D 0 )
+ throw runtime_error( string( "Unable to find unique name for temporary f=
ile in " ) + Configuration::instance()->tempDir() + ": " + ::strerror( errn=
o ) );
+ mLocalName =3D templ;
+ return s;
+
+}
=20
//=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=20
--2B/JsCI69OhZNC5r--
--OBd5C1Lgu00Gd/Tn
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (FreeBSD)
Comment: For info see http://www.gnupg.org
iEYEARECAAYFAjwMBQIACgkQttcWHAnWiGcCgwCeKQi0Za0SkKWy6I75tVRC3RQy
MJ0AnRkka2a+H3yg/BhF4mNq4TmwP+sN
=LwmG
-----END PGP SIGNATURE-----
--OBd5C1Lgu00Gd/Tn--
To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-libh" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20011203230435.GB2908>
