| | @@ -28,10 +28,17 @@ |
| 28 | 28 | #include <unistd.h> |
| 29 | 29 | #include <string.h> |
| 30 | 30 | #include <errno.h> |
| 31 | 31 | #include "file.h" |
| 32 | 32 | |
| 33 | +/* |
| 34 | +** On Windows, include the Platform SDK header file. |
| 35 | +*/ |
| 36 | +#ifdef _WIN32 |
| 37 | +# include <windows.h> |
| 38 | +#endif |
| 39 | + |
| 33 | 40 | /* |
| 34 | 41 | ** The file status information from the most recent stat() call. |
| 35 | 42 | ** |
| 36 | 43 | ** Use _stati64 rather than stat on windows, in order to handle files |
| 37 | 44 | ** larger than 2GB. |
| | @@ -168,11 +175,11 @@ |
| 168 | 175 | zName = mprintf("%s", zLinkFile); |
| 169 | 176 | }else{ |
| 170 | 177 | zName = zBuf; |
| 171 | 178 | memcpy(zName, zLinkFile, nName+1); |
| 172 | 179 | } |
| 173 | | - nName = file_simplify_name(zName, nName); |
| 180 | + nName = file_simplify_name(zName, nName, 0); |
| 174 | 181 | for(i=1; i<nName; i++){ |
| 175 | 182 | if( zName[i]=='/' ){ |
| 176 | 183 | zName[i] = 0; |
| 177 | 184 | if( file_mkdir(zName, 1) ){ |
| 178 | 185 | fossil_fatal_recursive("unable to create directory %s", zName); |
| | @@ -259,11 +266,11 @@ |
| 259 | 266 | int file_isdir(const char *zFilename){ |
| 260 | 267 | int rc; |
| 261 | 268 | |
| 262 | 269 | if( zFilename ){ |
| 263 | 270 | char *zFN = mprintf("%s", zFilename); |
| 264 | | - file_simplify_name(zFN, -1); |
| 271 | + file_simplify_name(zFN, -1, 0); |
| 265 | 272 | rc = getStat(zFN, 0); |
| 266 | 273 | free(zFN); |
| 267 | 274 | }else{ |
| 268 | 275 | rc = getStat(0, 0); |
| 269 | 276 | } |
| | @@ -276,11 +283,11 @@ |
| 276 | 283 | int file_wd_isdir(const char *zFilename){ |
| 277 | 284 | int rc; |
| 278 | 285 | |
| 279 | 286 | if( zFilename ){ |
| 280 | 287 | char *zFN = mprintf("%s", zFilename); |
| 281 | | - file_simplify_name(zFN, -1); |
| 288 | + file_simplify_name(zFN, -1, 0); |
| 282 | 289 | rc = getStat(zFN, 1); |
| 283 | 290 | free(zFN); |
| 284 | 291 | }else{ |
| 285 | 292 | rc = getStat(0, 1); |
| 286 | 293 | } |
| | @@ -314,11 +321,11 @@ |
| 314 | 321 | fossil_free(z); |
| 315 | 322 | z = mprintf("%s-%s-%d", zBase, zSuffix, cnt++); |
| 316 | 323 | } |
| 317 | 324 | if( relFlag ){ |
| 318 | 325 | Blob x; |
| 319 | | - file_relative_name(z, &x); |
| 326 | + file_relative_name(z, &x, 0); |
| 320 | 327 | fossil_free(z); |
| 321 | 328 | z = blob_str(&x); |
| 322 | 329 | } |
| 323 | 330 | return z; |
| 324 | 331 | } |
| | @@ -473,12 +480,14 @@ |
| 473 | 480 | ** * removing any trailing and duplicate / |
| 474 | 481 | ** * removing /./ |
| 475 | 482 | ** * removing /A/../ |
| 476 | 483 | ** |
| 477 | 484 | ** Changes are made in-place. Return the new name length. |
| 485 | +** If the slash parameter is non-zero, the trailing slash, if any, |
| 486 | +** is retained. |
| 478 | 487 | */ |
| 479 | | -int file_simplify_name(char *z, int n){ |
| 488 | +int file_simplify_name(char *z, int n, int slash){ |
| 480 | 489 | int i, j; |
| 481 | 490 | if( n<0 ) n = strlen(z); |
| 482 | 491 | |
| 483 | 492 | /* On windows convert all \ characters to / */ |
| 484 | 493 | #if defined(_WIN32) |
| | @@ -486,11 +495,13 @@ |
| 486 | 495 | if( z[i]=='\\' ) z[i] = '/'; |
| 487 | 496 | } |
| 488 | 497 | #endif |
| 489 | 498 | |
| 490 | 499 | /* Removing trailing "/" characters */ |
| 491 | | - while( n>1 && z[n-1]=='/' ){ n--; } |
| 500 | + if ( !slash ){ |
| 501 | + while( n>1 && z[n-1]=='/' ){ n--; } |
| 502 | + } |
| 492 | 503 | |
| 493 | 504 | /* Remove duplicate '/' characters. Except, two // at the beginning |
| 494 | 505 | ** of a pathname is allowed since this is important on windows. */ |
| 495 | 506 | for(i=j=1; i<n; i++){ |
| 496 | 507 | z[j++] = z[i]; |
| | @@ -540,11 +551,11 @@ |
| 540 | 551 | int i; |
| 541 | 552 | char *z; |
| 542 | 553 | for(i=2; i<g.argc; i++){ |
| 543 | 554 | z = mprintf("%s", g.argv[i]); |
| 544 | 555 | fossil_print("[%s] -> ", z); |
| 545 | | - file_simplify_name(z, -1); |
| 556 | + file_simplify_name(z, -1, 0); |
| 546 | 557 | fossil_print("[%s]\n", z); |
| 547 | 558 | fossil_free(z); |
| 548 | 559 | } |
| 549 | 560 | } |
| 550 | 561 | |
| | @@ -606,22 +617,45 @@ |
| 606 | 617 | ** Compute a canonical pathname for a file or directory. |
| 607 | 618 | ** Make the name absolute if it is relative. |
| 608 | 619 | ** Remove redundant / characters |
| 609 | 620 | ** Remove all /./ path elements. |
| 610 | 621 | ** Convert /A/../ to just / |
| 622 | +** If the slash parameter is non-zero, the trailing slash, if any, |
| 623 | +** is retained. |
| 611 | 624 | */ |
| 612 | | -void file_canonical_name(const char *zOrigName, Blob *pOut){ |
| 625 | +void file_canonical_name(const char *zOrigName, Blob *pOut, int slash){ |
| 613 | 626 | if( file_is_absolute_path(zOrigName) ){ |
| 627 | +#if defined(_WIN32) |
| 628 | + char *zOut; |
| 629 | +#endif |
| 614 | 630 | blob_set(pOut, zOrigName); |
| 615 | 631 | blob_materialize(pOut); |
| 632 | +#if defined(_WIN32) |
| 633 | + /* |
| 634 | + ** On Windows, normalize the drive letter to upper case. |
| 635 | + */ |
| 636 | + zOut = blob_str(pOut); |
| 637 | + if( fossil_isalpha(zOut[0]) && zOut[1]==':' ){ |
| 638 | + zOut[0] = fossil_toupper(zOut[0]); |
| 639 | + } |
| 640 | +#endif |
| 616 | 641 | }else{ |
| 617 | 642 | char zPwd[2000]; |
| 618 | 643 | file_getcwd(zPwd, sizeof(zPwd)-strlen(zOrigName)); |
| 644 | +#if defined(_WIN32) |
| 645 | + /* |
| 646 | + ** On Windows, normalize the drive letter to upper case. |
| 647 | + */ |
| 648 | + if( fossil_isalpha(zPwd[0]) && zPwd[1]==':' ){ |
| 649 | + zPwd[0] = fossil_toupper(zPwd[0]); |
| 650 | + } |
| 651 | +#endif |
| 619 | 652 | blob_zero(pOut); |
| 620 | 653 | blob_appendf(pOut, "%//%/", zPwd, zOrigName); |
| 621 | 654 | } |
| 622 | | - blob_resize(pOut, file_simplify_name(blob_buffer(pOut), blob_size(pOut))); |
| 655 | + blob_resize(pOut, file_simplify_name(blob_buffer(pOut), |
| 656 | + blob_size(pOut), slash)); |
| 623 | 657 | } |
| 624 | 658 | |
| 625 | 659 | /* |
| 626 | 660 | ** COMMAND: test-canonical-name |
| 627 | 661 | ** Usage: %fossil test-canonical-name FILENAME... |
| | @@ -634,11 +668,11 @@ |
| 634 | 668 | Blob x; |
| 635 | 669 | blob_zero(&x); |
| 636 | 670 | for(i=2; i<g.argc; i++){ |
| 637 | 671 | char zBuf[100]; |
| 638 | 672 | const char *zName = g.argv[i]; |
| 639 | | - file_canonical_name(zName, &x); |
| 673 | + file_canonical_name(zName, &x, 0); |
| 640 | 674 | fossil_print("[%s] -> [%s]\n", zName, blob_buffer(&x)); |
| 641 | 675 | blob_reset(&x); |
| 642 | 676 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", file_wd_size(zName)); |
| 643 | 677 | fossil_print(" file_size = %s\n", zBuf); |
| 644 | 678 | sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", file_wd_mtime(zName)); |
| | @@ -688,16 +722,18 @@ |
| 688 | 722 | return zIn; |
| 689 | 723 | } |
| 690 | 724 | |
| 691 | 725 | /* |
| 692 | 726 | ** Compute a pathname for a file or directory that is relative |
| 693 | | -** to the current directory. |
| 727 | +** to the current directory. If the slash parameter is non-zero, |
| 728 | +** the trailing slash, if any, is retained. |
| 694 | 729 | */ |
| 695 | | -void file_relative_name(const char *zOrigName, Blob *pOut){ |
| 730 | +void file_relative_name(const char *zOrigName, Blob *pOut, int slash){ |
| 696 | 731 | char *zPath; |
| 697 | 732 | blob_set(pOut, zOrigName); |
| 698 | | - blob_resize(pOut, file_simplify_name(blob_buffer(pOut), blob_size(pOut))); |
| 733 | + blob_resize(pOut, file_simplify_name(blob_buffer(pOut), |
| 734 | + blob_size(pOut), slash)); |
| 699 | 735 | zPath = file_without_drive_letter(blob_buffer(pOut)); |
| 700 | 736 | if( zPath[0]=='/' ){ |
| 701 | 737 | int i, j; |
| 702 | 738 | Blob tmp; |
| 703 | 739 | char *zPwd; |
| | @@ -753,11 +789,11 @@ |
| 753 | 789 | void cmd_test_relative_name(void){ |
| 754 | 790 | int i; |
| 755 | 791 | Blob x; |
| 756 | 792 | blob_zero(&x); |
| 757 | 793 | for(i=2; i<g.argc; i++){ |
| 758 | | - file_relative_name(g.argv[i], &x); |
| 794 | + file_relative_name(g.argv[i], &x, 0); |
| 759 | 795 | fossil_print("%s\n", blob_buffer(&x)); |
| 760 | 796 | blob_reset(&x); |
| 761 | 797 | } |
| 762 | 798 | } |
| 763 | 799 | |
| | @@ -768,37 +804,46 @@ |
| 768 | 804 | ** false, then simply return 0. |
| 769 | 805 | ** |
| 770 | 806 | ** The root of the tree is defined by the g.zLocalRoot variable. |
| 771 | 807 | */ |
| 772 | 808 | int file_tree_name(const char *zOrigName, Blob *pOut, int errFatal){ |
| 773 | | - int n; |
| 809 | + Blob localRoot; |
| 810 | + int nLocalRoot; |
| 811 | + char *zLocalRoot; |
| 774 | 812 | Blob full; |
| 775 | 813 | int nFull; |
| 776 | 814 | char *zFull; |
| 777 | 815 | |
| 778 | 816 | blob_zero(pOut); |
| 779 | 817 | db_must_be_within_tree(); |
| 780 | | - file_canonical_name(zOrigName, &full); |
| 781 | | - n = strlen(g.zLocalRoot); |
| 782 | | - assert( n>0 && g.zLocalRoot[n-1]=='/' ); |
| 818 | + file_canonical_name(g.zLocalRoot, &localRoot, 1); |
| 819 | + nLocalRoot = blob_size(&localRoot); |
| 820 | + zLocalRoot = blob_buffer(&localRoot); |
| 821 | + assert( nLocalRoot>0 && zLocalRoot[nLocalRoot-1]=='/' ); |
| 822 | + file_canonical_name(zOrigName, &full, 0); |
| 783 | 823 | nFull = blob_size(&full); |
| 784 | 824 | zFull = blob_buffer(&full); |
| 785 | 825 | |
| 786 | 826 | /* Special case. zOrigName refers to g.zLocalRoot directory. */ |
| 787 | | - if( nFull==n-1 && memcmp(g.zLocalRoot, zFull, nFull)==0 ){ |
| 827 | + if( nFull==nLocalRoot-1 && memcmp(zLocalRoot, zFull, nFull)==0 ){ |
| 788 | 828 | blob_append(pOut, ".", 1); |
| 829 | + blob_reset(&localRoot); |
| 830 | + blob_reset(&full); |
| 789 | 831 | return 1; |
| 790 | 832 | } |
| 791 | 833 | |
| 792 | | - if( nFull<=n || memcmp(g.zLocalRoot, zFull, n) ){ |
| 834 | + if( nFull<=nLocalRoot || memcmp(zLocalRoot, zFull, nLocalRoot) ){ |
| 835 | + blob_reset(&localRoot); |
| 793 | 836 | blob_reset(&full); |
| 794 | 837 | if( errFatal ){ |
| 795 | 838 | fossil_fatal("file outside of checkout tree: %s", zOrigName); |
| 796 | 839 | } |
| 797 | 840 | return 0; |
| 798 | 841 | } |
| 799 | | - blob_append(pOut, &zFull[n], nFull-n); |
| 842 | + blob_append(pOut, &zFull[nLocalRoot], nFull-nLocalRoot); |
| 843 | + blob_reset(&localRoot); |
| 844 | + blob_reset(&full); |
| 800 | 845 | return 1; |
| 801 | 846 | } |
| 802 | 847 | |
| 803 | 848 | /* |
| 804 | 849 | ** COMMAND: test-tree-name |
| | @@ -861,25 +906,44 @@ |
| 861 | 906 | /* |
| 862 | 907 | ** Construct a random temporary filename into zBuf[]. |
| 863 | 908 | */ |
| 864 | 909 | void file_tempname(int nBuf, char *zBuf){ |
| 865 | 910 | static const char *azDirs[] = { |
| 911 | +#if defined(_WIN32) |
| 912 | + 0, /* GetTempPath */ |
| 913 | + 0, /* TEMP */ |
| 914 | + 0, /* TMP */ |
| 915 | +#else |
| 866 | 916 | "/var/tmp", |
| 867 | 917 | "/usr/tmp", |
| 868 | 918 | "/tmp", |
| 869 | 919 | "/temp", |
| 920 | +#endif |
| 870 | 921 | ".", |
| 871 | 922 | }; |
| 872 | 923 | static const unsigned char zChars[] = |
| 873 | 924 | "abcdefghijklmnopqrstuvwxyz" |
| 874 | 925 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 875 | 926 | "0123456789"; |
| 876 | 927 | unsigned int i, j; |
| 877 | 928 | const char *zDir = "."; |
| 878 | 929 | int cnt = 0; |
| 930 | + |
| 931 | +#if defined(_WIN32) |
| 932 | + char zTmpPath[MAX_PATH]; |
| 933 | + |
| 934 | + if( GetTempPath(sizeof(zTmpPath), zTmpPath) ){ |
| 935 | + azDirs[0] = zTmpPath; |
| 936 | + } |
| 937 | + |
| 938 | + azDirs[1] = fossil_getenv("TEMP"); |
| 939 | + azDirs[2] = fossil_getenv("TMP"); |
| 940 | +#endif |
| 941 | + |
| 879 | 942 | |
| 880 | 943 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){ |
| 944 | + if( azDirs[i]==0 ) continue; |
| 881 | 945 | if( !file_isdir(azDirs[i]) ) continue; |
| 882 | 946 | zDir = azDirs[i]; |
| 883 | 947 | break; |
| 884 | 948 | } |
| 885 | 949 | |
| | @@ -898,10 +962,15 @@ |
| 898 | 962 | for(i=0; i<15; i++, j++){ |
| 899 | 963 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 900 | 964 | } |
| 901 | 965 | zBuf[j] = 0; |
| 902 | 966 | }while( file_size(zBuf)>=0 ); |
| 967 | + |
| 968 | +#if defined(_WIN32) |
| 969 | + fossil_mbcs_free((char *)azDirs[1]); |
| 970 | + fossil_mbcs_free((char *)azDirs[2]); |
| 971 | +#endif |
| 903 | 972 | } |
| 904 | 973 | |
| 905 | 974 | |
| 906 | 975 | /* |
| 907 | 976 | ** Return true if a file named zName exists and has identical content |
| | @@ -930,13 +999,10 @@ |
| 930 | 999 | /************************************************************************** |
| 931 | 1000 | ** The following routines translate between MBCS and UTF8 on windows. |
| 932 | 1001 | ** Since everything is always UTF8 on unix, these routines are no-ops |
| 933 | 1002 | ** there. |
| 934 | 1003 | */ |
| 935 | | -#ifdef _WIN32 |
| 936 | | -# include <windows.h> |
| 937 | | -#endif |
| 938 | 1004 | |
| 939 | 1005 | /* |
| 940 | 1006 | ** Translate MBCS to UTF8. Return a pointer to the translated text. |
| 941 | 1007 | ** Call fossil_mbcs_free() to deallocate any memory used to store the |
| 942 | 1008 | ** returned pointer when done. |
| 943 | 1009 | |