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 import com.sun.xfile.XFile; 34 import com.sun.xfile.XFileInputStream; 35 import com.sun.xfile.XFileOutputStream; 36 37 38 public class NfsConnection implements BasicConnection 39 { 40 public static int buffer = 128000; 41 private String url = ""; 42 private String host = ""; 43 private String path = ""; 44 private String pwd = ""; 45 private Vector listeners = new Vector (); 46 private String [] files; 47 private String [] size = new String [0]; 48 private int[] perms = null; 49 50 private String baseFile; 52 private int fileCount; 53 private boolean isDirUpload = false; 54 private boolean shortProgress = false; 55 private boolean dummy = false; 56 57 public NfsConnection(String url) 58 { 59 this.url = url; 60 61 host = url.substring(6); 62 63 int x = host.indexOf("/"); 64 65 if(x >= 0) 66 { 67 host = host.substring(0, x); 68 } 69 70 Log.out("nfs host is: " + host); 71 } 72 73 public boolean login(String user, String pass) 74 { 75 Log.out("nfs login called: " + url); 76 77 try 78 { 79 XFile xf = new XFile(url); 80 81 if(xf.exists()) 82 { 83 Log.out("nfs url ok"); 84 } 85 else 86 { 87 Log.out("WARNING: nfs url not found, cennection will fail!"); 88 } 89 90 com.sun.nfs.XFileExtensionAccessor nfsx = (com.sun.nfs.XFileExtensionAccessor) xf.getExtensionAccessor(); 91 92 if(!nfsx.loginPCNFSD(host, user, pass)) 94 { 95 Log.out("login failed!"); 96 97 return false; 98 } 99 else 100 { 101 Log.debug("Login successful..."); 102 } 103 } 104 catch(Exception e) 105 { 106 e.printStackTrace(); 107 } 108 109 return true; 110 } 111 112 public String [] getExports() throws Exception 113 { 114 XFile xf = new XFile(url); 115 com.sun.nfs.XFileExtensionAccessor nfsx = (com.sun.nfs.XFileExtensionAccessor) xf.getExtensionAccessor(); 116 117 String [] tmp = nfsx.getExports(); 118 119 if(tmp == null) 120 { 121 return new String [0]; 122 } 123 124 for(int i = 0; i < tmp.length; i++) 125 { 126 Log.out("nfs export found: " + tmp[i]); 127 } 128 129 return tmp; 130 } 131 132 public int removeFileOrDir(String file) 133 { 134 try 135 { 136 String tmp = toNFS(file); 137 138 XFile f = new XFile(tmp); 139 140 if(!f.getAbsolutePath().equals(f.getCanonicalPath())) 141 { 142 Log.debug("WARNING: Skipping symlink, remove failed."); 143 Log.debug("This is necessary to prevent possible data loss when removing those symlinks."); 144 145 return -1; 146 } 147 148 if(f.exists() && f.isDirectory()) 149 { 150 cleanLocalDir(tmp); 151 } 152 153 if(!f.delete()) 155 { 156 return -1; 157 } 158 else 159 { 160 return 1; 161 } 162 } 163 catch(IOException ex) 164 { 165 Log.debug("Error: " + ex.toString()); 166 ex.printStackTrace(); 167 } 168 169 return -1; 170 } 171 172 private void cleanLocalDir(String dir) 173 { 174 dir = toNFS(dir); 175 176 if(dir.endsWith("\\")) 177 { 178 Log.out("need to fix \\-problem!!!"); 179 } 180 181 if(!dir.endsWith("/")) 182 { 183 dir = dir + "/"; 184 } 185 186 XFile f2 = new XFile(dir); 189 String [] tmp = f2.list(); 190 191 if(tmp == null) 192 { 193 return; 194 } 195 196 for(int i = 0; i < tmp.length; i++) 197 { 198 XFile f3 = new XFile(dir + tmp[i]); 199 200 if(f3.isDirectory()) 201 { 202 cleanLocalDir(dir + tmp[i]); 204 f3.delete(); 205 } 206 else 207 { 208 f3.delete(); 210 } 211 } 212 } 213 214 public void sendRawCommand(String cmd) 215 { 216 } 217 218 public void disconnect() 219 { 220 } 221 222 public boolean isConnected() 223 { 224 return true; 225 } 226 227 public String getPWD() 228 { 229 String tmp = toNFS(pwd); 230 231 if(!tmp.endsWith("/")) 232 { 233 tmp = tmp + "/"; 234 } 235 236 return tmp; 237 } 238 239 public boolean cdup() 240 { 241 String tmp = pwd; 242 243 if(pwd.endsWith("/") && !pwd.equals("nfs://")) 244 { 245 tmp = pwd.substring(0, pwd.lastIndexOf("/")); 246 } 247 248 return chdir(tmp.substring(0, tmp.lastIndexOf("/") + 1)); 249 } 250 251 public boolean mkdir(String dirName) 252 { 253 if(!dirName.endsWith("/")) 254 { 255 dirName = dirName + "/"; 256 } 257 258 dirName = toNFS(dirName); 259 260 File f = new File (dirName); 261 262 boolean x = f.mkdir(); 263 fireDirectoryUpdate(); 264 265 return x; 266 } 267 268 public void list() throws IOException 269 { 270 } 271 272 public boolean chdir(String p) 273 { 274 return chdir(p, true); 275 } 276 277 public boolean chdir(String p, boolean refresh) 278 { 279 if(p.endsWith("..")) 280 { 281 return cdup(); 282 } 283 284 String tmp = toNFS(p); 285 286 if(!tmp.endsWith("/")) 287 { 288 tmp = tmp + "/"; 289 } 290 291 if(check(tmp) < 3) 292 { 293 return false; 294 } 295 296 pwd = tmp; 297 298 if(refresh) 299 { 300 fireDirectoryUpdate(); 301 } 302 303 return true; 304 } 305 306 private int check(String url) 307 { 308 int x = 0; 309 310 for(int j = 0; j < url.length(); j++) 311 { 312 if(url.charAt(j) == '/') 313 { 314 x++; 315 } 316 } 317 318 return x; 319 } 320 321 public boolean chdirNoRefresh(String p) 322 { 323 331 return chdir(p, false); 332 } 333 334 public String getLocalPath() 335 { 336 return path; 338 } 339 340 private String toNFS(String f) 341 { 342 String file; 343 344 if(f.lastIndexOf("nfs://") > 0) 345 { 346 f = f.substring(f.lastIndexOf("nfs://")); 347 } 348 349 if(f.startsWith("nfs://")) 350 { 351 file = f; 352 } 353 else 354 { 355 file = getPWD() + f; 356 } 357 358 file = file.replace('\\', '/'); 359 360 Log.out("nfs url: " + file); 361 362 return file; 363 } 364 365 public boolean setLocalPath(String p) 366 { 367 if(!p.startsWith("/") && !p.startsWith(":", 1)) 368 { 369 p = path + p; 370 } 371 372 File f = new File (p); 373 374 if(f.exists()) 375 { 376 try 377 { 378 path = f.getCanonicalPath(); 379 path = path.replace('\\', '/'); 380 381 if(!path.endsWith("/")) 382 { 383 path = path + "/"; 384 } 385 386 } 388 catch(IOException ex) 389 { 390 Log.debug("Error: can not get pathname (local)!"); 391 392 return false; 393 } 394 } 395 else 396 { 397 Log.debug("(local) No such path: \"" + p + "\""); 398 399 return false; 400 } 401 402 return true; 403 } 404 405 public String [] sortLs() 406 { 407 String dir = getPWD(); 408 409 if(check(toNFS(dir)) == 3) 410 { 411 try 412 { 413 files = getExports(); 414 } 415 catch(Exception ex) 416 { 417 Log.debug("Can not list exports:" + ex.toString()); 418 ex.printStackTrace(); 419 } 420 } 421 else 422 { 423 XFile f = new XFile(dir); 424 files = f.list(); 425 } 426 427 if(files == null) 428 { 429 return new String [0]; 430 } 431 432 size = new String [files.length]; 433 perms = new int[files.length]; 434 435 int accessible = 0; 436 437 for(int i = 0; i < files.length; i++) 438 { 439 XFile f2 = new XFile(dir + files[i]); 440 441 if(f2.isDirectory() && !files[i].endsWith("/")) 442 { 443 files[i] = files[i] + "/"; 444 } 445 446 size[i] = "" + f2.length(); 447 448 if(f2.canWrite()) 449 { 450 accessible = FtpConnection.W; 451 } 452 else if(f2.canRead()) 453 { 454 accessible = FtpConnection.R; 455 } 456 else 457 { 458 accessible = FtpConnection.DENIED; 459 } 460 461 perms[i] = accessible; 462 463 } 465 466 return files; 467 } 468 469 public String [] sortSize() 470 { 471 return size; 472 } 473 474 public int[] getPermissions() 475 { 476 return perms; 477 } 478 479 public int handleUpload(String f) 480 { 481 upload(f); 482 483 return 0; 484 } 485 486 public int handleDownload(String f) 487 { 488 download(f); 489 490 return 0; 491 } 492 493 public int upload(String f) 494 { 495 String file = toNFS(f); 496 497 if(file.endsWith("/")) 498 { 499 String out = StringUtils.getDir(file); 500 uploadDir(file, getLocalPath() + out); 501 fireActionFinished(this); 502 } 503 else 504 { 505 String outfile = StringUtils.getFile(file); 506 507 work(getLocalPath() + outfile, file); 509 fireActionFinished(this); 510 } 511 512 return 0; 513 } 514 515 public int download(String f) 516 { 517 String file = toNFS(f); 518 519 if(file.endsWith("/")) 520 { 521 String out = StringUtils.getDir(file); 522 downloadDir(file, getLocalPath() + out); 523 fireActionFinished(this); 524 } 525 else 526 { 527 String outfile = StringUtils.getFile(file); 528 529 work(file, getLocalPath() + outfile); 531 fireActionFinished(this); 532 } 533 534 return 0; 535 } 536 537 private void downloadDir(String dir, String out) 538 { 539 try 540 { 541 fileCount = 0; 543 shortProgress = true; 544 baseFile = StringUtils.getDir(dir); 545 546 XFile f2 = new XFile(dir); 547 String [] tmp = f2.list(); 548 549 if(tmp == null) 550 { 551 return; 552 } 553 554 File fx = new File (out); 555 fx.mkdir(); 556 557 for(int i = 0; i < tmp.length; i++) 558 { 559 tmp[i] = tmp[i].replace('\\', '/'); 560 561 XFile f3 = new XFile(dir + tmp[i]); 563 564 if(f3.isDirectory()) 565 { 566 if(!tmp[i].endsWith("/")) 567 { 568 tmp[i] = tmp[i] + "/"; 569 } 570 571 downloadDir(dir + tmp[i], out + tmp[i]); 572 } 573 else 574 { 575 fileCount++; 576 fireProgressUpdate(baseFile, 577 DataConnection.GETDIR + ":" + fileCount, 578 -1); 579 work(dir + tmp[i], out + tmp[i]); 580 } 581 } 582 583 fireProgressUpdate(baseFile, 584 DataConnection.DFINISHED + ":" + fileCount, -1); 585 } 586 catch(Exception ex) 587 { 588 ex.printStackTrace(); 589 590 Log.debug("Transfer error: " + ex); 592 fireProgressUpdate(baseFile, 593 DataConnection.FAILED + ":" + fileCount, -1); 594 } 595 596 shortProgress = false; 597 } 598 599 private void uploadDir(String dir, String out) 600 { 601 try 602 { 603 isDirUpload = true; 605 fileCount = 0; 606 shortProgress = true; 607 baseFile = StringUtils.getDir(dir); 608 609 File f2 = new File (out); 610 String [] tmp = f2.list(); 611 612 if(tmp == null) 613 { 614 return; 615 } 616 617 XFile fx = new XFile(dir); 618 fx.mkdir(); 619 620 for(int i = 0; i < tmp.length; i++) 621 { 622 tmp[i] = tmp[i].replace('\\', '/'); 623 624 File f3 = new File (out + tmp[i]); 626 627 if(f3.isDirectory()) 628 { 629 if(!tmp[i].endsWith("/")) 630 { 631 tmp[i] = tmp[i] + "/"; 632 } 633 634 uploadDir(dir + tmp[i], out + tmp[i]); 635 } 636 else 637 { 638 fileCount++; 639 fireProgressUpdate(baseFile, 640 DataConnection.PUTDIR + ":" + fileCount, 641 -1); 642 work(out + tmp[i], dir + tmp[i]); 643 } 644 } 645 646 fireProgressUpdate(baseFile, 647 DataConnection.DFINISHED + ":" + fileCount, -1); 648 } 649 catch(Exception ex) 650 { 651 ex.printStackTrace(); 652 653 Log.debug("Transfer error: " + ex); 655 fireProgressUpdate(baseFile, 656 DataConnection.FAILED + ":" + fileCount, -1); 657 } 658 659 isDirUpload = false; 660 shortProgress = true; 661 } 662 663 private void work(String file, String outfile) 664 { 665 BufferedOutputStream out = null; 666 BufferedInputStream in = null; 667 668 try 669 { 670 boolean outflag = false; 671 672 if(outfile.startsWith("nfs://")) 673 { 674 outflag = true; 675 out = new BufferedOutputStream (new XFileOutputStream(outfile)); 676 } 677 else 678 { 679 out = new BufferedOutputStream (new FileOutputStream (outfile)); 680 } 681 682 if(file.startsWith("nfs://")) 684 { 685 in = new BufferedInputStream (new XFileInputStream(file)); 686 } 687 else 688 { 689 in = new BufferedInputStream (new FileInputStream (file)); 690 } 691 692 byte[] buf = new byte[buffer]; 693 int len = 0; 694 int reallen = 0; 695 696 while(true) 698 { 699 len = in.read(buf); 700 701 if(len == StreamTokenizer.TT_EOF) 703 { 704 break; 705 } 706 707 out.write(buf, 0, len); 708 reallen += len; 709 710 if(outflag) 712 { 713 fireProgressUpdate(StringUtils.getFile(outfile), 714 DataConnection.PUT, reallen); 715 } 716 else 717 { 718 fireProgressUpdate(StringUtils.getFile(file), 719 DataConnection.GET, reallen); 720 } 721 } 722 723 fireProgressUpdate(file, DataConnection.FINISHED, -1); 724 } 725 catch(IOException ex) 726 { 727 Log.debug("Error with file IO (" + ex + ")!"); 728 fireProgressUpdate(file, DataConnection.FAILED, -1); 729 } 730 finally 731 { 732 try 733 { 734 out.flush(); 735 out.close(); 736 in.close(); 737 } 738 catch(Exception ex) 739 { 740 ex.printStackTrace(); 741 } 742 } 743 } 744 745 private void update(String file, String type, int bytes) 746 { 747 if(listeners == null) 748 { 749 return; 750 } 751 else 752 { 753 for(int i = 0; i < listeners.size(); i++) 754 { 755 ConnectionListener listener = (ConnectionListener) listeners.elementAt(i); 756 listener.updateProgress(file, type, bytes); 757 } 758 } 759 } 760 761 public void addConnectionListener(ConnectionListener l) 762 { 763 listeners.add(l); 764 } 765 766 public void setConnectionListeners(Vector l) 767 { 768 listeners = l; 769 } 770 771 772 public void fireDirectoryUpdate() 773 { 774 if(listeners == null) 775 { 776 return; 777 } 778 else 779 { 780 for(int i = 0; i < listeners.size(); i++) 781 { 782 ((ConnectionListener) listeners.elementAt(i)).updateRemoteDirectory(this); 783 } 784 } 785 } 786 787 788 public void fireProgressUpdate(String file, String type, int bytes) 789 { 790 if(listeners == null) 792 { 793 return; 794 } 795 else 796 { 797 for(int i = 0; i < listeners.size(); i++) 798 { 799 ConnectionListener listener = (ConnectionListener) listeners.elementAt(i); 800 801 if(shortProgress && Settings.shortProgress) 802 { 803 if(type.startsWith(DataConnection.DFINISHED)) 804 { 805 listener.updateProgress(baseFile, 806 DataConnection.DFINISHED + ":" + 807 fileCount, bytes); 808 } 809 else if(isDirUpload) 810 { 811 listener.updateProgress(baseFile, 812 DataConnection.PUTDIR + ":" + 813 fileCount, bytes); 814 } 815 else 816 { 817 listener.updateProgress(baseFile, 818 DataConnection.GETDIR + ":" + 819 fileCount, bytes); 820 } 821 } 822 else 823 { 824 listener.updateProgress(file, type, bytes); 825 } 826 } 827 } 828 } 829 830 public void fireActionFinished(NfsConnection con) 831 { 832 if(listeners == null) 833 { 834 return; 835 } 836 else 837 { 838 for(int i = 0; i < listeners.size(); i++) 839 { 840 ((ConnectionListener) listeners.elementAt(i)).actionFinished(con); 841 } 842 } 843 } 844 845 public int upload(String file, InputStream i) 846 { 847 BufferedInputStream in = null; 848 BufferedOutputStream out = null; 849 850 try 851 { 852 file = toNFS(file); 853 854 out = new BufferedOutputStream (new XFileOutputStream(file)); 855 in = new BufferedInputStream (i); 856 857 byte[] buf = new byte[buffer]; 858 int len = 0; 859 int reallen = 0; 860 861 while(true) 862 { 863 len = in.read(buf); 864 865 if(len == StreamTokenizer.TT_EOF) 866 { 867 break; 868 } 869 870 out.write(buf, 0, len); 871 reallen += len; 872 873 fireProgressUpdate(StringUtils.getFile(file), 874 DataConnection.PUT, reallen); 875 } 876 877 fireProgressUpdate(file, DataConnection.FINISHED, -1); 878 } 879 catch(IOException ex) 880 { 881 Log.debug("Error with file IO (" + ex + ")!"); 882 fireProgressUpdate(file, DataConnection.FAILED, -1); 883 884 return -1; 885 } 886 finally 887 { 888 try 889 { 890 out.flush(); 891 out.close(); 892 in.close(); 893 } 894 catch(Exception ex) 895 { 896 ex.printStackTrace(); 897 } 898 } 899 900 return 0; 901 } 902 903 public InputStream getDownloadInputStream(String file) 904 { 905 file = toNFS(file); 906 Log.debug(file); 907 908 try 909 { 910 return new BufferedInputStream (new XFileInputStream(file)); 911 } 912 catch(Exception ex) 913 { 914 ex.printStackTrace(); 915 Log.debug(ex.toString() + 916 " @NfsConnection::getDownloadInputStream"); 917 918 return null; 919 } 920 } 921 922 public Date [] sortDates() 923 { 924 return null; 925 } 926 927 public boolean rename(String from, String to) 928 { 929 Log.debug("Not implemented!"); 930 931 return false; 932 } 933 } 934 | Popular Tags |