Added GfFileCopy and GfFileExists functions to TGF API, implemented in new file.cpp, the 1st one being stolen and moved from filesetup.cpp

git-svn-id: https://svn.code.sf.net/p/speed-dreams/code/trunk@2884 30fe4595-0a0c-4342-8851-515496e4dcbd

Former-commit-id: acff3cc2dc988a10cfdf1cb27f58f265a9badad3
Former-commit-id: a1818ecd2ef770af2486a456708fcaae695ba43e
This commit is contained in:
pouillot 2010-10-13 22:34:05 +00:00
parent c893600c61
commit 1e67d255bd
6 changed files with 195 additions and 149 deletions

View file

@ -8,8 +8,10 @@ ADD_OSSPEC_INCLUDEDIR()
ADD_SDLIB_INCLUDEDIR(txml portability)
SET(TGF_HEADERS os.h tgf.h modinfo.h carinfo.h)
SET(TGF_SOURCES directory.cpp filesetup.cpp formula.cpp hash.cpp module.cpp modinfo.cpp os.cpp
params.cpp profiler.cpp schedulespy.cpp tgf.cpp trace.cpp carinfo.cpp ${TGF_HEADERS})
SET(TGF_SOURCES directory.cpp file.cpp filesetup.cpp
formula.cpp hash.cpp module.cpp modinfo.cpp os.cpp
params.cpp profiler.cpp schedulespy.cpp tgf.cpp trace.cpp
carinfo.cpp ${TGF_HEADERS})
IF(WIN32)
# DLL export stuff under Windows (to avoid .def file)

View file

@ -23,9 +23,14 @@
@ingroup dir
*/
#ifndef WIN32
#include <sys/stat.h>
#endif
#include <cstdlib>
#include <cerrno>
#include "tgf.h"
#include "portability.h"
#include "os.h"
void
@ -93,3 +98,58 @@ void GfDirFreeList(tFList *list, tfDirfreeUserData freeUserData, bool freeName,
list = NULL;
}
/** Create a directory and the parents if needed
@ingroup dir
@param dir full directory path-name
@return GF_DIR_CREATED on success, GF_DIR_CREATION_FAILED otherwise.
*/
int GfDirCreate(const char *path)
{
if (path == NULL) {
return GF_DIR_CREATION_FAILED;
}
static const int nPathBufSize = 1024;
char buf[nPathBufSize];
strncpy(buf, path, nPathBufSize);
#ifdef WIN32
// Translate path.
static const char cPathSeparator = '\\';
int i;
for (i = 0; i < nPathBufSize && buf[i] != '\0'; i++)
if (buf[i] == '/')
buf[i] = cPathSeparator;
#else // WIN32
// mkdir with u+rwx access grants by default
#ifdef mkdir
# undef mkdir
#endif
#define mkdir(x) mkdir((x), S_IRWXU)
static const char cPathSeparator = '/';
#endif // WIN32
// Try to create the requested folder.
int err = mkdir(buf);
// If this fails, try and create the parent one (recursive), and the retry.
if (err == -1 && errno == ENOENT)
{
// Try the parent one (recursive).
char *end = strrchr(buf, cPathSeparator);
*end = '\0';
GfDirCreate(buf);
// Retry.
*end = cPathSeparator;
err = mkdir(buf);
}
return (err == -1 && errno != EEXIST) ? GF_DIR_CREATION_FAILED : GF_DIR_CREATED;
}

116
src/libs/tgf/file.cpp Executable file
View file

