home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / PROG / REXX / CMD / EAPREP.CMD < prev    next >
Encoding:
Text File  |  1993-08-15  |  22.5 KB  |  408 lines

  1. /* ================================================================= */
  2. /* EAPREP.CMD - Create EA Backup/Restore Command Files               */
  3. /* ================================================================= */
  4. /*                                                                   */
  5. /* Author:         Mercer H. Harz                                    */
  6. /* Compuserve ID:  70431,150                                         */
  7. /* Written:        14 Aug 1993                                       */
  8. /*                                                                   */
  9. /* All standard disclaimers apply; this procedure is provided "as    */
  10. /* is and no guarantees or warranties are provided regarding its     */
  11. /* fitness for a particular task.                                    */
  12. /*                                                                   */
  13. /* This program is placed in the public domain by the author.        */
  14. /*                                                                   */
  15. /* ================================================================= */
  16. /*                                                                   */
  17. /* syntax:  EAPREP <drive> <EADir>                                   */
  18. /*                                                                   */
  19. /* <drive> is a required parameter.  It specifies the drive to be    */
  20. /*         processed.                                                */
  21. /* <EADir> is a required parameter.  It specifies the location where */
  22. /*         the DOS-visible Extended Attribute files are to be stored.*/
  23. /*         If the directory does not exist, it will be created.      */
  24. /*                                                                   */
  25. /* output:  Two command files:  EADir\x_BKP.CMD    (for BACKUP)      */
  26. /*                              EADir\x_RST.CMD    (for RESTORE)     */
  27. /*                                                                   */
  28. /*          where               x = drive being processed            */
  29. /*                                                                   */
  30. /* ================================================================= */
  31. /*                                                                   */
  32. /* This procedure creates OS/2 command files that can be executed to */
  33. /* copy and reattach extended attributes to/from files that are      */
  34. /* visible to a DOS backup/restore program that would otherwise not  */
  35. /* be able to handle Extended Attributes.  To backup HPFS volumes    */
  36. /* with your DOS backup software, you must run the backup in an OS/2 */
  37. /* DOS session rather than under native DOS.  If your hardware isn't */
  38. /* usable inside an OS/2 DOS session, you won't be able to backup an */
  39. /* HPFS partition with a DOS backup program.  You can backup FAT     */
  40. /* partitions with your DOS backup software from either an OS/2 DOS  */
  41. /* session or from native DOS.  This program (since it's written in  */
  42. /* OS/2 Rexx) must run from within an OS/2 command-line session.     */
  43. /*                                                                   */
  44. /* NOTE!!!!!                                                         */
  45. /*                                                                   */
  46. /* EAPREP does not perform the actual copy and reattach operations   */
  47. /* on the extended attributes.  It merely creates command files that */
  48. /* you must execute when you are ready to:                           */
  49. /*     1.  BACKUP -                                                  */
  50. /*         create the DOS-visible copies of the extended attributes  */
  51. /*  or 2.  RESTORE -                                                 */
  52. /*         reattach the extended attributes after a full restore     */
  53. /*         using the DOS backup/restore software                     */
  54. /*                                                                   */
  55. /* You should run EAPREP, followed by the backup command file that   */
  56. /* it creates, immediately prior to running the DOS backup software. */
  57. /* Ensure that your DOS backup software backs up the command files   */
  58. /* created by this program, as well as the files containing the EA   */
  59. /* copies that were created by the backup command file created by    */
  60. /* EAPREP.                                                           */
  61. /*                                                                   */
  62. /* EAPREP does not backup/restore the Workplace Shell desktop, even  */
  63. /* though some WPS state info may be saved in EAs.  Use one of the   */
  64. /* other utilities in the CIS OS/2 librarires for this purpose,      */
  65. /* either the WPSBackup program (WPSBKP.ZIP) or the DeskMan/2        */
  66. /* program (DSKMN2.ZIP).  You will need to contact the authors and   */
  67. /* pay a fee to receive unrestricted use of these other utilities.   */
  68. /*                                                                   */
  69. /* Currently the procedure uses a brute force method to achieve its  */
  70. /* ends, since there is no built-in way in REXX to determine whether */
  71. /* a file has extended attributes.  EAPREP builds a list of files on */
  72. /* the drive, then scans the list looking for a non-zero value in    */
  73. /* the extended attribute size field; for each hit, it creates a     */
  74. /* command in the backup and restore command files.  Files that are  */
  75. /* "in use" by OS/2 (such as OS2KRNL on my system) will generate     */
  76. /* errors during execution of the backup and restore command files.  */
  77. /* These can, in my experience, be ignored.                          */
  78. /*                                                                   */
  79. /* The backup and restore command files use the EAUTIL program that  */
  80. /* is furnished with OS/2.  Operation of EAUTIL is documented in the */
  81. /* online command reference.                                         */
  82. /*                                                                   */
  83. /* EAPREP has no selective processing capability; the entire drive   */
  84. /* will be scanned.  You can further refine the process by editing   */
  85. /* the output with any ASCII text editor.                            */
  86. /*                                                                   */
  87. /* EAPREP assumes you are not disk-space-constrained.  At a minimum, */
  88. /* when you run the backup command file generated by this procedure, */
  89. /* you will need slightly more free space on the drive than is       */
  90. /* occupied by the extended attributes on that drive.  The reason is */
  91. /* that the backup command file preserves the existing EAs as it     */
  92. /* makes copies that are visible to EA-unaware DOS programs.  To see */
  93. /* how much space you'll need, run CHKDSK against the drive before   */
  94. /* you run this procedure, then double the amount allocated to       */
  95. /* extended attributes, and add a "fudge" factor for directory space */
  96. /* overhead and wasted space at the end of clusters.  For those with */
  97. /* tight disk space requirements, there are instructions below on    */
  98. /* how to change the behavior of the backup command file so that the */
  99. /* extended attributes are deleted as the backup copy is created.    */
  100. /* EAPREP uses the <EADir> value as the ouput location for the work  */
  101. /* files and command files that are the output of this program, as   */
  102. /* well as the storage directory for the extended attribute copies.  */
  103. /* Work files could occupy several hundred kilobytes but are deleted */
  104. /* at the end of the program.  Each file or directory with EAs adds  */
  105. /* about 90 bytes to the backup and restore command files.           */
  106. /*                                                                   */
  107. /* The backup copies of the extended attributes will be stored in the*/
  108. /* location pointed to by the <EADir> parameter.  They are stored as */
  109. /* a set of files named dnnnnnnn.EA, where "d" represents the drive  */
  110. /* letter being processed, and "nnnnnnn" is a sequence number.  The  */
  111. /* directory, if it does not currently exist, will be created by     */
  112. /* this program, but its existence will not be checked for by the    */
  113. /* generated command files.  It is not necessary, but it is probably */
  114. /* a good idea, for you to empty the <EADir> subdirectory before     */
  115. /* running this program.  The naming convention allows you to store  */
  116. /* all DOS-visible EA files for all drives on your system in the     */
  117. /* same location, though you do not have to do so.                   */
  118. /*                                                                   */
  119. /* The restore command file contains the mirror commands required to */
  120. /* join the EAs to their owners after you've restored the system     */
  121. /* from a DOS backup.                                                */
  122. /*                                                                   */
  123. /* The backup command file uses the /P /R /S switches; these split   */
  124. /* (/S) the EAs into a backup copy, preserving (/P) the EAs with the */
  125. /* parent file, and replacing (/R) any existing backup copy of the   */
  126. /* parent's EAs that might be stored in the <EADir> subdirectory.    */
  127. /*                                                                   */
  128. /* The restore command file uses the /O /J switches; these join (/J) */
  129. /* the EAs to the parent file, overlaying (/O) any existing EAs      */
  130. /* associated with the parent.  The backup copy will be erased.  To  */
  131. /* prevent the erasure of the backup, add the /P switch to these     */
  132. /* commands.  If, rather than overlaying the current EAs (if any),   */
  133. /* you want to merge the backup EAs with the current EAs, use the    */
  134. /* /M switch rather than /O for the restoration.                     */
  135. /*                                                                   */
  136. /* Long file names may not operate properly with DOS backup/restore  */
  137. /* software; this will depend on whether OS/2 will properly create   */
  138. /* filenames in 8.3 format when backing up/restoring with DOS s/w.   */
  139. /* I haven't tested long file names because I don't have any HPFS    */
  140. /* volumes right now.  My advice is to stick with 8.3 filenames on   */
  141. /* HPFS volumes until you have long-name-aware backup/restore s/w.   */
  142. /*                                                                   */
  143. /* ================================================================= */
  144.  
  145. /* ------------------------------------------------------------------ */
  146. /*  MAKE COMMAND-LINE ARGUMENTS AVAILABLE                             */
  147. /* ------------------------------------------------------------------ */
  148.     arg Drive EADir
  149.  
  150. /* ------------------------------------------------------------------ */
  151. /*  MAKE REXX UTILITY FUNCTIONS AVAILABLE                             */
  152. /* ------------------------------------------------------------------ */
  153.     call RxFuncAdd 'SysLoadFuncs','RexxUtil','SysLoadFuncs'
  154.     call SysLoadFuncs
  155.  
  156. /* ------------------------------------------------------------------ */
  157. /*  SAY HELLO                                                         */
  158. /* ------------------------------------------------------------------ */
  159.     TimeStamp = time()
  160.     DateStamp = date()
  161.     hdr = "EAP"
  162.     say " "
  163.     say hdr"00I: EAPREP v1.1 - Ext. Attribute Backup/Restore Preparation"
  164.     say hdr"01I: Written by Mercer H. Harz, 14 Aug 1993."
  165.     say hdr"02I: Starting at "TimeStamp" on "DateStamp"."
  166.     say " "
  167.  
  168. /* ------------------------------------------------------------------ */
  169. /*  CHECK FOR PRESENCE OF COMMAND-LINE ARGUMENTS                      */
  170. /* ------------------------------------------------------------------ */
  171.     if length(Drive) = 0   then signal USAGE
  172.     if length(EADir) = 0   then signal USAGE
  173.     if right(EADir,1) = '\' then
  174.         EADir = substr(EADir, 1, length(EADir) - 1)
  175.  
  176. /* ------------------------------------------------------------------ */
  177. /*  CHECK FOR VALIDITY OF SELECTED DRIVE                              */
  178. /* ------------------------------------------------------------------ */
  179.     ValDrives = SysDriveMap()
  180.     Drive = left(Drive,1)
  181.     if pos(Drive,ValDrives) = 0 then signal BADDRV
  182.  
  183. /* ------------------------------------------------------------------ */
  184. /*  CHECK FOR VALIDITY OF SELECTED EA STORAGE DIRECTORY               */
  185. /* ------------------------------------------------------------------ */
  186.     CurrDir = directory()                   /* save current directory */
  187.     CheckDir = directory(EADir)             /* does EADir exist?      */
  188.     if CheckDir = EADir then signal SETWORK /* if so, continue        */
  189.     rc = SysMkDir(EADir)                    /* if not, create it      */
  190.     if rc <> 0 then signal BADMD            /* advise user of failure */
  191.     say hdr"03W: Created directory "EADir
  192.  
  193. /* ------------------------------------------------------------------ */
  194. /*  SETUP AND INITIALIZE THE WORK FILES                               */
  195. /* ------------------------------------------------------------------ */
  196. SETWORK:
  197.     CurrDir = directory(CurrDir)            /* return to starting dir */
  198.  
  199.     WorkFile = EADir'\'Drive'.wrk'
  200.     BackupFile = EADir'\'Drive'_bkp.cmd'    /* now initialize the     */
  201.     RestoreFile = EADir'\'Drive'_rst.cmd' /* work files               */
  202.     if length(stream(BackupFile,'c','query exists')) > 0 then
  203.         call SysFileDelete(BackupFile)
  204.     if length(stream(RestoreFile,'c','query exists')) > 0 then
  205.         call SysFileDelete(RestoreFile)
  206.  
  207. /* ------------------------------------------------------------------ */
  208. /*  CREATE THE WORK FILE DIRECTORY LISTING IN HPFS FORMAT             */
  209. /* ------------------------------------------------------------------ */
  210.     say hdr'10I: Obtaining directory for drive 'Drive':... please be patient...'
  211.     say " "
  212.  
  213.     '@dir 'Drive':\*.* /N /S /A /O:GN > 'WorkFile
  214.     if rc <> 0 then signal BADDIR
  215.  
  216. /* ------------------------------------------------------------------ */
  217. /*  OPEN THE WORK FILES FOR USE BY THE PROGRAM                        */
  218. /* ------------------------------------------------------------------ */
  219.     Result = stream(WorkFile,'c','open read')
  220.     if Result <> "READY:" then signal BADWRK
  221.  
  222.     Result = stream(BackupFile,'c','open write')
  223.     if Result <> "READY:" then signal BADBKP
  224.  
  225.     Result = stream(RestoreFile,'c','open write')
  226.     if Result <> "READY:" then signal BADRST
  227.  
  228. /* ------------------------------------------------------------------ */
  229. /*  FINAL INITIALIZATIONS                                             */
  230. /* ------------------------------------------------------------------ */
  231.     EACount = 0         /* number of objects with extended attributes */
  232.     NumDirs = 0         /* number of (sub)directories processed       */
  233.     NumFiles = 0        /* number of directory entries processed      */
  234.  
  235.     say hdr"11I: Directories       Entries      Entries"
  236.     say hdr"12I:   Processed     Processed     with EAs"
  237.     say hdr"13I: -----------     ---------     --------"
  238.     parse value SysCurPos() with row col
  239.  
  240. /* ------------------------------------------------------------------ */
  241. /*   MAIN PROCESSING LOOP  ---  look for entries with EAs             */
  242. /* ------------------------------------------------------------------ */
  243.     do until lines(WorkFile) = 0    /* run until end-of-file          */
  244.         buff = linein(WorkFile)
  245.         parse var buff word1 word2 word3 EASize FName word6
  246.         if buff = " " then iterate           /* --------------------- */
  247.         if word1 = "The" then iterate        /* Ignore the whitespace */
  248.         if word1 = "Totals" then iterate     /* in the directory      */
  249.         if word1 = "Volume" then iterate     /* listing               */
  250.         if word2 = "bytes" then iterate      /*                       */
  251.         if word2 = "file(s)" then iterate    /* --------------------- */
  252.         if word1 = "Directory" then do
  253.             NumDirs = NumDirs + 1      /* perform control-break pro-  */
  254.             CurrentDir = word3         /* cessing for each directory  */
  255.             if CurrentDir = Drive":\" then CurrentDir = Drive":"
  256.             rc = SysCurPos(row,0)
  257.             buff = hdr'14I: 'right(NumDirs,11)'     'right(NumFiles,9)'     'right(EACount,8)
  258.             call charout ,buff
  259.             iterate
  260.         end
  261.         NumFiles = NumFiles + 1    /* this is a file/directory entry  */
  262.         if EASize > 0 then do      /* if it has EAs attached, create  */
  263.             EACount = EACount + 1  /* the backup/restore commands     */
  264.             EAIndex = right(EACount,7,'0')
  265.             DataFile = CurrentDir'\'FName
  266.             Len  = length(DataFile)
  267.             if Len < 50 then Len = 50
  268.             DataFile = left(DataFile,Len,' ')
  269.             EAFile  = EADir'\'Drive||EAIndex'.EA'
  270.             Len  = length(EAFile)
  271.             if Len < 20 then Len = 20
  272.             EAFile  = left(EAFile,Len,' ')
  273.             BackupCmd  = 'EAUTIL 'DataFile' 'EAFile' /P /R /S'
  274.             RestoreCmd = 'EAUTIL 'DataFile' 'EAFile' /O /J'
  275.             call lineout BackupFile, BackupCmd
  276.             call lineout RestoreFile, RestoreCmd
  277.         end
  278.     end
  279.     rc = SysCurPos(row,0)
  280.     buff = hdr'14I: 'right(NumDirs,11)'     'right(NumFiles,9)'     'right(EACount,8)
  281.     call charout ,buff
  282.  
  283. /* ------------------------------------------------------------------ */
  284. /*   WE'RE DONE - TIME TO CLEAN UP AFTER OURSELVES                    */
  285. /* ------------------------------------------------------------------ */
  286.     Result = stream(WorkFile,'c','close')
  287.     Result = stream(BackupFile,'c','close')
  288.     Result = stream(RestoreFile,'c','close')
  289.     call SysFileDelete(WorkFile)
  290.     if EACount = 0 then do
  291.         call SysFileDelete(BackupFile)
  292.         call SysFileDelete(RestoreFile)
  293.     end
  294.  
  295.     say " "
  296.     say " "
  297.     if EACount > 0 then do
  298.         say hdr"96I: "EACount" Commands to BACKUP EAs are stored in "BackupFile
  299.         say hdr"97I: "EACount" Commands to RESTORE EAs are stored in "RestoreFile
  300.       end
  301.     else say hdr"98W: EA Backup and Restore command files were not created."
  302.     signal ENDPGM
  303.  
  304. /* ------------------------------------------------------------------ */
  305. /*   DIRECTORY WORK FILE COULD NOT BE CREATED                         */
  306. /* ------------------------------------------------------------------ */
  307. BADDIR:
  308.     savrc = rc
  309.     call SysFileDelete(WorkFile)
  310.     say hdr"51E: Sorry, there was a problem obtaining a directory for drive "Drive":."
  311.     rc = savrc
  312.     signal DOSERR   /*  clean up after ourselves   */
  313.  
  314. /* ------------------------------------------------------------------ */
  315. /*   DIRECTORY WORK FILE COULD NOT BE OPENED FOR INPUT                */
  316. /* ------------------------------------------------------------------ */
  317. BADWRK:
  318.     call SysFileDelete(WorkFile)
  319.     say hdr"53E: Sorry, there was a problem reading directory file "WorkFile"."
  320.     signal BADSTRM  /*  clean up after ourselves   */
  321.  
  322. /* ------------------------------------------------------------------ */
  323. /*   BACKUP FILE COULD NOT BE OPENED FOR OUTPUT                       */
  324. /* ------------------------------------------------------------------ */
  325. BADBKP:
  326.     call SysFileDelete(BackupFile)
  327.     call SysFileDelete(WorkFile)
  328.     say hdr"54E: Sorry, there was a problem opening backup command file "BackupFile"."
  329.     signal BADSTRM  /*  clean up after ourselves   */
  330.  
  331. /* ------------------------------------------------------------------ */
  332. /*   RESTORE FILE COULD NOT BE OPENED FOR OUTPUT                      */
  333. /* ------------------------------------------------------------------ */
  334. BADRST:
  335.     call SysFileDelete(RestoreFile)
  336.     call SysFileDelete(BackupFile)
  337.     call SysFileDelete(WorkFile)
  338.     say hdr"55E: Sorry, there was a problem opening restore command file "RestoreFile"."
  339.     signal BADSTRM  /*  clean up after ourselves   */
  340.  
  341. /* ------------------------------------------------------------------ */
  342. /*   ERROR ACCESSING STREAM DEVICE                                    */
  343. /* ------------------------------------------------------------------ */
  344. BADSTRM:
  345.     say hdr"59E: Stream Error ("Reason")"
  346.     signal ENDPGM
  347.  
  348. /* ------------------------------------------------------------------ */
  349. /*   USER HAS NOT ENTERED A VALID DRIVE LETTER                        */
  350. /* ------------------------------------------------------------------ */
  351. BADDRV:
  352.     say hdr"61E: Drive "Drive": is not a valid selection."
  353.     say hdr"62W: Please select a drive from the following list."
  354.     say hdr"63W: "ValDrives
  355.     signal ENDPGM
  356.  
  357. /* ------------------------------------------------------------------ */
  358. /*   UNABLE TO CREATE EA STORAGE DIRECTORY                            */
  359. /* ------------------------------------------------------------------ */
  360. BADMD:
  361.     say hdr"71E: Sorry, unable to create "EADir
  362.     signal DOSERR
  363.  
  364. /* ------------------------------------------------------------------ */
  365. /*   TRANSLATE DOS ERROR CODES                                        */
  366. /* ------------------------------------------------------------------ */
  367. DOSERR:
  368.     select
  369.        when rc =   2 then reason = "File not found."
  370.        when rc =   3 then reason = "Path not found."
  371.        when rc =   5 then reason = "Access Denied."
  372.        when rc =  26 then reason = "Not a DOS disk."
  373.        when rc =  87 then reason = "Invalid Parameter."
  374.        when rc = 108 then reason = "Drive Locked."
  375.        when rc = 206 then reason = "Filename exceeds range."
  376.        otherwise          reason = "Unknown Error."
  377.     end
  378.     say hdr"90E: Return Code = "right(rc,3,'0')" : "reason
  379.     signal ENDPGM
  380.  
  381. /* ------------------------------------------------------------------ */
  382. /*   USER HAS NOT ENTERED A REQUIRED PARAMETER                        */
  383. /* ------------------------------------------------------------------ */
  384. USAGE:
  385.     say hdr"80E: A required parameter is missing."
  386.     say "  "
  387.     say hdr"81I: Usage:"
  388.     say hdr"82I:        EAPREP  Drive  EADir"
  389.     say hdr"83I: where"
  390.     say hdr"84I:        Drive  = disk drive to be processed"
  391.     say hdr"85I:        EADir  = location (on any drive with adequate space)"
  392.     say hdr"86I:                 where EAPREP will store its output and the"
  393.     say hdr"87I:                 files containing extended attribute copies."
  394.     say "  "
  395.     say hdr"88I: Both parameters are required; no defaults are taken."
  396.     say hdr"89I: EADir will be created if it does not exist."
  397.  
  398. /* ------------------------------------------------------------------ */
  399. /*   ALL ROUTINES EXIT FROM THIS POINT TO ENSURE CORRECT CLEANUP      */
  400. /* ------------------------------------------------------------------ */
  401. ENDPGM:
  402.     call SysDropFuncs
  403.     TimeStamp = time()
  404.     DateStamp = date()
  405.     say " "
  406.     say hdr"99I: Finished at "TimeStamp" on "DateStamp"."
  407.     return
  408.