Fossil SCM
Add comments to the 3 different temp-filename generator routines to cross-link them to one another. Add fossil_temp_filename() as an option to the "test-tempfile" command.
Commit
1aff463371d637654b72e4c0131a351fb65b6c61c845a5fc70c0b46aba58d85a
Parent
450cfbbfac77886…
2 files changed
+17
-3
+2
+17
-3
| --- src/file.c | ||
| +++ src/file.c | ||
| @@ -1880,10 +1880,12 @@ | ||
| 1880 | 1880 | ** If zTag is not NULL, then try to create the temp-file using zTag |
| 1881 | 1881 | ** as a differentiator. If that fails, or if zTag is NULL, then use |
| 1882 | 1882 | ** a bunch of random characters as the tag. |
| 1883 | 1883 | ** |
| 1884 | 1884 | ** Dangerous characters in zBasis are changed. |
| 1885 | +** | |
| 1886 | +** See also fossil_temp_filename() and file_time_tempname(); | |
| 1885 | 1887 | */ |
| 1886 | 1888 | void file_tempname(Blob *pBuf, const char *zBasis, const char *zTag){ |
| 1887 | 1889 | #if defined(_WIN32) |
| 1888 | 1890 | const char *azDirs[] = { |
| 1889 | 1891 | 0, /* GetTempPath */ |
| @@ -1959,15 +1961,16 @@ | ||
| 1959 | 1961 | } |
| 1960 | 1962 | do{ |
| 1961 | 1963 | blob_zero(pBuf); |
| 1962 | 1964 | if( cnt++>20 ) fossil_fatal("cannot generate a temporary filename"); |
| 1963 | 1965 | if( zTag==0 ){ |
| 1964 | - sqlite3_randomness(15, zRand); | |
| 1965 | - for(i=0; i<15; i++){ | |
| 1966 | + const int nRand = sizeof(zRand)-1; | |
| 1967 | + sqlite3_randomness(nRand, zRand); | |
| 1968 | + for(i=0; i<nRand; i++){ | |
| 1966 | 1969 | zRand[i] = (char)zChars[ ((unsigned char)zRand[i])%(sizeof(zChars)-1) ]; |
| 1967 | 1970 | } |
| 1968 | - zRand[15] = 0; | |
| 1971 | + zRand[nRand] = 0; | |
| 1969 | 1972 | zTag = zRand; |
| 1970 | 1973 | } |
| 1971 | 1974 | blob_appendf(pBuf, "%s/%.*s~%s%s", zDir, nBasis, zBasis, zTag, zSuffix); |
| 1972 | 1975 | zTag = 0; |
| 1973 | 1976 | for(z=blob_str(pBuf); z!=0 && (z=strpbrk(z,"'\"`;|$&"))!=0; z++){ |
| @@ -1989,10 +1992,12 @@ | ||
| 1989 | 1992 | } |
| 1990 | 1993 | |
| 1991 | 1994 | /* |
| 1992 | 1995 | ** Compute a temporary filename in zDir. The filename is based on |
| 1993 | 1996 | ** the current time. |
| 1997 | +** | |
| 1998 | +** See also fossil_temp_filename() and file_tempname(); | |
| 1994 | 1999 | */ |
| 1995 | 2000 | char *file_time_tempname(const char *zDir, const char *zSuffix){ |
| 1996 | 2001 | struct tm *tm; |
| 1997 | 2002 | unsigned int r; |
| 1998 | 2003 | static unsigned int cnt = 0; |
| @@ -2011,18 +2016,27 @@ | ||
| 2011 | 2016 | ** Usage: fossil test-name [--time SUFFIX] [--tag NAME] BASENAME ... |
| 2012 | 2017 | ** |
| 2013 | 2018 | ** Generate temporary filenames derived from BASENAME. Use the --time |
| 2014 | 2019 | ** option to generate temp names based on the time of day. If --tag NAME |
| 2015 | 2020 | ** is specified, try to use NAME as the differentiator in the temp file. |
| 2021 | +** | |
| 2022 | +** If --time is used, file_time_tempname() generates the filename. | |
| 2023 | +** If BASENAME is present, file_tempname() generates the filename. | |
| 2024 | +** Without --time or BASENAME, fossil_temp_filename() generates the filename. | |
| 2016 | 2025 | */ |
| 2017 | 2026 | void file_test_tempname(void){ |
| 2018 | 2027 | int i; |
| 2019 | 2028 | const char *zSuffix = find_option("time",0,1); |
| 2020 | 2029 | Blob x = BLOB_INITIALIZER; |
| 2021 | 2030 | char *z; |
| 2022 | 2031 | const char *zTag = find_option("tag",0,1); |
| 2023 | 2032 | verify_all_options(); |
| 2033 | + if( g.argc<=2 ){ | |
| 2034 | + z = fossil_temp_filename(); | |
| 2035 | + fossil_print("%s\n", z); | |
| 2036 | + sqlite3_free(z); | |
| 2037 | + } | |
| 2024 | 2038 | for(i=2; i<g.argc; i++){ |
| 2025 | 2039 | if( zSuffix ){ |
| 2026 | 2040 | z = file_time_tempname(g.argv[i], zSuffix); |
| 2027 | 2041 | fossil_print("%s\n", z); |
| 2028 | 2042 | fossil_free(z); |
| 2029 | 2043 |
| --- src/file.c | |
| +++ src/file.c | |
| @@ -1880,10 +1880,12 @@ | |
| 1880 | ** If zTag is not NULL, then try to create the temp-file using zTag |
| 1881 | ** as a differentiator. If that fails, or if zTag is NULL, then use |
| 1882 | ** a bunch of random characters as the tag. |
| 1883 | ** |
| 1884 | ** Dangerous characters in zBasis are changed. |
| 1885 | */ |
| 1886 | void file_tempname(Blob *pBuf, const char *zBasis, const char *zTag){ |
| 1887 | #if defined(_WIN32) |
| 1888 | const char *azDirs[] = { |
| 1889 | 0, /* GetTempPath */ |
| @@ -1959,15 +1961,16 @@ | |
| 1959 | } |
| 1960 | do{ |
| 1961 | blob_zero(pBuf); |
| 1962 | if( cnt++>20 ) fossil_fatal("cannot generate a temporary filename"); |
| 1963 | if( zTag==0 ){ |
| 1964 | sqlite3_randomness(15, zRand); |
| 1965 | for(i=0; i<15; i++){ |
| 1966 | zRand[i] = (char)zChars[ ((unsigned char)zRand[i])%(sizeof(zChars)-1) ]; |
| 1967 | } |
| 1968 | zRand[15] = 0; |
| 1969 | zTag = zRand; |
| 1970 | } |
| 1971 | blob_appendf(pBuf, "%s/%.*s~%s%s", zDir, nBasis, zBasis, zTag, zSuffix); |
| 1972 | zTag = 0; |
| 1973 | for(z=blob_str(pBuf); z!=0 && (z=strpbrk(z,"'\"`;|$&"))!=0; z++){ |
| @@ -1989,10 +1992,12 @@ | |
| 1989 | } |
| 1990 | |
| 1991 | /* |
| 1992 | ** Compute a temporary filename in zDir. The filename is based on |
| 1993 | ** the current time. |
| 1994 | */ |
| 1995 | char *file_time_tempname(const char *zDir, const char *zSuffix){ |
| 1996 | struct tm *tm; |
| 1997 | unsigned int r; |
| 1998 | static unsigned int cnt = 0; |
| @@ -2011,18 +2016,27 @@ | |
| 2011 | ** Usage: fossil test-name [--time SUFFIX] [--tag NAME] BASENAME ... |
| 2012 | ** |
| 2013 | ** Generate temporary filenames derived from BASENAME. Use the --time |
| 2014 | ** option to generate temp names based on the time of day. If --tag NAME |
| 2015 | ** is specified, try to use NAME as the differentiator in the temp file. |
| 2016 | */ |
| 2017 | void file_test_tempname(void){ |
| 2018 | int i; |
| 2019 | const char *zSuffix = find_option("time",0,1); |
| 2020 | Blob x = BLOB_INITIALIZER; |
| 2021 | char *z; |
| 2022 | const char *zTag = find_option("tag",0,1); |
| 2023 | verify_all_options(); |
| 2024 | for(i=2; i<g.argc; i++){ |
| 2025 | if( zSuffix ){ |
| 2026 | z = file_time_tempname(g.argv[i], zSuffix); |
| 2027 | fossil_print("%s\n", z); |
| 2028 | fossil_free(z); |
| 2029 |
| --- src/file.c | |
| +++ src/file.c | |
| @@ -1880,10 +1880,12 @@ | |
| 1880 | ** If zTag is not NULL, then try to create the temp-file using zTag |
| 1881 | ** as a differentiator. If that fails, or if zTag is NULL, then use |
| 1882 | ** a bunch of random characters as the tag. |
| 1883 | ** |
| 1884 | ** Dangerous characters in zBasis are changed. |
| 1885 | ** |
| 1886 | ** See also fossil_temp_filename() and file_time_tempname(); |
| 1887 | */ |
| 1888 | void file_tempname(Blob *pBuf, const char *zBasis, const char *zTag){ |
| 1889 | #if defined(_WIN32) |
| 1890 | const char *azDirs[] = { |
| 1891 | 0, /* GetTempPath */ |
| @@ -1959,15 +1961,16 @@ | |
| 1961 | } |
| 1962 | do{ |
| 1963 | blob_zero(pBuf); |
| 1964 | if( cnt++>20 ) fossil_fatal("cannot generate a temporary filename"); |
| 1965 | if( zTag==0 ){ |
| 1966 | const int nRand = sizeof(zRand)-1; |
| 1967 | sqlite3_randomness(nRand, zRand); |
| 1968 | for(i=0; i<nRand; i++){ |
| 1969 | zRand[i] = (char)zChars[ ((unsigned char)zRand[i])%(sizeof(zChars)-1) ]; |
| 1970 | } |
| 1971 | zRand[nRand] = 0; |
| 1972 | zTag = zRand; |
| 1973 | } |
| 1974 | blob_appendf(pBuf, "%s/%.*s~%s%s", zDir, nBasis, zBasis, zTag, zSuffix); |
| 1975 | zTag = 0; |
| 1976 | for(z=blob_str(pBuf); z!=0 && (z=strpbrk(z,"'\"`;|$&"))!=0; z++){ |
| @@ -1989,10 +1992,12 @@ | |
| 1992 | } |
| 1993 | |
| 1994 | /* |
| 1995 | ** Compute a temporary filename in zDir. The filename is based on |
| 1996 | ** the current time. |
| 1997 | ** |
| 1998 | ** See also fossil_temp_filename() and file_tempname(); |
| 1999 | */ |
| 2000 | char *file_time_tempname(const char *zDir, const char *zSuffix){ |
| 2001 | struct tm *tm; |
| 2002 | unsigned int r; |
| 2003 | static unsigned int cnt = 0; |
| @@ -2011,18 +2016,27 @@ | |
| 2016 | ** Usage: fossil test-name [--time SUFFIX] [--tag NAME] BASENAME ... |
| 2017 | ** |
| 2018 | ** Generate temporary filenames derived from BASENAME. Use the --time |
| 2019 | ** option to generate temp names based on the time of day. If --tag NAME |
| 2020 | ** is specified, try to use NAME as the differentiator in the temp file. |
| 2021 | ** |
| 2022 | ** If --time is used, file_time_tempname() generates the filename. |
| 2023 | ** If BASENAME is present, file_tempname() generates the filename. |
| 2024 | ** Without --time or BASENAME, fossil_temp_filename() generates the filename. |
| 2025 | */ |
| 2026 | void file_test_tempname(void){ |
| 2027 | int i; |
| 2028 | const char *zSuffix = find_option("time",0,1); |
| 2029 | Blob x = BLOB_INITIALIZER; |
| 2030 | char *z; |
| 2031 | const char *zTag = find_option("tag",0,1); |
| 2032 | verify_all_options(); |
| 2033 | if( g.argc<=2 ){ |
| 2034 | z = fossil_temp_filename(); |
| 2035 | fossil_print("%s\n", z); |
| 2036 | sqlite3_free(z); |
| 2037 | } |
| 2038 | for(i=2; i<g.argc; i++){ |
| 2039 | if( zSuffix ){ |
| 2040 | z = file_time_tempname(g.argv[i], zSuffix); |
| 2041 | fossil_print("%s\n", z); |
| 2042 | fossil_free(z); |
| 2043 |
+2
| --- src/util.c | ||
| +++ src/util.c | ||
| @@ -645,10 +645,12 @@ | ||
| 645 | 645 | /* |
| 646 | 646 | ** Construct a temporary filename. |
| 647 | 647 | ** |
| 648 | 648 | ** The returned string is obtained from sqlite3_malloc() and must be |
| 649 | 649 | ** freed by the caller. |
| 650 | +** | |
| 651 | +** See also: file_tempname() and file_time_timename(); | |
| 650 | 652 | */ |
| 651 | 653 | char *fossil_temp_filename(void){ |
| 652 | 654 | char *zTFile = 0; |
| 653 | 655 | const char *zDir; |
| 654 | 656 | char cDirSep; |
| 655 | 657 |
| --- src/util.c | |
| +++ src/util.c | |
| @@ -645,10 +645,12 @@ | |
| 645 | /* |
| 646 | ** Construct a temporary filename. |
| 647 | ** |
| 648 | ** The returned string is obtained from sqlite3_malloc() and must be |
| 649 | ** freed by the caller. |
| 650 | */ |
| 651 | char *fossil_temp_filename(void){ |
| 652 | char *zTFile = 0; |
| 653 | const char *zDir; |
| 654 | char cDirSep; |
| 655 |
| --- src/util.c | |
| +++ src/util.c | |
| @@ -645,10 +645,12 @@ | |
| 645 | /* |
| 646 | ** Construct a temporary filename. |
| 647 | ** |
| 648 | ** The returned string is obtained from sqlite3_malloc() and must be |
| 649 | ** freed by the caller. |
| 650 | ** |
| 651 | ** See also: file_tempname() and file_time_timename(); |
| 652 | */ |
| 653 | char *fossil_temp_filename(void){ |
| 654 | char *zTFile = 0; |
| 655 | const char *zDir; |
| 656 | char cDirSep; |
| 657 |