@ -0,0 +1,116 @@
/***************************************************************************
file.cpp -- directory management
-------------------
created : Thu Oct 12 21:58:55 CEST 2010
copyright : (C) 2010 by Mart Kelder, Jean-Philippe Meuret
web : http://www.speed-reams.org
version : $Id$
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
/** @file
This is used for file manipulations.
@author Mart Kelder, Jean-Philippe Meuret
@version $Id: directory.cpp,v 1.7 2005/02/01 15:55:54 berniw $
@ingroup file
*/
#include <cstdio>
#include <cerrno>
#include <sys/stat.h>
#include "tgf.h"
bool GfFileExists(const char* pszName)
{
struct stat st;
return stat(pszName, &st) ? false : true;
}
bool GfFileCopy(const char* pszSrcName, const char* pszTgtName)
{
static const size_t maxBufSize = 1024;
char buf[maxBufSize];
FILE *in;
FILE *out;
size_t size;
int errnum;
bool res = true;
// Create the target local directory (and parents) if not already done
// (first, we have to deduce its path from the target file path-name).
strncpy(buf, pszTgtName, strlen(pszTgtName)+1);
#ifdef WIN32
for (int i = 0; i < maxBufSize && buf[i] != '\0'; i++)
if (buf[i] == '\\')
buf[i] = '/';
#endif
char *lastSlash = strrchr(buf, '/');
if (lastSlash)
{
*lastSlash = '\0';
GfDirCreate( buf );
}
if( ( in = fopen( pszSrcName, "rb" ) ) == NULL )
{
errnum = errno; // Get errno before it is overwritten by some system call.
GfLogError("Could not open %s in 'rb' mode when copying it to %s (%s).\n",
pszSrcName, pszTgtName, strerror(errnum));
return false;
}
if( ( out = fopen( pszTgtName, "wb" ) ) == NULL )
{
errnum = errno; // Get errno before it is overwritten by some system call.
GfLogError("Could not open %s in 'wb' mode when creating it from %s (%s).\n",
pszTgtName, pszSrcName, strerror(errnum));
fclose( in );
return false;
}
GfLogDebug("Updating %s\n", pszTgtName);
while( !feof( in ) )
{
size = fread( buf, 1, 1024, in );
if( size > 0 )
{
fwrite( buf, 1, size, out );
if( ferror( out ) )
{
errnum = errno; // Get errno before it is overwritten by some system call.
GfLogError("Failed to write data to %s when creating it from %s (%s).\n",
pszTgtName, pszSrcName, strerror(errnum));
res = false;
break;
}
}
else if( ferror( in ) )
{
errnum = errno; // Get errno before it is overwritten by some system call.
GfLogError("Failed to read data from %s when copying it to %s (%s).\n",
pszSrcName, pszTgtName, strerror(errnum));
res = false;
break;
}
}
fclose( in );
fclose( out );
#ifndef WIN32
chmod( pszTgtName, 0640 );
#endif //!WIN32
return false;
}

View file

