FtpURI.st
changeset 1518 2e021f09320f
parent 1434 b415dec41ae1
child 2872 88dbc60ee0b3
equal deleted inserted replaced
1517:57f7f61d6c40 1518:2e021f09320f
    38 !FtpURI class methodsFor:'accessing'!
    38 !FtpURI class methodsFor:'accessing'!
    39 
    39 
    40 schemes
    40 schemes
    41 
    41 
    42     ^ #(ftp)
    42     ^ #(ftp)
       
    43 ! !
       
    44 
       
    45 !FtpURI class methodsFor:'ftp requests'!
       
    46 
       
    47 pathExists:aPathname ftpClient:aFtpClient
       
    48 
       
    49     |stream ok|
       
    50 
       
    51     FTPClient fileNotFoundErrorSignal handle:[:ex|] do:[
       
    52 	stream := aFtpClient getStreamFor:aPathname.
       
    53     ].
       
    54 
       
    55     ok := stream notNil.
       
    56     stream notNil ifTrue:[
       
    57 	stream close
       
    58     ].
       
    59 
       
    60     ^ ok
    43 ! !
    61 ! !
    44 
    62 
    45 !FtpURI methodsFor:'defaults'!
    63 !FtpURI methodsFor:'defaults'!
    46 
    64 
    47 defaultPassword
    65 defaultPassword
    77     ] ensure:[
    95     ] ensure:[
    78         ftp notNil ifTrue:[
    96         ftp notNil ifTrue:[
    79             ftp close.
    97             ftp close.
    80         ].
    98         ].
    81     ]
    99     ]
    82 !
       
    83 
       
    84 exists
       
    85     "does the file represented by this uri exist?
       
    86      establish a connection for try to get a readStream"
       
    87 
       
    88     |exists|
       
    89 
       
    90     self connectThenDo:[:aFtpClient|
       
    91         exists := self pathExistsFtp:aFtpClient.
       
    92     ].
       
    93 
       
    94     ^ exists ? false
       
    95 
       
    96 "
       
    97     |pwd uri|
       
    98 
       
    99     pwd := Dialog requestPassword:'Password:'. 
       
   100     uri := (URI fromString:('ftp://tm:%1@exept/home/tm/tmp/test.txt' bindWith:pwd) ).
       
   101     uri pathExists
       
   102 "
       
   103 !
       
   104 
       
   105 pathExists:aPathname ftpClient:aFtpClient
       
   106 
       
   107     |stream ok|
       
   108 
       
   109     FTPClient fileNotFoundErrorSignal handle:[:ex|] do:[
       
   110 	stream := aFtpClient getStreamFor:aPathname.
       
   111     ].
       
   112 
       
   113     ok := stream notNil.
       
   114     stream notNil ifTrue:[
       
   115 	stream close
       
   116     ].
       
   117 
       
   118     ^ ok
       
   119 !
       
   120 
       
   121 pathExistsFtp:aFtpClient
       
   122 
       
   123     ^ self pathExists:self path ftpClient:aFtpClient
       
   124 ! !
   100 ! !
   125 
   101 
   126 !FtpURI methodsFor:'stream access'!
   102 !FtpURI methodsFor:'stream access'!
   127 
   103 
   128 readStreamDo:aBlock
   104 readStreamDo:aBlock
   144             ].
   120             ].
   145         ].
   121         ].
   146     ].
   122     ].
   147 
   123 
   148     "
   124     "
   149      'ftp://stefan:password@hippo/etc/group' asURI readStreamDo:[:stream :attributes | 
   125      'ftp://stefan:password@ftp.exept.de/etc/group' asURI readStreamDo:[:stream :attributes | 
   150          self halt
   126          self halt
   151       ].
   127       ].
   152     "
   128     "
   153 !
   129 !
   154 
   130 
   187 
   163 
   188     |attributes list requestDirectory path dirUri dirPath|
   164     |attributes list requestDirectory path dirUri dirPath|
   189 
   165 
   190     requestDirectory := false.
   166     requestDirectory := false.
   191     path := self path.
   167     path := self path.
   192     "kludge"
   168     "kludge for pathnames starting with a users home dirctory"
   193     (path startsWith:'/~') ifTrue:[
   169     (path startsWith:'/~') ifTrue:[
   194         path := path copyFrom:2.
   170         path := path copyFrom:2.
   195     ].
   171     ].
   196 
   172 
   197     attributes := self class attributes.
   173     attributes := self class attributes.
   203         baseName := self baseName.
   179         baseName := self baseName.
   204         (baseName includesAny:'*?[]') ifTrue:[
   180         (baseName includesAny:'*?[]') ifTrue:[
   205             requestDirectory := true.
   181             requestDirectory := true.
   206             dirUri  := self directory.
   182             dirUri  := self directory.
   207             dirPath := dirUri path.
   183             dirPath := dirUri path.
   208             "kludge"
   184             "kludge for pathnames starting with a users home dirctory"
   209             (dirPath startsWith:'/~') ifTrue:[
   185             (dirPath startsWith:'/~') ifTrue:[
   210                 dirPath := dirPath copyFrom:2.
   186                 dirPath := dirPath copyFrom:2.
   211             ].
   187             ].
   212             ftp cd:dirPath.
   188             ftp cd:dirPath.
   213             FTPClient fileNotFoundErrorSignal handle:[:ex|] 
   189             FTPClient fileNotFoundErrorSignal handle:[:ex|] 
   232             ].
   208             ].
   233         ].
   209         ].
   234         requestDirectory ifFalse:[
   210         requestDirectory ifFalse:[
   235             dirUri := self directory.
   211             dirUri := self directory.
   236             dirPath := dirUri path.
   212             dirPath := dirUri path.
   237             "kludge"
   213             "kludge for pathnames starting with a users home dirctory"
   238             (dirPath startsWith:'/~') ifTrue:[
   214             (dirPath startsWith:'/~') ifTrue:[
   239                 dirPath := dirPath copyFrom:2.
   215                 dirPath := dirPath copyFrom:2.
   240             ].
   216             ].
   241             ftp cd:dirPath.
   217             ftp cd:dirPath.
   242             list add:self baseName.
   218             list add:self baseName.
   332 writeStreamDo:aBlock create:doCreate atomic:doAtomic
   308 writeStreamDo:aBlock create:doCreate atomic:doAtomic
   333     "use FTPClient for now.
   309     "use FTPClient for now.
   334 
   310 
   335      If doCreate is true, a nonExistent directory will be created.
   311      If doCreate is true, a nonExistent directory will be created.
   336      If doAtomic is true, files will appear atomically, by using
   312      If doAtomic is true, files will appear atomically, by using
   337 	an intermediate file theat will be renamed"
   313         an intermediate file theat will be renamed"
   338 
   314 
   339     |path toPath directory|
   315     |path toPath directory|
   340 
   316 
   341     path := self path.
   317     path := self path.
   342     "kludge"
   318     "kludge"
   343     (path startsWith:'/~') ifTrue:[
   319     (path startsWith:'/~') ifTrue:[
   344 	path := path copyFrom:2.
   320         path := path copyFrom:2.
   345     ].
   321     ].
   346 
   322 
   347     doAtomic ifTrue:[
   323     doAtomic ifTrue:[
   348 	toPath := self directoryPath, '/.transferFile'.
   324         toPath := self directoryPath, '/.transferFile'.
   349 	"kludge"
   325         "kludge"
   350 	(toPath startsWith:'/~') ifTrue:[
   326         (toPath startsWith:'/~') ifTrue:[
   351 	    toPath := toPath copyFrom:2.
   327             toPath := toPath copyFrom:2.
   352 	].
   328         ].
   353     ] ifFalse:[
   329     ] ifFalse:[
   354 	toPath := path.
   330         toPath := path.
   355     ].
   331     ].
   356         
   332         
   357     self connectThenDo:[:ftp| |stream|
   333     self connectThenDo:[:ftp| |stream|
   358 	[
   334         [
   359 	    [
   335             [
   360 		(self pathExists:path ftpClient:ftp) ifTrue:[ |infoStream|
   336                 (self class pathExists:path ftpClient:ftp) ifTrue:[ |infoStream|
   361 		    infoStream := '' writeStream.
   337                     infoStream := '' writeStream.
   362 		    self publicPrintOn:infoStream.
   338                     self publicPrintOn:infoStream.
   363 		    self error:('FTP write: Datei %1 already exists!!' bindWith:infoStream contents).
   339                     self error:('FTP write: Datei %1 already exists!!' bindWith:infoStream contents).
   364 		].
   340                 ].
   365 		stream := ftp putStreamFor:toPath.
   341                 stream := ftp putStreamFor:toPath.
   366 	    ] on:FTPClient filePutErrorSignal do:[:ex|
   342             ] on:FTPClient filePutErrorSignal do:[:ex|
   367 		doCreate ifFalse:[
   343                 doCreate ifFalse:[
   368 		    ex reject
   344                     ex reject
   369 		].
   345                 ].
   370 		"create the missing directory on the fly"
   346                 "create the missing directory on the fly"
   371 		directory := self directoryPath.
   347                 directory := self directoryPath.
   372 		FTPClient fileNotFoundErrorSignal handle:[:ex| ] do:[
   348                 FTPClient fileNotFoundErrorSignal handle:[:ex| ] do:[
   373 		    ftp mkdir:directory.
   349                     ftp mkdir:directory.
   374 		].
   350                 ].
   375 		ftp cd:directory.
   351                 ftp cd:directory.
   376 		(self pathExists:path ftpClient:ftp) ifTrue:[ |infoStream|
   352                 (self class pathExists:path ftpClient:ftp) ifTrue:[ |infoStream|
   377 		    infoStream := '' writeStream.
   353                     infoStream := '' writeStream.
   378 		    self publicPrintOn:infoStream.
   354                     self publicPrintOn:infoStream.
   379 		    self error:('FTP write: Datei %1 already exists!!' bindWith:infoStream contents).
   355                     self error:('FTP write: Datei %1 already exists!!' bindWith:infoStream contents).
   380 		].
   356                 ].
   381 		stream := ftp putStreamFor:toPath.
   357                 stream := ftp putStreamFor:toPath.
   382 	    ].
   358             ].
   383 	    aBlock value:stream value:self class attributes.
   359             aBlock value:stream value:self class attributes.
   384 	    stream close.
   360             stream close.
   385 	    doAtomic ifTrue:[
   361             doAtomic ifTrue:[
   386 		ftp rename:toPath to:path
   362                 ftp rename:toPath to:path
   387 	    ].
   363             ].
   388 	] ifCurtailed:[
   364         ] ifCurtailed:[
   389 	    stream notNil ifTrue:[
   365             stream notNil ifTrue:[
   390 		stream close.
   366                 stream close.
   391 	    ].
   367             ].
   392 	].
   368         ].
   393     ]
   369     ]
   394 ! !
   370 ! !
   395 
   371 
   396 !FtpURI methodsFor:'testing'!
   372 !FtpURI methodsFor:'testing'!
       
   373 
       
   374 exists
       
   375     "does the file represented by this uri exist?
       
   376      establish a connection for try to get a readStream"
       
   377 
       
   378     |exists|
       
   379 
       
   380     self connectThenDo:[:aFtpClient|
       
   381         exists :=  self class pathExists:self path ftpClient:aFtpClient
       
   382     ].
       
   383 
       
   384     ^ exists ? false
       
   385 
       
   386 "
       
   387     |pwd uri|
       
   388 
       
   389     pwd := Dialog requestPassword:'Password:'. 
       
   390     uri := (URI fromString:('ftp://tm:%1@exept/home/tm/tmp/test.txt' bindWith:pwd) ).
       
   391     uri pathExists
       
   392 "
       
   393 !
   397 
   394 
   398 isAbsolute
   395 isAbsolute
   399     "there is nothing like a relative ftp URI"
   396     "there is nothing like a relative ftp URI"
   400 
   397 
   401     ^ true
   398     ^ true
   408 ! !
   405 ! !
   409 
   406 
   410 !FtpURI class methodsFor:'documentation'!
   407 !FtpURI class methodsFor:'documentation'!
   411 
   408 
   412 version
   409 version
   413     ^ '$Header: /cvs/stx/stx/libbasic2/FtpURI.st,v 1.16 2004-03-20 15:36:05 stefan Exp $'
   410     ^ '$Header: /cvs/stx/stx/libbasic2/FtpURI.st,v 1.17 2005-02-01 12:41:37 stefan Exp $'
   414 ! !
   411 ! !