1 17 package org.alfresco.filesys.smb.server; 18 19 import org.alfresco.filesys.server.filesys.FileInfo; 20 import org.alfresco.filesys.server.filesys.UnsupportedInfoLevelException; 21 import org.alfresco.filesys.smb.NTTime; 22 import org.alfresco.filesys.smb.SMBDate; 23 import org.alfresco.filesys.util.DataBuffer; 24 25 30 class FindInfoPacker 31 { 32 33 35 private static final boolean Enable8Dot3Names = false; 36 37 39 private static final boolean EnableFileIdPacking = false; 40 41 43 public static final int InfoStandard = 1; 44 public static final int InfoQueryEASize = 2; 45 public static final int InfoQueryEAFromList = 3; 46 public static final int InfoDirectory = 0x101; 47 public static final int InfoFullDirectory = 0x102; 48 public static final int InfoNames = 0x103; 49 public static final int InfoDirectoryBoth = 0x104; 50 public static final int InfoMacHfsInfo = 0x302; 51 52 54 public static final int InfoStandardLen = 24; 55 public static final int InfoQueryEASizeLen = 28; 56 public static final int InfoDirectoryLen = 64; 57 public static final int InfoFullDirectoryLen = 68; 58 public static final int InfoNamesLen = 12; 59 public static final int InfoDirectoryBothLen = 94; 60 public static final int InfoMacHfsLen = 120; 61 62 71 public final static int packInfo(FileInfo info, DataBuffer buf, int infoLevel, boolean uni) 72 throws UnsupportedInfoLevelException 73 { 74 75 77 int curPos = buf.getPosition(); 78 79 switch (infoLevel) 80 { 81 82 84 case InfoStandard: 85 packInfoStandard(info, buf, false, uni); 86 break; 87 88 90 case InfoQueryEASize: 91 packInfoStandard(info, buf, true, uni); 92 break; 93 94 96 case InfoNames: 97 packInfoFileName(info, buf, uni); 98 break; 99 100 102 case InfoDirectory: 103 packInfoDirectory(info, buf, uni); 104 break; 105 106 108 case InfoFullDirectory: 109 packInfoDirectoryFull(info, buf, uni); 110 break; 111 112 114 case InfoDirectoryBoth: 115 packInfoDirectoryBoth(info, buf, uni); 116 break; 117 118 120 case InfoMacHfsInfo: 121 packInfoMacHfs(info, buf, uni); 122 break; 123 } 124 125 127 if (curPos == buf.getPosition()) 128 throw new UnsupportedInfoLevelException(); 129 130 132 return buf.getPosition() - curPos; 133 } 134 135 142 public final static int calcFileNameOffset(int infoLev, int offset) 143 { 144 145 147 int pos = offset; 148 149 switch (infoLev) 150 { 151 152 154 case InfoStandard: 155 pos += InfoStandard; 156 break; 157 158 160 case InfoQueryEASize: 161 pos += InfoQueryEASizeLen; 162 break; 163 164 166 case InfoNames: 167 pos += InfoNamesLen; 168 break; 169 170 172 case InfoDirectory: 173 pos += InfoDirectoryLen; 174 break; 175 176 178 case InfoFullDirectory: 179 pos += InfoFullDirectoryLen; 180 break; 181 182 184 case InfoDirectoryBoth: 185 pos += InfoDirectoryBothLen; 186 break; 187 } 188 189 191 return pos; 192 } 193 194 204 public final static int calcInfoSize(FileInfo info, int infoLev, boolean resKey, boolean uni) 205 { 206 207 209 int len = -1; 210 int nameLen = info.getFileName().length() + 1; 211 if (uni) 212 nameLen *= 2; 213 214 switch (infoLev) 215 { 216 217 219 case InfoStandard: 220 len = InfoStandardLen + nameLen; 221 break; 222 223 225 case InfoQueryEASize: 226 len = InfoQueryEASizeLen + nameLen; 227 break; 228 229 231 case InfoNames: 232 len += InfoNamesLen + nameLen; 233 break; 234 235 237 case InfoDirectory: 238 len = InfoDirectoryLen + nameLen; 239 break; 240 241 243 case InfoFullDirectory: 244 len += InfoFullDirectoryLen + nameLen; 245 break; 246 247 249 case InfoDirectoryBoth: 250 len = InfoDirectoryBothLen + nameLen; 251 break; 252 253 255 case InfoMacHfsInfo: 256 len = InfoMacHfsLen + nameLen; 257 break; 258 } 259 260 262 if (resKey) 263 len += 4; 264 265 267 return len; 268 } 269 270 277 public static final void clearNextOffset(DataBuffer buf, int level, int offset) 278 { 279 280 282 if (level == InfoStandard) 283 return; 284 285 287 int curPos = buf.getPosition(); 288 buf.setPosition(offset); 289 buf.putInt(0); 290 buf.setPosition(curPos); 291 } 292 293 302 protected final static void packInfoStandard(FileInfo info, DataBuffer buf, boolean EAflag, boolean uni) 303 { 304 305 319 321 SMBDate date = new SMBDate(0); 322 323 if (info.hasCreationDateTime()) 324 { 325 date.setTime(info.getCreationDateTime()); 326 buf.putShort(date.asSMBDate()); 327 buf.putShort(date.asSMBTime()); 328 } 329 else 330 buf.putZeros(4); 331 332 334 if (info.hasAccessDateTime()) 335 { 336 date.setTime(info.getAccessDateTime()); 337 buf.putShort(date.asSMBDate()); 338 buf.putShort(date.asSMBTime()); 339 } 340 else 341 buf.putZeros(4); 342 343 345 if (info.hasModifyDateTime()) 346 { 347 date.setTime(info.getModifyDateTime()); 348 buf.putShort(date.asSMBDate()); 349 buf.putShort(date.asSMBTime()); 350 } 351 else 352 buf.putZeros(4); 353 354 356 buf.putInt(info.getSizeInt()); 357 358 if (info.getAllocationSize() < info.getSize()) 359 buf.putInt(info.getSizeInt()); 360 else 361 buf.putInt(info.getAllocationSizeInt()); 362 363 365 buf.putShort(info.getFileAttributes()); 366 367 369 if (EAflag) 370 buf.putInt(0); 371 372 374 if (uni == true) 375 { 376 377 379 buf.putByte(info.getFileName().length() * 2); 380 buf.wordAlign(); 381 buf.putString(info.getFileName(), uni, true); 382 } 383 else 384 { 385 386 388 buf.putByte(info.getFileName().length()); 389 buf.putString(info.getFileName(), uni, true); 390 } 391 } 392 393 400 protected final static void packInfoFileName(FileInfo info, DataBuffer buf, boolean uni) 401 { 402 403 409 411 int startPos = buf.getPosition(); 412 buf.putZeros(4); 413 buf.putInt(EnableFileIdPacking ? info.getFileId() : 0); 414 415 417 int nameLen = info.getFileName().length(); 418 if (uni) 419 nameLen *= 2; 420 421 buf.putInt(nameLen); 422 423 425 buf.putString(info.getFileName(), uni, false); 426 427 429 buf.longwordAlign(); 430 431 int curPos = buf.getPosition(); 432 buf.setPosition(startPos); 433 buf.putInt(curPos - startPos); 434 buf.setPosition(curPos); 435 } 436 437 444 protected final static void packInfoDirectory(FileInfo info, DataBuffer buf, boolean uni) 445 { 446 447 460 462 int startPos = buf.getPosition(); 463 buf.putZeros(4); 464 buf.putInt(EnableFileIdPacking ? info.getFileId() : 0); 465 466 468 if (info.hasCreationDateTime()) 469 { 470 buf.putLong(NTTime.toNTTime(info.getCreationDateTime())); 471 } 472 else 473 buf.putZeros(8); 474 475 477 if (info.hasAccessDateTime()) 478 { 479 buf.putLong(NTTime.toNTTime(info.getAccessDateTime())); 480 } 481 else 482 buf.putZeros(8); 483 484 486 if (info.hasModifyDateTime()) 487 { 488 buf.putLong(NTTime.toNTTime(info.getModifyDateTime())); 489 buf.putLong(NTTime.toNTTime(info.getModifyDateTime())); 490 } 491 else 492 buf.putZeros(16); 493 494 496 buf.putLong(info.getSize()); 497 498 if (info.getAllocationSize() < info.getSize()) 499 buf.putLong(info.getSize()); 500 else 501 buf.putLong(info.getAllocationSize()); 502 503 505 buf.putInt(info.getFileAttributes()); 506 507 509 int nameLen = info.getFileName().length(); 510 if (uni) 511 nameLen *= 2; 512 513 buf.putInt(nameLen); 514 515 517 buf.putString(info.getFileName(), uni, false); 518 519 521 buf.longwordAlign(); 522 523 int curPos = buf.getPosition(); 524 buf.setPosition(startPos); 525 buf.putInt(curPos - startPos); 526 buf.setPosition(curPos); 527 } 528 529 536 protected final static void packInfoDirectoryFull(FileInfo info, DataBuffer buf, boolean uni) 537 { 538 539 553 555 int startPos = buf.getPosition(); 556 buf.putZeros(4); 557 buf.putInt(EnableFileIdPacking ? info.getFileId() : 0); 558 559 561 if (info.hasCreationDateTime()) 562 { 563 buf.putLong(NTTime.toNTTime(info.getCreationDateTime())); 564 } 565 else 566 buf.putZeros(8); 567 568 570 if (info.hasAccessDateTime()) 571 { 572 buf.putLong(NTTime.toNTTime(info.getAccessDateTime())); 573 } 574 else 575 buf.putZeros(8); 576 577 579 if (info.hasModifyDateTime()) 580 { 581 buf.putLong(NTTime.toNTTime(info.getModifyDateTime())); 582 buf.putLong(NTTime.toNTTime(info.getModifyDateTime())); 583 } 584 else 585 buf.putZeros(16); 586 587 589 buf.putLong(info.getSize()); 590 591 if (info.getAllocationSize() < info.getSize()) 592 buf.putLong(info.getSize()); 593 else 594 buf.putLong(info.getAllocationSize()); 595 596 598 buf.putInt(info.getFileAttributes()); 599 600 602 int nameLen = info.getFileName().length(); 603 if (uni) 604 nameLen *= 2; 605 606 buf.putInt(nameLen); 607 608 610 buf.putZeros(4); 611 612 614 buf.putString(info.getFileName(), uni, false); 615 616 618 buf.longwordAlign(); 619 620 int curPos = buf.getPosition(); 621 buf.setPosition(startPos); 622 buf.putInt(curPos - startPos); 623 buf.setPosition(curPos); 624 } 625 626 633 protected final static void packInfoDirectoryBoth(FileInfo info, DataBuffer buf, boolean uni) 634 { 635 636 652 654 int startPos = buf.getPosition(); 655 buf.putZeros(4); 656 buf.putInt(EnableFileIdPacking ? info.getFileId() : 0); 657 658 660 if (info.hasCreationDateTime()) 661 { 662 buf.putLong(NTTime.toNTTime(info.getCreationDateTime())); 663 } 664 else 665 buf.putZeros(8); 666 667 669 if (info.hasAccessDateTime()) 670 { 671 buf.putLong(NTTime.toNTTime(info.getAccessDateTime())); 672 } 673 else 674 buf.putZeros(8); 675 676 678 if (info.hasModifyDateTime()) 679 { 680 buf.putLong(NTTime.toNTTime(info.getModifyDateTime())); 681 buf.putLong(NTTime.toNTTime(info.getModifyDateTime())); 682 } 683 else 684 buf.putZeros(16); 685 686 688 buf.putLong(info.getSize()); 689 690 if (info.getAllocationSize() < info.getSize()) 691 buf.putLong(info.getSize()); 692 else 693 buf.putLong(info.getAllocationSize()); 694 695 697 buf.putInt(info.getFileAttributes()); 698 699 701 int nameLen = info.getFileName().length(); 702 if (uni) 703 nameLen *= 2; 704 705 buf.putInt(nameLen); 706 707 709 buf.putZeros(4); 710 711 713 pack8Dot3Name(buf, info.getFileName(), uni); 714 715 717 buf.putString(info.getFileName(), uni, false); 718 719 721 buf.longwordAlign(); 722 723 int curPos = buf.getPosition(); 724 buf.setPosition(startPos); 725 buf.putInt(curPos - startPos); 726 buf.setPosition(curPos); 727 } 728 729 736 protected final static void packInfoMacHfs(FileInfo info, DataBuffer buf, boolean uni) 737 { 738 739 762 764 int startPos = buf.getPosition(); 765 buf.putZeros(4); 766 buf.putInt(EnableFileIdPacking ? info.getFileId() : 0); 767 768 770 if (info.hasCreationDateTime()) 771 { 772 buf.putLong(NTTime.toNTTime(info.getCreationDateTime())); 773 } 774 else 775 buf.putZeros(8); 776 777 779 if (info.hasModifyDateTime()) 780 { 781 buf.putLong(NTTime.toNTTime(info.getModifyDateTime())); 782 buf.putLong(NTTime.toNTTime(info.getModifyDateTime())); 783 } 784 else 785 buf.putZeros(16); 786 787 789 buf.putLong(info.getSize()); 790 buf.putZeros(8); 791 792 794 if (info.getAllocationSize() < info.getSize()) 795 buf.putLong(info.getSize()); 796 else 797 buf.putLong(info.getAllocationSize()); 798 buf.putZeros(8); 799 800 802 buf.putInt(info.getFileAttributes()); 803 804 806 buf.putZeros(2); 807 808 810 buf.putShort(0); 811 812 814 buf.putInt(0); 815 816 818 buf.putZeros(32); 819 820 822 int nameLen = info.getFileName().length(); 823 if (uni) 824 nameLen *= 2; 825 826 buf.putInt(nameLen); 827 828 830 pack8Dot3Name(buf, info.getFileName(), uni); 831 832 834 buf.putString(info.getFileName(), uni, false); 835 836 838 buf.putInt(0); 839 840 842 buf.longwordAlign(); 843 844 int curPos = buf.getPosition(); 845 buf.setPosition(startPos); 846 buf.putInt(curPos - startPos); 847 buf.setPosition(curPos); 848 } 849 850 858 private static final void pack8Dot3Name(DataBuffer buf, String fileName, boolean uni) 859 { 860 861 if (Enable8Dot3Names == false) 862 { 863 864 866 buf.putZeros(26); 867 } 868 else 869 { 870 871 873 int pos = fileName.lastIndexOf('.'); 874 875 String namePart = null; 876 String extPart = null; 877 878 if (pos != -1) 879 { 880 881 883 namePart = fileName.substring(0, pos); 884 extPart = fileName.substring(pos + 1); 885 } 886 else 887 namePart = fileName; 888 889 891 if (namePart.length() <= 8 && (extPart == null || extPart.length() <= 3)) 892 { 893 894 896 buf.putZeros(26); 897 return; 898 } 899 900 902 if (namePart.length() > 8) 903 namePart = namePart.substring(0, 6) + "~1"; 904 905 if (extPart != null && extPart.length() > 3) 906 extPart = extPart.substring(0, 3); 907 908 910 StringBuffer str = new StringBuffer (16); 911 912 str.append(namePart); 913 while (str.length() < 8) 914 str.append(" "); 915 916 if (extPart != null) 917 { 918 str.append("."); 919 str.append(extPart); 920 } 921 else 922 str.append(" "); 923 924 926 while (str.length() < 12) 927 str.append(" "); 928 929 931 int len = namePart.length(); 932 if (extPart != null) 933 len = extPart.length() + 9; 934 935 len *= 2; 936 937 939 buf.putByte(len); 940 buf.putByte(0); 941 942 buf.putString(str.toString(), true, false); 943 } 944 } 945 } | Popular Tags |