示例#1
0
bool make_backup(const char * name)
{
    char * backup;

    if(fexist(name)) {
      backup = backup_fn(name);
      if(rename(name, backup) == 0) {
        fprintf(stderr, "\nBack Off! I just backed up %s to %s\n",
                name, backup);
      } else {
        fprintf(stderr, "Sorry couldn't backup %s to %s\n", name, backup);
        return FALSE;
      }
    }
    return TRUE;
}
示例#2
0
gmx_bool make_backup(const char * name)
{
    char * env;
    int    count_max;
    char * backup;

#ifdef GMX_FAHCORE
    return FALSE; /* skip making backups */
#else

    if (gmx_fexist(name))
    {
        env = getenv("GMX_MAXBACKUP");
        if (env != NULL)
        {
            count_max = strtol(env, NULL, 10);
            if (count_max == -1)
            {
                /* Do not make backups and possibly overwrite old files */
                return TRUE;
            }
        }
        else
        {
            /* Use the default maximum */
            count_max = -1;
        }
        backup = backup_fn(name, count_max);
        if (rename(name, backup) == 0)
        {
            fprintf(stderr, "\nBack Off! I just backed up %s to %s\n",
                    name, backup);
        }
        else
        {
            fprintf(stderr, "Sorry couldn't backup %s to %s\n", name, backup);
            return FALSE;
        }
        sfree(backup);
    }
    return TRUE;
#endif
}
示例#3
0
void make_backup(const char *name)
{
    if (s_maxBackupCount <= 0)
    {
        return;
    }
    if (gmx_fexist(name))
    {
        char *backup = backup_fn(name);
        if (rename(name, backup) == 0)
        {
            fprintf(stderr, "\nBack Off! I just backed up %s to %s\n",
                    name, backup);
        }
        else
        {
            fprintf(stderr, "\nSorry couldn't backup %s to %s\n", name, backup);
        }
        sfree(backup);
    }
}
示例#4
0
文件: gmxfio.c 项目: aar2163/GROMACS
/*****************************************************************
 *
 *                     EXPORTED SECTION
 *
 *****************************************************************/
int gmx_fio_open(const char *fn,const char *mode)
{
    t_fileio *fio=NULL;
    int      i,nfio=0;
    char     *bf,newmode[5];
    bool     bRead;
    int      xdrid;

    if (fn2ftp(fn)==efTPA)
    {
        strcpy(newmode,mode);
    }
    else
    {
        if (mode[0]=='r')
        {
            strcpy(newmode,"r");
        }
        else if (mode[0]=='w')
        {
            strcpy(newmode,"w");
        }
        else if (mode[0]=='a')
        {
            strcpy(newmode,"a");
        }
        else
        {
            gmx_fatal(FARGS,"DEATH HORROR in gmx_fio_open, mode is '%s'",mode);
        }
    }

    /* Check if it should be opened as a binary file */
    if (strncmp(ftp2ftype(fn2ftp(fn)),"ASCII",5))
    {
        /* Not ascii, add b to file mode */
        if ((strchr(newmode,'b')==NULL) && (strchr(newmode,'B')==NULL))
        {
            strcat(newmode,"b");
        }
    }

    /* Determine whether we have to make a new one */
    for(i=0; (i<nFIO); i++)
    {
        if (!FIO[i].bOpen)
        {
            fio  = &(FIO[i]);
            nfio = i;
            break;
        }
    }

    if (i == nFIO)
    {
        nFIO++;
        srenew(FIO,nFIO);
        fio  = &(FIO[nFIO-1]);
        nfio = nFIO-1;
    }
    bRead = (newmode[0]=='r');
    fio->fp  = NULL;
    fio->xdr = NULL;
    if (fn)
    {
        fio->iFTP   = fn2ftp(fn);
        fio->fn     = strdup(fn);
        fio->bStdio = FALSE;

        /* If this file type is in the list of XDR files, open it like that */
        if (in_ftpset(fio->iFTP,asize(ftpXDR),ftpXDR))
        {
            /* First check whether we have to make a backup,
             * only for writing, not for read or append.
             */
            if (newmode[0]=='w')
            {
#ifndef GMX_FAHCORE
                /* only make backups for normal gromacs */
                if (gmx_fexist(fn))
                {
                    bf=(char *)backup_fn(fn);
                    if (rename(fn,bf) == 0)
                    {
                        fprintf(stderr,"\nBack Off! I just backed up %s to %s\n",fn,bf);
                    }
                    else
                    {
                        fprintf(stderr,"Sorry, I couldn't backup %s to %s\n",fn,bf);
                    }
                }
#endif
            }
            else
            {
                /* Check whether file exists */
                if (!gmx_fexist(fn))
                {
                    gmx_open(fn);
                }
            }
            snew(fio->xdr,1);
            xdrid = xdropen(fio->xdr,fn,newmode);
            if (xdrid == 0)
            {
                if(newmode[0]=='r')
                    gmx_fatal(FARGS,"Cannot open file %s for reading\nCheck permissions if it exists.",fn);
                else
                    gmx_fatal(FARGS,"Cannot open file %s for writing.\nCheck your permissions, disk space and/or quota.",fn);
            }
            fio->fp = xdr_get_fp(xdrid);
        }
        else
        {
            /* If it is not, open it as a regular file */
            fio->fp = ffopen(fn,newmode);
        }
    }
    else
    {
        /* Use stdin/stdout for I/O */
        fio->iFTP   = efTPA;
        fio->fp     = bRead ? stdin : stdout;
        fio->fn     = strdup("STDIO");
        fio->bStdio = TRUE;
    }
    fio->bRead  = bRead;
    fio->bDouble= (sizeof(real) == sizeof(double));
    fio->bDebug = FALSE;
    fio->bOpen  = TRUE;

    return nfio;
}