@ -29,93 +29,10 @@
#include "portability.h"
static int gfFileSetupCopyFile( const char* dataLocation, const char* localLocation )
{
FILE *in;
FILE *out;
char buf[1024];
size_t size;
int errnum;
int res = 0;
if( ( in = fopen( dataLocation, "rb" ) ) == NULL )
{
errnum = errno; // Get errno before it is overwritten by some system call.
GfLogError("Could not open %s in 'rb' mode when copying it to %s (%s).\n",
dataLocation, localLocation, strerror(errnum));
return -1;
}
if( ( out = fopen( localLocation, "wb" ) ) == NULL )
{
errnum = errno; // Get errno before it is overwritten by some system call.
GfLogError("Could not open %s in 'w' mode when creating it from %s (%s).\n",
localLocation, dataLocation, strerror(errnum));
fclose( in );
return -1;
}
GfLogDebug("Updating %s\n", localLocation);
while( !feof( in ) )
{
size = fread( buf, 1, 1024, in );
if( size > 0 )
{
fwrite( buf, 1, size, out );
if( ferror( out ) )
{
errnum = errno; // Get errno before it is overwritten by some system call.
GfLogError("Failed to write data to %s when creating it from %s (%s).\n",
localLocation, dataLocation, strerror(errnum));
res = -1;
break;
}
}
else if( ferror( in ) )
{
errnum = errno; // Get errno before it is overwritten by some system call.
GfLogError("Failed to read data from %s when copying it to %s (%s).\n",
dataLocation, localLocation, strerror(errnum));
res = -1;
break;
}
}
fclose( in );
fclose( out );
#ifndef WIN32
chmod( localLocation, 0640 );
#endif //!WIN32
return res;
}
static void gfFileSetupCopy( char* dataLocation, char* localLocation, int major, int minor, void *localHandle, int count )
{
static const size_t maxBufSizeSize = 1024;
char stringBuf[maxBufSizeSize];
// Create the target local directory (and parents) if not already done
// (first, we have to deduce its path from the target file path-name).
strncpy(stringBuf, localLocation, strlen(localLocation)+1);
#ifdef WIN32
int i;
for (i = 0; i < maxBufSizeSize && stringBuf[i] != '\0'; i++) {
if (stringBuf[i] == '\\') {
stringBuf[i] = '/';
}
}
#endif
char *lastSlash = strrchr(stringBuf, '/');
if (lastSlash)
{
*lastSlash = '\0';
GfDirCreate( stringBuf );
}
// Copy the source file to its target place.
if( gfFileSetupCopyFile( dataLocation, localLocation ) != 0 )
if( !GfFileCopy( dataLocation, localLocation ) )
return;
// Update local version.xml file.
@ -130,11 +47,12 @@ static void gfFileSetupCopy( char* dataLocation, char* localLocation, int major,
}
else
{
snprintf( stringBuf, 30, "versions/%d", count );
GfParmSetStr( localHandle, stringBuf, "Data location", dataLocation );
GfParmSetStr( localHandle, stringBuf, "Local location", localLocation );
GfParmSetNum( localHandle, stringBuf, "Major version", NULL, (tdble)major );
GfParmSetNum( localHandle, stringBuf, "Minor version", NULL, (tdble)minor );
char buf[32];
snprintf( buf, 30, "versions/%d", count );
GfParmSetStr( localHandle, buf, "Data location", dataLocation );
GfParmSetStr( localHandle, buf, "Local location", localLocation );
GfParmSetNum( localHandle, buf, "Major version", NULL, (tdble)major );
GfParmSetNum( localHandle, buf, "Minor version", NULL, (tdble)minor );
}
}
}

View file

@ -21,12 +21,10 @@
#include <direct.h>
#include <shlobj.h>
#else
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h> // getcwd
#endif
#include <errno.h>
#include <cerrno>
#include <ctime>
#include <cstring>
@ -895,58 +893,3 @@ int GfNearestPow2 (int x)
return (1 << r);
}
/** Create a directory and the parents if needed
@ingroup dir
@param dir full directory path-name
@return GF_DIR_CREATED on success, GF_DIR_CREATION_FAILED otherwise.
*/
int GfDirCreate(const char *path)
{
if (path == NULL) {
return GF_DIR_CREATION_FAILED;
}
static const int nPathBufSize = 1024;
char buf[nPathBufSize];
strncpy(buf, path, nPathBufSize);
#ifdef WIN32
// Translate path.
static const char cPathSeparator = '\\';
int i;
for (i = 0; i < nPathBufSize && buf[i] != '\0'; i++)
if (buf[i] == '/')
buf[i] = cPathSeparator;
#else // WIN32
// mkdir with u+rwx access grants by default
#ifdef mkdir
# undef mkdir
#endif
#define mkdir(x) mkdir((x), S_IRWXU)
static const char cPathSeparator = '/';
#endif // WIN32
// Try to create the requested folder.
int err = mkdir(buf);
// If this fails, try and create the parent one (recursive), and the retry.
if (err == -1 && errno == ENOENT)
{
// Try the parent one (recursive).
char *end = strrchr(buf, cPathSeparator);
*end = '\0';
GfDirCreate(buf);
// Retry.
*end = cPathSeparator;
err = mkdir(buf);
}
return (err == -1 && errno != EEXIST) ? GF_DIR_CREATION_FAILED : GF_DIR_CREATED;
}

View file

@ -258,6 +258,13 @@ typedef void (*tfDirfreeUserData)(void*); /**< Function to call for releasing th
TGF_API void GfDirFreeList(tFList *list, tfDirfreeUserData freeUserData, bool freeName = false, bool freeDispName = false);
/************************
* File management *
************************/
TGF_API bool GfFileExists(const char* pszName);
TGF_API bool GfFileCopy(const char* pszSrcName, const char* pszTgtName);
/**************************************
* Directory and file path management *
**************************************/