IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 21116


Ignore:
Timestamp:
Jan 13, 2009, 10:53:38 AM (17 years ago)
Author:
Paul Price
Message:

Cleaning up.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/fits/psFits.c

    r21081 r21116  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.82 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2009-01-03 01:05:47 $
     9 *  @version $Revision: 1.83 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2009-01-13 20:53:38 $
    1111 *
    1212 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    3636
    3737#define MAX_STRING_LENGTH 256  // maximum length string for FITS routines
     38
     39#define FITS_OPEN_RETRIES 5             // Number of retries to attempt when opening a FITS file
     40#define FITS_OPEN_RETRY_WAIT 200000     // Wait between retries (usec)
     41
    3842static char *defaultExtword = "EXTNAME";
    3943
    40 bool p_psFitsDumpErrors (const char* filename, unsigned int lineno, const char* func,
    41                          psErrorCode code, const char *message, ...) {
     44
     45bool p_psFitsDumpErrors (const char* filename, unsigned int lineno, const char* func,
     46                         psErrorCode code, const char *message, ...) {
    4247
    4348    char fitsErr[MAX_STRING_LENGTH];
     
    4954
    5055    while (fits_read_errmsg(fitsErr)) {
    51         p_psError(filename, lineno, func, PS_ERR_BAD_FITS, false, "CFITSIO error: %s", fitsErr);
     56        p_psError(filename, lineno, func, PS_ERR_BAD_FITS, false, "CFITSIO error: %s", fitsErr);
    5257    }
    5358
     
    5560}
    5661
    57 psErrorCode p_psFitsError(const char* filename, unsigned int lineno, const char* func, 
    58                           int status, bool new, const char *errorMsg, ...)
     62psErrorCode p_psFitsError(const char* filename, unsigned int lineno, const char* func,
     63                          int status, bool new, const char *errorMsg, ...)
    5964{
    6065    char fitsErr[MAX_STRING_LENGTH];
     
    6873
    6974    while (fits_read_errmsg(fitsErr)) {
    70         psError(PS_ERR_IO, false, "[CFITSIO error: %s]", fitsErr);
     75        psError(PS_ERR_IO, false, "[CFITSIO error: %s]", fitsErr);
    7176    }
    7277    return PS_ERR_IO;
     
    97102    if (fits != NULL) {
    98103        if (fits_close_file(fits->fd, &status)) {
    99             psFitsDumpErrors (PS_ERR_IO, "Error while closing psFits object");
     104            psFitsDumpErrors (PS_ERR_IO, "Error while closing psFits object");
    100105            return false;
    101106        }
     
    124129}
    125130
    126 # define FITS_OPEN_RETRIES 5
    127 # define FITS_OPEN_RETRY_WAIT 200000
    128 
    129131psFits* psFitsOpen(const char* name, const char* mode)
    130132{
    131133    PS_ASSERT_STRING_NON_EMPTY(name, NULL);
    132134
    133     int status = 0;
    134     fitsfile *fptr = NULL;      /* Pointer to the FITS file */
    135 
    136     // check that the directory exists : for NFS-mounted file systems, the directory may
     135    // Check that the directory exists: for NFS-mounted file systems, the directory may
    137136    // take some time to appear
    138137
    139     int dirstat = 0;
    140     char *tmpname = psStringCopy (name);
    141     char *tmpdir = dirname (tmpname);
    142     char *dir = psStringCopy (tmpdir);
     138    int dirstat = 0;                    // Status of directory access
     139    psString tmpname = psStringCopy(name); // Copy of filename, since dirname() may modify
     140    const char *dir = dirname(tmpname);
    143141    useconds_t waittime = FITS_OPEN_RETRY_WAIT;
    144    
    145     for (int i = 0; (i < FITS_OPEN_RETRIES) && ((dirstat = access (dir, F_OK)) != 0); i++) {
    146         // if the error is not ENOENT, then the error is more serious than NFS-mount delays
    147         if (errno != ENOENT) {
    148             int thisErrno = errno;
    149             char errorBuf[64], *errorMsg;
    150 # if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE)
    151             errorMsg = strerror_r (thisErrno, errorBuf, 64);
    152 # else
    153             strerror_r (thisErrno, errorBuf, 64);
    154             errorMsg = errorBuf;               
    155 # endif
    156             psError (PS_ERR_IO, true, "Directory (%s) for requested file is not accessible: %s", dir, errorMsg);
    157 
    158             psFree (dir);
    159             psFree (tmpname);
    160             return NULL;
    161         }
    162 
    163         usleep (waittime);
    164         waittime *= 2;
     142
     143    for (int i = 0; (i < FITS_OPEN_RETRIES) && ((dirstat = access(dir, F_OK)) != 0); i++) {
     144        if (errno != ENOENT) {
     145            // The error is more serious than NFS-mount delays
     146            int thisErrno = errno;      // Error number
     147            char errorBuf[MAX_STRING_LENGTH], *errorMsg;
     148#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE)
     149            errorMsg = strerror_r(thisErrno, errorBuf, MAX_STRING_LENGTH);
     150#else
     151            strerror_r(thisErrno, errorBuf, MAX_STRING_LENGTH);
     152            errorMsg = errorBuf;
     153#endif
     154            psError(PS_ERR_IO, true, "Directory (%s) for requested file is not accessible: %s",
     155                    dir, errorMsg);
     156
     157            psFree(tmpname);
     158            return NULL;
     159        }
     160
     161        usleep(waittime);
     162        waittime *= 2;
    165163    }
    166164    if (dirstat != 0) {
    167         psError (PS_ERR_IO, true, "Directory (%s) for requested file is not accessible, timed out", dir);
    168         psFree (dir);
    169         psFree (tmpname);
    170         return NULL;
    171     }
    172     psFree (dir);
    173     psFree (tmpname);
     165        psError(PS_ERR_IO, true, "Directory (%s) for requested file is not accessible, timed out", dir);
     166        psFree(tmpname);
     167        return NULL;
     168    }
     169    psFree(tmpname);
    174170
    175171    /* check the mode to determine how to open/create file */
     
    196192    }
    197193
     194    int status = 0;                     // CFITSIO status
     195    fitsfile *fptr = NULL;              // Pointer to the FITS file
     196
    198197    if (newFile) {
    199198        /* Check if an existing file is in the way before creating file */
     
    201200            // file exists, delete old one first
    202201            if (remove(name)) {
    203                 int thisErrno = errno;
    204                 char errorBuf[64], *errorMsg;
     202                int thisErrno = errno;
     203                char errorBuf[64], *errorMsg;
    205204# if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && ! _GNU_SOURCE)
    206                 errorMsg = strerror_r (errno, errorBuf, 64);
     205                errorMsg = strerror_r (errno, errorBuf, 64);
    207206# else
    208                 strerror_r (errno, errorBuf, 64);
    209                 errorMsg = errorBuf;           
     207                strerror_r (errno, errorBuf, 64);
     208                errorMsg = errorBuf;
    210209# endif
    211                 psError (PS_ERR_IO, true, "Failed to delete a previously-existing file: %s", errorMsg);
    212                 fprintf (stderr, "errno: %d, %s, %s : %lx, %lx\n", thisErrno, errorMsg, errorBuf, (long int) errorMsg, (long int) errorBuf);
    213                 return NULL;
    214             }
     210                psError (PS_ERR_IO, true, "Failed to delete a previously-existing file: %s", errorMsg);
     211                fprintf (stderr, "errno: %d, %s, %s : %lx, %lx\n", thisErrno, errorMsg, errorBuf, (long int) errorMsg, (long int) errorBuf);
     212                return NULL;
     213            }
    215214        }
    216215        if (!access(name, F_OK)) {
    217             psError (PS_ERR_IO, true, "deleted file still exists!");
    218             return NULL;
    219         }
     216            psError (PS_ERR_IO, true, "deleted file still exists!");
     217            return NULL;
     218        }
    220219
    221220        #if ( CFITSIO_DISKFILE == 1 )
     
    226225        (&fptr, name, &status);
    227226        if (fptr == NULL || status != 0) {
    228             psFitsDumpErrors (PS_ERR_IO, _("Could not create file,'%s'"), name);
     227            psFitsDumpErrors (PS_ERR_IO, _("Could not create file,'%s'"), name);
    229228            return NULL;
    230229        }
     
    237236        (&fptr, name, iomode, &status);
    238237        if (fptr == NULL || status != 0) {
    239             psFitsDumpErrors(PS_ERR_IO, _("Could not open file,'%s'"), name);
     238            psFitsDumpErrors(PS_ERR_IO, _("Could not open file,'%s'"), name);
    240239            return NULL;
    241240        }
     
    414413        fits_movrel_hdu(fits->fd, extnum, &hdutype, &status);
    415414        if (status != 0) {
    416             psFitsDumpErrors (PS_ERR_LOCATION_INVALID, _("Could not move %d HDUs from current position"), extnum);
     415            psFitsDumpErrors (PS_ERR_LOCATION_INVALID, _("Could not move %d HDUs from current position"), extnum);
    417416            return false;
    418417        }
     
    420419        fits_movabs_hdu(fits->fd, extnum+1, &hdutype, &status);
    421420        if (status != 0) {
    422             psFitsDumpErrors (PS_ERR_LOCATION_INVALID, _("Could not move to specified HDU #%d."), extnum);
     421            psFitsDumpErrors (PS_ERR_LOCATION_INVALID, _("Could not move to specified HDU #%d."), extnum);
    423422            return false;
    424423        }
     
    475474
    476475    if (fits_update_key_str(fits->fd, extword, (char*)name, NULL, &status) != 0) {
    477         psFitsDumpErrors (PS_ERR_IO, _("Could not write data to file %s"), name);
     476        psFitsDumpErrors (PS_ERR_IO, _("Could not write data to file %s"), name);
    478477        return false;
    479478    }
Note: See TracChangeset for help on using the changeset viewer.