fixed temp file creation issues on win32
This commit is contained in:
@@ -123,7 +123,6 @@ INC += epicsTypes.h
|
||||
INC += gsd_sync_defs.h
|
||||
INC += shareLib.h
|
||||
INC += epicsExport.h
|
||||
INC += truncateFile.h
|
||||
INC += unixFileName.h
|
||||
INC += locationException.h
|
||||
INC += ipAddrToAsciiAsynchronous.h
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
#include <limits.h>
|
||||
|
||||
#define epicsExportSharedSymbols
|
||||
#include "truncateFile.h"
|
||||
#include "epicsStdio.h"
|
||||
|
||||
#ifndef SEEK_END
|
||||
#define SEEK_END 2
|
||||
@@ -27,9 +27,8 @@
|
||||
*/
|
||||
epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName, unsigned size)
|
||||
{
|
||||
char tmpName[L_tmpnam];
|
||||
char tmpName[256>L_tmpnam?256:L_tmpnam];
|
||||
long filePos;
|
||||
char *pTmpFN;
|
||||
FILE *pFile;
|
||||
FILE *ptmp;
|
||||
int status;
|
||||
@@ -68,18 +67,18 @@ epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName
|
||||
return TF_OK;
|
||||
}
|
||||
|
||||
pTmpFN = tmpnam (tmpName);
|
||||
if (!pTmpFN) {
|
||||
epicsTempName ( tmpName, sizeof (tmpName) );
|
||||
if ( tmpName[0] == '\0' ) {
|
||||
fprintf (stderr,"Unable to create tmp file name?\n");
|
||||
fclose (pFile);
|
||||
return TF_ERROR;
|
||||
}
|
||||
|
||||
ptmp = fopen (pTmpFN, "w");
|
||||
ptmp = fopen (tmpName, "w");
|
||||
if (!ptmp) {
|
||||
fprintf (stderr,
|
||||
"File access problems to `%s' because `%s'\n",
|
||||
pTmpFN,
|
||||
tmpName,
|
||||
strerror(errno));
|
||||
fclose (pFile);
|
||||
return TF_ERROR;
|
||||
@@ -95,18 +94,18 @@ epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName
|
||||
strerror(errno));
|
||||
fclose (pFile);
|
||||
fclose (ptmp);
|
||||
remove (pTmpFN);
|
||||
remove (tmpName);
|
||||
return TF_ERROR;
|
||||
}
|
||||
status = putc (c, ptmp);
|
||||
if (status==EOF) {
|
||||
fprintf(stderr,
|
||||
"File access problems to `%s' because `%s'\n",
|
||||
pTmpFN,
|
||||
tmpName,
|
||||
strerror(errno));
|
||||
fclose (pFile);
|
||||
fclose (ptmp);
|
||||
remove (pTmpFN);
|
||||
remove (tmpName);
|
||||
return TF_ERROR;
|
||||
}
|
||||
charNo++;
|
||||
@@ -119,17 +118,17 @@ epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName
|
||||
"Unable to remove `%s' during truncate because `%s'\n",
|
||||
pFileName,
|
||||
strerror(errno));
|
||||
remove (pTmpFN);
|
||||
remove (tmpName);
|
||||
return TF_ERROR;
|
||||
}
|
||||
status = rename (pTmpFN, pFileName);
|
||||
status = rename (tmpName, pFileName);
|
||||
if (status!=TF_OK) {
|
||||
fprintf (stderr,
|
||||
"Unable to rename %s to `%s' because `%s'\n",
|
||||
pTmpFN,
|
||||
tmpName,
|
||||
pFileName,
|
||||
strerror(errno));
|
||||
remove (pTmpFN);
|
||||
remove (tmpName);
|
||||
return TF_ERROR;
|
||||
}
|
||||
return TF_OK;
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
/*************************************************************************\
|
||||
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
|
||||
* National Laboratory.
|
||||
* Copyright (c) 2002 The Regents of the University of California, as
|
||||
* Operator of Los Alamos National Laboratory.
|
||||
* EPICS BASE Versions 3.13.7
|
||||
* and higher are distributed subject to a Software License Agreement found
|
||||
* in file LICENSE that is included with this distribution.
|
||||
\*************************************************************************/
|
||||
|
||||
#include "shareLib.h"
|
||||
|
||||
/*
|
||||
* truncate to specified size (we dont use truncate()
|
||||
* because it is not portable)
|
||||
*
|
||||
* pFileName - name (and optionally path) of file
|
||||
* size - the new file size (if file is curretly larger)
|
||||
*
|
||||
* returns TF_OK if the file is less than size bytes
|
||||
* or if it was successfully truncated. Returns
|
||||
* TF_ERROR if the file could not be truncated.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
enum TF_RETURN {TF_OK=0, TF_ERROR=1};
|
||||
epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile (const char *pFileName, unsigned size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -25,7 +25,23 @@ epicsShareFunc int epicsShareAPI epicsSnprintf(
|
||||
char *str, size_t size, const char *format, ...) EPICS_PRINTF_STYLE(3,4);
|
||||
epicsShareFunc int epicsShareAPI epicsVsnprintf(
|
||||
char *str, size_t size, const char *format, va_list ap);
|
||||
epicsShareFunc FILE * epicsShareAPI epicsTempFile ( void );
|
||||
epicsShareFunc void epicsShareAPI epicsTempName (
|
||||
char * pNameBuf, size_t nameBufLength );
|
||||
epicsShareFunc FILE * epicsShareAPI epicsTempFile ();
|
||||
|
||||
/*
|
||||
* truncate to specified size (we dont use truncate()
|
||||
* because it is not portable)
|
||||
*
|
||||
* pFileName - name (and optionally path) of file
|
||||
* size - the new file size (if file is curretly larger)
|
||||
*
|
||||
* returns TF_OK if the file is less than size bytes
|
||||
* or if it was successfully truncated. Returns
|
||||
* TF_ERROR if the file could not be truncated.
|
||||
*/
|
||||
enum TF_RETURN {TF_OK=0, TF_ERROR=1};
|
||||
epicsShareFunc enum TF_RETURN epicsShareAPI truncateFile ( const char *pFileName, unsigned size );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -20,7 +20,29 @@
|
||||
#include <sys\stat.h>
|
||||
|
||||
#define epicsExportSharedSymbols
|
||||
#include <epicsStdio.h>
|
||||
#include "epicsStdio.h"
|
||||
|
||||
//
|
||||
// epicsTempName
|
||||
//
|
||||
// allow the teporary file directory to be set with the
|
||||
// TMP environment varianble
|
||||
//
|
||||
extern "C"
|
||||
epicsShareFunc void epicsShareAPI epicsTempName (
|
||||
char * pNameBuf, size_t nameBufLength )
|
||||
{
|
||||
if ( nameBufLength ) {
|
||||
pNameBuf[0] = '\0';
|
||||
char * pName = _tempnam ( "c:\\tmp", "epics" );
|
||||
if ( pName ) {
|
||||
if ( nameBufLength > strlen ( pName ) ) {
|
||||
strncpy ( pNameBuf, pName, nameBufLength );
|
||||
}
|
||||
free ( pName );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// epicsTmpFile
|
||||
@@ -29,10 +51,8 @@
|
||||
// TMP environment varianble
|
||||
//
|
||||
extern "C"
|
||||
epicsShareFunc FILE * epicsShareAPI epicsTempFile ( void )
|
||||
epicsShareFunc FILE * epicsShareAPI epicsTempFile ()
|
||||
{
|
||||
// Allow the TMP env variable to set the location
|
||||
// where temporary files live.
|
||||
char * pName = _tempnam ( "c:\\tmp", "epics" );
|
||||
if( ! pName ) {
|
||||
return 0;
|
||||
@@ -59,13 +79,16 @@ epicsShareFunc FILE * epicsShareAPI epicsTempFile ( void )
|
||||
const int openFlag = _O_CREAT | _O_EXCL | _O_RDWR |
|
||||
_O_SHORT_LIVED | _O_BINARY | _O_TEMPORARY;
|
||||
int fd = open ( pName, openFlag, _S_IWRITE );
|
||||
if ( fd < 0 ) {
|
||||
FILE * pNewFile = 0;
|
||||
if ( fd >=0 ) {
|
||||
pNewFile = _fdopen ( fd, "w+bTD" );
|
||||
}
|
||||
else {
|
||||
printf (
|
||||
"Temporary file \"%s\" open failed because "
|
||||
"\"%s\"\n", pName, strerror ( errno ) );
|
||||
return 0;
|
||||
}
|
||||
free ( pName );
|
||||
return _fdopen ( fd, "w+bTD" );
|
||||
return pNewFile;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
* in file LICENSE that is included with this distribution.
|
||||
\*************************************************************************/
|
||||
|
||||
#include <epicsStdio.h>
|
||||
#include <osiUnistd.h>
|
||||
#include "epicsStdio.h"
|
||||
#include "osiUnistd.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
|
||||
@@ -8,7 +8,23 @@
|
||||
* in file LICENSE that is included with this distribution.
|
||||
\*************************************************************************/
|
||||
|
||||
#include <epicsStdio.h>
|
||||
#include "epicsStdio.h"
|
||||
|
||||
extern "C"
|
||||
epicsShareFunc void epicsShareAPI epicsTempName (
|
||||
char * pNameBuf, size_t nameBufLength )
|
||||
{
|
||||
if ( nameBufLength ) {
|
||||
pNameBuf[0] = '\0';
|
||||
char nameBuf[L_tmpnam];
|
||||
if ( tmpnam ( nameBuf ) ) {
|
||||
if ( nameBufLength > strlen ( nameBuf ) ) {
|
||||
strncpy ( pNameBuf, nameBuf, nameBufLength );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extern "C"
|
||||
epicsShareFunc FILE * epicsShareAPI epicsTempFile ( void )
|
||||
|
||||
Reference in New Issue
Block a user