Archiver.st
changeset 1591 f655110a107f
parent 1590 61d8f450a232
child 1783 c7c8d7faccc0
equal deleted inserted replaced
1590:61d8f450a232 1591:f655110a107f
     1 "
     1 "
     2  COPYRIGHT (c) 2003 by eXept Software AG
     2  COPYRIGHT (c) 2003 by eXept Software AG
     3 	      All Rights Reserved
     3               All Rights Reserved
     4 
     4 
     5  This software is furnished under a license and may be used
     5  This software is furnished under a license and may be used
     6  only in accordance with the terms of that license and with the
     6  only in accordance with the terms of that license and with the
     7  inclusion of the above copyright notice.   This software may not
     7  inclusion of the above copyright notice.   This software may not
     8  be provided or otherwise made available to, or used by, any
     8  be provided or otherwise made available to, or used by, any
    13 "{ Package: 'stx:libbasic2' }"
    13 "{ Package: 'stx:libbasic2' }"
    14 
    14 
    15 Object subclass:#Archiver
    15 Object subclass:#Archiver
    16 	instanceVariableNames:'process temporaryDirectory fileName outStream errorStream
    16 	instanceVariableNames:'process temporaryDirectory fileName outStream errorStream
    17 		synchron'
    17 		synchron'
    18 	classVariableNames:''
    18 	classVariableNames:'MimeTypeMapping'
    19 	poolDictionaries:''
    19 	poolDictionaries:''
    20 	category:'System-Support-FileFormats'
    20 	category:'System-Support-FileFormats'
    21 !
    21 !
    22 
    22 
    23 Archiver subclass:#MultiFileArchive
    23 Archiver subclass:#MultiFileArchive
   100 !Archiver class methodsFor:'documentation'!
   100 !Archiver class methodsFor:'documentation'!
   101 
   101 
   102 copyright
   102 copyright
   103 "
   103 "
   104  COPYRIGHT (c) 2003 by eXept Software AG
   104  COPYRIGHT (c) 2003 by eXept Software AG
   105 	      All Rights Reserved
   105               All Rights Reserved
   106 
   106 
   107  This software is furnished under a license and may be used
   107  This software is furnished under a license and may be used
   108  only in accordance with the terms of that license and with the
   108  only in accordance with the terms of that license and with the
   109  inclusion of the above copyright notice.   This software may not
   109  inclusion of the above copyright notice.   This software may not
   110  be provided or otherwise made available to, or used by, any
   110  be provided or otherwise made available to, or used by, any
   111  other person.  No title to or ownership of the software is
   111  other person.  No title to or ownership of the software is
   112  hereby transferred.
   112  hereby transferred.
   113 "
   113 "
   114 ! !
   114 ! !
   115 
   115 
       
   116 !Archiver class methodsFor:'initialization'!
       
   117 
       
   118 initialize
       
   119     MimeTypeMapping := Dictionary withKeysAndValues:
       
   120     #(
       
   121         'application/x-tar-compressed'         CompressedTarArchive     "abstract - special handling"
       
   122         'application/x-tar-gzip-compressed'    TarGZipArchive       
       
   123         'application/x-tar-bzip2-compressed'   TarBZ2Archive        
       
   124         'application/x-tar'                    TarArchive          
       
   125         'application/x-gzip-compressed'        GZipCompressed      
       
   126         'application/x-zip-compressed'         ZipArchive          
       
   127         'application/x-bzip2-compressed'       BZ2Compressed       
       
   128         'application/x-squeak-archive'         ZipArchive          
       
   129         'application/java-archive'             ZipArchive          
       
   130 
       
   131         'application/x-ar-archive'             ArArchive           
       
   132         'application/x-ar-library'             ArArchive           
       
   133         'application/library'                  ArArchive           
       
   134 
       
   135 "/       'application/x-rpm'                    RPMArchive          
       
   136 "/       'application/x-rpm-archive'            RPMArchive          
       
   137 "/       'application/x-redhat packet manager'  RPMArchive          
       
   138     ).
       
   139 ! !
       
   140 
   116 !Archiver class methodsFor:'instance creation'!
   141 !Archiver class methodsFor:'instance creation'!
   117 
       
   118 classForMimeType:aMimeType
       
   119     ^ self classForMimeType:aMimeType fileName:nil
       
   120 
       
   121     "
       
   122      self classForMimeType:'application/x-tar'
       
   123      self classForMimeType:'application/x-foo'
       
   124      self classForMimeType:'application/x-squeak-archive'
       
   125      self classForMimeType:'application/java-archive'
       
   126      'foo.sar' asFilename mimeTypeFromName
       
   127      'foo.jar' asFilename mimeTypeFromName
       
   128      'foo.a' asFilename mimeTypeFromName
       
   129     "
       
   130 !
       
   131 
   142 
   132 classForMimeType:aMimeType fileName:aFileNameOrNil
   143 classForMimeType:aMimeType fileName:aFileNameOrNil
   133     |className|
   144     |className|
   134 
   145 
   135     aMimeType isNil ifTrue:[^ nil].
   146     className := MimeTypeMapping at:aMimeType ifAbsent:[^ nil].
   136 
   147 
   137     className := (
   148     className = #CompressedTarArchive ifTrue:[
   138 	    #(
   149         (aFileNameOrNil notNil and:[aFileNameOrNil suffix = 'bz2']) ifTrue:[
   139 		('application/x-tar-compressed'         CompressedTarArchive    )  "/ abstract - see below
   150             className := #TarBZ2Archive
   140 		('application/x-tar-gzip-compressed'    TarGZipArchive      )
   151         ] ifFalse:[
   141 		('application/x-tar-bzip2-compressed'   TarBZ2Archive       )
   152             "this is the default"
   142 		('application/x-tar'                    TarArchive          )
   153             className := #TarGZipArchive
   143 		('application/x-gzip-compressed'        GZipCompressed      )
   154         ]
   144 		('application/x-zip-compressed'         ZipArchive          )
       
   145 		('application/x-bzip2-compressed'       BZ2Compressed       )
       
   146 		('application/x-squeak-archive'         ZipArchive          )
       
   147 		('application/java-archive'             ZipArchive          )
       
   148 
       
   149 		('application/x-ar-archive'             ArArchive           )
       
   150 		('application/x-ar-library'             ArArchive           )
       
   151 		('application/library'                  ArArchive           )
       
   152 
       
   153 "/                ('application/x-rpm'                    RPMArchive          )
       
   154 "/                ('application/x-rpm-archive'            RPMArchive          )
       
   155 "/                ('application/x-redhat packet manager'  RPMArchive          )
       
   156 	    ) detect:[:entry | entry first = aMimeType] ifNone:#(nil nil)
       
   157 	) last.
       
   158     className isNil ifTrue:[^ nil].
       
   159 
       
   160     className = CompressedTarArchive ifTrue:[
       
   161 	aFileNameOrNil isNil ifTrue:[
       
   162 	    className := #TarGZipArchive
       
   163 	] ifFalse:[
       
   164 	    aFileNameOrNil suffix = 'bz2' ifTrue:[
       
   165 		className := #TarBZ2Archive
       
   166 	    ] ifFalse:[
       
   167 		className := #TarGZipArchive
       
   168 	    ]
       
   169 	].
       
   170     ].
   155     ].
   171     ^ self privateClassesAt:className.
   156     ^ self privateClassesAt:className.
   172 
   157 
   173     "
   158     "
   174      self classForMimeType:'application/x-tar'
   159      self classForMimeType:nil fileName:nil   
   175      self classForMimeType:'application/x-foo'
   160      self classForMimeType:'application/x-tar-compressed' fileName:nil   
   176      self classForMimeType:'application/x-squeak-archive'
   161      self classForMimeType:'application/x-tar' fileName:nil   
   177      self classForMimeType:'application/java-archive'
   162      self classForMimeType:'application/x-foo' fileName:nil   
   178      'foo.sar' asFilename mimeTypeFromName
   163      self classForMimeType:'application/x-squeak-archive' fileName:nil  
   179      'foo.jar' asFilename mimeTypeFromName
   164      self classForMimeType:'application/java-archive' fileName:nil 
   180      'foo.a' asFilename mimeTypeFromName
   165      'foo.sar' asFilename mimeTypeFromName               
       
   166      'foo.jar' asFilename mimeTypeFromName               
       
   167      'foo.a' asFilename mimeTypeFromName               
   181     "
   168     "
   182 !
   169 !
   183 
   170 
   184 newFor:aFilename
   171 newFor:aFilename
   185     |fn mimeType archiverClass|
   172     |fn archiverClass|
   186 
   173 
   187     fn := aFilename asFilename.
   174     fn := aFilename asFilename.
   188     mimeType := fn mimeTypeFromName.
   175     archiverClass := self classForMimeType:fn mimeTypeFromName fileName:fn.
   189     archiverClass := self classForMimeType:mimeType fileName:fn.
       
   190     archiverClass isNil ifTrue:[^ nil].
   176     archiverClass isNil ifTrue:[^ nil].
   191     ^ archiverClass with:aFilename
   177     ^ archiverClass with:fn
   192 !
   178 !
   193 
   179 
   194 with:aFilename
   180 with:aFilename
   195 
   181     ^ self new fileName:aFilename.
   196     | instance |
       
   197 
       
   198     instance := self new.
       
   199     instance fileName:aFilename.
       
   200     ^ instance
       
   201 ! !
   182 ! !
   202 
   183 
   203 !Archiver class methodsFor:'classAccess'!
   184 !Archiver class methodsFor:'classAccess'!
   204 
   185 
   205 arArchive
   186 arArchive
   236 
   217 
   237 stringWithQuotedFileBaseNames:aColOfFiles
   218 stringWithQuotedFileBaseNames:aColOfFiles
   238 
   219 
   239     aColOfFiles isNil ifTrue:[^ ''].
   220     aColOfFiles isNil ifTrue:[^ ''].
   240 
   221 
   241     ^ String
   222     ^ String 
   242 	streamContents:[:str |
   223         streamContents:[:str |
   243 	    aColOfFiles do:[:fn |
   224             aColOfFiles do:[:fn | 
   244 		str nextPutAll:' "'.
   225                 str nextPutAll:' "'.
   245 		str nextPutAll:(fn asFilename baseName).
   226                 str nextPutAll:(fn asFilename baseName).
   246 		str nextPutAll:'"'.
   227                 str nextPutAll:'"'.
   247 	    ].
   228             ].
   248 	]
   229         ]
   249 ! !
   230 ! !
   250 
   231 
   251 !Archiver class methodsFor:'commandOutputReader'!
   232 !Archiver class methodsFor:'commandOutputReader'!
   252 
   233 
   253 commandOutputParser
   234 commandOutputParser
   354 
   335 
   355 temporaryDirectory
   336 temporaryDirectory
   356     "return the value of the instance variable 'temporaryDirectory' (automatically generated)"
   337     "return the value of the instance variable 'temporaryDirectory' (automatically generated)"
   357 
   338 
   358     temporaryDirectory isNil ifTrue:[
   339     temporaryDirectory isNil ifTrue:[
   359 	temporaryDirectory := Filename newTemporary.
   340         temporaryDirectory := Filename newTemporary.
   360 	temporaryDirectory makeDirectory.
   341         temporaryDirectory makeDirectory.
   361     ].
   342     ].
   362     ^ temporaryDirectory
   343     ^ temporaryDirectory
   363 ! !
   344 ! !
   364 
   345 
   365 !Archiver methodsFor:'actions'!
   346 !Archiver methodsFor:'actions'!
   372     |cmd dir|
   353     |cmd dir|
   373 
   354 
   374     self fileName isNil ifTrue:[ ^ self].
   355     self fileName isNil ifTrue:[ ^ self].
   375     dir := self fileName directory.
   356     dir := self fileName directory.
   376     cmd := self getCommandToListFiles:aColOfFiles.
   357     cmd := self getCommandToListFiles:aColOfFiles.
   377     self executeCommand:cmd directory:dir
   358     self executeCommand:cmd directory:dir 
   378 !
   359 !
   379 
   360 
   380 removeFilesFromArchive:aColOfFiles
   361 removeFilesFromArchive:aColOfFiles
   381 
   362 
   382     self subclassResponsibility.
   363     self subclassResponsibility.
   397 removeTemporaryDirectory
   378 removeTemporaryDirectory
   398 
   379 
   399     | tmp |
   380     | tmp |
   400 
   381 
   401     temporaryDirectory notNil ifTrue:[
   382     temporaryDirectory notNil ifTrue:[
   402 	tmp := self temporaryDirectory.
   383         tmp := self temporaryDirectory.
   403 	(FileDirectory directoryNamed:(tmp directory)) removeDirectory:tmp baseName.
   384         (FileDirectory directoryNamed:(tmp directory)) removeDirectory:tmp baseName.
   404 	temporaryDirectory := nil.
   385         temporaryDirectory := nil.
   405     ].
   386     ].
   406 !
   387 !
   407 
   388 
   408 stopProcess
   389 stopProcess
   409 
   390 
   410     process notNil ifTrue:[
   391     process notNil ifTrue:[
   411 	process terminateWithAllSubprocesses.
   392         process terminateWithAllSubprocesses.
   412 	process waitUntilTerminated.
   393         process waitUntilTerminated.
   413     ].
   394     ].
   414 ! !
   395 ! !
   415 
   396 
   416 !Archiver methodsFor:'command execution'!
   397 !Archiver methodsFor:'command execution'!
   417 
   398 
   418 executeCommand:cmd directory:aDirectory
   399 executeCommand:cmd directory:aDirectory
   419 
   400 
   420 
   401 
   421     synchron isNil ifTrue:[synchron := true].
   402     synchron isNil ifTrue:[synchron := true].
   422     synchron ifTrue:[
   403     synchron ifTrue:[
   423 	 OperatingSystem
   404          OperatingSystem 
   424 	    executeCommand:cmd
   405             executeCommand:cmd
   425 	    inputFrom:nil
   406             inputFrom:nil
   426 	    outputTo:outStream
   407             outputTo:outStream
   427 	    errorTo:errorStream
   408             errorTo:errorStream
   428 	    inDirectory:aDirectory
   409             inDirectory:aDirectory
   429 	    lineWise:true
   410             lineWise:true
   430 	    onError:[:status| false].
   411             onError:[:status| false].
   431     ] ifFalse:[
   412     ] ifFalse:[
   432 	process := Process for:[
   413         process := Process for:[
   433 		[
   414                 [ 
   434 		     OperatingSystem
   415                      OperatingSystem 
   435 			executeCommand:cmd
   416                         executeCommand:cmd
   436 			inputFrom:nil
   417                         inputFrom:nil
   437 			outputTo:outStream
   418                         outputTo:outStream
   438 			errorTo:errorStream
   419                         errorTo:errorStream
   439 			inDirectory:aDirectory
   420                         inDirectory:aDirectory
   440 			lineWise:true
   421                         lineWise:true
   441 			onError:[:status| false].
   422                         onError:[:status| false].
   442 		] ensure:[
   423                 ] ensure:[
   443 		    process := nil.
   424                     process := nil.
   444 		].
   425                 ].
   445 
   426 
   446 	] priority:(Processor systemBackgroundPriority).
   427         ] priority:(Processor systemBackgroundPriority).
   447 	process name:('Archiver command: ', cmd).
   428         process name:('Archiver command: ', cmd).
   448 	process resume.
   429         process resume.
   449     ]
   430     ]
   450 !
   431 !
   451 
   432 
   452 isValidOutputLine:line
   433 isValidOutputLine:line
   453     "return true, if line contains a valid list-files output line"
   434     "return true, if line contains a valid list-files output line"
   478 
   459 
   479 !Archiver methodsFor:'command strings'!
   460 !Archiver methodsFor:'command strings'!
   480 
   461 
   481 addDoubleQuotedFilenames:collectionOfFilenames toStream:aStream
   462 addDoubleQuotedFilenames:collectionOfFilenames toStream:aStream
   482     collectionOfFilenames notNil ifTrue:[
   463     collectionOfFilenames notNil ifTrue:[
   483 	collectionOfFilenames do:[:el |
   464         collectionOfFilenames do:[:el | 
   484 	    aStream nextPutAll:' "'.
   465             aStream nextPutAll:' "'.
   485 	    aStream nextPutAll:(el asString).
   466             aStream nextPutAll:(el asString).
   486 	    aStream nextPutAll:'"'
   467             aStream nextPutAll:'"'
   487 	].
   468         ].
   488     ].
   469     ].
   489 ! !
   470 ! !
   490 
   471 
   491 !Archiver methodsFor:'initialization & release'!
   472 !Archiver methodsFor:'initialization & release'!
   492 
   473 
   509     "/ copy archiv to tempDir
   490     "/ copy archiv to tempDir
   510     archivFile copyTo:archivInTemp.
   491     archivFile copyTo:archivInTemp.
   511     "/ keep a save copy
   492     "/ keep a save copy
   512     archivFile renameTo:(archivFile withSuffix:'sav').
   493     archivFile renameTo:(archivFile withSuffix:'sav').
   513     [
   494     [
   514 	"/ copy files to be added to tempDir
   495         "/ copy files to be added to tempDir
   515 	colOfFiles do:[:file |
   496         colOfFiles do:[:file |
   516 	    file recursiveCopyTo:(tempDir construct:(file asFilename baseName))
   497             file recursiveCopyTo:(tempDir construct:(file asFilename baseName))
   517 	].
   498         ].
   518 
   499 
   519 	"/ addFiles to the tar archive
   500         "/ addFiles to the tar archive
   520 	cmd := self getCommandToAdd:colOfFiles toArchive:archivInTemp.
   501         cmd := self getCommandToAdd:colOfFiles toArchive:archivInTemp.
   521 	self executeCommand:cmd directory:tempDir.
   502         self executeCommand:cmd directory:tempDir.
   522 
   503 
   523 	"/ copy tar archiv back
   504         "/ copy tar archiv back
   524 	archivInTemp copyTo:archivFile.
   505         archivInTemp copyTo:archivFile.
   525     ] ensure:[
   506     ] ensure:[
   526 	"/ cg: remove the tempFile
   507         "/ cg: remove the tempFile
   527 	archivInTemp remove.
   508         archivInTemp remove.
   528 	"/ cg: remove copied files
   509         "/ cg: remove copied files
   529 	colOfFiles do:[:file |
   510         colOfFiles do:[:file |
   530 	    (tempDir construct:(file asFilename baseName)) remove.
   511             (tempDir construct:(file asFilename baseName)) remove.
   531 	].
   512         ].
   532     ].
   513     ].
   533 !
   514 !
   534 
   515 
   535 extractFiles:aColOfFilesOrNil to:aDirectory
   516 extractFiles:aColOfFilesOrNil to:aDirectory
   536     |execDir cmd|
   517     |execDir cmd|
   546     execDir := self fileName directory.
   527     execDir := self fileName directory.
   547     tempDir := self temporaryDirectory.
   528     tempDir := self temporaryDirectory.
   548     self extractFiles:aColOfFiles to:tempDir.
   529     self extractFiles:aColOfFiles to:tempDir.
   549 
   530 
   550     aColOfFiles do:[ : aFileString |
   531     aColOfFiles do:[ : aFileString |
   551 	tempFile := self temporaryDirectory construct:aFileString.
   532         tempFile := self temporaryDirectory construct:aFileString.
   552 	targetFile := aDirectory construct:(aFileString asFilename baseName).
   533         targetFile := aDirectory construct:(aFileString asFilename baseName).
   553 	targetFile exists ifTrue:[
   534         targetFile exists ifTrue:[
   554 	    targetFile recursiveRemove.
   535             targetFile recursiveRemove.
   555 	].
   536         ].
   556 	tempFile exists ifTrue:[
   537         tempFile exists ifTrue:[
   557 	    tempFile recursiveCopyTo:targetFile.
   538             tempFile recursiveCopyTo:targetFile.
   558 	].
   539         ].
   559     ].
   540     ].
   560 !
   541 !
   561 
   542 
   562 removeFilesFromArchive:aColOfFiles
   543 removeFilesFromArchive:aColOfFiles
   563 
   544 
   564     |cmd|
   545     |cmd|
   565 
   546 
   566     cmd := self getCommandToRemoveFiles:aColOfFiles.
   547     cmd := self getCommandToRemoveFiles:aColOfFiles.
   567     self executeCommand:cmd directory:(self fileName directory).
   548     self executeCommand:cmd directory:(self fileName directory). 
   568 ! !
   549 ! !
   569 
   550 
   570 !Archiver::MultiFileArchive methodsFor:'command strings'!
   551 !Archiver::MultiFileArchive methodsFor:'command strings'!
   571 
   552 
   572 getCommandToAdd:colOfFiles toArchive:archivIn
   553 getCommandToAdd:colOfFiles toArchive:archivIn
   609 
   590 
   610 parseLine:line forItemClass:itemClass
   591 parseLine:line forItemClass:itemClass
   611     |words archiverColumns item index key|
   592     |words archiverColumns item index key|
   612 
   593 
   613     (firstLineRead not and:[archiver class hasTitleLine]) ifTrue:[
   594     (firstLineRead not and:[archiver class hasTitleLine]) ifTrue:[
   614 	firstLineRead := true.
   595         firstLineRead := true.
   615 	^ nil.
   596         ^ nil.
   616     ].
   597     ].
   617 
   598 
   618     (archiver isValidOutputLine:line) ifFalse:[
   599     (archiver isValidOutputLine:line) ifFalse:[
   619 	^ nil.
   600         ^ nil.
   620     ].
   601     ].
   621 
   602 
   622     words := line asCollectionOfWords.
   603     words := line asCollectionOfWords.
   623     archiverColumns := archiver columns.
   604     archiverColumns := archiver columns.
   624     item := itemClass new.
   605     item := itemClass new.
   625     index := 1.
   606     index := 1.
   626 
   607 
   627     archiverColumns do:[:colDescr |
   608     archiverColumns do:[:colDescr |
   628 	| itemWordCount itemStream itemFieldSelector itemWriter |
   609         | itemWordCount itemStream itemFieldSelector itemWriter |
   629 
   610 
   630 	itemWordCount := colDescr second.
   611         itemWordCount := colDescr second.
   631 	itemFieldSelector := colDescr first.
   612         itemFieldSelector := colDescr first.
   632 	itemFieldSelector notNil ifTrue:[
   613         itemFieldSelector notNil ifTrue:[
   633 	    itemWriter := (itemFieldSelector , ':') asSymbol.
   614             itemWriter := (itemFieldSelector , ':') asSymbol.
   634 	].
   615         ].
   635 	itemStream := WriteStream on:''.
   616         itemStream := WriteStream on:''.
   636 	itemWordCount == #rest ifTrue:[
   617         itemWordCount == #rest ifTrue:[
   637 	    words from:index do:[:w|
   618             words from:index do:[:w|
   638 		itemStream nextPutAll:w.
   619                 itemStream nextPutAll:w.
   639 		itemStream space.
   620                 itemStream space.
   640 	    ].
   621             ].
   641 	] ifFalse:[
   622         ] ifFalse:[
   642 	    words from:index to:(index + itemWordCount - 1) do:[:w|
   623             words from:index to:(index + itemWordCount - 1) do:[:w|
   643 		itemStream nextPutAll:w.
   624                 itemStream nextPutAll:w.
   644 		itemStream space.
   625                 itemStream space.
   645 	    ].
   626             ].
   646 	    index := index + itemWordCount.
   627             index := index + itemWordCount.
   647 	].
   628         ].
   648 	itemWriter notNil ifTrue:[
   629         itemWriter notNil ifTrue:[
   649 	    item perform:itemWriter with:(itemStream contents withoutSeparators).
   630             item perform:itemWriter with:(itemStream contents withoutSeparators).
   650 	].
   631         ].
   651 	itemStream close.
   632         itemStream close.
   652     ].
   633     ].
   653     ((archiverColumns collect:[:el| el first]) includes:#permissions) ifTrue:[
   634     ((archiverColumns collect:[:el| el first]) includes:#permissions) ifTrue:[
   654 	(item permissions startsWith:$d) ifTrue:[
   635         (item permissions startsWith:$d) ifTrue:[
   655 	    key := #directory.
   636             key := #directory.
   656 	    item isDirectory:true.
   637             item isDirectory:true.
   657 	] ifFalse:[
   638         ] ifFalse:[
   658 	    key := MIMETypes mimeTypeForFilename:(item fileName asFilename baseName).
   639             key := MIMETypes mimeTypeForFilename:(item fileName asFilename baseName).
   659 	    item isDirectory:false.
   640             item isDirectory:false.
   660 	].
   641         ].
   661     ] ifFalse:[
   642     ] ifFalse:[
   662 	key := MIMETypes mimeTypeForFilename:(item fileName asFilename baseName).
   643         key := MIMETypes mimeTypeForFilename:(item fileName asFilename baseName).
   663     ].
   644     ].
   664     item icon:(FileBrowser iconForKeyMatching:key).
   645     item icon:(FileBrowser iconForKeyMatching:key).
   665     ^ item
   646     ^ item
   666 ! !
   647 ! !
   667 
   648 
   687 compressFile:aFile to:newFile
   668 compressFile:aFile to:newFile
   688     | cmd directory|
   669     | cmd directory|
   689 
   670 
   690     directory := newFile directory.
   671     directory := newFile directory.
   691     (directory exists) ifFalse:[
   672     (directory exists) ifFalse:[
   692 	DialogBox warn:'cannot compress to non-existing directory ', directory asString.
   673         DialogBox warn:'cannot compress to non-existing directory ', directory asString.
   693     ].
   674     ].
   694     (directory isDirectory) ifFalse:[
   675     (directory isDirectory) ifFalse:[
   695 	DialogBox warn:'cannot compress to non-directory ', directory asString.
   676         DialogBox warn:'cannot compress to non-directory ', directory asString.
   696     ].
   677     ].
   697 
   678 
   698     cmd := self getCommandToCompress:aFile asNew:newFile.
   679     cmd := self getCommandToCompress:aFile asNew:newFile.
   699     self executeCommand:cmd directory:directory.
   680     self executeCommand:cmd directory:directory.
   700     newFile exists ifTrue:[
   681     newFile exists ifTrue:[
   701 	self fileName:newFile.
   682         self fileName:newFile.
   702     ].
   683     ].
   703 !
   684 !
   704 
   685 
   705 extractFiles:aColOfFilesOrNil to:aDirectory
   686 extractFiles:aColOfFilesOrNil to:aDirectory
   706     self uncompressTo:aDirectory
   687     self uncompressTo:aDirectory
   708 
   689 
   709 uncompressTo:aDirectory
   690 uncompressTo:aDirectory
   710     | cmd file newFile|
   691     | cmd file newFile|
   711 
   692 
   712     (aDirectory exists) ifFalse:[
   693     (aDirectory exists) ifFalse:[
   713 	DialogBox warn:'cannot uncompress to non-existing directory ', aDirectory asString.
   694         DialogBox warn:'cannot uncompress to non-existing directory ', aDirectory asString.
   714 	^ self
   695         ^ self
   715     ].
   696     ].
   716     (aDirectory isDirectory) ifFalse:[
   697     (aDirectory isDirectory) ifFalse:[
   717 	DialogBox warn:'cannot uncompress to file ', aDirectory asString.
   698         DialogBox warn:'cannot uncompress to file ', aDirectory asString.
   718 	^ self
   699         ^ self
   719     ].
   700     ].
   720     file := newFile := self fileName.
   701     file := newFile := self fileName.
   721     (file directory pathName = aDirectory pathName) ifFalse:[
   702     (file directory pathName = aDirectory pathName) ifFalse:[
   722 	newFile := aDirectory construct:(file baseName).
   703         newFile := aDirectory construct:(file baseName).
   723 	file copyTo:newFile.
   704         file copyTo:newFile.
   724     ].
   705     ].
   725     cmd := self getCommandToUncompress:newFile.
   706     cmd := self getCommandToUncompress:newFile.
   726     self executeCommand:cmd directory:aDirectory.
   707     self executeCommand:cmd directory:aDirectory. 
   727 ! !
   708 ! !
   728 
   709 
   729 !Archiver::CompressedFile methodsFor:'columns'!
   710 !Archiver::CompressedFile methodsFor:'columns'!
   730 
   711 
   731 columns
   712 columns
   737 ! !
   718 ! !
   738 
   719 
   739 !Archiver::CompressedFile methodsFor:'command strings'!
   720 !Archiver::CompressedFile methodsFor:'command strings'!
   740 
   721 
   741 getCommandToCompress:aFile asNew:newFile
   722 getCommandToCompress:aFile asNew:newFile
   742     ^ '%1 -c %2 > %3'
   723     ^ '%1 -c %2 > %3' 
   743 	bindWith:self class compressCommand
   724         bindWith:self class compressCommand
   744 	with:aFile asString
   725         with:aFile asString 
   745 	with:newFile asString
   726         with:newFile asString
   746 !
   727 !
   747 
   728 
   748 getCommandToListFiles:dummyArg
   729 getCommandToListFiles:dummyArg
   749     ^ 'gzip -l "' , self fileName baseName , '"'
   730     ^ 'gzip -l "' , self fileName baseName , '"'
   750 !
   731 !
   751 
   732 
   752 getCommandToUncompress:aFileName
   733 getCommandToUncompress:aFileName 
   753     ^ '%1 %2'
   734     ^ '%1 %2' 
   754 	bindWith:self class uncompressCommand
   735         bindWith:self class uncompressCommand
   755 	with:aFileName baseName
   736         with:aFileName baseName
   756 ! !
   737 ! !
   757 
   738 
   758 !Archiver::BZ2Compressed class methodsFor:'command strings'!
   739 !Archiver::BZ2Compressed class methodsFor:'command strings'!
   759 
   740 
   760 compressCommand
   741 compressCommand
   766 ! !
   747 ! !
   767 
   748 
   768 !Archiver::BZ2Compressed methodsFor:'columns'!
   749 !Archiver::BZ2Compressed methodsFor:'columns'!
   769 
   750 
   770 columns
   751 columns
   771     ^ #(
   752     ^ #(      
   772 	 #(#fileName        1)
   753          #(#fileName        1)
   773     )
   754     ) 
   774 !
   755 !
   775 
   756 
   776 isValidOutputLine:line
   757 isValidOutputLine:line
   777     ^ true
   758     ^ true
   778 ! !
   759 ! !
   814     compressor uncompressTo:tempDir.
   795     compressor uncompressTo:tempDir.
   815     suffix := self fileName suffix.
   796     suffix := self fileName suffix.
   816     file := self fileName withoutSuffix.
   797     file := self fileName withoutSuffix.
   817     tarFilename := file baseName.
   798     tarFilename := file baseName.
   818     file suffix ~= 'tar' ifTrue:[
   799     file suffix ~= 'tar' ifTrue:[
   819 	tarFilename := tarFilename , '.tar'
   800         tarFilename := tarFilename , '.tar'
   820     ].
   801     ].
   821     tarFile := self temporaryDirectory construct:tarFilename.
   802     tarFile := self temporaryDirectory construct:tarFilename.
   822     tarArchiver := Archiver::TarArchive with:tarFile.
   803     tarArchiver := Archiver::TarArchive with:tarFile.
   823 ! !
   804 ! !
   824 
   805 
   901 
   882 
   902 columns
   883 columns
   903 
   884 
   904     "/ columns in stream order
   885     "/ columns in stream order
   905     "/  colums id/readSelector          words to read
   886     "/  colums id/readSelector          words to read
   906     ^ #( "/ #(#method          1)
   887     ^ #( "/ #(#method          1)      
   907 	 "/ #(#crc             1)
   888          "/ #(#crc             1)    
   908 	 "/ #(#dateAndTime     3)
   889          "/ #(#dateAndTime     3)   
   909 	 #(#compressSize    1)
   890          #(#compressSize    1)  
   910 	 #(#size            1)
   891          #(#size            1) 
   911 	 #(#ratio           1)
   892          #(#ratio           1) 
   912 	 #(#fileName        #rest)
   893          #(#fileName        #rest)
   913     )
   894     ) 
   914 !
   895 !
   915 
   896 
   916 isValidOutputLine:line
   897 isValidOutputLine:line
   917     ^ line endsWith:self fileName withoutSuffix baseName.
   898     ^ line endsWith:self fileName withoutSuffix baseName.
   918 ! !
   899 ! !
   931 
   912 
   932 !Archiver::ArArchive methodsFor:'columns'!
   913 !Archiver::ArArchive methodsFor:'columns'!
   933 
   914 
   934 columns
   915 columns
   935     "/  colums id/readSelector words to read
   916     "/  colums id/readSelector words to read
   936     ^ #(
   917     ^ #( 
   937 	 #(#permissions     1)
   918          #(#permissions     1)      
   938 	 #(#ownerGroup      1)
   919          #(#ownerGroup      1)    
   939 	 #(#size            1)
   920          #(#size            1)   
   940 	 #(#monthName       1)
   921          #(#monthName       1)  
   941 	 #(#dayString       1)
   922          #(#dayString       1)  
   942 	 #(#timeString      1)
   923          #(#timeString      1)  
   943 	 #(#yearString      1)
   924          #(#yearString      1)  
   944 	 #(#fileName        #rest)
   925          #(#fileName        #rest)
   945     )
   926     ) 
   946 !
   927 !
   947 
   928 
   948 isValidOutputLine:line
   929 isValidOutputLine:line
   949     ('[-r][-w][-x]' match:(line copyTo:3)) ifTrue:[^ true].
   930     ('[-r][-w][-x]' match:(line copyTo:3)) ifTrue:[^ true].
   950     ^ false.
   931     ^ false.
   951 ! !
   932 ! !
   952 
   933 
   953 !Archiver::ArArchive methodsFor:'command strings'!
   934 !Archiver::ArArchive methodsFor:'command strings'!
   954 
   935 
   955 getCommandToExtractFiles:sel intoDirectory:dir
   936 getCommandToExtractFiles:sel intoDirectory:dir 
   956     |stream|
   937     |stream|
   957 
   938 
   958     stream := WriteStream on:''.
   939     stream := WriteStream on:''.
   959 
   940 
   960     "/ 'x'  arArchivUnpackOption
   941     "/ 'x'  arArchivUnpackOption
   961     stream nextPutAll:('(cd %3 ; %1 x "%2" '
   942     stream nextPutAll:('(cd %3 ; %1 x "%2" ' 
   962 		    bindWith:self class arCommand
   943                     bindWith:self class arCommand
   963 		    with:self fileName asString string
   944                     with:self fileName asString string
   964 		    with:dir asString string).
   945                     with:dir asString string).
   965 
   946 
   966     sel notNil ifTrue:[
   947     sel notNil ifTrue:[
   967 	sel do:[:el |
   948         sel do:[:el | 
   968 	    stream nextPutAll:' "'.
   949             stream nextPutAll:' "'.
   969 	    stream nextPutAll:(el asString).
   950             stream nextPutAll:(el asString).
   970 	    stream nextPutAll:'"'
   951             stream nextPutAll:'"'
   971 	].
   952         ].
   972     ].
   953     ].
   973     stream nextPutAll:')'.
   954     stream nextPutAll:')'.
   974     ^ stream contents.
   955     ^ stream contents.
   975 !
   956 !
   976 
   957 
   977 getCommandToListFiles:aColOfFiles
   958 getCommandToListFiles:aColOfFiles 
   978     |stream|
   959     |stream|
   979 
   960 
   980     stream := WriteStream on:''.
   961     stream := WriteStream on:''.
   981 
   962 
   982     "/ 't'  arArchivListContentsOption
   963     "/ 't'  arArchivListContentsOption
   983     "/ 'v'  arArchivVerboseOption
   964     "/ 'v'  arArchivVerboseOption
   984     stream nextPutAll:('%1 tv "%2"'
   965     stream nextPutAll:('%1 tv "%2"' 
   985 		    bindWith:self class arCommand
   966                     bindWith:self class arCommand
   986 		    with:self fileName baseName).
   967                     with:self fileName baseName).
   987 
   968 
   988     stream nextPutAll:(self class stringWithQuotedFileBaseNames:aColOfFiles).
   969     stream nextPutAll:(self class stringWithQuotedFileBaseNames:aColOfFiles).
   989     ^ stream contents.
   970     ^ stream contents.
   990 ! !
   971 ! !
   991 
   972 
  1015 !Archiver::TarArchive methodsFor:'columns'!
   996 !Archiver::TarArchive methodsFor:'columns'!
  1016 
   997 
  1017 columns
   998 columns
  1018 
   999 
  1019     "/  colums id/readSelector words to read
  1000     "/  colums id/readSelector words to read
  1020     ^ #( #(#permissions     1)
  1001     ^ #( #(#permissions     1)      
  1021 	 #(#ownerGroup      1)
  1002          #(#ownerGroup      1)    
  1022 	 #(#size            1)
  1003          #(#size            1)   
  1023 	 #(#dateAndTime     2)
  1004          #(#dateAndTime     2)  
  1024 	 #(#fileName        #rest)
  1005          #(#fileName        #rest)
  1025     )
  1006     ) 
  1026 !
  1007 !
  1027 
  1008 
  1028 isValidOutputLine:line
  1009 isValidOutputLine:line
  1029     ('[-d][-r][-w][-x]' match:(line copyTo:4)) ifTrue:[^ true].
  1010     ('[-d][-r][-w][-x]' match:(line copyTo:4)) ifTrue:[^ true].
  1030     ^ false.
  1011     ^ false.
  1039 
  1020 
  1040     stream := WriteStream on:''.
  1021     stream := WriteStream on:''.
  1041 
  1022 
  1042     "/ 'r'  TarArchivAddOption
  1023     "/ 'r'  TarArchivAddOption
  1043     "/ 'f'  TarArchivFileOption
  1024     "/ 'f'  TarArchivFileOption
  1044     stream nextPutAll:('%1 rf "%2"'
  1025     stream nextPutAll:('%1 rf "%2"' 
  1045 		    bindWith:self class tarCommand
  1026                     bindWith:self class tarCommand
  1046 		    with:archiveFile asString string).
  1027                     with:archiveFile asString string).
  1047 
  1028 
  1048     stream nextPutAll:(self class stringWithQuotedFileBaseNames:aColOfFiles).
  1029     stream nextPutAll:(self class stringWithQuotedFileBaseNames:aColOfFiles).
  1049     ^ stream contents
  1030     ^ stream contents
  1050 !
  1031 !
  1051 
  1032 
  1052 getCommandToExtractFiles:sel intoDirectory:dir
  1033 getCommandToExtractFiles:sel intoDirectory:dir 
  1053     |stream|
  1034     |stream|
  1054 
  1035 
  1055     stream := WriteStream on:''.
  1036     stream := WriteStream on:''.
  1056 
  1037 
  1057     "/ 'x'  TarArchivUnpackOption
  1038     "/ 'x'  TarArchivUnpackOption
  1058     "/ 'f'  TarArchivFileOption
  1039     "/ 'f'  TarArchivFileOption
  1059     "/ 'C'  TarArchivUnpackInDirectoryOption
  1040     "/ 'C'  TarArchivUnpackInDirectoryOption
  1060     stream nextPutAll:('%1 -xf "%2" -C %3'
  1041     stream nextPutAll:('%1 -xf "%2" -C %3' 
  1061 		    bindWith:self class tarCommand
  1042                     bindWith:self class tarCommand
  1062 		    with:self fileName asString string
  1043                     with:self fileName asString string
  1063 		    with:dir asString).
  1044                     with:dir asString).
  1064 
  1045 
  1065     sel notNil ifTrue:[
  1046     sel notNil ifTrue:[
  1066 	sel do:[:el |
  1047         sel do:[:el | 
  1067 	    stream nextPutAll:' "'.
  1048             stream nextPutAll:' "'.
  1068 	    stream nextPutAll:(el asString).
  1049             stream nextPutAll:(el asString).
  1069 	    stream nextPutAll:'"'
  1050             stream nextPutAll:'"'
  1070 	].
  1051         ].
  1071     ].
  1052     ].
  1072     ^ stream contents.
  1053     ^ stream contents.
  1073 !
  1054 !
  1074 
  1055 
  1075 getCommandToListFiles:aColOfFiles
  1056 getCommandToListFiles:aColOfFiles 
  1076     |stream|
  1057     |stream|
  1077 
  1058 
  1078     stream := WriteStream on:''.
  1059     stream := WriteStream on:''.
  1079 
  1060 
  1080     "/ 't'  TarArchivListContentsOption
  1061     "/ 't'  TarArchivListContentsOption
  1081     "/ 'v'  TarArchivVerboseOption
  1062     "/ 'v'  TarArchivVerboseOption
  1082     "/ 'f'  TarArchivFileOption
  1063     "/ 'f'  TarArchivFileOption
  1083     stream nextPutAll:('%1 -tvf "%2"'
  1064     stream nextPutAll:('%1 -tvf "%2"' 
  1084 		    bindWith:self class tarCommand
  1065                     bindWith:self class tarCommand
  1085 		    with:self fileName baseName).
  1066                     with:self fileName baseName).
  1086 
  1067 
  1087     stream nextPutAll:(self class stringWithQuotedFileBaseNames:aColOfFiles).
  1068     stream nextPutAll:(self class stringWithQuotedFileBaseNames:aColOfFiles).
  1088     ^ stream contents.
  1069     ^ stream contents.
  1089 !
  1070 !
  1090 
  1071 
  1091 getCommandToRemoveFiles:sel
  1072 getCommandToRemoveFiles:sel 
  1092     |stream filename|
  1073     |stream filename|
  1093 
  1074 
  1094     filename := self fileName.
  1075     filename := self fileName.
  1095     filename exists ifFalse:[^ nil].
  1076     filename exists ifFalse:[^ nil].
  1096 
  1077 
  1097     stream := WriteStream on:''.
  1078     stream := WriteStream on:''.
  1098 
  1079 
  1099     "/ 'f'  TarArchivFileOption
  1080     "/ 'f'  TarArchivFileOption
  1100     stream nextPutAll:('%1 --delete -f "%2"'
  1081     stream nextPutAll:('%1 --delete -f "%2"' 
  1101 		    bindWith:self class tarCommand
  1082                     bindWith:self class tarCommand
  1102 		    with:self fileName baseName).
  1083                     with:self fileName baseName).
  1103 
  1084 
  1104     stream nextPutAll:(self class stringWithQuotedFileBaseNames:sel).
  1085     stream nextPutAll:(self class stringWithQuotedFileBaseNames:sel).
  1105     ^ stream contents
  1086     ^ stream contents
  1106 ! !
  1087 ! !
  1107 
  1088 
  1203 !Archiver::ZipArchive methodsFor:'columns'!
  1184 !Archiver::ZipArchive methodsFor:'columns'!
  1204 
  1185 
  1205 columns
  1186 columns
  1206 
  1187 
  1207     "/  colums id/readSelector    words to read
  1188     "/  colums id/readSelector    words to read
  1208     ^ #( (#permissions     1)
  1189     ^ #( (#permissions     1)      
  1209 	 (#version         2)
  1190          (#version         2)    
  1210 	 (#size            1)
  1191          (#size            1)     
  1211 	 (#type            1)
  1192          (#type            1)    
  1212 	 (#ratio           1)
  1193          (#ratio           1)    
  1213 	 (nil              1)
  1194          (nil              1)  
  1214 	 (#dateAndTime     2)
  1195          (#dateAndTime     2)  
  1215 	 (#fileName        #rest)
  1196          (#fileName        #rest)
  1216     )
  1197     ) 
  1217 !
  1198 !
  1218 
  1199 
  1219 isValidOutputLine:line
  1200 isValidOutputLine:line
  1220     ('[-d][-r][-w][-x]' match:(line copyTo:4)) ifTrue:[^ true].
  1201     ('[-d][-r][-w][-x]' match:(line copyTo:4)) ifTrue:[^ true].
  1221     ^ false.
  1202     ^ false.
  1227     |stream|
  1208     |stream|
  1228 
  1209 
  1229     archiveFile exists ifFalse:[^ nil].
  1210     archiveFile exists ifFalse:[^ nil].
  1230 
  1211 
  1231     stream := WriteStream on:''.
  1212     stream := WriteStream on:''.
  1232 
  1213     
  1233     stream nextPutAll:('%1 -r "%2"'
  1214     stream nextPutAll:('%1 -r "%2"' 
  1234 		    bindWith:self class zipCommand
  1215                     bindWith:self class zipCommand
  1235 		    with:archiveFile asString string).
  1216                     with:archiveFile asString string).
  1236 
  1217 
  1237     self
  1218     self 
  1238 	addDoubleQuotedFilenames:(aColOfFiles collect:[:each | each asFilename baseName])
  1219         addDoubleQuotedFilenames:(aColOfFiles collect:[:each | each asFilename baseName])
  1239 	toStream:stream.
  1220         toStream:stream.
  1240 
  1221 
  1241     ^ stream contents
  1222     ^ stream contents
  1242 !
  1223 !
  1243 
  1224 
  1244 getCommandToExtractFiles:aColOfFiles intoDirectory:dir
  1225 getCommandToExtractFiles:aColOfFiles intoDirectory:dir
  1247     stream := WriteStream on:''.
  1228     stream := WriteStream on:''.
  1248 
  1229 
  1249     "/ -o   UnzipOverwriteExistingFilesOption
  1230     "/ -o   UnzipOverwriteExistingFilesOption
  1250     "/ -d   UnzipExtDirectoryOption
  1231     "/ -d   UnzipExtDirectoryOption
  1251 
  1232 
  1252     stream nextPutAll:('%1 -o -d "%2" "%3"'
  1233     stream nextPutAll:('%1 -o -d "%2" "%3"' 
  1253 		    bindWith:self class unzipCommand
  1234                     bindWith:self class unzipCommand
  1254 		    with:dir asString string
  1235                     with:dir asString string
  1255 		    with:self fileName asString).
  1236                     with:self fileName asString).
  1256 
  1237 
  1257     self addDoubleQuotedFilenames:aColOfFiles toStream:stream.
  1238     self addDoubleQuotedFilenames:aColOfFiles toStream:stream.
  1258     ^ stream contents.
  1239     ^ stream contents.
  1259 !
  1240 !
  1260 
  1241 
  1261 getCommandToListFiles:aColOfFiles
  1242 getCommandToListFiles:aColOfFiles 
  1262     |stream|
  1243     |stream|
  1263 
  1244 
  1264     stream := WriteStream on:''.
  1245     stream := WriteStream on:''.
  1265 
  1246 
  1266     "/  -Z      ZipInfoOption
  1247     "/  -Z      ZipInfoOption
  1267     "/  -h     ZipHeaderOption
  1248     "/  -h     ZipHeaderOption
  1268     "/  -t      ZipTotalOption
  1249     "/  -t      ZipTotalOption
  1269     stream nextPutAll:('%1 -Z -m -h "%2"'
  1250     stream nextPutAll:('%1 -Z -m -h "%2"' 
  1270 			bindWith:self class unzipCommand
  1251                         bindWith:self class unzipCommand
  1271 			with:self fileName asString string).
  1252                         with:self fileName asString string).
  1272 
  1253 
  1273     aColOfFiles notNil ifTrue:[       self halt.
  1254     aColOfFiles notNil ifTrue:[       self halt.
  1274 	self
  1255         self 
  1275 	    addDoubleQuotedFilenames:(aColOfFiles collect:[:each | each asFilename baseName])
  1256             addDoubleQuotedFilenames:(aColOfFiles collect:[:each | each asFilename baseName])
  1276 	    toStream:stream.
  1257             toStream:stream.
  1277     ].
  1258     ].
  1278     ^ stream contents.
  1259     ^ stream contents.
  1279 !
  1260 !
  1280 
  1261 
  1281 getCommandToRemoveFiles:aColOfFiles
  1262 getCommandToRemoveFiles:aColOfFiles 
  1282     |stream|
  1263     |stream|
  1283 
  1264 
  1284     stream := WriteStream on:''.
  1265     stream := WriteStream on:''.
  1285 
  1266 
  1286     stream nextPutAll:('%1 -d "%2"'
  1267     stream nextPutAll:('%1 -d "%2"' 
  1287 			bindWith:self class zipCommand
  1268                         bindWith:self class zipCommand
  1288 			with:self fileName asString string).
  1269                         with:self fileName asString string).
  1289 
  1270 
  1290     self addDoubleQuotedFilenames:aColOfFiles toStream:stream.
  1271     self addDoubleQuotedFilenames:aColOfFiles toStream:stream.
  1291     ^ stream contents.
  1272     ^ stream contents.
  1292 ! !
  1273 ! !
  1293 
  1274 
  1294 !Archiver class methodsFor:'documentation'!
  1275 !Archiver class methodsFor:'documentation'!
  1295 
  1276 
  1296 version
  1277 version
  1297     ^ '$Header: /cvs/stx/stx/libbasic2/Archiver.st,v 1.26 2006-02-01 11:05:09 stefan Exp $'
  1278     ^ '$Header: /cvs/stx/stx/libbasic2/Archiver.st,v 1.27 2006-02-01 12:27:39 stefan Exp $'
  1298 ! !
  1279 ! !
       
  1280 
       
  1281 Archiver initialize!