 |
Changeset 3484
- Timestamp:
- 05/04/08 17:02:48
(7 months ago)
- Author:
- lmartin92
- Message:
I fixed a OutputStream?.copy() problem in FtpClient?.d and it still works perfectly. Old code may not even be removed, probably just commented out. Needs comments for all methods.
I have worked a little on FtpFolder? today and have made parts of it where it would download do this: rather than look to see if it had it and if not then store it. To get updated information one would need to recreate the object or something with the same parameters. I'll work on a way that will update it maybe every 10 accesses or something or know if there have been any Files written to the server or something.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3388 |
r3484 |
|
| 1 | | /* |
|---|
| 2 | | * Author: Lester L. Martin II |
|---|
| 3 | | * Copyright(c) Lester L. Martin II 2008 All rights reserved |
|---|
| 4 | | * Liscense: $(BSD) |
|---|
| 5 | | * |
|---|
| 6 | | */ |
|---|
| 7 | | |
|---|
| 8 | | //fix flatness |
|---|
| 9 | | module tango.io.vfs.FtpFolder; |
|---|
| 10 | | |
|---|
| 11 | | import tango.net.ftp.FtpClient; |
|---|
| 12 | | import tango.io.vfs.model.Vfs; |
|---|
| 13 | | import tango.io.vfs.FileFolder; |
|---|
| 14 | | import tango.io.Conduit; |
|---|
| 15 | | import tango.text.Util; |
|---|
| | 1 | module tango.io.vfs.FtpFolder; |
|---|
| | 2 | |
|---|
| | 3 | private { |
|---|
| | 4 | import tango.net.ftp.FtpClient; |
|---|
| | 5 | import tango.io.vfs.model.Vfs; |
|---|
| | 6 | import tango.io.vfs.FileFolder; |
|---|
| | 7 | import tango.io.Conduit; |
|---|
| | 8 | import tango.text.Util; |
|---|
| | 9 | } |
|---|
| 16 | 10 | |
|---|
| 17 | 11 | private FtpFileInfo[] getFiles(FTPConnection ftp, char[] path = "") { |
|---|
| … | … | |
| 41 | 35 | assert(server != ""); |
|---|
| 42 | 36 | } |
|---|
| 43 | | out { |
|---|
| 44 | | assert(server != ""); |
|---|
| 45 | | } |
|---|
| 46 | 37 | body { |
|---|
| 47 | 38 | toString_ = server; |
|---|
| … | … | |
| 57 | 48 | |
|---|
| 58 | 49 | VfsFolder create() { |
|---|
| 59 | | if(exists()) { |
|---|
| 60 | | ftp_.close(); |
|---|
| 61 | | throw new Exception("This FTP Directory is already in existence."); |
|---|
| 62 | | } else { |
|---|
| 63 | | ftp_.close(); |
|---|
| 64 | | ftp_ = new FTPConnection(); |
|---|
| 65 | | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 66 | | ftp_.mkdir(name_); |
|---|
| 67 | | ftp_.close(); |
|---|
| 68 | | } |
|---|
| | 50 | ftp_ = new FTPConnection(); |
|---|
| | 51 | ftp_.connect(toString_, username_, password_, port_); |
|---|
| | 52 | ftp_.mkdir(name_); |
|---|
| | 53 | ftp_.close(); |
|---|
| 69 | 54 | return new FtpFolder(toString_, name_, username_, password_, port_); |
|---|
| 70 | 55 | } |
|---|
| … | … | |
| 73 | 58 | ftp_ = new FTPConnection(); |
|---|
| 74 | 59 | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 75 | | return name_ == "" ? true : (ftp_.exist(name_) == 2 ? true : false); |
|---|
| | 60 | switch(ftp_.exist(name_ != "" ? name_ : toString_)) { |
|---|
| | 61 | case 0: |
|---|
| | 62 | ftp_.close(); |
|---|
| | 63 | return false; |
|---|
| | 64 | break; |
|---|
| | 65 | case 1: |
|---|
| | 66 | ftp_.close(); |
|---|
| | 67 | return false; |
|---|
| | 68 | break; |
|---|
| | 69 | case 2: |
|---|
| | 70 | ftp_.close(); |
|---|
| | 71 | return true; |
|---|
| | 72 | break; |
|---|
| | 73 | default: |
|---|
| | 74 | ftp_.close(); |
|---|
| | 75 | return false; |
|---|
| | 76 | break; |
|---|
| | 77 | } |
|---|
| | 78 | return false; |
|---|
| 76 | 79 | } |
|---|
| 77 | 80 | } |
|---|
| … | … | |
| 82 | 85 | char[] toString_, name_, username_, password_; |
|---|
| 83 | 86 | uint port_; |
|---|
| 84 | | bool writeable_, writeableSet_; |
|---|
| | 87 | bool writeable_, writeableSet_, fileInfosSet_; |
|---|
| | 88 | FtpFileInfo[] fileInfos_; |
|---|
| 85 | 89 | |
|---|
| 86 | 90 | public this(char[] server, char[] path, char[] username = "anonymous", |
|---|
| … | … | |
| 109 | 113 | |
|---|
| 110 | 114 | VfsFile file(char[] path) { |
|---|
| 111 | | return new FtpFile(toString_ ~ name_, path, username_, password_, port_); |
|---|
| | 115 | return new FtpFile(toString_ ~ name_, '/' ~ path, username_, password_, port_); |
|---|
| 112 | 116 | } |
|---|
| 113 | 117 | |
|---|
| 114 | 118 | VfsFolderEntry folder(char[] path) { |
|---|
| 115 | | return new FtpFolderEntry(toString_, name_, username_, password_, port_); |
|---|
| | 119 | return new FtpFolderEntry(toString_, '/' ~ path, username_, password_, port_); |
|---|
| 116 | 120 | } |
|---|
| 117 | 121 | |
|---|
| … | … | |
| 127 | 131 | |
|---|
| 128 | 132 | int opApply(int delegate(inout VfsFolder) dg) { |
|---|
| 129 | | ftp_ = new FTPConnection(); |
|---|
| 130 | | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 131 | | FtpFileInfo[] fis = getFiles(ftp_, name_); |
|---|
| 132 | | ftp_.close(); |
|---|
| | 133 | if(!fileInfosSet_) { |
|---|
| | 134 | ftp_ = new FTPConnection(); |
|---|
| | 135 | ftp_.connect(toString_, username_, password_, port_); |
|---|
| | 136 | fileInfos_ = getFiles(ftp_, name_); |
|---|
| | 137 | ftp_.close(); |
|---|
| | 138 | fileInfosSet_ = true; |
|---|
| | 139 | } |
|---|
| 133 | 140 | |
|---|
| 134 | 141 | int result; |
|---|
| 135 | 142 | |
|---|
| 136 | | foreach(FtpFileInfo fi; fis) { |
|---|
| | 143 | foreach(FtpFileInfo fi; fileInfos_) { |
|---|
| 137 | 144 | if(fi.type == FtpFileType.dir) { |
|---|
| 138 | 145 | VfsFolder x = new FtpFolder(toString_, fi.name, username_, |
|---|
| … | … | |
| 163 | 170 | try { |
|---|
| 164 | 171 | ftp_.mkdir("diw"); |
|---|
| 165 | | ftp_.rm("diw"); |
|---|
| | 172 | ftp_.rm("/diw"); |
|---|
| 166 | 173 | writeable_ = true; |
|---|
| 167 | 174 | } catch(Exception e) { |
|---|
| … | … | |
| 175 | 182 | |
|---|
| 176 | 183 | VfsFolder close(bool commit = true) { |
|---|
| 177 | | ftp_.close(); |
|---|
| | 184 | ftp_.close(); |
|---|
| 178 | 185 | return new FtpFolder(toString_, name_, username_, password_, port_); |
|---|
| 179 | 186 | } |
|---|
| 180 | 187 | |
|---|
| 181 | 188 | void verify(VfsFolder folder, bool mounting) { |
|---|
| 182 | | return null; |
|---|
| | 189 | return null; |
|---|
| 183 | 190 | } |
|---|
| 184 | 191 | } |
|---|
| … | … | |
| 186 | 193 | class FtpFolders: VfsFolders { |
|---|
| 187 | 194 | |
|---|
| 188 | | FTPConnection ftp_; |
|---|
| | 195 | FTPConnection ftp_; |
|---|
| 189 | 196 | char[] toString_, name_, username_, password_; |
|---|
| 190 | | uint port_; |
|---|
| 191 | | bool flat_; |
|---|
| | 197 | uint port_ , fileNum_, folderNum_; |
|---|
| | 198 | ulong bytes_; |
|---|
| | 199 | bool flat_, fileInfosSet_, fileNumSet_, folderNumSet_, bytesSet_; |
|---|
| | 200 | FtpFileInfo[] fileInfos_; |
|---|
| 192 | 201 | |
|---|
| 193 | 202 | public this(char[] server, char[] path, char[] username = "anonymous", |
|---|
| 194 | | char[] password = "anonymous@anonymous", uint port = 21) |
|---|
| | 203 | char[] password = "anonymous@anonymous", uint port = 21) |
|---|
| 195 | 204 | in { |
|---|
| 196 | 205 | assert(server != ""); |
|---|
| … | … | |
| 208 | 217 | |
|---|
| 209 | 218 | public this(char[] server, char[] path, char[] username = "anonymous", |
|---|
| 210 | | char[] password = "anonymous@anonymous", uint port = 21, |
|---|
| 211 | | bool flat = false) |
|---|
| | 219 | char[] password = "anonymous@anonymous", uint port = 21, |
|---|
| | 220 | bool flat = false) |
|---|
| 212 | 221 | in { |
|---|
| 213 | 222 | assert(server != ""); |
|---|
| … | … | |
| 226 | 235 | |
|---|
| 227 | 236 | int opApply(int delegate(inout VfsFolder) dg) { |
|---|
| 228 | | FtpFolder ftp = new FtpFolder(toString_, name_, username_, password_, |
|---|
| 229 | | port_); |
|---|
| 230 | | return ftp.opApply(dg); |
|---|
| | 237 | if(!fileInfosSet_) { |
|---|
| | 238 | ftp_ = new FTPConnection(); |
|---|
| | 239 | ftp_.connect(toString_, username_, password_, port_); |
|---|
| | 240 | fileInfos_ = getFiles(ftp_, name_); |
|---|
| | 241 | ftp_.close(); |
|---|
| | 242 | fileInfosSet_ = true; |
|---|
| | 243 | } |
|---|
| | 244 | |
|---|
| | 245 | int result; |
|---|
| | 246 | |
|---|
| | 247 | foreach(FtpFileInfo fi; fileInfos_) { |
|---|
| | 248 | if(fi.type == FtpFileType.dir) { |
|---|
| | 249 | VfsFolder x = new FtpFolder(toString_, fi.name, username_, |
|---|
| | 250 | password_, port_); |
|---|
| | 251 | if((result = dg(x)) != 0) |
|---|
| | 252 | break; |
|---|
| | 253 | } |
|---|
| | 254 | } |
|---|
| | 255 | |
|---|
| | 256 | return result; |
|---|
| 231 | 257 | } |
|---|
| 232 | 258 | |
|---|
| 233 | 259 | uint files() { |
|---|
| 234 | | ftp_ = new FTPConnection(); |
|---|
| 235 | | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 236 | | FtpFileInfo[] fis = getFiles(ftp_); |
|---|
| 237 | | ftp_.close(); |
|---|
| 238 | | uint size; |
|---|
| 239 | | |
|---|
| 240 | | foreach(FtpFileInfo fi; fis) { |
|---|
| 241 | | if(fi.type == FtpFileType.unknown || fi.type == FtpFileType.file || fi.type == FtpFileType.other) |
|---|
| | 260 | if(!fileNumSet_) |
|---|
| | 261 | { |
|---|
| | 262 | ftp_ = new FTPConnection(); |
|---|
| | 263 | ftp_.connect(toString_, username_, password_, port_); |
|---|
| | 264 | FtpFileInfo[] fis = getFiles(ftp_); |
|---|
| | 265 | ftp_.close(); |
|---|
| | 266 | uint size; |
|---|
| | 267 | |
|---|
| | 268 | foreach(FtpFileInfo fi; fis) { |
|---|
| | 269 | if(fi.type == FtpFileType.unknown || fi.type == FtpFileType.file || fi.type == FtpFileType.other) |
|---|
| 242 | 270 | size++; |
|---|
| 243 | | } |
|---|
| 244 | | |
|---|
| 245 | | return size; |
|---|
| | 271 | } |
|---|
| | 272 | |
|---|
| | 273 | fileNum_ = size; |
|---|
| | 274 | fileNumSet_ = true; |
|---|
| | 275 | } |
|---|
| | 276 | |
|---|
| | 277 | return fileNum_; |
|---|
| 246 | 278 | } |
|---|
| 247 | 279 | |
|---|
| 248 | 280 | uint folders() { |
|---|
| 249 | | ftp_ = new FTPConnection(); |
|---|
| 250 | | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 251 | | FtpFileInfo[] fis = getFiles(ftp_); |
|---|
| 252 | | ftp_.close(); |
|---|
| 253 | | uint size; |
|---|
| 254 | | |
|---|
| 255 | | foreach(FtpFileInfo fi; fis) { |
|---|
| 256 | | if(fi.type == FtpFileType.dir) |
|---|
| 257 | | size++; |
|---|
| 258 | | } |
|---|
| 259 | | |
|---|
| 260 | | return size; |
|---|
| | 281 | if(!folderNumSet_) { |
|---|
| | 282 | uint size = 0; |
|---|
| | 283 | if(!flat_) { |
|---|
| | 284 | ftp_ = new FTPConnection(); |
|---|
| | 285 | ftp_.connect(toString_, username_, password_, port_); |
|---|
| | 286 | FtpFileInfo[] fis = getFiles(ftp_); |
|---|
| | 287 | ftp_.close(); |
|---|
| | 288 | |
|---|
| | 289 | foreach(FtpFileInfo fi; fis) { |
|---|
| | 290 | if(fi.type == FtpFileType.dir) |
|---|
| | 291 | size++; |
|---|
| | 292 | } |
|---|
| | 293 | } |
|---|
| | 294 | folderNum_ = size; |
|---|
| | 295 | folderNumSet_ = true; |
|---|
| | 296 | } |
|---|
| | 297 | |
|---|
| | 298 | return folderNum_; |
|---|
| 261 | 299 | } |
|---|
| 262 | 300 | |
|---|
| 263 | 301 | uint entries() { |
|---|
| 264 | | return files + folders; |
|---|
| | 302 | return files + folders; |
|---|
| 265 | 303 | } |
|---|
| 266 | 304 | |
|---|
| 267 | 305 | ulong bytes() { |
|---|
| 268 | | ftp_ = new FTPConnection(); |
|---|
| 269 | | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 270 | | FtpFileInfo[] fis = getFiles(ftp_); |
|---|
| 271 | | ftp_.close(); |
|---|
| 272 | | uint size; |
|---|
| 273 | | |
|---|
| 274 | | foreach(FtpFileInfo fi; fis) { |
|---|
| 275 | | if(fi.type == FtpFileType.unknown || fi.type == FtpFileType.file || fi.type == FtpFileType.other) |
|---|
| 276 | | size += fi.size; |
|---|
| 277 | | } |
|---|
| 278 | | |
|---|
| 279 | | return size; |
|---|
| | 306 | if(!bytesSet_) { |
|---|
| | 307 | ftp_ = new FTPConnection(); |
|---|
| | 308 | ftp_.connect(toString_, username_, password_, port_); |
|---|
| | 309 | FtpFileInfo[] fis = getFiles(ftp_); |
|---|
| | 310 | ftp_.close(); |
|---|
| | 311 | uint size; |
|---|
| | 312 | |
|---|
| | 313 | foreach(FtpFileInfo fi; fis) { |
|---|
| | 314 | if(fi.type == FtpFileType.unknown || fi.type == FtpFileType.file || fi.type == FtpFileType.other) |
|---|
| | 315 | size += fi.size; |
|---|
| | 316 | } |
|---|
| | 317 | |
|---|
| | 318 | bytes_ = size; |
|---|
| | 319 | bytesSet_ = true; |
|---|
| | 320 | } |
|---|
| | 321 | |
|---|
| | 322 | return bytesSet_; |
|---|
| | 323 | } |
|---|
| | 324 | |
|---|
| | 325 | VfsFolders subset(char[] pattern) { |
|---|
| | 326 | //to be done |
|---|
| 280 | 327 | } |
|---|
| 281 | 328 | |
|---|
| 282 | 329 | VfsFiles catalog(char[] pattern) { |
|---|
| 283 | | return new FtpFiles(toString_, name_, username_, password_, port_, |
|---|
| 284 | | pattern); |
|---|
| | 330 | return new FtpFiles(toString_, name_, username_, password_, port_, |
|---|
| | 331 | pattern); |
|---|
| 285 | 332 | } |
|---|
| 286 | 333 | |
|---|
| 287 | 334 | VfsFiles catalog(VfsFilter filter = null) { |
|---|
| 288 | | return new FtpFiles(toString_, name_, username_, password_, port_, |
|---|
| 289 | | filter); |
|---|
| 290 | | } |
|---|
| 291 | | |
|---|
| 292 | | VfsFolders subset(char[] pattern) { |
|---|
| 293 | | return null; |
|---|
| | 335 | return new FtpFiles(toString_, name_, username_, password_, port_, |
|---|
| | 336 | filter); |
|---|
| 294 | 337 | } |
|---|
| 295 | 338 | } |
|---|
| … | … | |
| 297 | 340 | class FtpFile: VfsFile { |
|---|
| 298 | 341 | |
|---|
| 299 | | FTPConnection ftp_; |
|---|
| 300 | | char[] toString_, name_, username_, password_; |
|---|
| 301 | | uint port_; |
|---|
| 302 | | bool writeable_, writeableSet_; |
|---|
| 303 | | |
|---|
| 304 | | public this(char[] server, char[] path, char[] username = "anonymous", |
|---|
| 305 | | char[] password = "anonymous@anonymous", uint port = 21) |
|---|
| 306 | | in { |
|---|
| 307 | | assert(server != ""); |
|---|
| 308 | | } |
|---|
| 309 | | out { |
|---|
| 310 | | assert(server != ""); |
|---|
| 311 | | } |
|---|
| 312 | | body { |
|---|
| 313 | | toString_ = server; |
|---|
| 314 | | name_ = path; |
|---|
| 315 | | username_ = username; |
|---|
| 316 | | password_ = password; |
|---|
| 317 | | port_ = port; |
|---|
| 318 | | } |
|---|
| 319 | | |
|---|
| 320 | 342 | char[] name() { |
|---|
| 321 | | return name_; |
|---|
| 322 | 343 | } |
|---|
| 323 | 344 | |
|---|
| 324 | 345 | char[] toString() { |
|---|
| 325 | | return toString_ ~ '/' ~ name_; |
|---|
| 326 | 346 | } |
|---|
| 327 | 347 | |
|---|
| 328 | 348 | bool exists() { |
|---|
| 329 | | ftp_ = new FTPConnection(); |
|---|
| 330 | | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 331 | | bool result = ftp_.exist(name_) == 1 ? true : false; |
|---|
| 332 | | ftp_.close(); |
|---|
| 333 | | return result; |
|---|
| 334 | 349 | } |
|---|
| 335 | 350 | |
|---|
| 336 | 351 | ulong size() { |
|---|
| 337 | | ulong result = 0; |
|---|
| 338 | | if(exists()) { |
|---|
| 339 | | ftp_ = new FTPConnection(); |
|---|
| 340 | | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 341 | | result = ftp_.getFileInfo(name_).size; |
|---|
| 342 | | ftp_.close(); |
|---|
| 343 | | } |
|---|
| 344 | | return result; |
|---|
| 345 | 352 | } |
|---|
| 346 | 353 | |
|---|
| 347 | 354 | VfsFile copy(VfsFile source) { |
|---|
| 348 | | void[] dest; |
|---|
| 349 | | source.input.read(dest); |
|---|
| 350 | | output.write(dest); |
|---|
| 351 | | return new FtpFile(toString_, name_, username_, password_, port_); |
|---|
| 352 | 355 | } |
|---|
| 353 | 356 | |
|---|
| 354 | 357 | VfsFile move(VfsFile source) { |
|---|
| 355 | | copy(source); |
|---|
| 356 | | source.remove; |
|---|
| 357 | | return new FtpFile(toString_, name_, username_, password_, port_); |
|---|
| 358 | 358 | } |
|---|
| 359 | 359 | |
|---|
| 360 | 360 | VfsFile create() { |
|---|
| 361 | | VfsFolder f = new FileFolder(""); |
|---|
| 362 | | VfsFile ff = f.file("/NF.NF"); |
|---|
| 363 | | ftp_ = new FTPConnection(); |
|---|
| 364 | | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 365 | | ftp_.put(name_, ff.input); |
|---|
| 366 | | ftp_.close(); |
|---|
| 367 | | ff.remove(); |
|---|
| 368 | | return new FtpFile(toString_, name_, username_, password_, port_); |
|---|
| 369 | 361 | } |
|---|
| 370 | 362 | |
|---|
| 371 | 363 | VfsFile create(InputStream stream) { |
|---|
| 372 | | ftp_ = new FTPConnection(); |
|---|
| 373 | | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 374 | | ftp_.put(name_, stream); |
|---|
| 375 | | ftp_.close(); |
|---|
| 376 | | return new FtpFile(toString_, name_, username_, password_, port_); |
|---|
| 377 | 364 | } |
|---|
| 378 | 365 | |
|---|
| 379 | 366 | VfsFile remove() { |
|---|
| 380 | | ftp_ = new FTPConnection(); |
|---|
| 381 | | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 382 | | ftp_.del(name_); |
|---|
| 383 | | return new FtpFile(toString_, name_, username_, password_, port_); |
|---|
| 384 | 367 | } |
|---|
| 385 | 368 | |
|---|
| 386 | 369 | InputStream input() { |
|---|
| 387 | | ftp_ = new FTPConnection(); |
|---|
| 388 | | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 389 | | InputStream input = ftp_.input; |
|---|
| 390 | | prepareInputStream(ftp_, input, FtpCommand.RETR, ["", name_]); |
|---|
| 391 | | ftp_.close(); |
|---|
| 392 | | return input; |
|---|
| 393 | 370 | } |
|---|
| 394 | 371 | |
|---|
| 395 | 372 | OutputStream output() { |
|---|
| 396 | | ftp_ = new FTPConnection(); |
|---|
| 397 | | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 398 | | OutputStream output = ftp_.output; |
|---|
| 399 | | prepareOutputStream(ftp_, output, FtpCommand.STOR, ["", name_]); |
|---|
| 400 | | ftp_.close(); |
|---|
| 401 | | return output; |
|---|
| 402 | 373 | } |
|---|
| 403 | 374 | |
|---|
| 404 | 375 | VfsFile dup() { |
|---|
| 405 | | return new FtpFile(toString_, name_, username_, password_, port_); |
|---|
| 406 | 376 | } |
|---|
| 407 | 377 | } |
|---|
| … | … | |
| 409 | 379 | class FtpFiles: VfsFiles { |
|---|
| 410 | 380 | |
|---|
| 411 | | FTPConnection ftp_; |
|---|
| 412 | | char[] toString_, name_, username_, password_; |
|---|
| 413 | | uint port_; |
|---|
| 414 | | bool writeable_, writeableSet_; |
|---|
| 415 | | char[] filter_; |
|---|
| 416 | | VfsFilter ffilter_; |
|---|
| 417 | | bool useFilter_ = false; |
|---|
| 418 | | bool doFilter_ = true; |
|---|
| 419 | | |
|---|
| 420 | | public this(char[] server, char[] path, char[] username = "anonymous", |
|---|
| 421 | | char[] password = "anonymous@anonymous", uint port = 21, |
|---|
| 422 | | char[] filtPattern = "") |
|---|
| 423 | | in { |
|---|
| 424 | | assert(server != ""); |
|---|
| 425 | | } |
|---|
| 426 | | out { |
|---|
| 427 | | assert(server != ""); |
|---|
| 428 | | } |
|---|
| 429 | | body { |
|---|
| 430 | | toString_ = server; |
|---|
| 431 | | name_ = path != "" ? path : server; |
|---|
| 432 | | username_ = username; |
|---|
| 433 | | password_ = password; |
|---|
| 434 | | port_ = port; |
|---|
| 435 | | filter_ = filtPattern; |
|---|
| 436 | | |
|---|
| 437 | | useFilter_ = true; |
|---|
| 438 | | } |
|---|
| 439 | | |
|---|
| 440 | | public this(char[] server, char[] path, char[] username = "anonymous", |
|---|
| 441 | | char[] password = "anonymous@anonymous", uint port = 21, |
|---|
| 442 | | VfsFilter filter = null) |
|---|
| 443 | | in { |
|---|
| 444 | | assert(server != ""); |
|---|
| 445 | | } |
|---|
| 446 | | out { |
|---|
| 447 | | assert(server != ""); |
|---|
| 448 | | } |
|---|
| 449 | | body { |
|---|
| 450 | | toString_ = server; |
|---|
| 451 | | name_ = path != "" ? path : server; |
|---|
| 452 | | username_ = username; |
|---|
| 453 | | password_ = password; |
|---|
| 454 | | port_ = port; |
|---|
| 455 | | ffilter_ = filter; |
|---|
| 456 | | } |
|---|
| 457 | | |
|---|
| 458 | 381 | int opApply(int delegate(inout VfsFile) dg) { |
|---|
| 459 | | int result; |
|---|
| 460 | | foreach(FtpFileInfo inf; getFileInfo()) { |
|---|
| 461 | | VfsFile x = new FtpFile(toString_, inf.name, username_, password_, |
|---|
| 462 | | port_); |
|---|
| 463 | | if((result = dg(x)) != 0) |
|---|
| 464 | | break; |
|---|
| 465 | | } |
|---|
| 466 | | return result; |
|---|
| 467 | | } |
|---|
| 468 | | |
|---|
| 469 | | FtpFileInfo[] getFileInfo() { |
|---|
| 470 | | ftp_ = new FTPConnection(); |
|---|
| 471 | | ftp_.connect(toString_, username_, password_, port_); |
|---|
| 472 | | scope(exit) { |
|---|
| 473 | | ftp_.close(); |
|---|
| 474 | | } |
|---|
| 475 | | if(doFilter_) { |
|---|
| 476 | | FtpFileInfo[] infs = getFiles(ftp_); |
|---|
| 477 | | FtpFileInfo[] toReturn; |
|---|
| 478 | | if(useFilter_) { |
|---|
| 479 | | foreach(FtpFileInfo inf; infs) { |
|---|
| 480 | | if(containsPattern(inf.name, filter_)) { |
|---|
| 481 | | toReturn ~= inf; |
|---|
| 482 | | } |
|---|
| 483 | | } |
|---|
| 484 | | } else { |
|---|
| 485 | | VfsFilterInfo[] toUse; |
|---|
| 486 | | foreach(FtpFileInfo inf; infs) { |
|---|
| 487 | | VfsFilterInfo temp; |
|---|
| 488 | | temp.name = inf.name; |
|---|
| 489 | | temp.folder = false; |
|---|
| 490 | | temp.path = toString_ ~ temp.name; |
|---|
| 491 | | temp.bytes = inf.size; |
|---|
| 492 | | toUse ~= temp; |
|---|
| 493 | | } |
|---|
| 494 | | foreach(VfsFilterInfo inf; toUse) { |
|---|
| 495 | | if(ffilter_(&inf)) { |
|---|
| 496 | | FtpFileInfo temp; |
|---|
| 497 | | temp.name = inf.name; |
|---|
| 498 | | toReturn ~= temp; |
|---|
| 499 | | } |
|---|
| 500 | | } |
|---|
| 501 | | } |
|---|
| 502 | | return toReturn; |
|---|
| 503 | | } else |
|---|
| 504 | | return getFiles(ftp_); |
|---|
| 505 | 382 | } |
|---|
| 506 | 383 | |
|---|
| 507 | 384 | uint files() { |
|---|
| 508 | | return getFileInfo().length; |
|---|
| 509 | 385 | } |
|---|
| 510 | 386 | |
|---|
| 511 | 387 | ulong bytes() { |
|---|
| 512 | | ulong result; |
|---|
| 513 | | foreach(FtpFileInfo inf; getFileInfo()) { |
|---|
| 514 | | result += inf.size; |
|---|
| 515 | | } |
|---|
| 516 | | return result; |
|---|
| 517 | | } |
|---|
| 518 | | } |
|---|
| 519 | | |
|---|
| 520 | | debug(UnitTest) { |
|---|
| 521 | | /* |
|---|
| 522 | | * |
|---|
| 523 | | */ |
|---|
| 524 | | |
|---|
| 525 | | import tango.io.vfs.model.Vfs; |
|---|
| 526 | | import FtpFolder; |
|---|
| 527 | | import tango.net.ftp.FtpClient; |
|---|
| 528 | | import tango.io.Stdout; |
|---|
| 529 | | import tango.util.Convert; |
|---|
| 530 | | |
|---|
| 531 | | int main() { |
|---|
| 532 | | |
|---|
| 533 | | //variables |
|---|
| 534 | | |
|---|
| 535 | | VfsFolder folder; |
|---|
| 536 | | VfsFile file; |
|---|
| 537 | | VfsFolders folders; |
|---|
| 538 | | VfsFiles files; |
|---|
| 539 | | VfsFolderEntry entry; |
|---|
| 540 | | char[] toPrint; |
|---|
| 541 | | |
|---|
| 542 | | //init code |
|---|
| 543 | | |
|---|
| 544 | | entry = new FtpFolderEntry("127.0.0.1", "", "lmartin92", "asunset!", 21); |
|---|
| 545 | | Stdout("Inited entry.").newline.flush; |
|---|
| 546 | | folder = entry.open(); |
|---|
| 547 | | //folder = new FtpFolder("ftp.digitalmars.com", ""); |
|---|
| 548 | | Stdout("Inited folder.").newline.flush; |
|---|
| 549 | | folders = folder.tree(); |
|---|
| 550 | | Stdout("Inited tree.").newline.flush; |
|---|
| 551 | | file = folder.file("client.exe"); |
|---|
| 552 | | Stdout("Inited file.").newline.flush; |
|---|
| 553 | | files = folders.catalog("cli"); |
|---|
| 554 | | Stdout("Inited catalog.").newline.flush; |
|---|
| 555 | | Stdout("Initialization Passed.").newline.newline.flush; |
|---|
| 556 | | |
|---|
| 557 | | //print information gatherable from all of |
|---|
| 558 | | //the variables |
|---|
| 559 | | |
|---|
| 560 | | //entry |
|---|
| 561 | | Stdout("Folder exists: ").flush; |
|---|
| 562 | | toPrint = to!(char[])(entry.exists()); |
|---|
| 563 | | Stdout(toPrint).newline.flush; |
|---|
| 564 | | |
|---|
| 565 | | //folder |
|---|
| 566 | | Stdout("Folder:").newline.flush; |
|---|
| 567 | | toPrint = "\n\t writeable: " ~ to!(char[])(folder.writable()); |
|---|
| 568 | | toPrint ~= "\n\t name: " ~ folder.name(); |
|---|
| 569 | | toPrint ~= "\n\t path: " ~ folder.toString(); |
|---|
| 570 | | Stdout(toPrint).newline.flush; |
|---|
| 571 | | |
|---|
| 572 | | //folders |
|---|
| 573 | | Stdout("Folders: ").newline.flush; |
|---|
| 574 | | toPrint = "\n\t #of Files: " ~ to!(char[])(folders.files()); |
|---|
| 575 | | toPrint ~= "\n\t #of Folders: " ~ to!(char[])(folders.folders()); |
|---|
| 576 | | toPrint ~= "\n\t Total Objects: " ~ to!(char[])(folders.entries()); |
|---|
| 577 | | toPrint ~= "\n\t Folders Occupies Space: " ~ to!(char[])( |
|---|
| 578 | | folders.bytes()); |
|---|
| 579 | | Stdout(toPrint).newline.flush; |
|---|
| 580 | | |
|---|
| 581 | | //file |
|---|
| 582 | | Stdout("File: ").newline.flush; |
|---|
| 583 | | toPrint = "\n\t exists: " ~ to!(char[])(file.exists()); |
|---|
| 584 | | toPrint ~= "\n\t size: " ~ to!(char[])(file.size()); |
|---|
| 585 | | toPrint ~= "\n\t name: " ~ file.name(); |
|---|
| 586 | | toPrint ~= "\n\t path: " ~ file.toString(); |
|---|
| 587 | | Stdout(toPrint).newline.flush; |
|---|
| 588 | | |
|---|
| 589 | | //files |
|---|
| 590 | | Stdout("Files: ").newline.flush; |
|---|
| 591 | | Stdout("\n\t Size sum: "); |
|---|
| 592 | | Stdout(files.bytes()); |
|---|
| 593 | | Stdout("\n\t #of Files: ").flush; |
|---|
| 594 | | Stdout(files.files()).newline.flush; |
|---|
| 595 | | |
|---|
| 596 | | return 0; |
|---|
| 597 | | } |
|---|
| 598 | | } |
|---|
| | 388 | } |
|---|
| | 389 | } |
|---|
| r3481 |
r3484 |
|
| 32 | 32 | debug(UnitTest) { |
|---|
| 33 | 33 | import tango.io.Stdout; |
|---|
| 34 | | import tango.io.FileSystem; |
|---|
| | 34 | import tango.io.FileSystem; |
|---|
| | 35 | import tango.core.thread; |
|---|
| | 36 | import tango.io.stream.SnoopStream; |
|---|
| 35 | 37 | } |
|---|
| 36 | 38 | } |
|---|
| … | … | |
| 326 | 328 | |
|---|
| 327 | 329 | public void close() { |
|---|
| 328 | | //make sure no open data connection and if open data connection then kill |
|---|
| 329 | | if(dataSocket_ !is null) |
|---|
| 330 | | this.finishDataCommand(dataSocket_); |
|---|
| | 330 | //make sure no open data connection and if open data connection then kill |
|---|
| | 331 | if(dataSocket_ !is null) |
|---|
| | 332 | this.finishDataCommand(dataSocket_); |
|---|
| 331 | 333 | if(socket_ !is null) { |
|---|
| 332 | 334 | try { |
|---|
| … | … | |
| 673 | 675 | /// Returns: the data socket or a listener |
|---|
| 674 | 676 | protected SocketConduit getDataSocket() { |
|---|
| 675 | | //make sure no open data connection and if open data connection then kill |
|---|
| 676 | | if(dataSocket_ !is null) |
|---|
| 677 | | this.finishDataCommand(dataSocket_); |
|---|
| 678 | | |
|---|
| | 677 | //make sure no open data connection and if open data connection then kill |
|---|
| | 678 | if(dataSocket_ !is null) |
|---|
| | 679 | this.finishDataCommand(dataSocket_); |
|---|
| | 680 | |
|---|
| 679 | 681 | // What type are we using? |
|---|
| 680 | 682 | switch(this.inf_.type) { |
|---|
| … | … | |
| 892 | 894 | |
|---|
| 893 | 895 | // We shouldn't get a 250 in STREAM mode. |
|---|
| 894 | | FtpResponse r = readResponse(); |
|---|
| 895 | | if(!(r.code == "226" || r.code == "420")) |
|---|
| 896 | | exception("Bad finish"); |
|---|
| 897 | | |
|---|
| | 896 | FtpResponse r = readResponse(); |
|---|
| | 897 | if(!(r.code == "226" || r.code == "420")) |
|---|
| | 898 | exception("Bad finish"); |
|---|
| | 899 | |
|---|
| 898 | 900 | } |
|---|
| 899 | 901 | |
|---|
| … | … | |
| 1536 | 1538 | } |
|---|
| 1537 | 1539 | |
|---|
| 1538 | | public InputStream input(char[] path){ |
|---|
| 1539 | | dataSocket_ = this.processDataCommand("RETR", path); |
|---|
| 1540 | | return dataSocket_; |
|---|
| 1541 | | } |
|---|
| 1542 | | |
|---|
| 1543 | | public OutputStream output(char[] path){ |
|---|
| 1544 | | dataSocket_ = this.processDataCommand("STOR", path); |
|---|
| 1545 | | return dataSocket_; |
|---|
| 1546 | | } |
|---|
| | 1540 | public InputStream input(char[] path) { |
|---|
| | 1541 | dataSocket_ = this.processDataCommand("RETR", path); |
|---|
| | 1542 | return dataSocket_; |
|---|
| | 1543 | } |
|---|
| | 1544 | |
|---|
| | 1545 | public OutputStream output(char[] path) { |
|---|
| | 1546 | dataSocket_ = this.processDataCommand("STOR", path); |
|---|
| | 1547 | return dataSocket_; |
|---|
| | 1548 | } |
|---|
| 1547 | 1549 | |
|---|
| 1548 | 1550 | } |
|---|
| … | … | |
| 1554 | 1556 | |
|---|
| 1555 | 1557 | void main() { |
|---|
| 1556 | | auto layout = new Locale; |
|---|
| 1557 | | FileSystem.setDirectory(FileSystem.toAbsolute("Z:/")); |
|---|
| 1558 | | auto fp = new FilePath(FileSystem.toAbsolute("Z:/")); |
|---|
| | 1558 | |
|---|
| | 1559 | auto layout = new Locale; |
|---|
| | 1560 | FileSystem.setDirectory(FileSystem.toAbsolute("Z:/")); |
|---|
| | 1561 | auto fp = new FilePath(FileSystem.toAbsolute("Z:/")); |
|---|
| | 1562 | |
|---|
| | 1563 | Stdout("Testing the new parts in FtpClient.d").newline.flush; |
|---|
| | 1564 | FTPConnection conn = new FTPConnection("localhost", "me", "pass", 21); |
|---|
| | 1565 | Stdout("Connecting works!").newline.flush; |
|---|
| | 1566 | |
|---|
| | 1567 | Stdout("Making directory SS.").newline.flush; |
|---|
| | 1568 | conn.mkdir("/SS"); |
|---|
| | 1569 | |
|---|
| | 1570 | Stdout("Setting current directory to SS.").newline.flush; |
|---|
| | 1571 | conn.cd("/SS"); |
|---|
| | 1572 | |
|---|
| | 1573 | Stdout("Setting current directory to parent directory.").newline.flush; |
|---|
| | 1574 | conn.cdup(); |
|---|
| | 1575 | |
|---|
| | 1576 | Stdout("Removing directory SS.").newline.flush; |
|---|
| | 1577 | conn.rm("/SS"); |
|---|
| | 1578 | |
|---|
| | 1579 | Stdout("Checking for the existence of a file.").newline.flush; |
|---|
| | 1580 | if(conn.exist("ServingSchedule.tif")) { |
|---|
| | 1581 | Stdout("File exists.").newline.flush; |
|---|
| | 1582 | Stdout("Getting size of file.").newline.flush; |
|---|
| | 1583 | long size = conn.size("ServingSchedule.tif"); |
|---|
| | 1584 | Stdout("The size of the file is: "); |
|---|
| | 1585 | Stdout(size).newline.flush; |
|---|
| | 1586 | Stdout("Renaming the file to No.").newline.flush; |
|---|
| | 1587 | conn.rename("ServingSchedule.tif", "No"); |
|---|
| | 1588 | Stdout("Getting the mod date.").newline.flush; |
|---|
| | 1589 | Time modified = conn.modified("No"); |
|---|
| | 1590 | Stdout("The file was modified: "); |
|---|
| | 1591 | Stdout(layout("{:ddd, dd MMMM yyyy HH:mm:ss z}", modified)).newline.flush; |
|---|
| | 1592 | Stdout("Deleting the file No.").newline.flush; |
|---|
| | 1593 | conn.del("No"); |
|---|
| | 1594 | } else |
|---|
| | 1595 | Stdout("The file does not exist.").newline.flush; |
|---|
| | 1596 | |
|---|
| | 1597 | Stdout("Listing content.").newline.flush; |
|---|
| | 1598 | FtpFileInfo[] infs = conn.ls(); |
|---|
| | 1599 | foreach(FtpFileInfo inf; infs) { |
|---|
| | 1600 | Stdout(inf.name).newline.flush; |
|---|
| | 1601 | } |
|---|
| | 1602 | |
|---|
| | 1603 | Stdout("Now getting a file and writing it to a place.").newline.flush; |
|---|
| | 1604 | conn.get("/Backup/ServingSchedule.tif", "/newFile.whatever"); |
|---|
| | 1605 | |
|---|
| | 1606 | Stdout("Now putting a file to the server.").newline.flush; |
|---|
| | 1607 | conn.put("ServingSchedule.tif", "/newFile.whatever"); |
|---|
| | 1608 | |
|---|
| | 1609 | |
|---|
| | 1610 | |
|---|
| | 1611 | Stdout("Testing input.").newline.flush; |
|---|
| | 1612 | FileConduit fcon = new FileConduit("Z:/whatever", |
|---|
| | 1613 | FileConduit.ReadWriteCreate); |
|---|
| | 1614 | fcon.copy(new SnoopInput(conn.input("/Backup/ServingSchedule.tif"))); |
|---|
| | 1615 | |
|---|
| | 1616 | fcon.close(); |
|---|
| | 1617 | fcon = new FileConduit("Z:/whatever", FileConduit.ReadExisting); |
|---|
| | 1618 | Stdout("Testing output.").newline.flush; |
|---|
| | 1619 | auto os = conn.output("/ServingSchedule.tif"); |
|---|
| | 1620 | os.copy(new SnoopInput(fcon)); |
|---|
| 1559 | 1621 | |
|---|
| 1560 | | Stdout("Testing the new parts in FtpClient.d").newline.flush; |
|---|
| 1561 | | FTPConnection conn = new FTPConnection("localhost", "me", "pass", 21); |
|---|
| 1562 | | Stdout("Connecting works!").newline.flush; |
|---|
| 1563 | | |
|---|
| 1564 | | Stdout("Making directory SS.").newline.flush; |
|---|
| 1565 | | conn.mkdir("/SS"); |
|---|
| 1566 | | |
|---|
| 1567 | | Stdout("Setting current directory to SS.").newline.flush; |
|---|
| 1568 | | conn.cd("/SS"); |
|---|
| 1569 | | |
|---|
| 1570 | | Stdout("Setting current directory to parent directory.").newline.flush; |
|---|
| 1571 | | conn.cdup(); |
|---|
| 1572 | | |
|---|
| 1573 | | Stdout("Removing directory SS.").newline.flush; |
|---|
| 1574 | | conn.rm("/SS"); |
|---|
| 1575 | | |
|---|
| 1576 | | Stdout("Checking for the existence of a file.").newline.flush; |
|---|
| 1577 | | if(conn.exist("ServingSchedule.tif")) { |
|---|
| 1578 | | Stdout("File exists.").newline.flush; |
|---|
| 1579 | | Stdout("Getting size of file.").newline.flush; |
|---|
| 1580 | | long size = conn.size("ServingSchedule.tif"); |
|---|
| 1581 | | Stdout("The size of the file is: "); |
|---|
| 1582 | | Stdout(size).newline.flush; |
|---|
| 1583 | | Stdout("Renaming the file to No.").newline.flush; |
|---|
| 1584 | | conn.rename("ServingSchedule.tif", "No"); |
|---|
| 1585 | | Stdout("Getting the mod date.").newline.flush; |
|---|
| 1586 | | Time modified = conn.modified("No"); |
|---|
| 1587 | | Stdout("The file was modified: "); |
|---|
| 1588 | | Stdout(layout("{:ddd, dd MMMM yyyy HH:mm:ss z}", modified)).newline.flush; |
|---|
| 1589 | | Stdout("Deleting the file No.").newline.flush; |
|---|
| 1590 | | conn.del("No"); |
|---|
| 1591 | | } else |
|---|
| 1592 | | Stdout("The file does not exist.").newline.flush; |
|---|
| 1593 | | |
|---|
| 1594 | | Stdout("Listing content.").newline.flush; |
|---|
| 1595 | | FtpFileInfo[] infs = conn.ls(); |
|---|
| 1596 | | foreach(FtpFileInfo inf; infs) { |
|---|
| 1597 | | Stdout(inf.name).newline.flush; |
|---|
| 1598 | | } |
|---|
| 1599 | | |
|---|
| 1600 | | Stdout("Now getting a file and writing it to a place.").newline.flush; |
|---|
| 1601 | | conn.get("/Backup/ServingSchedule.tif", "/newFile.whatever"); |
|---|
| 1602 | | |
|---|
| 1603 | | Stdout("Now putting a file to the server.").newline.flush; |
|---|
| 1604 | | conn.put("ServingSchedule.tif", "/newFile.whatever"); |
|---|
| 1605 | | |
|---|
| 1606 | | Stdout("Testing input.").newline.flush; |
|---|
| 1607 | | FileConduit fcon = new FileConduit("Z:/whatever", FileConduit.ReadWriteCreate); |
|---|
| 1608 | | fcon.copy(conn.input("/Backup/ServingSchedule.tif")); |
|---|
| 1609 | | |
|---|
| 1610 | | Stdout("Testing output.").newline.flush; |
|---|
| 1611 | | OutputStream os = conn.output("ServingSchedule.tif"); |
|---|
| 1612 | | os.copy(fcon.input()); |
|---|
| 1613 | | |
|---|
| 1614 | | conn.close(); |
|---|
| 1615 | | fcon.close(); |
|---|
| 1616 | | fp = fp.file("/newFile.whatever"); |
|---|
| 1617 | | fp.remove(); |
|---|
| 1618 | | Stdout("Finished.").newline.flush; |
|---|
| 1619 | | } |
|---|
| | 1622 | os.close; |
|---|
| | 1623 | conn.close(); |
|---|
| | 1624 | fcon.close(); |
|---|
| | 1625 | fp = fp.file("/newFile.whatever"); |
|---|
| | 1626 | fp.remove(); |
|---|
| | 1627 | Stdout("Finished.").newline.flush; |
|---|
| | 1628 | |
|---|
| | 1629 | } |
|---|
| 1620 | 1630 | } |
|---|
Download in other formats:
|
 |