⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 database.c

📁 Linux系统备份源代码 可基于用户自定义策略实现系统、应用数据备份
💻 C
字号:
/***************************************************************************    file                 : database.c    begin                : Thu Feb 10 2000    copyright            : (C) 2000 by Henrik Witt-Hansen    email                : bean@daisy.net ***************************************************************************//*************************************************************************** *                                                                         * *   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.                                   * *                                                                         * ***************************************************************************//*base configuration*/#ifdef HAVE_CONFIG_H#include <config.h>#endif/*standard includes*/#include <stdio.h>#include <pthread.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>/*local headers*/#include "sitback.h"/*special includes*/#include <sys/errno.h>#include <time.h>/*#include <linux/kd.h>*/#include <sys/ioctl.h>#include <signal.h>/*local prototypes*//********************************************************************  int DatabaseInsert(char *id)  Insert a completed backup into the database..  sitback.archivelist, sitback.archiveid, sitback.archivename  must be present  arguments:    id:  id for this backup  return:    0 on success    -1********************************************************************/int DataBaseInsert(char *id){  struct stat info;  char location[1024];  char entry[1024];  FILE *in;  FILE *out;  char name[257];  char rundate[257];  char runtime[257];  char version[257];  char ch;  /*make sure we got the base database directory*/  sprintf(location,"%sdatabase",conf__scriptdir);  if(stat("database",&info)==-1)  {    debug("%s does not exits. trying to create\n",location);    /*try to create the database directory*/    if(mkdir(location,0755)==-1)    {      /*printf("Unable to create database directory\n");*/      UI__Warning("Unable to create database directory\n");      /*perror("System say's:  ");*/      UI__Warning("System say's:  ");      UI__Warning(strerror(errno));      UI__Warning("\n");      errcode=31;      return -1;    }  }  /*create the new new entry (directory) if it does not already exists*/  sprintf(entry,"%sdatabase/%s",conf__scriptdir,id);  debug("trying to create database entry '%s'\n",entry);  if(stat(entry,&info)==-1)  {    if(mkdir(entry,0755)==-1)    {      /*printf("Unable to create new entry\n");*/      debug("Failed to create entry.. '%s'\n",strerror(errno));      UI__Warning("Unable to create new entry\n");      /*perror("System say's:  ");*/      UI__Warning("System say's:  ");      UI__Warning(strerror(errno));      UI__Warning("\n");      errcode=32;      return -1;    }  }  /*get the list*/  if((in=fopen("sitback.archivelist","r"))==NULL)  {    remove(entry);    /*printf("Unable to open the archivelist\n");*/    UI__Warning("Unable to open the archivelist\n");    errcode=33;    return -1;  }  sprintf(location,"%sdatabase/%s/LIST",conf__scriptdir,id);  if((out=fopen(location,"w"))==NULL)  {    remove(entry);    /*printf("Unable to open or create a file in the database entry\n");*/    UI__Warning("Unable to open or create a file in the database entry\n");    errcode=34;    return -1;  }  while(!feof(in))  {    ch=fgetc(in);    if(!feof(in))      fputc(ch,out);  }  fclose(in);  fclose(out);  /*get the volume info*/  if((in=fopen("sitback.archivename","r"))==NULL)  {    remove(entry);    /*printf("Unable to open the archivename file\n");*/    UI__Warning("Unable to open the archivename file\n");    errcode=35;    return -1;  }  if(fgets(name,256,in)==NULL)  {    remove(entry);    /*printf("Invalid format in sitback.archivename\n");*/    UI__Warning("Invalid format in sitback.archivename\n");    errcode=36;    return -1;  }  while(name[strlen(name)-1]=='\n')    name[strlen(name)-1]='\0';  debug("volume name is \"%s\"\n",name);  if(fgets(rundate,256,in)==NULL)  {    remove(entry);    /*printf("Invalid format in sitback.archivename\n");*/    UI__Warning("Invalid format in sitback.archivename\n");    errcode=36;    return -1;  }  while(rundate[strlen(rundate)-1]=='\n')    rundate[strlen(rundate)-1]='\0';  debug("volume date is \"%s\"\n",rundate);  if(fgets(runtime,256,in)==NULL)  {    remove(entry);    /*printf("Invalid format in sitback.archivename\n");*/    UI__Warning("Invalid format in sitback.archivename\n");    errcode=36;    return -1;  }  while(runtime[strlen(runtime)-1]=='\n')    runtime[strlen(runtime)-1]='\0';  debug("volume time is \"%s\"\n",runtime);  if(fgets(version,256,in)==NULL)  {    remove(entry);    /*printf("Invalid format in sitback.archivename\n");*/    UI__Warning("Invalid format in sitback.archivename\n");    errcode=36;    return -1;  }  while(version[strlen(version)-1]=='\n')    version[strlen(version)-1]='\0';  debug("volume creator version is \"%s\"\n",version);  fclose(in);  /*write volume name*/  sprintf(location,"%sdatabase/%s/NAME",conf__scriptdir,id);  if((out=fopen(location,"w"))==NULL)  {    remove(entry);    UI__Warning("Unable to open or create a file in the database entry\n");    errcode=34;    return -1;  }  fprintf(out,"%s",name);  fclose(out);  /*write volume date*/  sprintf(location,"%sdatabase/%s/DATE",conf__scriptdir,id);  if((out=fopen(location,"w"))==NULL)  {    remove(entry);    UI__Warning("Unable to open or create a file in the database entry\n");    errcode=34;    return -1;  }  fprintf(out,"%s",rundate);  fclose(out);  /*write volume time*/  sprintf(location,"%sdatabase/%s/TIME",conf__scriptdir,id);  if((out=fopen(location,"w"))==NULL)  {    remove(entry);    UI__Warning("Unable to open or create a file in the database entry\n");    errcode=34;    return -1;  }  fprintf(out,"%s",runtime);  fclose(out);  /*write volume creator version*/  sprintf(location,"%sdatabase/%s/VERSION",conf__scriptdir,id);  if((out=fopen(location,"w"))==NULL)  {    remove(entry);    UI__Warning("Unable to open or create a file in the database entry\n");    errcode=34;    return -1;  }  fprintf(out,"%s",version);  fclose(out);  debug("database entry \"%s\" created or updated\n",id);  /*no problems*/  return 0;}/********************************************************************  int DatabaseRename(char *oldid,char *newid)  Insert a completed backup into the database..  sitback.archivelist, sitback.archiveid, sitback.archivename  must be present  arguments:    oldid:  Old id    newid:  New id  return:    0 on success    -1********************************************************************/int DataBaseRename(char *oldid,char *newid){  char oldlocation[1024];  char newlocation[1024];  /*dont to anything if the id has not changed*/  if(!strcmp(oldid,newid))  {    debug("old and new id is the same, refusing to rename\n");    return 0;  }  /*try to rename the entry*/  sprintf(oldlocation,"%sdatabase/%s",conf__scriptdir,oldid);  sprintf(newlocation,"%sdatabase/%s",conf__scriptdir,newid);  debug("renaming \"%s\" to \"%s\"\n",oldlocation,newlocation);  if(rename(oldlocation,newlocation)==-1)  {    errcode=37;    /*no error message, sometimes we are called to rename a entry that has not yet      been created, (the -i switch)*/    return 1;  }  /*no problems*/  return 0;}/********************************************************************  int DatabaseSetStatus(char *id)  Set the status of an entry  arguments:    id:  entry-id    status: status-string  return:    0 on success    -1********************************************************************/int DataBaseSetStatus(char *id,char *status){  char location[1024];  FILE *file;  /*set the status*/  sprintf(location,"%sdatabase/%s/STATUS",conf__scriptdir,id);  if((file=fopen(location,"w"))==NULL)  {    errcode=38;    /*no visible error message, sometimes a failure is      acceptable, f.ex. when someone tries to set status      UNKNOWN on an, not yet existing, database entry*/    return -1;  }  fprintf(file,"%s",status);  fclose(file);  /*no problems*/  return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -