home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / ArchiveUtils / JumpBack / Source / RdistControl.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  8.6 KB  |  313 lines

  1.  
  2. //======================================================================
  3. //
  4. //    Portions written by FreemanSoft Inc.
  5. //
  6. //    FreemanSoft disclaims any warranty of any kind, expressed or implied,
  7. //    as to this source code's fitness for any particular use.
  8. //
  9. //    For more information, use the following electronic mail addresses:
  10. //     
  11. //        info@FreemanSoft.com    general questions
  12. //        support@FreemanSoft.com    technical questions
  13. //
  14. //======================================================================
  15.  
  16.  
  17. /* Written by
  18.  *    Joe Freeman    jfreeman@next.com    
  19.  *    RdistControl
  20.  *
  21.  *    This code has no warranty.  
  22.  *    It is provided so that the consumer may maintain and modify it
  23.  *    at their own risk.  Use this code at your own risk.
  24.  */
  25.  
  26. #import "RdistControl.h"
  27. #import "Subprocess.h"
  28. #import "PreferencesPanel.h"
  29. #import "pathutil.h"
  30. #import "common.h"
  31. #import "rdistControl.h"        // strings entries
  32.  
  33. #import "JumpBackControl.h"
  34.  
  35.  
  36. /*================================================================
  37.  *    Private methods 
  38.  *================================================================*/
  39.  
  40. @implementation RdistControl
  41.  
  42. /*================================================================
  43.  *    all strings related to rdist are right before runPassTwo: 
  44.  *================================================================*/
  45.  
  46.     
  47.  
  48. - (const char *)buttonImageName
  49. {
  50.     return "JumpBack_notext.tiff";
  51. }
  52.  
  53. - (const char *)buttonAltImageName
  54. {
  55.     return "JumpBack_abort.tiff";
  56. }
  57.  
  58. - (const char *)stringsTable
  59. {
  60.     return "rdistControl";
  61. }
  62.  
  63. - free
  64. {
  65.     [[NXApp delegate] dropDragJumpBackController:self];
  66.     return [super free];
  67. }
  68.  
  69.  
  70. /*
  71.  * try and figure out if the values have changed 
  72.  * support relative and absolute 
  73.  */
  74. - updateFromTextFields:sender
  75. {
  76.     /* first do the simpler dest path */
  77.     if (strcmp(dPathList, [destinationText stringValue]))
  78.     {
  79.         /* destination path has changed */
  80.     strcpy(dPathList, [destinationText stringValue]);
  81.     } 
  82.     
  83.     /* then do the more complicated source path */
  84.     if (!strcmp(NXGetDefaultValue([NXApp appName],path_default), 
  85.                 rdist_absolute))
  86.     {
  87.         /* running in absolute mode so text field is full path name */
  88.     if (strcmp(sPathList, [sourceText stringValue])){
  89.         strcpy(sPathList, [sourceText stringValue]);
  90.     } 
  91.     }
  92.     else if (!strcmp(NXGetDefaultValue([NXApp appName],path_default),
  93.             rdist_relative))
  94.     {
  95.         /* running in relative mode so only append the base name */
  96.     if (strcmp(basename(sPathList), [sourceText stringValue])){
  97.         strcpy(basename(sPathList), [sourceText stringValue]);
  98.     } 
  99.     }
  100.  
  101.     
  102.     return self;
  103. }
  104.  
  105.  
  106. /* figure out our current mode (relative or absolute) and 
  107.  * show path names appropriately 
  108.  */
  109. -updateTextFields:sender
  110. {
  111.     [destinationText setStringValue: dPathList];
  112.     if (!strcmp(NXGetDefaultValue([NXApp appName],path_default),
  113.             rdist_absolute))
  114.     {
  115.         [sourceText setStringValue: sPathList];
  116.     }
  117.     else if (!strcmp(NXGetDefaultValue([NXApp appName],path_default),
  118.             rdist_relative))
  119.     {
  120.         [sourceText setStringValue: basename(sPathList)];
  121.     }
  122.     return self;
  123. }
  124.  
  125. #define find_string \
  126.     "/usr/bin/find %s -type d ! -exec %s {} \\; -print"
  127.     
  128. - runPassOne:sender
  129. {
  130.     char    buf[MAXPATHLEN+1];
  131.     char    fwPath[MAXPATHLEN+1];    /* writable check executable */
  132.     
  133.     /* assume ok */
  134.     canContinue = YES;    
  135.     if (!strcmp(NXGetDefaultValue([NXApp appName],find_no_write), yes_str))
  136.     {
  137.         [[NXBundle mainBundle] getPath: fwPath
  138.                 forResource:file_writable_prog
  139.                 ofType:""];
  140.         if (fwPath && *fwPath){
  141.             [self setState: PASS_1_RUNNING];
  142.             [self logIt: start_pass_1_string];
  143.             sprintf(buf, find_string, sPathList,fwPath);
  144.             subprocessObj = [Subprocess new:buf
  145.                 withDelegate:self
  146.                 andPtySupport:NO
  147.                 andStdErr:YES];    
  148.         } else {
  149.             [self logIt: missing_file_writable];
  150.             [self abortProcess:self];
  151.         }
  152.         return self;
  153.     } else {
  154.         return nil;    /* didn't run find */
  155.     }
  156.      
  157.     return self;
  158. }
  159.  
  160. #define    line_1            "%s -> localhost"
  161. #define    line_2            "    install %s%s ;"
  162. #define    line_3            "    except_pat lost+found ;"
  163. #define    line_4            "    except_pat %s ;"
  164.                 // first param is verify string
  165.                 // second is removestring
  166. #define    rdist_script_cmd     "nice -19 /usr/ucb/rdist%s%s -w -f  - "
  167.                 // first is source dir rest as above
  168.                 // must put cd in before nice cause builtin
  169. #define    rdist_script_rel    "cd %s ; nice -19 /usr/ucb/rdist%s%s -w -f - "
  170.                 // delete files in backup but not in src dir    
  171. #define    rdist_remove_old    " -R "    
  172.                 // don't delete dest files missing from source
  173. #define    rdist_keep_old        " "
  174.                 // run verify mode don't copy anything
  175. #define rdist_verify_only    " -v"
  176.                 // run and copy 
  177. #define rdist_execute        " "
  178.         
  179. - runPassTwo:sender
  180. {    
  181.     char    runString[MAXPATHLEN+1];
  182.     char    scriptString[MAXPATHLEN+1];
  183.     char    *removeString;     /* pointer used to insert -R if needed */
  184.     char    *verifyString;     /* pointer used to insert -v if needed */
  185.     int        i;
  186.     char    *exemptStrings;    /* returned from app delegate */
  187.     int        exemptCount;    /* returned from app delegate */
  188.     char    buildPattern[MAXPATHLEN+1];    /* loads from except stream */
  189.     int        charCnt;    /* num copied into buildPattern */                
  190.     
  191.     removeString = rdist_keep_old;
  192.     verifyString = rdist_execute;
  193.     
  194.     canContinue = YES;
  195.     
  196.  
  197.     [self setState:PASS_2_RUNNING];
  198.     /* now start the backup */
  199.     [self logIt:start_pass_2_string];
  200.     /* does not work for some reason [logText delete:self];    */
  201.     if (!strcmp(NXGetDefaultValue([NXApp appName], dup_default ),
  202.                             dup_update))
  203.         removeString = rdist_keep_old;
  204.     else if (!strcmp(NXGetDefaultValue([NXApp appName], dup_default ),
  205.                             dup_exact))
  206.         removeString = rdist_remove_old;
  207.     if (!strcmp(NXGetDefaultValue([NXApp appName],path_default),
  208.                             rdist_relative))
  209.         sprintf(runString, rdist_script_rel, 
  210.             parentname(sPathList), verifyString,  removeString );
  211.     else
  212.         sprintf(runString, rdist_script_cmd,
  213.                 verifyString, removeString );
  214.     if (debug_level >= debug_except_pat)
  215.         fprintf(stderr,"subprocess string is \n%s\n",runString);
  216.     //subprocessObj = [Subprocess new:runString];
  217.     //[subprocessObj setDelegate:self];
  218.     subprocessObj = [Subprocess new:runString
  219.                 withDelegate:self
  220.                 andPtySupport:NO
  221.                 andStdErr:YES];
  222.     if (!strcmp(NXGetDefaultValue([NXApp appName],path_default),
  223.                             rdist_relative))
  224.         sprintf(scriptString,line_1,basename(sPathList) );
  225.     else
  226.         sprintf(scriptString,line_1,[sourceText stringValue]);
  227.     [subprocessObj send:scriptString];
  228.     if (debug_level >= debug_except_pat)
  229.         fprintf(stderr,"%s\n",scriptString);
  230.     
  231.     sprintf(scriptString,line_2, removeString, 
  232.             [destinationText stringValue]); 
  233.     [subprocessObj send:scriptString];
  234.     if (debug_level >= debug_except_pat)
  235.         fprintf(stderr,"%s\n",scriptString);
  236.     
  237.     ///sprintf(scriptString,line_3);
  238.     ///[subprocessObj send:scriptString];
  239.     
  240.     /* grab the global preferences panel and ask it a question */
  241.     [[PreferencesPanel new]     getExemptString:&exemptStrings
  242.                     length:&exemptCount];
  243.         
  244.     /* 
  245.      * we need to escape for the sprintf and for the shell 
  246.      *     that runs these commands
  247.      * we no longer sprintf into the buffer, so we only need to escape
  248.      * special characters one time 
  249.      * the \ does not need to be escaped 
  250.      */    
  251.     charCnt = 0;
  252.     buildPattern[0] = '\0';        
  253.     for ( i = 0 ; i <= exemptCount; i++){
  254.         /* note: we go one past end of string so catch that first */
  255.         /* this is so we can catch files where no final \n */
  256.         if (    i == exemptCount ||
  257.             exemptStrings[i] == '\n' ) {
  258.             if (charCnt > 0 ) {
  259.                 buildPattern[charCnt]= '\0';
  260.             
  261.                 sprintf(scriptString,line_4, buildPattern);
  262.                 if (debug_level >= debug_except_pat)
  263.                     fprintf(stderr,"%s\n",scriptString);
  264.                 [subprocessObj send:scriptString];
  265.                 [self logIt:"    exempting: "];
  266.                 [self logIt:buildPattern];
  267.                 [self logIt:"\n"];
  268.             
  269.                 charCnt = 0;
  270.                 buildPattern[0] = '\0';    
  271.             }    
  272.         } else 
  273.         if (    exemptStrings[i] == '$' ||
  274.             exemptStrings[i] == '.' ||
  275.             exemptStrings[i] == '^' ){
  276.             buildPattern[charCnt++] = '\\';
  277.             buildPattern[charCnt++] = exemptStrings[i];
  278.         } else 
  279.         if (    exemptStrings[i] == '\\') {
  280.             buildPattern[charCnt++] = exemptStrings[i];
  281.         } else 
  282.         {
  283.             buildPattern[charCnt++] = exemptStrings[i];
  284.         }
  285.     }
  286.     /* should free exemptStrings */        
  287.         
  288.     
  289.     
  290.     sprintf(scriptString,line_4,[destinationText stringValue]); 
  291.     [subprocessObj send:scriptString];
  292.     if (debug_level >= debug_except_pat)
  293.         fprintf(stderr,"%s\n",scriptString);
  294.                         
  295.     /* this is to try and protect us from the case where the dest 
  296.      * directory is inside the source folder */
  297.     if ((!strcmp(NXGetDefaultValue([NXApp appName],path_default),
  298.                             rdist_relative))&&
  299.         (strlen(parentname(sPathList))<strlen(parentname(dPathList))) )
  300.         if (!strncmp(dPathList,parentname(sPathList),
  301.                 strlen(parentname(sPathList)) ) ){
  302.             sprintf(scriptString,line_4, 
  303.                 &dPathList[strlen(parentname(sPathList))+1]);
  304.             [subprocessObj send:scriptString];
  305.         }
  306.     [subprocessObj terminateInput];
  307.  
  308.     return self;
  309. }
  310.  
  311.  
  312. @end
  313.