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.

drh 2021-12-22 15:17 trunk
Commit 1aff463371d637654b72e4c0131a351fb65b6c61c845a5fc70c0b46aba58d85a
2 files changed +17 -3 +2
+17 -3
--- src/file.c
+++ src/file.c
@@ -1880,10 +1880,12 @@
18801880
** If zTag is not NULL, then try to create the temp-file using zTag
18811881
** as a differentiator. If that fails, or if zTag is NULL, then use
18821882
** a bunch of random characters as the tag.
18831883
**
18841884
** Dangerous characters in zBasis are changed.
1885
+**
1886
+** See also fossil_temp_filename() and file_time_tempname();
18851887
*/
18861888
void file_tempname(Blob *pBuf, const char *zBasis, const char *zTag){
18871889
#if defined(_WIN32)
18881890
const char *azDirs[] = {
18891891
0, /* GetTempPath */
@@ -1959,15 +1961,16 @@
19591961
}
19601962
do{
19611963
blob_zero(pBuf);
19621964
if( cnt++>20 ) fossil_fatal("cannot generate a temporary filename");
19631965
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++){
19661969
zRand[i] = (char)zChars[ ((unsigned char)zRand[i])%(sizeof(zChars)-1) ];
19671970
}
1968
- zRand[15] = 0;
1971
+ zRand[nRand] = 0;
19691972
zTag = zRand;
19701973
}
19711974
blob_appendf(pBuf, "%s/%.*s~%s%s", zDir, nBasis, zBasis, zTag, zSuffix);
19721975
zTag = 0;
19731976
for(z=blob_str(pBuf); z!=0 && (z=strpbrk(z,"'\"`;|$&"))!=0; z++){
@@ -1989,10 +1992,12 @@
19891992
}
19901993
19911994
/*
19921995
** Compute a temporary filename in zDir. The filename is based on
19931996
** the current time.
1997
+**
1998
+** See also fossil_temp_filename() and file_tempname();
19941999
*/
19952000
char *file_time_tempname(const char *zDir, const char *zSuffix){
19962001
struct tm *tm;
19972002
unsigned int r;
19982003
static unsigned int cnt = 0;
@@ -2011,18 +2016,27 @@
20112016
** Usage: fossil test-name [--time SUFFIX] [--tag NAME] BASENAME ...
20122017
**
20132018
** Generate temporary filenames derived from BASENAME. Use the --time
20142019
** option to generate temp names based on the time of day. If --tag NAME
20152020
** 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.
20162025
*/
20172026
void file_test_tempname(void){
20182027
int i;
20192028
const char *zSuffix = find_option("time",0,1);
20202029
Blob x = BLOB_INITIALIZER;
20212030
char *z;
20222031
const char *zTag = find_option("tag",0,1);
20232032
verify_all_options();
2033
+ if( g.argc<=2 ){
2034
+ z = fossil_temp_filename();
2035
+ fossil_print("%s\n", z);
2036
+ sqlite3_free(z);
2037
+ }
20242038
for(i=2; i<g.argc; i++){
20252039
if( zSuffix ){
20262040
z = file_time_tempname(g.argv[i], zSuffix);
20272041
fossil_print("%s\n", z);
20282042
fossil_free(z);
20292043
--- 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 @@
645645
/*
646646
** Construct a temporary filename.
647647
**
648648
** The returned string is obtained from sqlite3_malloc() and must be
649649
** freed by the caller.
650
+**
651
+** See also: file_tempname() and file_time_timename();
650652
*/
651653
char *fossil_temp_filename(void){
652654
char *zTFile = 0;
653655
const char *zDir;
654656
char cDirSep;
655657
--- 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

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button