home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / network / eqtree / main.c < prev    next >
C/C++ Source or Header  |  1994-01-18  |  7KB  |  301 lines

  1. /*
  2.  * EqTree
  3.  * Equal Tree
  4.  *
  5.  * Vergleicht zwei Verzeichnisbäume und stellt im Zielverzeichnisbaum
  6.  * den Zustand des Quellverzeichnisbaums her.
  7.  * Gelöschte Dateien werden bis zu einer einstellbaren Anzahl von Tagen
  8.  * beibehalten.
  9.  *
  10.  * Autor: SG
  11.  * Stand: 29.1.93
  12.  */
  13.  
  14. #define VERVersionString "Version 2.3\n" \
  15.              "\t(c) 1993 ExperTeam GmbH\n" \
  16.              "\twritten by Stephan Gerle\n"
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22.  
  23. #include "treeload.h"
  24. #include "copy.h"
  25. #include "logfile.h"
  26.  
  27. char *MAProgramName;
  28. char *MASourceDir = NULL;
  29. char *MADestDir = NULL;
  30. char *MALogFile = "EQTREE.LOG";
  31.  
  32. int MAMaxAge = 7; /* eine Woche */
  33. int MASafety = 1; /* Sicherheitsabfrage */
  34.  
  35. int MAEqual(TRTree *TSource,TREntry *Source,TRTree *TDest,TREntry *Dest);
  36. int MANewer(TRTree *TSource,TREntry *Source,TRTree *TDest,TREntry *Dest);
  37. int MAOlder(TRTree *TSource,TREntry *Source,TRTree *TDest,TREntry *Dest);
  38. int MASrcNew(TRTree *TSource,TREntry *Source,TRTree *TDest,TREntry *Dest);
  39. int MADstNew(TRTree *TSource,TREntry *Source,TRTree *TDest,TREntry *Dest);
  40.  
  41. TRTree *MASourceTree = NULL;
  42. TRTree *MADestTree = NULL;
  43. TRFuncEntry MAFuncArray[] = {
  44.     MAEqual, /* Quelle und Ziel sind identisch */
  45.     MANewer, /* Quelle ist neuer als Ziel */
  46.     MAOlder, /* Quelle ist älter als Ziel */
  47.     MASrcNew,/* Quelle ist neu */
  48.     MADstNew/* Destination ist neu */
  49. };
  50.  
  51. int MAEqual(TRTree *TSource,TREntry *Source,TRTree *TDest,TREntry *Dest)
  52. {
  53.     /*LOItem("File is equal! Nothing done '%s'",Source->File);*/
  54.     return 1;
  55. }
  56.  
  57. int MANewer(TRTree *TSource,TREntry *Source,TRTree *TDest,TREntry *Dest)
  58. {
  59.     if (TRIsDir(Source))
  60.     return 1;
  61.  
  62.     LOItem("Src is newer! Copying '%s' -> '%s'",Source->File,Dest->File);
  63.     if (COCopy(Source->File,Dest->File)==0)
  64.     return 1;
  65.     ERMessage("Copy failed ***");
  66.     return 0;
  67. }
  68.  
  69. #include <time.h>
  70. #include <sys\types.h>
  71. #include <sys\stat.h>
  72.  
  73. static char *File2DateStr(const char *Filename)
  74. {
  75.     struct stat statbuf;
  76.  
  77.     if (stat((char *)Filename,&statbuf)==0)
  78.     asctime(gmtime(&statbuf.st_atime));
  79.     else
  80.     return "Unknown time/date";
  81. }
  82.  
  83. int MAOlder(TRTree *TSource,TREntry *Source,TRTree *TDest,TREntry *Dest)
  84. {
  85.     if (TRIsDir(Source))
  86.     return 1;
  87.  
  88.     LOItem("Src is older! Should not occur! Nothing done! '%s %s' ***",Source->File,File2DateStr(Source->File));
  89.     return 1;
  90. }
  91.  
  92. int MASrcNew(TRTree *TSource,TREntry *Source,TRTree *TDest,TREntry *Dest)
  93. {
  94. #define CDBUFSIZE   2048
  95.  
  96.     char    DestFileBuf[CDBUFSIZE];
  97.  
  98.     if (TRIsDir(Source))
  99.     return 1;
  100.  
  101.     if (TDest->BaseLen+strlen(Source->File)-TSource->BaseLen+1 > CDBUFSIZE)
  102.     {
  103.     ERMessage("Unable to copy '%s' because Buffer is to small",Source->File);
  104.     return 0;
  105.     }
  106.     strcpy(DestFileBuf,TDest->Base);
  107.     strcat(DestFileBuf,Source->File+TSource->BaseLen);
  108.  
  109.     LOItem("Src is new! Copying '%s' -> '%s'",Source->File,DestFileBuf);
  110.     if (COCopy(Source->File,DestFileBuf)==0)
  111.     return 1;
  112.     ERMessage("Copy failed ***");
  113.     return 0;
  114. }
  115.  
  116. unsigned long Date2Days(FDATE date)
  117. {
  118.     return date.year * 365 + date.month *31 + date.day;
  119. }
  120.  
  121. unsigned long Time2Secs(FTIME time)
  122. {
  123.     return time.hours * 3600l + time.minutes * 60 + time.twosecs * 2;
  124. }
  125.  
  126.  
  127. #include <direct.h>
  128.  
  129. static int MARemoveIfEmpty(char *Path)
  130. {
  131.     int ret;
  132.  
  133.     if (Path[1] == ':')
  134.     _chdrive(toupper(*Path)-'A'+1);
  135.  
  136.     if (chdir(Path)==0)
  137.     {
  138.     chdir("\\");
  139.     ret = rmdir(Path);
  140.     if (ret == 0)
  141.         LOItem("Path '%s' removed because is empty",Path);
  142.     }
  143.  
  144.     return 1;
  145. }
  146.  
  147. int MADstNew(TRTree *TSource,TREntry *Source,TRTree *TDest,TREntry *Dest)
  148. {
  149.     FDATE date;
  150.     struct tm *ltm;
  151.     time_t    ttime = time(NULL);
  152.     int     DelIt;
  153.  
  154.     ltm = localtime(&ttime);
  155.     date.year = ltm->tm_year - 80;
  156.     date.month = ltm->tm_mon + 1;
  157.     date.day = ltm->tm_mday;
  158.     if (Date2Days(date) - MAMaxAge > Date2Days(Dest->Date))
  159.     DelIt = 1;
  160.     else
  161.     DelIt = 0;
  162.  
  163.     if (TRIsDir(Dest))
  164.     {
  165.     MARemoveIfEmpty(Dest->File);
  166.     }
  167.     else
  168.     {
  169.     if (DelIt)
  170.     {
  171.         LOItem("Src deleted! Deleting '%s'",Dest->File);
  172.         COMakeWritable(Dest->File);
  173.         if (remove(Dest->File)!=0)
  174.         ERMessage("File '%s' could not be deleted ***",Dest->File);
  175.         if (strchr(Dest->File,'\\'))
  176.         *strchr(Dest->File,'\\') = 0;
  177.         MARemoveIfEmpty(Dest->File);
  178.     }
  179.     else
  180.         LOItem("Src deleted! Dest not old enough! Nothing done! '%s' ***",Dest->File);
  181.     }
  182.     return 1;
  183. }
  184.  
  185. void DoHelp(char *argv[])
  186. {
  187.     fprintf(stderr, "Usage: %s [Options] <SourceDir> <DestDir>\n"
  188.             "Options: -h this help message\n"
  189.             "         -l:<LogFileName> Change Logfile to ... Default is EQTREE.LOG\n"
  190.             "         -d:<Days> Change max age to ... Default is 7\n"
  191.             "         -q Quiet: Keine Sicherheitsabfragen\n"
  192.             "         -v: get version\n\n",
  193.             argv[0]);
  194. }
  195.  
  196. int ParseOptions(int argc,char *argv[])
  197. {
  198.     int i;
  199.  
  200.     for (i = 1; i < argc; i++)
  201.     {
  202.     if (strcmp(argv[i],"-h")==0)
  203.     {
  204.         DoHelp(argv);
  205.         return 0;
  206.     }
  207.     if (strncmp(argv[i],"-v",2)==0)
  208.     {
  209.         fprintf(stderr,"%s: %s",argv[0],VERVersionString);
  210.         exit(1);
  211.     }
  212.  
  213.     if (strncmp(argv[i],"-l:",3)==0)
  214.         MALogFile = argv[i]+3;
  215.  
  216.     if (strncmp(argv[i],"-d:",3)==0)
  217.         MAMaxAge = atoi(argv[i]+3);
  218.  
  219.     if (strcmp(argv[i],"-q")==0)
  220.         MASafety = 0;
  221.  
  222.     if (*argv[i] != '-')
  223.         if (MASourceDir == NULL)
  224.         MASourceDir = argv[i];
  225.         else
  226.         if (MADestDir == NULL)
  227.             MADestDir = argv[i];
  228.         else
  229.         {
  230.             fprintf(stderr,"%s: Illegal Commandline '%s'\n",
  231.                     argv[0],argv[i]);
  232.             DoHelp(argv);
  233.             return 0;
  234.         }
  235.     }
  236.     if ((MADestDir == NULL) || (MASourceDir == NULL))
  237.     {
  238.     fprintf(stderr,"%s: Kommandline missing Source- and/or Destination directory\n",argv[0]);
  239.     DoHelp(argv);
  240.     return 0;
  241.     }
  242.     return 1;
  243. }
  244.  
  245. int main(int argc,char *argv[])
  246. {
  247.     fprintf(stdout,"\n");
  248.     MAProgramName = argv[0];
  249.  
  250.     if (ParseOptions(argc,argv))
  251.     {
  252.     time_t    ltime;
  253.  
  254.     LOInit(MALogFile);
  255.  
  256.     time(<ime);
  257.     LOItem( "Started              = %s"
  258.         "Maximum age          = %d days\n"
  259.         "Logfile              = %s\n"
  260.         "Sourcedirectory      = %s\n"
  261.         "Destinationdirectory = %s\n"
  262.         "Starting Process....",
  263.         asctime(localtime(<ime)),
  264.         MAMaxAge,MALogFile,MASourceDir,MADestDir);
  265.  
  266.     if ((MASourceTree = TRLoadTree(MASourceDir))==NULL)
  267.         ERFatal("Source-Directory '%s' does not exist.",MASourceDir);
  268.  
  269.     if ((MADestTree = TRLoadTree(MADestDir))==NULL)
  270.         ERFatal("Destination-Directory '%s' does not exist.",MADestDir);
  271.  
  272.     if (MASafety)
  273.     {
  274.         if ((labs(MASourceTree->EntryCount-MADestTree->EntryCount)/2) >
  275.         (long)(MASourceTree->EntryCount+MADestTree->EntryCount)/20)
  276.         {
  277.         int answer;
  278.  
  279.         fprintf(stderr,"%s: Source and Destination different?!?\n"
  280.                 "\tContinue (y/n)?",argv[0]);
  281.         do {
  282.             answer = toupper(getchar());
  283.         } while ((answer != 'Y') && (answer != 'N'));
  284.         if (answer == 'N')
  285.             exit(1);
  286.         }
  287.     }
  288.  
  289.     if (!TRCompare(MASourceTree,MADestTree,MAFuncArray))
  290.         ERFatal("Treecompare *not* successfully terminated");
  291.  
  292.     time(<ime);
  293.     ERMessage("Treecompare successfully terminated at %s",asctime(localtime(<ime)));
  294.     TRFree(MASourceTree);
  295.     TRFree(MADestTree);
  296.     return 0;
  297.     }
  298.     else
  299.     exit(1);
  300. }
  301.