1 16 package net.sf.jftp.net; 17 18 import java.io.BufferedInputStream ; 19 import java.io.BufferedOutputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.StreamTokenizer ; 26 import java.util.Date ; 27 import java.util.Vector ; 28 29 import net.sf.jftp.config.Settings; 30 import net.sf.jftp.system.StringUtils; 31 import net.sf.jftp.system.logging.Log; 32 33 34 public class FilesystemConnection implements BasicConnection 35 { 36 public static int filesystemBuffer = 128000; 37 private String path = ""; 38 private String pwd = ""; 39 private Vector listeners = new Vector (); 40 private String [] files; 41 private String [] size = new String [0]; 42 private int[] perms = null; 43 private String baseFile; 44 private int fileCount; 45 private boolean shortProgress = false; 46 public Vector dateVector = new Vector (); 47 48 public FilesystemConnection() 49 { 50 } 51 52 public FilesystemConnection(String path, ConnectionListener l) 53 { 54 listeners.add(l); 55 chdir(path); 56 } 57 58 public int removeFileOrDir(String file) 59 { 60 try 61 { 62 if((file == null) || file.equals("")) 63 { 64 return -1; 65 } 66 67 String tmp = file; 68 69 if(StringUtils.isRelative(file)) 70 { 71 tmp = getPWD() + file; 72 } 73 74 File f = new File (tmp); 75 76 if(!f.getAbsolutePath().equals(f.getCanonicalPath())) 77 { 78 if(!f.delete()) 82 { 83 return -1; 84 } 85 } 86 87 if(f.exists() && f.isDirectory()) 88 { 89 cleanLocalDir(tmp); 90 } 91 92 if(!f.delete()) 94 { 95 Log.debug("Removal failed."); 96 97 return -1; 98 } 99 } 100 catch(IOException ex) 101 { 102 Log.debug("Error: " + ex.toString()); 103 ex.printStackTrace(); 104 } 105 106 return -1; 107 } 108 109 private void cleanLocalDir(String dir) 110 { 111 try 112 { 113 dir = dir.replace('\\', '/'); 114 115 if(!dir.endsWith("/")) 116 { 117 dir = dir + "/"; 118 } 119 120 File f2 = new File (dir); 123 String [] tmp = f2.list(); 124 125 if(tmp == null) 126 { 127 return; 128 } 129 130 for(int i = 0; i < tmp.length; i++) 131 { 132 File f3 = new File (dir + tmp[i]); 133 134 if(!f3.getAbsolutePath().equals(f3.getCanonicalPath())) 135 { 136 f3.delete(); 138 139 } 142 143 if(f3.isDirectory()) 144 { 145 cleanLocalDir(dir + tmp[i]); 147 f3.delete(); 148 } 149 else 150 { 151 f3.delete(); 153 } 154 } 155 } 156 catch(IOException ex) 157 { 158 Log.debug("Error: " + ex.toString()); 159 ex.printStackTrace(); 160 } 161 } 162 163 public void sendRawCommand(String cmd) 164 { 165 } 166 167 public void disconnect() 168 { 169 } 170 171 public boolean isConnected() 172 { 173 return true; 174 } 175 176 public String getPWD() 177 { 178 return pwd; 179 } 180 181 public boolean cdup() 182 { 183 return chdir(pwd.substring(0, pwd.lastIndexOf("/") + 1)); 184 } 185 186 public boolean mkdir(String dirName) 187 { 188 if(StringUtils.isRelative(dirName)) 189 { 190 dirName = getPWD() + dirName; 191 } 192 193 File f = new File (dirName); 194 195 boolean x = f.mkdir(); 196 fireDirectoryUpdate(); 197 198 return x; 199 } 200 201 public void list() throws IOException 202 { 203 } 204 205 public boolean chdir(String p) 206 { 207 String p2 = processPath(p); 208 209 if(p2 == null) 210 { 211 return false; 212 } 213 214 File f = new File (p2); 215 216 if(!f.exists() || !f.isDirectory() || !f.canRead()) 217 { 218 Log.debug("Access denied."); 219 220 return false; 221 } 222 223 pwd = p2; 224 225 fireDirectoryUpdate(); 226 227 return true; 228 } 229 230 public boolean chdirNoRefresh(String p) 231 { 232 String p2 = processPath(p); 233 234 if(p2 == null) 235 { 236 return false; 237 } 238 239 pwd = p2; 241 242 return true; 243 } 244 245 public String getLocalPath() 246 { 247 return path; 249 } 250 251 public String processPath(String p) 252 { 253 p = p.replace('\\', '/'); 254 255 if(StringUtils.isRelative(p)) 257 { 258 p = pwd + p; 259 } 260 261 p = p.replace('\\', '/'); 262 263 File f = new File (p); 265 String p2; 266 267 if(f.exists()) 268 { 269 try 270 { 271 p2 = f.getCanonicalPath(); 272 p2 = p2.replace('\\', '/'); 273 274 if(!p2.endsWith("/")) 275 { 276 p2 = p2 + "/"; 277 } 278 279 return p2; 280 } 281 catch(IOException ex) 282 { 283 Log.debug("Error: can not get pathname (processPath)!"); 284 285 return null; 286 } 287 } 288 else 289 { 290 Log.debug("(processpPath) No such path: \"" + p + "\""); 291 292 return null; 293 } 294 } 295 296 public boolean setLocalPath(String p) 297 { 298 p = p.replace('\\', '/'); 299 300 if(StringUtils.isRelative(p)) 302 { 303 p = path + p; 304 } 305 306 p = p.replace('\\', '/'); 307 308 File f = new File (p); 310 311 if(f.exists()) 312 { 313 try 314 { 315 path = f.getCanonicalPath(); 316 path = path.replace('\\', '/'); 317 318 if(!path.endsWith("/")) 319 { 320 path = path + "/"; 321 } 322 323 } 325 catch(IOException ex) 326 { 327 Log.debug("Error: can not get pathname (local)!"); 328 329 return false; 330 } 331 } 332 else 333 { 334 Log.out("(local) obsolete setLocalDir: \"" + p + "\""); 335 336 return false; 337 } 338 339 return true; 340 } 341 342 public String [] sortLs() 343 { 344 dateVector = new Vector (); 345 346 File f = new File (pwd); 347 files = f.list(); 348 349 if(files == null) 350 { 351 return new String [0]; 352 } 353 354 size = new String [files.length]; 355 perms = new int[files.length]; 356 357 int accessible = 0; 358 359 for(int i = 0; i < files.length; i++) 360 { 361 File f2 = new File (pwd + files[i]); 362 363 if(f2.isDirectory() && !files[i].endsWith("/")) 364 { 365 files[i] = files[i] + "/"; 366 } 367 368 size[i] = "" + new File (pwd + files[i]).length(); 369 370 if(f2.canWrite()) 371 { 372 accessible = FtpConnection.W; 373 } 374 else if(f2.canRead()) 375 { 376 accessible = FtpConnection.R; 377 } 378 else 379 { 380 accessible = FtpConnection.DENIED; 381 } 382 383 perms[i] = accessible; 384 385 Date d = new Date (f2.lastModified()); 387 388 dateVector.add(d); 390 } 391 392 return files; 393 } 394 395 public String [] sortSize() 396 { 397 return size; 398 } 399 400 public int[] getPermissions() 401 { 402 return perms; 403 } 404 405 public int handleDownload(String file) 406 { 407 transfer(file); 408 409 return 0; 410 } 411 412 public int handleUpload(String file) 413 { 414 transfer(file); 415 416 return 0; 417 } 418 419 public int download(String file) 420 { 421 transfer(file); 422 423 return 0; 424 } 425 426 public int upload(String file) 427 { 428 transfer(file); 429 430 return 0; 431 } 432 433 private void transferDir(String dir, String out) 434 { 435 fileCount = 0; 436 shortProgress = true; 437 baseFile = StringUtils.getDir(dir); 438 439 File f2 = new File (dir); 440 String [] tmp = f2.list(); 441 442 if(tmp == null) 443 { 444 return; 445 } 446 447 File fx = new File (out); 448 449 if(!fx.mkdir()) 450 { 451 Log.debug("Can not create directory: " + out + 452 " - already exist or permission denied?"); 453 } 454 455 for(int i = 0; i < tmp.length; i++) 456 { 457 tmp[i] = tmp[i].replace('\\', '/'); 458 459 File f3 = new File (dir + tmp[i]); 461 462 if(f3.isDirectory()) 463 { 464 if(!tmp[i].endsWith("/")) 465 { 466 tmp[i] = tmp[i] + "/"; 467 } 468 469 transferDir(dir + tmp[i], out + tmp[i]); 470 } 471 else 472 { 473 fireProgressUpdate(baseFile, 474 DataConnection.GETDIR + ":" + fileCount, -1); 475 work(dir + tmp[i], out + tmp[i]); 476 } 477 } 478 479 fireProgressUpdate(baseFile, 480 DataConnection.DFINISHED + ":" + fileCount, -1); 481 shortProgress = false; 482 } 483 484 private void transfer(String file) 485 { 486 String out = StringUtils.getDir(file); 487 488 if(StringUtils.isRelative(file)) 489 { 490 file = getPWD() + file; 491 } 492 493 file = file.replace('\\', '/'); 494 out = out.replace('\\', '/'); 495 496 String outfile = StringUtils.getFile(file); 497 498 if(file.endsWith("/")) 499 { 500 transferDir(file, getLocalPath() + out); 501 502 return; 503 } 504 else 505 { 506 work(file, getLocalPath() + outfile); 507 } 508 } 509 510 private void work(String file, String outfile) 511 { 512 try 513 { 514 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (outfile)); 515 BufferedInputStream in = new BufferedInputStream (new FileInputStream (file)); 516 byte[] buf = new byte[filesystemBuffer]; 517 int len = 0; 518 int reallen = 0; 519 520 while(true) 522 { 523 len = in.read(buf); 524 525 if(len == StreamTokenizer.TT_EOF) 527 { 528 break; 529 } 530 531 out.write(buf, 0, len); 532 533 reallen += len; 534 fireProgressUpdate(StringUtils.getFile(file), 535 DataConnection.GET, reallen); 536 } 537 538 out.flush(); 539 out.close(); 540 in.close(); 541 fireProgressUpdate(file, DataConnection.FINISHED, -1); 542 } 543 catch(IOException ex) 544 { 545 Log.debug("Error with file IO (" + ex + ")!"); 546 fireProgressUpdate(file, DataConnection.FAILED, -1); 547 } 548 } 549 550 public int upload(String file, InputStream in) 551 { 552 if(StringUtils.isRelative(file)) 553 { 554 file = getPWD() + file; 555 } 556 557 file = file.replace('\\', '/'); 558 559 try 560 { 561 work(new BufferedInputStream (in), file, file); 562 563 return 0; 564 } 565 catch(Exception ex) 566 { 567 Log.debug("Error: " + ex.toString()); 568 ex.printStackTrace(); 569 570 return -1; 571 } 572 } 573 574 private void work(BufferedInputStream in, String outfile, String file) 575 { 576 try 577 { 578 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (outfile)); 579 byte[] buf = new byte[filesystemBuffer]; 580 int len = 0; 581 int reallen = 0; 582 583 while(true) 584 { 585 len = in.read(buf); 586 System.out.print("."); 587 588 if(len == StreamTokenizer.TT_EOF) 589 { 590 break; 591 } 592 593 out.write(buf, 0, len); 594 595 reallen += len; 596 fireProgressUpdate(StringUtils.getFile(file), 597 DataConnection.GET, reallen); 598 } 599 600 out.flush(); 601 out.close(); 602 in.close(); 603 fireProgressUpdate(file, DataConnection.FINISHED, -1); 604 } 605 catch(IOException ex) 606 { 607 Log.debug("Error with file IO (" + ex + ")!"); 608 fireProgressUpdate(file, DataConnection.FAILED, -1); 609 } 610 } 611 612 public InputStream getDownloadInputStream(String file) 613 { 614 if(StringUtils.isRelative(file)) 615 { 616 file = getPWD() + file; 617 } 618 619 file = file.replace('\\', '/'); 620 621 try 622 { 623 return new FileInputStream (file); 624 } 625 catch(Exception ex) 626 { 627 ex.printStackTrace(); 628 Log.debug(ex.toString() + 629 " @Filesystemconnection::getDownloadInputStream"); 630 631 return null; 632 } 633 } 634 635 public void addConnectionListener(ConnectionListener l) 636 { 637 listeners.add(l); 638 fireDirectoryUpdate(); 639 } 640 641 public void setConnectionListeners(Vector l) 642 { 643 listeners = l; 644 fireDirectoryUpdate(); 645 } 646 647 648 public void fireDirectoryUpdate() 649 { 650 if(listeners == null) 651 { 652 return; 653 } 654 else 655 { 656 for(int i = 0; i < listeners.size(); i++) 657 { 658 ((ConnectionListener) listeners.elementAt(i)).updateRemoteDirectory(this); 659 } 660 } 661 } 662 663 public boolean login(String user, String pass) 664 { 665 return true; 666 } 667 668 public void fireProgressUpdate(String file, String type, int bytes) 669 { 670 if(listeners == null) 671 { 672 return; 673 } 674 else 675 { 676 for(int i = 0; i < listeners.size(); i++) 677 { 678 ConnectionListener listener = (ConnectionListener) listeners.elementAt(i); 679 680 if(shortProgress && Settings.shortProgress) 681 { 682 if(type.startsWith(DataConnection.DFINISHED)) 683 { 684 listener.updateProgress(baseFile, 685 DataConnection.DFINISHED + ":" + 686 fileCount, bytes); 687 } 688 689 listener.updateProgress(baseFile, 690 DataConnection.GETDIR + ":" + 691 fileCount, bytes); 692 } 693 else 694 { 695 listener.updateProgress(file, type, bytes); 696 } 697 } 698 } 699 } 700 701 public Date [] sortDates() 702 { 703 if(dateVector.size() > 0) 704 { 705 return (Date []) dateVector.toArray(); 706 } 707 else 708 { 709 return null; 710 } 711 } 712 713 public boolean rename(String file, String to) 714 { 715 if(StringUtils.isRelative(file)) 716 { 717 file = getPWD() + file; 718 } 719 720 file = file.replace('\\', '/'); 721 722 File f = new File (file); 723 724 if(StringUtils.isRelative(to)) 725 { 726 to = getPWD() + to; 727 } 728 729 to = to.replace('\\', '/'); 730 731 return f.renameTo(new File (to)); 732 } 733 } 734 | Popular Tags |