|
dbda8d6…
|
drh
|
1 |
/* |
|
f15ce59…
|
mistachkin
|
2 |
** Copyright (c) 2006 D. Richard Hipp |
|
dbda8d6…
|
drh
|
3 |
** |
|
dbda8d6…
|
drh
|
4 |
** This program is free software; you can redistribute it and/or |
|
c06edd2…
|
drh
|
5 |
** modify it under the terms of the Simplified BSD License (also |
|
c06edd2…
|
drh
|
6 |
** known as the "2-Clause License" or "FreeBSD License".) |
|
1772357…
|
drh
|
7 |
** |
|
dbda8d6…
|
drh
|
8 |
** This program is distributed in the hope that it will be useful, |
|
c06edd2…
|
drh
|
9 |
** but without any warranty; without even the implied warranty of |
|
c06edd2…
|
drh
|
10 |
** merchantability or fitness for a particular purpose. |
|
dbda8d6…
|
drh
|
11 |
** |
|
dbda8d6…
|
drh
|
12 |
** Author contact information: |
|
dbda8d6…
|
drh
|
13 |
** [email protected] |
|
dbda8d6…
|
drh
|
14 |
** http://www.hwaci.com/drh/ |
|
dbda8d6…
|
drh
|
15 |
** |
|
dbda8d6…
|
drh
|
16 |
******************************************************************************* |
|
dbda8d6…
|
drh
|
17 |
** |
|
c05f6af…
|
dmitry
|
18 |
** File utilities. |
|
dbda8d6…
|
drh
|
19 |
*/ |
|
dbda8d6…
|
drh
|
20 |
#include "config.h" |
|
dbda8d6…
|
drh
|
21 |
#include <sys/types.h> |
|
dbda8d6…
|
drh
|
22 |
#include <sys/stat.h> |
|
dbda8d6…
|
drh
|
23 |
#include <unistd.h> |
|
1f694e1…
|
mistachkin
|
24 |
#include <stdio.h> |
|
89e6eb9…
|
drh
|
25 |
#include <string.h> |
|
89e6eb9…
|
drh
|
26 |
#include <errno.h> |
|
38165f3…
|
drh
|
27 |
#include <time.h> |
|
dbda8d6…
|
drh
|
28 |
#include "file.h" |
|
dbda8d6…
|
drh
|
29 |
|
|
390b414…
|
drh
|
30 |
/* |
|
135ed93…
|
drh
|
31 |
** On Windows, include the Platform SDK header file. |
|
135ed93…
|
drh
|
32 |
*/ |
|
135ed93…
|
drh
|
33 |
#ifdef _WIN32 |
|
14733d1…
|
jan.nijtmans
|
34 |
# include <direct.h> |
|
135ed93…
|
drh
|
35 |
# include <windows.h> |
|
0c37874…
|
drh
|
36 |
# include <sys/utime.h> |
|
bf160cf…
|
drh
|
37 |
#else |
|
b963446…
|
drh
|
38 |
# include <sys/time.h> |
|
effdada…
|
drh
|
39 |
# include <pwd.h> |
|
effdada…
|
drh
|
40 |
# include <grp.h> |
|
135ed93…
|
drh
|
41 |
#endif |
|
135ed93…
|
drh
|
42 |
|
|
d9ff968…
|
mistachkin
|
43 |
#if INTERFACE |
|
1772357…
|
drh
|
44 |
|
|
99a319b…
|
wyoung
|
45 |
/* Many APIs take an eFType argument which must be one of ExtFILE, RepoFILE, |
|
1772357…
|
drh
|
46 |
** or SymFILE. |
|
1772357…
|
drh
|
47 |
** |
|
1772357…
|
drh
|
48 |
** The difference is in the handling of symbolic links. RepoFILE should be |
|
1772357…
|
drh
|
49 |
** used for files that are under management by a Fossil repository. ExtFILE |
|
1772357…
|
drh
|
50 |
** should be used for files that are not under management. SymFILE is for |
|
1772357…
|
drh
|
51 |
** a few special cases such as the "fossil test-tarball" command when we never |
|
1772357…
|
drh
|
52 |
** want to follow symlinks. |
|
1772357…
|
drh
|
53 |
** |
|
f741baa…
|
drh
|
54 |
** ExtFILE Symbolic links always refer to the object to which the |
|
f741baa…
|
drh
|
55 |
** link points. Symlinks are never recognized as symlinks but |
|
33d3bf3…
|
km
|
56 |
** instead always appear to be the target object. |
|
f741baa…
|
drh
|
57 |
** |
|
f741baa…
|
drh
|
58 |
** SymFILE Symbolic links always appear to be files whose name is |
|
f741baa…
|
drh
|
59 |
** the target pathname of the symbolic link. |
|
f741baa…
|
drh
|
60 |
** |
|
0537925…
|
andygoth
|
61 |
** RepoFILE Like SymFILE if allow-symlinks is true, or like |
|
0537925…
|
andygoth
|
62 |
** ExtFILE if allow-symlinks is false. In other words, |
|
f741baa…
|
drh
|
63 |
** symbolic links are only recognized as something different |
|
f741baa…
|
drh
|
64 |
** from files or directories if allow-symlinks is true. |
|
f741baa…
|
drh
|
65 |
*/ |
|
1f184d0…
|
danield
|
66 |
#include <stdlib.h> |
|
6d0d1f7…
|
drh
|
67 |
#define ExtFILE 0 /* Always follow symlinks */ |
|
6d0d1f7…
|
drh
|
68 |
#define RepoFILE 1 /* Follow symlinks if and only if allow-symlinks is OFF */ |
|
6d0d1f7…
|
drh
|
69 |
#define SymFILE 2 /* Never follow symlinks */ |
|
d9ff968…
|
mistachkin
|
70 |
|
|
d9ff968…
|
mistachkin
|
71 |
#include <dirent.h> |
|
d9ff968…
|
mistachkin
|
72 |
#if defined(_WIN32) |
|
d9ff968…
|
mistachkin
|
73 |
# define DIR _WDIR |
|
d9ff968…
|
mistachkin
|
74 |
# define dirent _wdirent |
|
d9ff968…
|
mistachkin
|
75 |
# define opendir _wopendir |
|
d9ff968…
|
mistachkin
|
76 |
# define readdir _wreaddir |
|
d9ff968…
|
mistachkin
|
77 |
# define closedir _wclosedir |
|
d9ff968…
|
mistachkin
|
78 |
#endif /* _WIN32 */ |
|
d9ff968…
|
mistachkin
|
79 |
|
|
6092935…
|
drh
|
80 |
#if defined(_WIN32) && (defined(__MSVCRT__) || defined(_MSC_VER)) |
|
1772357…
|
drh
|
81 |
/* |
|
1772357…
|
drh
|
82 |
** File status information for windows systems. |
|
1772357…
|
drh
|
83 |
*/ |
|
d9ff968…
|
mistachkin
|
84 |
struct fossilStat { |
|
3714782…
|
jan.nijtmans
|
85 |
i64 st_size; |
|
3714782…
|
jan.nijtmans
|
86 |
i64 st_mtime; |
|
3714782…
|
jan.nijtmans
|
87 |
int st_mode; |
|
3714782…
|
jan.nijtmans
|
88 |
}; |
|
3714782…
|
jan.nijtmans
|
89 |
#endif |
|
d9ff968…
|
mistachkin
|
90 |
|
|
b523bf0…
|
mistachkin
|
91 |
#if defined(_WIN32) || defined(__CYGWIN__) |
|
b523bf0…
|
mistachkin
|
92 |
# define fossil_isdirsep(a) (((a) == '/') || ((a) == '\\')) |
|
b523bf0…
|
mistachkin
|
93 |
#else |
|
b523bf0…
|
mistachkin
|
94 |
# define fossil_isdirsep(a) ((a) == '/') |
|
b523bf0…
|
mistachkin
|
95 |
#endif |
|
b523bf0…
|
mistachkin
|
96 |
|
|
d9ff968…
|
mistachkin
|
97 |
#endif /* INTERFACE */ |
|
d9ff968…
|
mistachkin
|
98 |
|
|
d9ff968…
|
mistachkin
|
99 |
#if !defined(_WIN32) || !(defined(__MSVCRT__) || defined(_MSC_VER)) |
|
1772357…
|
drh
|
100 |
/* |
|
1772357…
|
drh
|
101 |
** File status information for unix systems |
|
1772357…
|
drh
|
102 |
*/ |
|
d9ff968…
|
mistachkin
|
103 |
# define fossilStat stat |
|
d9ff968…
|
mistachkin
|
104 |
#endif |
|
d9ff968…
|
mistachkin
|
105 |
|
|
c05f6af…
|
dmitry
|
106 |
/* |
|
c05f6af…
|
dmitry
|
107 |
** On Windows S_ISLNK always returns FALSE. |
|
c05f6af…
|
dmitry
|
108 |
*/ |
|
fb07d86…
|
jan.nijtmans
|
109 |
#if !defined(S_ISLNK) |
|
c05f6af…
|
dmitry
|
110 |
# define S_ISLNK(x) (0) |
|
e4f1c1f…
|
drh
|
111 |
#endif |
|
e4f1c1f…
|
drh
|
112 |
|
|
c05f6af…
|
dmitry
|
113 |
/* |
|
1772357…
|
drh
|
114 |
** Local state information for the file status routines |
|
1772357…
|
drh
|
115 |
*/ |
|
1772357…
|
drh
|
116 |
static struct { |
|
1772357…
|
drh
|
117 |
struct fossilStat fileStat; /* File status from last fossil_stat() */ |
|
1772357…
|
drh
|
118 |
int fileStatValid; /* True if fileStat is valid */ |
|
1772357…
|
drh
|
119 |
} fx; |
|
1772357…
|
drh
|
120 |
|
|
1772357…
|
drh
|
121 |
/* |
|
1772357…
|
drh
|
122 |
** Fill *buf with information about zFilename. |
|
1772357…
|
drh
|
123 |
** |
|
1772357…
|
drh
|
124 |
** If zFilename refers to a symbolic link: |
|
1772357…
|
drh
|
125 |
** |
|
1772357…
|
drh
|
126 |
** (A) If allow-symlinks is on and eFType is RepoFILE, then fill |
|
1772357…
|
drh
|
127 |
** *buf with information about the symbolic link itself. |
|
1772357…
|
drh
|
128 |
** |
|
1772357…
|
drh
|
129 |
** (B) If allow-symlinks is off or eFType is ExtFILE, then fill |
|
1772357…
|
drh
|
130 |
** *buf with information about the object that the symbolic link |
|
1772357…
|
drh
|
131 |
** points to. |
|
c05f6af…
|
dmitry
|
132 |
*/ |
|
2375d6c…
|
mistachkin
|
133 |
static int fossil_stat( |
|
2375d6c…
|
mistachkin
|
134 |
const char *zFilename, /* name of file or directory to inspect. */ |
|
2375d6c…
|
mistachkin
|
135 |
struct fossilStat *buf, /* pointer to buffer where info should go. */ |
|
1772357…
|
drh
|
136 |
int eFType /* Look at symlink itself if RepoFILE and enabled. */ |
|
2375d6c…
|
mistachkin
|
137 |
){ |
|
caf2eb2…
|
jan.nijtmans
|
138 |
int rc; |
|
9571b68…
|
drh
|
139 |
void *zMbcs = fossil_utf8_to_path(zFilename, 0); |
|
503482a…
|
jan.nijtmans
|
140 |
#if !defined(_WIN32) |
|
4d445ea…
|
drh
|
141 |
if( (eFType==RepoFILE && db_allow_symlinks()) |
|
f741baa…
|
drh
|
142 |
|| eFType==SymFILE ){ |
|
f741baa…
|
drh
|
143 |
/* Symlinks look like files whose content is the name of the target */ |
|
caf2eb2…
|
jan.nijtmans
|
144 |
rc = lstat(zMbcs, buf); |
|
e4f1c1f…
|
drh
|
145 |
}else{ |
|
f741baa…
|
drh
|
146 |
/* Symlinks look like the object to which they point */ |
|
caf2eb2…
|
jan.nijtmans
|
147 |
rc = stat(zMbcs, buf); |
|
e4f1c1f…
|
drh
|
148 |
} |
|
503482a…
|
jan.nijtmans
|
149 |
#else |
|
1772357…
|
drh
|
150 |
rc = win32_stat(zMbcs, buf, eFType); |
|
503482a…
|
jan.nijtmans
|
151 |
#endif |
|
9571b68…
|
drh
|
152 |
fossil_path_free(zMbcs); |
|
c05f6af…
|
dmitry
|
153 |
return rc; |
|
e4f1c1f…
|
drh
|
154 |
} |
|
2932380…
|
drh
|
155 |
|
|
2932380…
|
drh
|
156 |
/* |
|
1772357…
|
drh
|
157 |
** Clears the fx.fileStat variable and its associated validity flag. |
|
2375d6c…
|
mistachkin
|
158 |
*/ |
|
2375d6c…
|
mistachkin
|
159 |
static void resetStat(){ |
|
1772357…
|
drh
|
160 |
fx.fileStatValid = 0; |
|
1772357…
|
drh
|
161 |
memset(&fx.fileStat, 0, sizeof(struct fossilStat)); |
|
2375d6c…
|
mistachkin
|
162 |
} |
|
2375d6c…
|
mistachkin
|
163 |
|
|
2375d6c…
|
mistachkin
|
164 |
/* |
|
1772357…
|
drh
|
165 |
** Fill in the fx.fileStat variable for the file named zFilename. |
|
1772357…
|
drh
|
166 |
** If zFilename==0, then use the previous value of fx.fileStat if |
|
390b414…
|
drh
|
167 |
** there is a previous value. |
|
390b414…
|
drh
|
168 |
** |
|
390b414…
|
drh
|
169 |
** Return the number of errors. No error messages are generated. |
|
390b414…
|
drh
|
170 |
*/ |
|
1772357…
|
drh
|
171 |
static int getStat(const char *zFilename, int eFType){ |
|
16e703b…
|
drh
|
172 |
int rc = 0; |
|
390b414…
|
drh
|
173 |
if( zFilename==0 ){ |
|
1772357…
|
drh
|
174 |
if( fx.fileStatValid==0 ) rc = 1; |
|
390b414…
|
drh
|
175 |
}else{ |
|
1772357…
|
drh
|
176 |
if( fossil_stat(zFilename, &fx.fileStat, eFType)!=0 ){ |
|
1772357…
|
drh
|
177 |
fx.fileStatValid = 0; |
|
16e703b…
|
drh
|
178 |
rc = 1; |
|
16e703b…
|
drh
|
179 |
}else{ |
|
1772357…
|
drh
|
180 |
fx.fileStatValid = 1; |
|
16e703b…
|
drh
|
181 |
rc = 0; |
|
16e703b…
|
drh
|
182 |
} |
|
390b414…
|
drh
|
183 |
} |
|
16e703b…
|
drh
|
184 |
return rc; |
|
390b414…
|
drh
|
185 |
} |
|
dbda8d6…
|
drh
|
186 |
|
|
dbda8d6…
|
drh
|
187 |
/* |
|
dbda8d6…
|
drh
|
188 |
** Return the size of a file in bytes. Return -1 if the file does not |
|
390b414…
|
drh
|
189 |
** exist. If zFilename is NULL, return the size of the most recently |
|
390b414…
|
drh
|
190 |
** stat-ed file. |
|
dbda8d6…
|
drh
|
191 |
*/ |
|
1772357…
|
drh
|
192 |
i64 file_size(const char *zFilename, int eFType){ |
|
1772357…
|
drh
|
193 |
return getStat(zFilename, eFType) ? -1 : fx.fileStat.st_size; |
|
dbda8d6…
|
drh
|
194 |
} |
|
dbda8d6…
|
drh
|
195 |
|
|
dbda8d6…
|
drh
|
196 |
/* |
|
dbda8d6…
|
drh
|
197 |
** Return the modification time for a file. Return -1 if the file |
|
390b414…
|
drh
|
198 |
** does not exist. If zFilename is NULL return the size of the most |
|
390b414…
|
drh
|
199 |
** recently stat-ed file. |
|
390b414…
|
drh
|
200 |
*/ |
|
1772357…
|
drh
|
201 |
i64 file_mtime(const char *zFilename, int eFType){ |
|
1772357…
|
drh
|
202 |
return getStat(zFilename, eFType) ? -1 : fx.fileStat.st_mtime; |
|
e4f1c1f…
|
drh
|
203 |
} |
|
e4f1c1f…
|
drh
|
204 |
|
|
e4f1c1f…
|
drh
|
205 |
/* |
|
2375d6c…
|
mistachkin
|
206 |
** Return the mode bits for a file. Return -1 if the file does not |
|
2375d6c…
|
mistachkin
|
207 |
** exist. If zFilename is NULL return the size of the most recently |
|
2375d6c…
|
mistachkin
|
208 |
** stat-ed file. |
|
c05f6af…
|
dmitry
|
209 |
*/ |
|
1772357…
|
drh
|
210 |
int file_mode(const char *zFilename, int eFType){ |
|
53db40e…
|
drh
|
211 |
return getStat(zFilename, eFType) ? -1 : (int)(fx.fileStat.st_mode); |
|
c05f6af…
|
dmitry
|
212 |
} |
|
c05f6af…
|
dmitry
|
213 |
|
|
c05f6af…
|
dmitry
|
214 |
/* |
|
1772357…
|
drh
|
215 |
** Return TRUE if either of the following are true: |
|
1772357…
|
drh
|
216 |
** |
|
1772357…
|
drh
|
217 |
** (1) zFilename is an ordinary file |
|
1772357…
|
drh
|
218 |
** |
|
1772357…
|
drh
|
219 |
** (2) allow_symlinks is on and zFilename is a symbolic link to |
|
1772357…
|
drh
|
220 |
** a file, directory, or other object |
|
1772357…
|
drh
|
221 |
*/ |
|
1772357…
|
drh
|
222 |
int file_isfile_or_link(const char *zFilename){ |
|
1772357…
|
drh
|
223 |
if( getStat(zFilename, RepoFILE) ){ |
|
1772357…
|
drh
|
224 |
return 0; /* stat() failed. Return false. */ |
|
1772357…
|
drh
|
225 |
} |
|
1772357…
|
drh
|
226 |
return S_ISREG(fx.fileStat.st_mode) || S_ISLNK(fx.fileStat.st_mode); |
|
390b414…
|
drh
|
227 |
} |
|
390b414…
|
drh
|
228 |
|
|
390b414…
|
drh
|
229 |
/* |
|
390b414…
|
drh
|
230 |
** Return TRUE if the named file is an ordinary file. Return false |
|
390b414…
|
drh
|
231 |
** for directories, devices, fifos, symlinks, etc. |
|
390b414…
|
drh
|
232 |
*/ |
|
1772357…
|
drh
|
233 |
int file_isfile(const char *zFilename, int eFType){ |
|
1772357…
|
drh
|
234 |
return getStat(zFilename, eFType) ? 0 : S_ISREG(fx.fileStat.st_mode); |
|
c97535c…
|
dmitry
|
235 |
} |
|
c97535c…
|
dmitry
|
236 |
|
|
c97535c…
|
dmitry
|
237 |
/* |
|
effdada…
|
drh
|
238 |
** Return TRUE if zFilename is a socket. |
|
effdada…
|
drh
|
239 |
*/ |
|
effdada…
|
drh
|
240 |
int file_issocket(const char *zFilename){ |
|
ba88445…
|
stephan
|
241 |
#ifdef _WIN32 |
|
ba88445…
|
stephan
|
242 |
return 0; |
|
ba88445…
|
stephan
|
243 |
#else |
|
effdada…
|
drh
|
244 |
if( getStat(zFilename, ExtFILE) ){ |
|
effdada…
|
drh
|
245 |
return 0; /* stat() failed. Return false. */ |
|
effdada…
|
drh
|
246 |
} |
|
effdada…
|
drh
|
247 |
return S_ISSOCK(fx.fileStat.st_mode); |
|
ba88445…
|
stephan
|
248 |
#endif |
|
effdada…
|
drh
|
249 |
} |
|
effdada…
|
drh
|
250 |
|
|
effdada…
|
drh
|
251 |
/* |
|
1772357…
|
drh
|
252 |
** Create a symbolic link named zLinkFile that points to zTargetFile. |
|
c97535c…
|
dmitry
|
253 |
** |
|
1772357…
|
drh
|
254 |
** If allow-symlinks is off, create an ordinary file named zLinkFile |
|
1772357…
|
drh
|
255 |
** with the name of zTargetFile as its content. |
|
e4f1c1f…
|
drh
|
256 |
**/ |
|
c05f6af…
|
dmitry
|
257 |
void symlink_create(const char *zTargetFile, const char *zLinkFile){ |
|
e4f1c1f…
|
drh
|
258 |
#if !defined(_WIN32) |
|
b727218…
|
drh
|
259 |
if( db_allow_symlinks() ){ |
|
e4f1c1f…
|
drh
|
260 |
int i, nName; |
|
e4f1c1f…
|
drh
|
261 |
char *zName, zBuf[1000]; |
|
e4f1c1f…
|
drh
|
262 |
|
|
e4f1c1f…
|
drh
|
263 |
nName = strlen(zLinkFile); |
|
53db40e…
|
drh
|
264 |
if( nName>=(int)sizeof(zBuf) ){ |
|
4c3e172…
|
danield
|
265 |
zName = fossil_strdup(zLinkFile); |
|
e4f1c1f…
|
drh
|
266 |
}else{ |
|
e4f1c1f…
|
drh
|
267 |
zName = zBuf; |
|
e4f1c1f…
|
drh
|
268 |
memcpy(zName, zLinkFile, nName+1); |
|
e4f1c1f…
|
drh
|
269 |
} |
|
135ed93…
|
drh
|
270 |
nName = file_simplify_name(zName, nName, 0); |
|
e4f1c1f…
|
drh
|
271 |
for(i=1; i<nName; i++){ |
|
e4f1c1f…
|
drh
|
272 |
if( zName[i]=='/' ){ |
|
e4f1c1f…
|
drh
|
273 |
zName[i] = 0; |
|
1772357…
|
drh
|
274 |
if( file_mkdir(zName, ExtFILE, 1) ){ |
|
1772357…
|
drh
|
275 |
fossil_fatal_recursive("unable to create directory %s", zName); |
|
1772357…
|
drh
|
276 |
return; |
|
1772357…
|
drh
|
277 |
} |
|
e4f1c1f…
|
drh
|
278 |
zName[i] = '/'; |
|
e4f1c1f…
|
drh
|
279 |
} |
|
e4f1c1f…
|
drh
|
280 |
} |
|
e4f1c1f…
|
drh
|
281 |
if( symlink(zTargetFile, zName)!=0 ){ |
|
f668ff4…
|
drh
|
282 |
fossil_fatal_recursive("unable to create symlink \"%s\"", zName); |
|
e4f1c1f…
|
drh
|
283 |
} |
|
04ab1af…
|
drh
|
284 |
if( zName!=zBuf ) free(zName); |
|
e4f1c1f…
|
drh
|
285 |
}else |
|
f668ff4…
|
drh
|
286 |
#endif |
|
e4f1c1f…
|
drh
|
287 |
{ |
|
e4f1c1f…
|
drh
|
288 |
Blob content; |
|
e4f1c1f…
|
drh
|
289 |
blob_set(&content, zTargetFile); |
|
e4f1c1f…
|
drh
|
290 |
blob_write_to_file(&content, zLinkFile); |
|
e4f1c1f…
|
drh
|
291 |
blob_reset(&content); |
|
e4f1c1f…
|
drh
|
292 |
} |
|
e4f1c1f…
|
drh
|
293 |
} |
|
e4f1c1f…
|
drh
|
294 |
|
|
e4f1c1f…
|
drh
|
295 |
/* |
|
c05f6af…
|
dmitry
|
296 |
** Copy symbolic link from zFrom to zTo. |
|
c05f6af…
|
dmitry
|
297 |
*/ |
|
c05f6af…
|
dmitry
|
298 |
void symlink_copy(const char *zFrom, const char *zTo){ |
|
c05f6af…
|
dmitry
|
299 |
Blob content; |
|
c05f6af…
|
dmitry
|
300 |
blob_read_link(&content, zFrom); |
|
c05f6af…
|
dmitry
|
301 |
symlink_create(blob_str(&content), zTo); |
|
c05f6af…
|
dmitry
|
302 |
blob_reset(&content); |
|
c05f6af…
|
dmitry
|
303 |
} |
|
c05f6af…
|
dmitry
|
304 |
|
|
c05f6af…
|
dmitry
|
305 |
/* |
|
c97535c…
|
dmitry
|
306 |
** Return file permissions (normal, executable, or symlink): |
|
dd94e59…
|
jan.nijtmans
|
307 |
** - PERM_EXE on Unix if file is executable; |
|
c97535c…
|
dmitry
|
308 |
** - PERM_LNK on Unix if file is symlink and allow-symlinks option is on; |
|
c97535c…
|
dmitry
|
309 |
** - PERM_REG for all other cases (regular file, directory, fifo, etc). |
|
1772357…
|
drh
|
310 |
** |
|
1772357…
|
drh
|
311 |
** If eFType is ExtFile then symbolic links are followed and so this |
|
1772357…
|
drh
|
312 |
** routine can only return PERM_EXE and PERM_REG. |
|
1772357…
|
drh
|
313 |
** |
|
1772357…
|
drh
|
314 |
** On windows, this routine returns only PERM_REG. |
|
c97535c…
|
dmitry
|
315 |
*/ |
|
1772357…
|
drh
|
316 |
int file_perm(const char *zFilename, int eFType){ |
|
dd94e59…
|
jan.nijtmans
|
317 |
#if !defined(_WIN32) |
|
388b5e5…
|
drh
|
318 |
if( !getStat(zFilename, eFType) ){ |
|
1772357…
|
drh
|
319 |
if( S_ISREG(fx.fileStat.st_mode) && ((S_IXUSR)&fx.fileStat.st_mode)!=0 ) |
|
dd94e59…
|
jan.nijtmans
|
320 |
return PERM_EXE; |
|
1772357…
|
drh
|
321 |
else if( db_allow_symlinks() && S_ISLNK(fx.fileStat.st_mode) ) |
|
dd94e59…
|
jan.nijtmans
|
322 |
return PERM_LNK; |
|
dd94e59…
|
jan.nijtmans
|
323 |
} |
|
c97535c…
|
dmitry
|
324 |
#endif |
|
dd94e59…
|
jan.nijtmans
|
325 |
return PERM_REG; |
|
c97535c…
|
dmitry
|
326 |
} |
|
c97535c…
|
dmitry
|
327 |
|
|
c97535c…
|
dmitry
|
328 |
/* |
|
390b414…
|
drh
|
329 |
** Return TRUE if the named file is an executable. Return false |
|
390b414…
|
drh
|
330 |
** for directories, devices, fifos, symlinks, etc. |
|
390b414…
|
drh
|
331 |
*/ |
|
1772357…
|
drh
|
332 |
int file_isexe(const char *zFilename, int eFType){ |
|
1772357…
|
drh
|
333 |
return file_perm(zFilename, eFType)==PERM_EXE; |
|
c97535c…
|
dmitry
|
334 |
} |
|
c97535c…
|
dmitry
|
335 |
|
|
c97535c…
|
dmitry
|
336 |
/* |
|
c97535c…
|
dmitry
|
337 |
** Return TRUE if the named file is a symlink and symlinks are allowed. |
|
c97535c…
|
dmitry
|
338 |
** Return false for all other cases. |
|
c97535c…
|
dmitry
|
339 |
** |
|
f741baa…
|
drh
|
340 |
** This routines assumes RepoFILE - that zFilename is always a file |
|
f741baa…
|
drh
|
341 |
** under management. |
|
1772357…
|
drh
|
342 |
** |
|
c97535c…
|
dmitry
|
343 |
** On Windows, always return False. |
|
c97535c…
|
dmitry
|
344 |
*/ |
|
1772357…
|
drh
|
345 |
int file_islink(const char *zFilename){ |
|
1772357…
|
drh
|
346 |
return file_perm(zFilename, RepoFILE)==PERM_LNK; |
|
f741baa…
|
drh
|
347 |
} |
|
f741baa…
|
drh
|
348 |
|
|
f741baa…
|
drh
|
349 |
/* |
|
f741baa…
|
drh
|
350 |
** Check every sub-directory of zRoot along the path to zFile. |
|
f741baa…
|
drh
|
351 |
** If any sub-directory is really an ordinary file or a symbolic link, |
|
f741baa…
|
drh
|
352 |
** return an integer which is the length of the prefix of zFile which |
|
f741baa…
|
drh
|
353 |
** is the name of that object. Return 0 if all no non-directory |
|
f741baa…
|
drh
|
354 |
** objects are found along the path. |
|
f741baa…
|
drh
|
355 |
** |
|
f741baa…
|
drh
|
356 |
** Example: Given inputs |
|
f741baa…
|
drh
|
357 |
** |
|
f741baa…
|
drh
|
358 |
** zRoot = /home/alice/project1 |
|
f741baa…
|
drh
|
359 |
** zFile = /home/alice/project1/main/src/js/fileA.js |
|
f741baa…
|
drh
|
360 |
** |
|
f741baa…
|
drh
|
361 |
** Look for objects in the following order: |
|
f741baa…
|
drh
|
362 |
** |
|
f741baa…
|
drh
|
363 |
** /home/alice/project/main |
|
f741baa…
|
drh
|
364 |
** /home/alice/project/main/src |
|
f741baa…
|
drh
|
365 |
** /home/alice/project/main/src/js |
|
f741baa…
|
drh
|
366 |
** |
|
f741baa…
|
drh
|
367 |
** If any of those objects exist and are something other than a directory |
|
f741baa…
|
drh
|
368 |
** then return the length of the name of the first non-directory object |
|
f741baa…
|
drh
|
369 |
** seen. |
|
f741baa…
|
drh
|
370 |
*/ |
|
f741baa…
|
drh
|
371 |
int file_nondir_objects_on_path(const char *zRoot, const char *zFile){ |
|
f741baa…
|
drh
|
372 |
int i = (int)strlen(zRoot); |
|
f741baa…
|
drh
|
373 |
char *z = fossil_strdup(zFile); |
|
f741baa…
|
drh
|
374 |
assert( fossil_strnicmp(zRoot, z, i)==0 ); |
|
f741baa…
|
drh
|
375 |
if( i && zRoot[i-1]=='/' ) i--; |
|
f741baa…
|
drh
|
376 |
while( z[i]=='/' ){ |
|
f741baa…
|
drh
|
377 |
int j, rc; |
|
f741baa…
|
drh
|
378 |
for(j=i+1; z[j] && z[j]!='/'; j++){} |
|
f741baa…
|
drh
|
379 |
if( z[j]!='/' ) break; |
|
f741baa…
|
drh
|
380 |
z[j] = 0; |
|
f741baa…
|
drh
|
381 |
rc = file_isdir(z, SymFILE); |
|
f741baa…
|
drh
|
382 |
if( rc!=1 ){ |
|
f741baa…
|
drh
|
383 |
if( rc==2 ){ |
|
f741baa…
|
drh
|
384 |
fossil_free(z); |
|
f741baa…
|
drh
|
385 |
return j; |
|
f741baa…
|
drh
|
386 |
} |
|
f741baa…
|
drh
|
387 |
break; |
|
f741baa…
|
drh
|
388 |
} |
|
f741baa…
|
drh
|
389 |
z[j] = '/'; |
|
f741baa…
|
drh
|
390 |
i = j; |
|
f741baa…
|
drh
|
391 |
} |
|
f741baa…
|
drh
|
392 |
fossil_free(z); |
|
f741baa…
|
drh
|
393 |
return 0; |
|
f741baa…
|
drh
|
394 |
} |
|
f741baa…
|
drh
|
395 |
|
|
f741baa…
|
drh
|
396 |
/* |
|
f741baa…
|
drh
|
397 |
** The file named zFile is suppose to be an in-tree file. Check to |
|
f741baa…
|
drh
|
398 |
** ensure that it will be safe to write to this file by verifying that |
|
f741baa…
|
drh
|
399 |
** there are no symlinks or other non-directory objects in between the |
|
bc36fdc…
|
danield
|
400 |
** root of the check-out and zFile. |
|
f741baa…
|
drh
|
401 |
** |
|
f741baa…
|
drh
|
402 |
** If a problem is found, print a warning message (using fossil_warning()) |
|
f741baa…
|
drh
|
403 |
** and return non-zero. If everything is ok, return zero. |
|
f741baa…
|
drh
|
404 |
*/ |
|
f741baa…
|
drh
|
405 |
int file_unsafe_in_tree_path(const char *zFile){ |
|
f741baa…
|
drh
|
406 |
int n; |
|
f741baa…
|
drh
|
407 |
if( !file_is_absolute_path(zFile) ){ |
|
31c7bdb…
|
larrybr
|
408 |
fossil_fatal("%s is not an absolute pathname",zFile); |
|
f741baa…
|
drh
|
409 |
} |
|
f741baa…
|
drh
|
410 |
if( fossil_strnicmp(g.zLocalRoot, zFile, (int)strlen(g.zLocalRoot)) ){ |
|
31c7bdb…
|
larrybr
|
411 |
fossil_fatal("%s is not a prefix of %s", g.zLocalRoot, zFile); |
|
f741baa…
|
drh
|
412 |
} |
|
f741baa…
|
drh
|
413 |
n = file_nondir_objects_on_path(g.zLocalRoot, zFile); |
|
f741baa…
|
drh
|
414 |
if( n ){ |
|
f741baa…
|
drh
|
415 |
fossil_warning("cannot write to %s because non-directory object %.*s" |
|
f741baa…
|
drh
|
416 |
" is in the way", zFile, n, zFile); |
|
f741baa…
|
drh
|
417 |
} |
|
f741baa…
|
drh
|
418 |
return n; |
|
e4f1c1f…
|
drh
|
419 |
} |
|
390b414…
|
drh
|
420 |
|
|
390b414…
|
drh
|
421 |
/* |
|
390b414…
|
drh
|
422 |
** Return 1 if zFilename is a directory. Return 0 if zFilename |
|
390b414…
|
drh
|
423 |
** does not exist. Return 2 if zFilename exists but is something |
|
390b414…
|
drh
|
424 |
** other than a directory. |
|
390b414…
|
drh
|
425 |
*/ |
|
1772357…
|
drh
|
426 |
int file_isdir(const char *zFilename, int eFType){ |
|
912a0d5…
|
mistachkin
|
427 |
int rc; |
|
912a0d5…
|
mistachkin
|
428 |
char *zFN; |
|
912a0d5…
|
mistachkin
|
429 |
|
|
4c3e172…
|
danield
|
430 |
zFN = fossil_strdup(zFilename); |
|
912a0d5…
|
mistachkin
|
431 |
file_simplify_name(zFN, -1, 0); |
|
1772357…
|
drh
|
432 |
rc = getStat(zFN, eFType); |
|
912a0d5…
|
mistachkin
|
433 |
if( rc ){ |
|
912a0d5…
|
mistachkin
|
434 |
rc = 0; /* It does not exist at all. */ |
|
1772357…
|
drh
|
435 |
}else if( S_ISDIR(fx.fileStat.st_mode) ){ |
|
912a0d5…
|
mistachkin
|
436 |
rc = 1; /* It exists and is a real directory. */ |
|
912a0d5…
|
mistachkin
|
437 |
}else{ |
|
912a0d5…
|
mistachkin
|
438 |
rc = 2; /* It exists and is something else. */ |
|
912a0d5…
|
mistachkin
|
439 |
} |
|
912a0d5…
|
mistachkin
|
440 |
free(zFN); |
|
912a0d5…
|
mistachkin
|
441 |
return rc; |
|
aad4c24…
|
drh
|
442 |
} |
|
aad4c24…
|
drh
|
443 |
|
|
aad4c24…
|
drh
|
444 |
/* |
|
aad4c24…
|
drh
|
445 |
** Return true (1) if zFilename seems like it seems like a valid |
|
aad4c24…
|
drh
|
446 |
** repository database. |
|
aad4c24…
|
drh
|
447 |
*/ |
|
aad4c24…
|
drh
|
448 |
int file_is_repository(const char *zFilename){ |
|
aad4c24…
|
drh
|
449 |
i64 sz; |
|
aad4c24…
|
drh
|
450 |
sqlite3 *db = 0; |
|
aad4c24…
|
drh
|
451 |
sqlite3_stmt *pStmt = 0; |
|
aad4c24…
|
drh
|
452 |
int rc; |
|
aad4c24…
|
drh
|
453 |
int i; |
|
aad4c24…
|
drh
|
454 |
static const char *azReqTab[] = { |
|
aad4c24…
|
drh
|
455 |
"blob", "delta", "rcvfrom", "user", "config" |
|
aad4c24…
|
drh
|
456 |
}; |
|
aad4c24…
|
drh
|
457 |
if( !file_isfile(zFilename, ExtFILE) ) return 0; |
|
aad4c24…
|
drh
|
458 |
sz = file_size(zFilename, ExtFILE); |
|
aad4c24…
|
drh
|
459 |
if( sz<35328 ) return 0; |
|
aad4c24…
|
drh
|
460 |
if( sz%512!=0 ) return 0; |
|
0537925…
|
andygoth
|
461 |
rc = sqlite3_open_v2(zFilename, &db, |
|
aad4c24…
|
drh
|
462 |
SQLITE_OPEN_READWRITE, 0); |
|
aad4c24…
|
drh
|
463 |
if( rc!=0 ) goto not_a_repo; |
|
aad4c24…
|
drh
|
464 |
for(i=0; i<count(azReqTab); i++){ |
|
aad4c24…
|
drh
|
465 |
if( sqlite3_table_column_metadata(db, "main", azReqTab[i],0,0,0,0,0,0) ){ |
|
aad4c24…
|
drh
|
466 |
goto not_a_repo; |
|
aad4c24…
|
drh
|
467 |
} |
|
aad4c24…
|
drh
|
468 |
} |
|
aad4c24…
|
drh
|
469 |
rc = sqlite3_prepare_v2(db, "SELECT 1 FROM config WHERE name='project-code'", |
|
aad4c24…
|
drh
|
470 |
-1, &pStmt, 0); |
|
aad4c24…
|
drh
|
471 |
if( rc ) goto not_a_repo; |
|
aad4c24…
|
drh
|
472 |
rc = sqlite3_step(pStmt); |
|
aad4c24…
|
drh
|
473 |
if( rc!=SQLITE_ROW ) goto not_a_repo; |
|
aad4c24…
|
drh
|
474 |
sqlite3_finalize(pStmt); |
|
aad4c24…
|
drh
|
475 |
sqlite3_close(db); |
|
aad4c24…
|
drh
|
476 |
return 1; |
|
aad4c24…
|
drh
|
477 |
|
|
aad4c24…
|
drh
|
478 |
not_a_repo: |
|
aad4c24…
|
drh
|
479 |
sqlite3_finalize(pStmt); |
|
aad4c24…
|
drh
|
480 |
sqlite3_close(db); |
|
aad4c24…
|
drh
|
481 |
return 0; |
|
c05f6af…
|
dmitry
|
482 |
} |
|
c05f6af…
|
dmitry
|
483 |
|
|
d8ec765…
|
drh
|
484 |
|
|
d8ec765…
|
drh
|
485 |
/* |
|
d8ec765…
|
drh
|
486 |
** Wrapper around the access() system call. |
|
d8ec765…
|
drh
|
487 |
*/ |
|
d8ec765…
|
drh
|
488 |
int file_access(const char *zFilename, int flags){ |
|
503482a…
|
jan.nijtmans
|
489 |
int rc; |
|
9571b68…
|
drh
|
490 |
void *zMbcs = fossil_utf8_to_path(zFilename, 0); |
|
12bc63a…
|
jan.nijtmans
|
491 |
#ifdef _WIN32 |
|
503482a…
|
jan.nijtmans
|
492 |
rc = win32_access(zMbcs, flags); |
|
12bc63a…
|
jan.nijtmans
|
493 |
#else |
|
503482a…
|
jan.nijtmans
|
494 |
rc = access(zMbcs, flags); |
|
12bc63a…
|
jan.nijtmans
|
495 |
#endif |
|
9571b68…
|
drh
|
496 |
fossil_path_free(zMbcs); |
|
503482a…
|
jan.nijtmans
|
497 |
return rc; |
|
12bc63a…
|
jan.nijtmans
|
498 |
} |
|
12bc63a…
|
jan.nijtmans
|
499 |
|
|
12bc63a…
|
jan.nijtmans
|
500 |
/* |
|
12bc63a…
|
jan.nijtmans
|
501 |
** Wrapper around the chdir() system call. |
|
12bc63a…
|
jan.nijtmans
|
502 |
** If bChroot=1, do a chroot to this dir as well |
|
12bc63a…
|
jan.nijtmans
|
503 |
** (UNIX only) |
|
12bc63a…
|
jan.nijtmans
|
504 |
*/ |
|
12bc63a…
|
jan.nijtmans
|
505 |
int file_chdir(const char *zChDir, int bChroot){ |
|
503482a…
|
jan.nijtmans
|
506 |
int rc; |
|
9571b68…
|
drh
|
507 |
void *zPath = fossil_utf8_to_path(zChDir, 1); |
|
14733d1…
|
jan.nijtmans
|
508 |
#ifdef _WIN32 |
|
503482a…
|
jan.nijtmans
|
509 |
rc = win32_chdir(zPath, bChroot); |
|
14733d1…
|
jan.nijtmans
|
510 |
#else |
|
503482a…
|
jan.nijtmans
|
511 |
rc = chdir(zPath); |
|
12bc63a…
|
jan.nijtmans
|
512 |
if( !rc && bChroot ){ |
|
12bc63a…
|
jan.nijtmans
|
513 |
rc = chroot(zPath); |
|
12bc63a…
|
jan.nijtmans
|
514 |
if( !rc ) rc = chdir("/"); |
|
558cef7…
|
drh
|
515 |
g.fJail = 1; |
|
12bc63a…
|
jan.nijtmans
|
516 |
} |
|
14733d1…
|
jan.nijtmans
|
517 |
#endif |
|
9571b68…
|
drh
|
518 |
fossil_path_free(zPath); |
|
503482a…
|
jan.nijtmans
|
519 |
return rc; |
|
9b7a6f8…
|
drh
|
520 |
} |
|
9b7a6f8…
|
drh
|
521 |
|
|
9b7a6f8…
|
drh
|
522 |
/* |
|
9b7a6f8…
|
drh
|
523 |
** Find an unused filename similar to zBase with zSuffix appended. |
|
9b7a6f8…
|
drh
|
524 |
** |
|
9b7a6f8…
|
drh
|
525 |
** Make the name relative to the working directory if relFlag is true. |
|
9b7a6f8…
|
drh
|
526 |
** |
|
9b7a6f8…
|
drh
|
527 |
** Space to hold the new filename is obtained form mprintf() and should |
|
9b7a6f8…
|
drh
|
528 |
** be freed by the caller. |
|
9b7a6f8…
|
drh
|
529 |
*/ |
|
9b7a6f8…
|
drh
|
530 |
char *file_newname(const char *zBase, const char *zSuffix, int relFlag){ |
|
9b7a6f8…
|
drh
|
531 |
char *z = 0; |
|
9b7a6f8…
|
drh
|
532 |
int cnt = 0; |
|
9b7a6f8…
|
drh
|
533 |
z = mprintf("%s-%s", zBase, zSuffix); |
|
1772357…
|
drh
|
534 |
while( file_size(z, ExtFILE)>=0 ){ |
|
9b7a6f8…
|
drh
|
535 |
fossil_free(z); |
|
9b7a6f8…
|
drh
|
536 |
z = mprintf("%s-%s-%d", zBase, zSuffix, cnt++); |
|
9b7a6f8…
|
drh
|
537 |
} |
|
9b7a6f8…
|
drh
|
538 |
if( relFlag ){ |
|
9b7a6f8…
|
drh
|
539 |
Blob x; |
|
135ed93…
|
drh
|
540 |
file_relative_name(z, &x, 0); |
|
9b7a6f8…
|
drh
|
541 |
fossil_free(z); |
|
9b7a6f8…
|
drh
|
542 |
z = blob_str(&x); |
|
9b7a6f8…
|
drh
|
543 |
} |
|
9b7a6f8…
|
drh
|
544 |
return z; |
|
e146d80…
|
drh
|
545 |
} |
|
e146d80…
|
drh
|
546 |
|
|
e146d80…
|
drh
|
547 |
/* |
|
e146d80…
|
drh
|
548 |
** Return the tail of a file pathname. The tail is the last component |
|
e146d80…
|
drh
|
549 |
** of the path. For example, the tail of "/a/b/c.d" is "c.d". |
|
e146d80…
|
drh
|
550 |
*/ |
|
e146d80…
|
drh
|
551 |
const char *file_tail(const char *z){ |
|
e146d80…
|
drh
|
552 |
const char *zTail = z; |
|
b523bf0…
|
mistachkin
|
553 |
if( !zTail ) return 0; |
|
e146d80…
|
drh
|
554 |
while( z[0] ){ |
|
b523bf0…
|
mistachkin
|
555 |
if( fossil_isdirsep(z[0]) ) zTail = &z[1]; |
|
b523bf0…
|
mistachkin
|
556 |
z++; |
|
b523bf0…
|
mistachkin
|
557 |
} |
|
b523bf0…
|
mistachkin
|
558 |
return zTail; |
|
b523bf0…
|
mistachkin
|
559 |
} |
|
b523bf0…
|
mistachkin
|
560 |
|
|
b523bf0…
|
mistachkin
|
561 |
/* |
|
0f0184e…
|
danield
|
562 |
** Return the tail of a command: the basename of the putative executable (which |
|
0f0184e…
|
danield
|
563 |
** could be quoted when containing spaces) and the following arguments. |
|
0f0184e…
|
danield
|
564 |
*/ |
|
0f0184e…
|
danield
|
565 |
const char *command_tail(const char *z){ |
|
0f0184e…
|
danield
|
566 |
const char *zTail = z; |
|
0f0184e…
|
danield
|
567 |
char chQuote = 0; |
|
0f0184e…
|
danield
|
568 |
if( !zTail ) return 0; |
|
0f0184e…
|
danield
|
569 |
while( z[0] && (!fossil_isspace(z[0]) || chQuote) ){ |
|
0f0184e…
|
danield
|
570 |
if( z[0]=='"' || z[0]=='\'' ){ |
|
0f0184e…
|
danield
|
571 |
chQuote = (chQuote==z[0]) ? 0 : z[0]; |
|
0f0184e…
|
danield
|
572 |
} |
|
0f0184e…
|
danield
|
573 |
if( fossil_isdirsep(z[0]) ) zTail = &z[1]; |
|
0f0184e…
|
danield
|
574 |
z++; |
|
0f0184e…
|
danield
|
575 |
} |
|
0f0184e…
|
danield
|
576 |
return zTail; |
|
0f0184e…
|
danield
|
577 |
} |
|
0f0184e…
|
danield
|
578 |
|
|
0f0184e…
|
danield
|
579 |
/* |
|
b523bf0…
|
mistachkin
|
580 |
** Return the directory of a file path name. The directory is all components |
|
b523bf0…
|
mistachkin
|
581 |
** except the last one. For example, the directory of "/a/b/c.d" is "/a/b". |
|
b523bf0…
|
mistachkin
|
582 |
** If there is no directory, NULL is returned; otherwise, the returned memory |
|
b523bf0…
|
mistachkin
|
583 |
** should be freed via fossil_free(). |
|
b523bf0…
|
mistachkin
|
584 |
*/ |
|
b523bf0…
|
mistachkin
|
585 |
char *file_dirname(const char *z){ |
|
b523bf0…
|
mistachkin
|
586 |
const char *zTail = file_tail(z); |
|
b523bf0…
|
mistachkin
|
587 |
if( zTail && zTail!=z ){ |
|
b523bf0…
|
mistachkin
|
588 |
return mprintf("%.*s", (int)(zTail-z-1), z); |
|
0f0184e…
|
danield
|
589 |
}else{ |
|
0f0184e…
|
danield
|
590 |
return 0; |
|
0f0184e…
|
danield
|
591 |
} |
|
0f0184e…
|
danield
|
592 |
} |
|
0f0184e…
|
danield
|
593 |
|
|
0f0184e…
|
danield
|
594 |
/* |
|
0f0184e…
|
danield
|
595 |
** Return the basename of the putative executable in a command (w/o arguments). |
|
0f0184e…
|
danield
|
596 |
** The returned memory should be freed via fossil_free(). |
|
0f0184e…
|
danield
|
597 |
*/ |
|
0f0184e…
|
danield
|
598 |
char *command_basename(const char *z){ |
|
0f0184e…
|
danield
|
599 |
const char *zTail = command_tail(z); |
|
0f0184e…
|
danield
|
600 |
const char *zEnd = zTail; |
|
0f0184e…
|
danield
|
601 |
while( zEnd[0] && !fossil_isspace(zEnd[0]) && zEnd[0]!='"' && zEnd[0]!='\'' ){ |
|
0f0184e…
|
danield
|
602 |
zEnd++; |
|
0f0184e…
|
danield
|
603 |
} |
|
0f0184e…
|
danield
|
604 |
if( zEnd ){ |
|
0f0184e…
|
danield
|
605 |
return mprintf("%.*s", (int)(zEnd-zTail), zTail); |
|
f132f86…
|
drh
|
606 |
}else{ |
|
f132f86…
|
drh
|
607 |
return 0; |
|
f132f86…
|
drh
|
608 |
} |
|
f132f86…
|
drh
|
609 |
} |
|
f132f86…
|
drh
|
610 |
|
|
f132f86…
|
drh
|
611 |
/* SQL Function: file_dirname(NAME) |
|
f132f86…
|
drh
|
612 |
** |
|
f132f86…
|
drh
|
613 |
** Return the directory for NAME |
|
f132f86…
|
drh
|
614 |
*/ |
|
f132f86…
|
drh
|
615 |
void file_dirname_sql_function( |
|
f132f86…
|
drh
|
616 |
sqlite3_context *context, |
|
f132f86…
|
drh
|
617 |
int argc, |
|
f132f86…
|
drh
|
618 |
sqlite3_value **argv |
|
f132f86…
|
drh
|
619 |
){ |
|
f132f86…
|
drh
|
620 |
const char *zName = (const char*)sqlite3_value_text(argv[0]); |
|
f132f86…
|
drh
|
621 |
char *zDir; |
|
f132f86…
|
drh
|
622 |
if( zName==0 ) return; |
|
f132f86…
|
drh
|
623 |
zDir = file_dirname(zName); |
|
f132f86…
|
drh
|
624 |
if( zDir ){ |
|
f132f86…
|
drh
|
625 |
sqlite3_result_text(context,zDir,-1,fossil_free); |
|
f132f86…
|
drh
|
626 |
} |
|
f132f86…
|
drh
|
627 |
} |
|
f132f86…
|
drh
|
628 |
|
|
1f694e1…
|
mistachkin
|
629 |
|
|
1f694e1…
|
mistachkin
|
630 |
/* |
|
1f694e1…
|
mistachkin
|
631 |
** Rename a file or directory. |
|
1f694e1…
|
mistachkin
|
632 |
** Returns zero upon success. |
|
1f694e1…
|
mistachkin
|
633 |
*/ |
|
1f694e1…
|
mistachkin
|
634 |
int file_rename( |
|
1f694e1…
|
mistachkin
|
635 |
const char *zFrom, |
|
1f694e1…
|
mistachkin
|
636 |
const char *zTo, |
|
1f694e1…
|
mistachkin
|
637 |
int isFromDir, |
|
1f694e1…
|
mistachkin
|
638 |
int isToDir |
|
1f694e1…
|
mistachkin
|
639 |
){ |
|
1f694e1…
|
mistachkin
|
640 |
int rc; |
|
1f694e1…
|
mistachkin
|
641 |
#if defined(_WIN32) |
|
1f694e1…
|
mistachkin
|
642 |
wchar_t *zMbcsFrom = fossil_utf8_to_path(zFrom, isFromDir); |
|
1f694e1…
|
mistachkin
|
643 |
wchar_t *zMbcsTo = fossil_utf8_to_path(zTo, isToDir); |
|
1f694e1…
|
mistachkin
|
644 |
rc = _wrename(zMbcsFrom, zMbcsTo); |
|
1f694e1…
|
mistachkin
|
645 |
#else |
|
1f694e1…
|
mistachkin
|
646 |
char *zMbcsFrom = fossil_utf8_to_path(zFrom, isFromDir); |
|
1f694e1…
|
mistachkin
|
647 |
char *zMbcsTo = fossil_utf8_to_path(zTo, isToDir); |
|
1f694e1…
|
mistachkin
|
648 |
rc = rename(zMbcsFrom, zMbcsTo); |
|
1f694e1…
|
mistachkin
|
649 |
#endif |
|
1f694e1…
|
mistachkin
|
650 |
fossil_path_free(zMbcsTo); |
|
1f694e1…
|
mistachkin
|
651 |
fossil_path_free(zMbcsFrom); |
|
1f694e1…
|
mistachkin
|
652 |
return rc; |
|
1f694e1…
|
mistachkin
|
653 |
} |
|
1f694e1…
|
mistachkin
|
654 |
|
|
1f694e1…
|
mistachkin
|
655 |
/* |
|
9236f0c…
|
drh
|
656 |
** Copy the content of a file from one place to another. |
|
9236f0c…
|
drh
|
657 |
*/ |
|
9236f0c…
|
drh
|
658 |
void file_copy(const char *zFrom, const char *zTo){ |
|
9236f0c…
|
drh
|
659 |
FILE *in, *out; |
|
9236f0c…
|
drh
|
660 |
int got; |
|
9236f0c…
|
drh
|
661 |
char zBuf[8192]; |
|
d8ec765…
|
drh
|
662 |
in = fossil_fopen(zFrom, "rb"); |
|
9236f0c…
|
drh
|
663 |
if( in==0 ) fossil_fatal("cannot open \"%s\" for reading", zFrom); |
|
1772357…
|
drh
|
664 |
file_mkfolder(zTo, ExtFILE, 0, 0); |
|
d8ec765…
|
drh
|
665 |
out = fossil_fopen(zTo, "wb"); |
|
9236f0c…
|
drh
|
666 |
if( out==0 ) fossil_fatal("cannot open \"%s\" for writing", zTo); |
|
9236f0c…
|
drh
|
667 |
while( (got=fread(zBuf, 1, sizeof(zBuf), in))>0 ){ |
|
9236f0c…
|
drh
|
668 |
fwrite(zBuf, 1, got, out); |
|
9236f0c…
|
drh
|
669 |
} |
|
9236f0c…
|
drh
|
670 |
fclose(in); |
|
9236f0c…
|
drh
|
671 |
fclose(out); |
|
cb2f254…
|
drh
|
672 |
if( file_isexe(zFrom, ExtFILE) ) file_setexe(zTo, 1); |
|
190353e…
|
drh
|
673 |
} |
|
190353e…
|
drh
|
674 |
|
|
190353e…
|
drh
|
675 |
/* |
|
190353e…
|
drh
|
676 |
** COMMAND: test-file-copy |
|
190353e…
|
drh
|
677 |
** |
|
190353e…
|
drh
|
678 |
** Usage: %fossil test-file-copy SOURCE DESTINATION |
|
190353e…
|
drh
|
679 |
** |
|
190353e…
|
drh
|
680 |
** Make a copy of the file at SOURCE into a new name DESTINATION. Any |
|
190353e…
|
drh
|
681 |
** directories in the path leading up to DESTINATION that do not already |
|
190353e…
|
drh
|
682 |
** exist are created automatically. |
|
190353e…
|
drh
|
683 |
*/ |
|
190353e…
|
drh
|
684 |
void test_file_copy(void){ |
|
190353e…
|
drh
|
685 |
if( g.argc!=4 ){ |
|
190353e…
|
drh
|
686 |
fossil_fatal("Usage: %s test-file-copy SOURCE DESTINATION", g.argv[0]); |
|
190353e…
|
drh
|
687 |
} |
|
190353e…
|
drh
|
688 |
file_copy(g.argv[2], g.argv[3]); |
|
390b414…
|
drh
|
689 |
} |
|
390b414…
|
drh
|
690 |
|
|
390b414…
|
drh
|
691 |
/* |
|
3ad119b…
|
drh
|
692 |
** Set or clear the execute bit on a file. Return true if a change |
|
3ad119b…
|
drh
|
693 |
** occurred and false if this routine is a no-op. |
|
1772357…
|
drh
|
694 |
** |
|
1772357…
|
drh
|
695 |
** This routine assumes RepoFILE as the eFType. In other words, if |
|
1772357…
|
drh
|
696 |
** zFilename is a symbolic link, it is the object that zFilename points |
|
1772357…
|
drh
|
697 |
** to that is modified. |
|
390b414…
|
drh
|
698 |
*/ |
|
1772357…
|
drh
|
699 |
int file_setexe(const char *zFilename, int onoff){ |
|
3ad119b…
|
drh
|
700 |
int rc = 0; |
|
3564af0…
|
drh
|
701 |
#if !defined(_WIN32) |
|
390b414…
|
drh
|
702 |
struct stat buf; |
|
0537925…
|
andygoth
|
703 |
if( fossil_stat(zFilename, &buf, RepoFILE)!=0 |
|
f741baa…
|
drh
|
704 |
|| S_ISLNK(buf.st_mode) |
|
f741baa…
|
drh
|
705 |
|| S_ISDIR(buf.st_mode) |
|
f741baa…
|
drh
|
706 |
){ |
|
1772357…
|
drh
|
707 |
return 0; |
|
1772357…
|
drh
|
708 |
} |
|
9236f0c…
|
drh
|
709 |
if( onoff ){ |
|
fd6f1c5…
|
drh
|
710 |
int targetMode = (buf.st_mode & 0444)>>2; |
|
2ccd046…
|
mistachkin
|
711 |
if( (buf.st_mode & 0100)==0 ){ |
|
fd6f1c5…
|
drh
|
712 |
chmod(zFilename, buf.st_mode | targetMode); |
|
3ad119b…
|
drh
|
713 |
rc = 1; |
|
9236f0c…
|
drh
|
714 |
} |
|
9236f0c…
|
drh
|
715 |
}else{ |
|
2ccd046…
|
mistachkin
|
716 |
if( (buf.st_mode & 0100)!=0 ){ |
|
9236f0c…
|
drh
|
717 |
chmod(zFilename, buf.st_mode & ~0111); |
|
3ad119b…
|
drh
|
718 |
rc = 1; |
|
3564af0…
|
drh
|
719 |
} |
|
3564af0…
|
drh
|
720 |
} |
|
3564af0…
|
drh
|
721 |
#endif /* _WIN32 */ |
|
3ad119b…
|
drh
|
722 |
return rc; |
|
3ad119b…
|
drh
|
723 |
} |
|
3ad119b…
|
drh
|
724 |
|
|
3ad119b…
|
drh
|
725 |
/* |
|
0c37874…
|
drh
|
726 |
** Set the mtime for a file. |
|
0c37874…
|
drh
|
727 |
*/ |
|
0c37874…
|
drh
|
728 |
void file_set_mtime(const char *zFilename, i64 newMTime){ |
|
0c37874…
|
drh
|
729 |
#if !defined(_WIN32) |
|
268d495…
|
drh
|
730 |
char *zMbcs; |
|
0c37874…
|
drh
|
731 |
struct timeval tv[2]; |
|
0c37874…
|
drh
|
732 |
memset(tv, 0, sizeof(tv[0])*2); |
|
0c37874…
|
drh
|
733 |
tv[0].tv_sec = newMTime; |
|
0c37874…
|
drh
|
734 |
tv[1].tv_sec = newMTime; |
|
9571b68…
|
drh
|
735 |
zMbcs = fossil_utf8_to_path(zFilename, 0); |
|
caf2eb2…
|
jan.nijtmans
|
736 |
utimes(zMbcs, tv); |
|
0c37874…
|
drh
|
737 |
#else |
|
6032dd5…
|
jan.nijtmans
|
738 |
struct _utimbuf tb; |
|
9571b68…
|
drh
|
739 |
wchar_t *zMbcs = fossil_utf8_to_path(zFilename, 0); |
|
0c37874…
|
drh
|
740 |
tb.actime = newMTime; |
|
0c37874…
|
drh
|
741 |
tb.modtime = newMTime; |
|
0c37874…
|
drh
|
742 |
_wutime(zMbcs, &tb); |
|
0c37874…
|
drh
|
743 |
#endif |
|
9571b68…
|
drh
|
744 |
fossil_path_free(zMbcs); |
|
0c37874…
|
drh
|
745 |
} |
|
0c37874…
|
drh
|
746 |
|
|
0c37874…
|
drh
|
747 |
/* |
|
0c37874…
|
drh
|
748 |
** COMMAND: test-set-mtime |
|
0c37874…
|
drh
|
749 |
** |
|
0c37874…
|
drh
|
750 |
** Usage: %fossil test-set-mtime FILENAME DATE/TIME |
|
0c37874…
|
drh
|
751 |
** |
|
0c37874…
|
drh
|
752 |
** Sets the mtime of the named file to the date/time shown. |
|
0c37874…
|
drh
|
753 |
*/ |
|
0c37874…
|
drh
|
754 |
void test_set_mtime(void){ |
|
0c37874…
|
drh
|
755 |
const char *zFile; |
|
0c37874…
|
drh
|
756 |
char *zDate; |
|
0c37874…
|
drh
|
757 |
i64 iMTime; |
|
0c37874…
|
drh
|
758 |
if( g.argc!=4 ){ |
|
57f1eae…
|
rberteig
|
759 |
usage("FILENAME DATE/TIME"); |
|
0c37874…
|
drh
|
760 |
} |
|
06aec61…
|
drh
|
761 |
db_open_or_attach(":memory:", "mem"); |
|
0c37874…
|
drh
|
762 |
iMTime = db_int64(0, "SELECT strftime('%%s',%Q)", g.argv[3]); |
|
0c37874…
|
drh
|
763 |
zFile = g.argv[2]; |
|
0c37874…
|
drh
|
764 |
file_set_mtime(zFile, iMTime); |
|
1772357…
|
drh
|
765 |
iMTime = file_mtime(zFile, RepoFILE); |
|
0c37874…
|
drh
|
766 |
zDate = db_text(0, "SELECT datetime(%lld, 'unixepoch')", iMTime); |
|
0c37874…
|
drh
|
767 |
fossil_print("Set mtime of \"%s\" to %s (%lld)\n", zFile, zDate, iMTime); |
|
0c37874…
|
drh
|
768 |
} |
|
0537925…
|
andygoth
|
769 |
|
|
0537925…
|
andygoth
|
770 |
/* |
|
effdada…
|
drh
|
771 |
** Change access permissions on a file. |
|
effdada…
|
drh
|
772 |
*/ |
|
effdada…
|
drh
|
773 |
void file_set_mode(const char *zFN, int fd, const char *zMode, int bNoErr){ |
|
effdada…
|
drh
|
774 |
#if !defined(_WIN32) |
|
effdada…
|
drh
|
775 |
mode_t m; |
|
effdada…
|
drh
|
776 |
char *zEnd = 0; |
|
effdada…
|
drh
|
777 |
m = strtol(zMode, &zEnd, 0); |
|
effdada…
|
drh
|
778 |
if( (zEnd[0] || fchmod(fd, m)) && !bNoErr ){ |
|
effdada…
|
drh
|
779 |
fossil_fatal("cannot change permissions on %s to \"%s\"", |
|
effdada…
|
drh
|
780 |
zFN, zMode); |
|
effdada…
|
drh
|
781 |
} |
|
effdada…
|
drh
|
782 |
#endif |
|
effdada…
|
drh
|
783 |
} |
|
effdada…
|
drh
|
784 |
|
|
effdada…
|
drh
|
785 |
/* Change the owner of a file to zOwner. zOwner can be of the form |
|
effdada…
|
drh
|
786 |
** USER:GROUP. |
|
effdada…
|
drh
|
787 |
*/ |
|
effdada…
|
drh
|
788 |
void file_set_owner(const char *zFN, int fd, const char *zOwner){ |
|
effdada…
|
drh
|
789 |
#if !defined(_WIN32) |
|
effdada…
|
drh
|
790 |
const char *zGrp; |
|
effdada…
|
drh
|
791 |
const char *zUsr = zOwner; |
|
effdada…
|
drh
|
792 |
struct passwd *pw; |
|
effdada…
|
drh
|
793 |
struct group *grp; |
|
effdada…
|
drh
|
794 |
uid_t uid = -1; |
|
effdada…
|
drh
|
795 |
gid_t gid = -1; |
|
effdada…
|
drh
|
796 |
zGrp = strchr(zUsr, ':'); |
|
effdada…
|
drh
|
797 |
if( zGrp ){ |
|
effdada…
|
drh
|
798 |
int n = (int)(zGrp - zUsr); |
|
effdada…
|
drh
|
799 |
zUsr = fossil_strndup(zUsr, n); |
|
effdada…
|
drh
|
800 |
zGrp++; |
|
effdada…
|
drh
|
801 |
} |
|
effdada…
|
drh
|
802 |
pw = getpwnam(zUsr); |
|
effdada…
|
drh
|
803 |
if( pw==0 ){ |
|
effdada…
|
drh
|
804 |
fossil_fatal("no such user: \"%s\"", zUsr); |
|
effdada…
|
drh
|
805 |
} |
|
effdada…
|
drh
|
806 |
uid = pw->pw_uid; |
|
effdada…
|
drh
|
807 |
if( zGrp ){ |
|
effdada…
|
drh
|
808 |
grp = getgrnam(zGrp); |
|
effdada…
|
drh
|
809 |
if( grp==0 ){ |
|
effdada…
|
drh
|
810 |
fossil_fatal("no such group: \"%s\"", zGrp); |
|
effdada…
|
drh
|
811 |
} |
|
effdada…
|
drh
|
812 |
gid = grp->gr_gid; |
|
effdada…
|
drh
|
813 |
} |
|
effdada…
|
drh
|
814 |
if( chown(zFN, uid, gid) ){ |
|
effdada…
|
drh
|
815 |
fossil_fatal("cannot change ownership of %s to %s",zFN, zOwner); |
|
effdada…
|
drh
|
816 |
} |
|
effdada…
|
drh
|
817 |
if( zOwner!=zUsr ){ |
|
effdada…
|
drh
|
818 |
fossil_free((char*)zUsr); |
|
effdada…
|
drh
|
819 |
} |
|
effdada…
|
drh
|
820 |
#endif |
|
effdada…
|
drh
|
821 |
} |
|
effdada…
|
drh
|
822 |
|
|
effdada…
|
drh
|
823 |
|
|
effdada…
|
drh
|
824 |
/* |
|
d8ec765…
|
drh
|
825 |
** Delete a file. |
|
1772357…
|
drh
|
826 |
** |
|
1772357…
|
drh
|
827 |
** If zFilename is a symbolic link, then it is the link itself that is |
|
1772357…
|
drh
|
828 |
** removed, not the object that zFilename points to. |
|
238c8da…
|
drh
|
829 |
** |
|
238c8da…
|
drh
|
830 |
** Returns zero upon success. |
|
14733d1…
|
jan.nijtmans
|
831 |
*/ |
|
238c8da…
|
drh
|
832 |
int file_delete(const char *zFilename){ |
|
238c8da…
|
drh
|
833 |
int rc; |
|
14733d1…
|
jan.nijtmans
|
834 |
#ifdef _WIN32 |
|
9571b68…
|
drh
|
835 |
wchar_t *z = fossil_utf8_to_path(zFilename, 0); |
|
238c8da…
|
drh
|
836 |
rc = _wunlink(z); |
|
14733d1…
|
jan.nijtmans
|
837 |
#else |
|
9571b68…
|
drh
|
838 |
char *z = fossil_utf8_to_path(zFilename, 0); |
|
238c8da…
|
drh
|
839 |
rc = unlink(zFilename); |
|
14733d1…
|
jan.nijtmans
|
840 |
#endif |
|
9571b68…
|
drh
|
841 |
fossil_path_free(z); |
|
238c8da…
|
drh
|
842 |
return rc; |
|
f132f86…
|
drh
|
843 |
} |
|
f132f86…
|
drh
|
844 |
|
|
f132f86…
|
drh
|
845 |
/* SQL Function: file_delete(NAME) |
|
f132f86…
|
drh
|
846 |
** |
|
f132f86…
|
drh
|
847 |
** Remove file NAME. Return zero on success and non-zero if anything goes |
|
f132f86…
|
drh
|
848 |
** wrong. |
|
f132f86…
|
drh
|
849 |
*/ |
|
f132f86…
|
drh
|
850 |
void file_delete_sql_function( |
|
f132f86…
|
drh
|
851 |
sqlite3_context *context, |
|
f132f86…
|
drh
|
852 |
int argc, |
|
f132f86…
|
drh
|
853 |
sqlite3_value **argv |
|
f132f86…
|
drh
|
854 |
){ |
|
f132f86…
|
drh
|
855 |
const char *zName = (const char*)sqlite3_value_text(argv[0]); |
|
f132f86…
|
drh
|
856 |
int rc; |
|
f132f86…
|
drh
|
857 |
if( zName==0 ){ |
|
f132f86…
|
drh
|
858 |
rc = 1; |
|
f132f86…
|
drh
|
859 |
}else{ |
|
f132f86…
|
drh
|
860 |
rc = file_delete(zName); |
|
f132f86…
|
drh
|
861 |
} |
|
f132f86…
|
drh
|
862 |
sqlite3_result_int(context, rc); |
|
1772357…
|
drh
|
863 |
} |
|
1772357…
|
drh
|
864 |
|
|
d8ec765…
|
drh
|
865 |
/* |
|
1772357…
|
drh
|
866 |
** Create a directory called zName, if it does not already exist. |
|
1772357…
|
drh
|
867 |
** If forceFlag is 1, delete any prior non-directory object |
|
dbda8d6…
|
drh
|
868 |
** with the same name. |
|
dbda8d6…
|
drh
|
869 |
** |
|
dbda8d6…
|
drh
|
870 |
** Return the number of errors. |
|
dbda8d6…
|
drh
|
871 |
*/ |
|
1772357…
|
drh
|
872 |
int file_mkdir(const char *zName, int eFType, int forceFlag){ |
|
1772357…
|
drh
|
873 |
int rc = file_isdir(zName, eFType); |
|
dbda8d6…
|
drh
|
874 |
if( rc==2 ){ |
|
dbda8d6…
|
drh
|
875 |
if( !forceFlag ) return 1; |
|
d8ec765…
|
drh
|
876 |
file_delete(zName); |
|
dbda8d6…
|
drh
|
877 |
} |
|
dbda8d6…
|
drh
|
878 |
if( rc!=1 ){ |
|
3564af0…
|
drh
|
879 |
#if defined(_WIN32) |
|
9571b68…
|
drh
|
880 |
wchar_t *zMbcs = fossil_utf8_to_path(zName, 1); |
|
14733d1…
|
jan.nijtmans
|
881 |
rc = _wmkdir(zMbcs); |
|
caf2eb2…
|
jan.nijtmans
|
882 |
#else |
|
9571b68…
|
drh
|
883 |
char *zMbcs = fossil_utf8_to_path(zName, 1); |
|
45fc083…
|
drh
|
884 |
rc = mkdir(zMbcs, 0755); |
|
238c8da…
|
drh
|
885 |
#endif |
|
9571b68…
|
drh
|
886 |
fossil_path_free(zMbcs); |
|
238c8da…
|
drh
|
887 |
return rc; |
|
238c8da…
|
drh
|
888 |
} |
|
238c8da…
|
drh
|
889 |
return 0; |
|
238c8da…
|
drh
|
890 |
} |
|
f991688…
|
drh
|
891 |
|
|
f991688…
|
drh
|
892 |
/* |
|
f991688…
|
drh
|
893 |
** Create the tree of directories in which zFilename belongs, if that sequence |
|
f991688…
|
drh
|
894 |
** of directories does not already exist. |
|
702331e…
|
drh
|
895 |
** |
|
702331e…
|
drh
|
896 |
** On success, return zero. On error, return errorReturn if positive, otherwise |
|
702331e…
|
drh
|
897 |
** print an error message and abort. |
|
f991688…
|
drh
|
898 |
*/ |
|
1772357…
|
drh
|
899 |
int file_mkfolder( |
|
1772357…
|
drh
|
900 |
const char *zFilename, /* Pathname showing directories to be created */ |
|
1772357…
|
drh
|
901 |
int eFType, /* Follow symlinks if ExtFILE */ |
|
1772357…
|
drh
|
902 |
int forceFlag, /* Delete non-directory objects in the way */ |
|
1772357…
|
drh
|
903 |
int errorReturn /* What to do when an error is seen */ |
|
1772357…
|
drh
|
904 |
){ |
|
4c666cc…
|
drh
|
905 |
int nName, rc = 0; |
|
f991688…
|
drh
|
906 |
char *zName; |
|
f991688…
|
drh
|
907 |
|
|
f991688…
|
drh
|
908 |
nName = strlen(zFilename); |
|
4c3e172…
|
danield
|
909 |
zName = fossil_strdup(zFilename); |
|
f991688…
|
drh
|
910 |
nName = file_simplify_name(zName, nName, 0); |
|
92ea618…
|
drh
|
911 |
while( nName>0 && zName[nName-1]!='/' ){ nName--; } |
|
74c4732…
|
drh
|
912 |
if( nName>1 ){ |
|
92ea618…
|
drh
|
913 |
zName[nName-1] = 0; |
|
1772357…
|
drh
|
914 |
if( file_isdir(zName, eFType)!=1 ){ |
|
1772357…
|
drh
|
915 |
rc = file_mkfolder(zName, eFType, forceFlag, errorReturn); |
|
92ea618…
|
drh
|
916 |
if( rc==0 ){ |
|
1772357…
|
drh
|
917 |
if( file_mkdir(zName, eFType, forceFlag) |
|
1772357…
|
drh
|
918 |
&& file_isdir(zName, eFType)!=1 |
|
1772357…
|
drh
|
919 |
){ |
|
92ea618…
|
drh
|
920 |
if( errorReturn <= 0 ){ |
|
702331e…
|
drh
|
921 |
fossil_fatal_recursive("unable to create directory %s", zName); |
|
702331e…
|
drh
|
922 |
} |
|
702331e…
|
drh
|
923 |
rc = errorReturn; |
|
92ea618…
|
drh
|
924 |
} |
|
92ea618…
|
drh
|
925 |
} |
|
92ea618…
|
drh
|
926 |
} |
|
92ea618…
|
drh
|
927 |
} |
|
9be3365…
|
stephan
|
928 |
fossil_free(zName); |
|
702331e…
|
drh
|
929 |
return rc; |
|
702331e…
|
drh
|
930 |
} |
|
9bb25a2…
|
mistachkin
|
931 |
|
|
9bb25a2…
|
mistachkin
|
932 |
#if defined(_WIN32) |
|
9bb25a2…
|
mistachkin
|
933 |
/* |
|
9bb25a2…
|
mistachkin
|
934 |
** Returns non-zero if the specified name represents a real directory, i.e. |
|
9bb25a2…
|
mistachkin
|
935 |
** not a junction or symbolic link. This is important for some operations, |
|
9bb25a2…
|
mistachkin
|
936 |
** e.g. removing directories via _wrmdir(), because its detection of empty |
|
9bb25a2…
|
mistachkin
|
937 |
** directories will (apparently) not work right for junctions and symbolic |
|
9bb25a2…
|
mistachkin
|
938 |
** links, etc. |
|
9bb25a2…
|
mistachkin
|
939 |
*/ |
|
9bb25a2…
|
mistachkin
|
940 |
int file_is_normal_dir(wchar_t *zName){ |
|
9bb25a2…
|
mistachkin
|
941 |
/* |
|
9bb25a2…
|
mistachkin
|
942 |
** Mask off attributes, applicable to directories, that are harmless for |
|
9bb25a2…
|
mistachkin
|
943 |
** our purposes. This may need to be updated if other attributes should |
|
9bb25a2…
|
mistachkin
|
944 |
** be ignored by this function. |
|
9bb25a2…
|
mistachkin
|
945 |
*/ |
|
9bb25a2…
|
mistachkin
|
946 |
DWORD dwAttributes = GetFileAttributesW(zName); |
|
9bb25a2…
|
mistachkin
|
947 |
if( dwAttributes==INVALID_FILE_ATTRIBUTES ) return 0; |
|
9bb25a2…
|
mistachkin
|
948 |
dwAttributes &= ~( |
|
9bb25a2…
|
mistachkin
|
949 |
FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_COMPRESSED | |
|
9bb25a2…
|
mistachkin
|
950 |
FILE_ATTRIBUTE_ENCRYPTED | FILE_ATTRIBUTE_NORMAL | |
|
9bb25a2…
|
mistachkin
|
951 |
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED |
|
9bb25a2…
|
mistachkin
|
952 |
); |
|
9bb25a2…
|
mistachkin
|
953 |
return dwAttributes==FILE_ATTRIBUTE_DIRECTORY; |
|
9bb25a2…
|
mistachkin
|
954 |
} |
|
9bb25a2…
|
mistachkin
|
955 |
|
|
9bb25a2…
|
mistachkin
|
956 |
/* |
|
9bb25a2…
|
mistachkin
|
957 |
** COMMAND: test-is-normal-dir |
|
9bb25a2…
|
mistachkin
|
958 |
** |
|
9bb25a2…
|
mistachkin
|
959 |
** Usage: %fossil test-is-normal-dir NAME... |
|
9bb25a2…
|
mistachkin
|
960 |
** |
|
9bb25a2…
|
mistachkin
|
961 |
** Returns non-zero if the specified names represent real directories, i.e. |
|
9bb25a2…
|
mistachkin
|
962 |
** not junctions, symbolic links, etc. |
|
9bb25a2…
|
mistachkin
|
963 |
*/ |
|
9bb25a2…
|
mistachkin
|
964 |
void test_is_normal_dir(void){ |
|
9bb25a2…
|
mistachkin
|
965 |
int i; |
|
9bb25a2…
|
mistachkin
|
966 |
for(i=2; i<g.argc; i++){ |
|
9bb25a2…
|
mistachkin
|
967 |
wchar_t *zMbcs = fossil_utf8_to_path(g.argv[i], 1); |
|
9bb25a2…
|
mistachkin
|
968 |
fossil_print("ATTRS \"%s\" -> %lx\n", g.argv[i], GetFileAttributesW(zMbcs)); |
|
9bb25a2…
|
mistachkin
|
969 |
fossil_print("ISDIR \"%s\" -> %d\n", g.argv[i], file_is_normal_dir(zMbcs)); |
|
9bb25a2…
|
mistachkin
|
970 |
fossil_path_free(zMbcs); |
|
9bb25a2…
|
mistachkin
|
971 |
} |
|
9bb25a2…
|
mistachkin
|
972 |
} |
|
9bb25a2…
|
mistachkin
|
973 |
#endif |
|
238c8da…
|
drh
|
974 |
|
|
238c8da…
|
drh
|
975 |
/* |
|
238c8da…
|
drh
|
976 |
** Removes the directory named in the argument, if it exists. The directory |
|
238c8da…
|
drh
|
977 |
** must be empty and cannot be the current directory or the root directory. |
|
238c8da…
|
drh
|
978 |
** |
|
238c8da…
|
drh
|
979 |
** Returns zero upon success. |
|
238c8da…
|
drh
|
980 |
*/ |
|
238c8da…
|
drh
|
981 |
int file_rmdir(const char *zName){ |
|
1772357…
|
drh
|
982 |
int rc = file_isdir(zName, RepoFILE); |
|
238c8da…
|
drh
|
983 |
if( rc==2 ) return 1; /* cannot remove normal file */ |
|
238c8da…
|
drh
|
984 |
if( rc==1 ){ |
|
238c8da…
|
drh
|
985 |
#if defined(_WIN32) |
|
9571b68…
|
drh
|
986 |
wchar_t *zMbcs = fossil_utf8_to_path(zName, 1); |
|
9bb25a2…
|
mistachkin
|
987 |
if( file_is_normal_dir(zMbcs) ){ |
|
9bb25a2…
|
mistachkin
|
988 |
rc = _wrmdir(zMbcs); |
|
9bb25a2…
|
mistachkin
|
989 |
}else{ |
|
9bb25a2…
|
mistachkin
|
990 |
rc = ENOTDIR; /* junction, symbolic link, etc. */ |
|
9bb25a2…
|
mistachkin
|
991 |
} |
|
238c8da…
|
drh
|
992 |
#else |
|
9571b68…
|
drh
|
993 |
char *zMbcs = fossil_utf8_to_path(zName, 1); |
|
238c8da…
|
drh
|
994 |
rc = rmdir(zName); |
|
d0305b3…
|
aku
|
995 |
#endif |
|
9571b68…
|
drh
|
996 |
fossil_path_free(zMbcs); |
|
caf2eb2…
|
jan.nijtmans
|
997 |
return rc; |
|
dbda8d6…
|
drh
|
998 |
} |
|
dbda8d6…
|
drh
|
999 |
return 0; |
|
f132f86…
|
drh
|
1000 |
} |
|
f132f86…
|
drh
|
1001 |
|
|
f132f86…
|
drh
|
1002 |
/* SQL Function: rmdir(NAME) |
|
f132f86…
|
drh
|
1003 |
** |
|
f132f86…
|
drh
|
1004 |
** Try to remove the directory NAME. Return zero on success and non-zero |
|
f132f86…
|
drh
|
1005 |
** for failure. |
|
f132f86…
|
drh
|
1006 |
*/ |
|
f132f86…
|
drh
|
1007 |
void file_rmdir_sql_function( |
|
f132f86…
|
drh
|
1008 |
sqlite3_context *context, |
|
f132f86…
|
drh
|
1009 |
int argc, |
|
f132f86…
|
drh
|
1010 |
sqlite3_value **argv |
|
f132f86…
|
drh
|
1011 |
){ |
|
f132f86…
|
drh
|
1012 |
const char *zName = (const char*)sqlite3_value_text(argv[0]); |
|
f132f86…
|
drh
|
1013 |
int rc; |
|
f132f86…
|
drh
|
1014 |
if( zName==0 ){ |
|
f132f86…
|
drh
|
1015 |
rc = 1; |
|
f132f86…
|
drh
|
1016 |
}else{ |
|
f132f86…
|
drh
|
1017 |
rc = file_rmdir(zName); |
|
f132f86…
|
drh
|
1018 |
} |
|
f132f86…
|
drh
|
1019 |
sqlite3_result_int(context, rc); |
|
89a8588…
|
drh
|
1020 |
} |
|
89a8588…
|
drh
|
1021 |
|
|
89a8588…
|
drh
|
1022 |
/* |
|
89a8588…
|
drh
|
1023 |
** Check the input argument to see if it looks like it has an prefix that |
|
89a8588…
|
drh
|
1024 |
** indicates a remote file. If so, return the tail of the specification, |
|
89a8588…
|
drh
|
1025 |
** which is the name of the file on the remote system. |
|
89a8588…
|
drh
|
1026 |
** |
|
89a8588…
|
drh
|
1027 |
** If the input argument does not have a prefix that makes it look like |
|
89a8588…
|
drh
|
1028 |
** a remote file reference, then return NULL. |
|
89a8588…
|
drh
|
1029 |
** |
|
89a8588…
|
drh
|
1030 |
** Remote files look like: "HOST:PATH" or "USER@HOST:PATH". Host must |
|
89a8588…
|
drh
|
1031 |
** be a valid hostname, meaning it must follow these rules: |
|
89a8588…
|
drh
|
1032 |
** |
|
89a8588…
|
drh
|
1033 |
** * Only characters [-.a-zA-Z0-9]. No spaces or other punctuation |
|
89a8588…
|
drh
|
1034 |
** * Does not begin or end with - |
|
385344e…
|
drh
|
1035 |
** * Name is two or more characters long (otherwise it might be |
|
385344e…
|
drh
|
1036 |
** confused with a drive-letter on Windows). |
|
89a8588…
|
drh
|
1037 |
** |
|
385344e…
|
drh
|
1038 |
** The USER section, if it exists, must not contain the '@' character. |
|
89a8588…
|
drh
|
1039 |
*/ |
|
89a8588…
|
drh
|
1040 |
const char *file_skip_userhost(const char *zIn){ |
|
89a8588…
|
drh
|
1041 |
const char *zTail; |
|
89a8588…
|
drh
|
1042 |
int n, i; |
|
89a8588…
|
drh
|
1043 |
if( zIn[0]==':' ) return 0; |
|
89a8588…
|
drh
|
1044 |
zTail = strchr(zIn, ':'); |
|
89a8588…
|
drh
|
1045 |
if( zTail==0 ) return 0; |
|
89a8588…
|
drh
|
1046 |
if( zTail - zIn > 10000 ) return 0; |
|
89a8588…
|
drh
|
1047 |
n = (int)(zTail - zIn); |
|
385344e…
|
drh
|
1048 |
if( n<2 ) return 0; |
|
89a8588…
|
drh
|
1049 |
if( zIn[n-1]=='-' || zIn[n-1]=='.' ) return 0; |
|
89a8588…
|
drh
|
1050 |
for(i=n-1; i>0 && zIn[i-1]!='@'; i--){ |
|
89a8588…
|
drh
|
1051 |
if( !fossil_isalnum(zIn[i]) && zIn[i]!='-' && zIn[i]!='.' ) return 0; |
|
89a8588…
|
drh
|
1052 |
} |
|
89a8588…
|
drh
|
1053 |
if( zIn[i]=='-' || zIn[i]=='.' || i==1 ) return 0; |
|
89a8588…
|
drh
|
1054 |
if( i>1 ){ |
|
89a8588…
|
drh
|
1055 |
i -= 2; |
|
89a8588…
|
drh
|
1056 |
while( i>=0 ){ |
|
89a8588…
|
drh
|
1057 |
if( zIn[i]=='@' ) return 0; |
|
89a8588…
|
drh
|
1058 |
i--; |
|
89a8588…
|
drh
|
1059 |
} |
|
89a8588…
|
drh
|
1060 |
} |
|
89a8588…
|
drh
|
1061 |
return zTail+1; |
|
b4aadf2…
|
drh
|
1062 |
} |
|
b4aadf2…
|
drh
|
1063 |
|
|
b4aadf2…
|
drh
|
1064 |
/* |
|
dbda8d6…
|
drh
|
1065 |
** Return true if the filename given is a valid filename for |
|
dbda8d6…
|
drh
|
1066 |
** a file in a repository. Valid filenames follow all of the |
|
dbda8d6…
|
drh
|
1067 |
** following rules: |
|
dbda8d6…
|
drh
|
1068 |
** |
|
dbda8d6…
|
drh
|
1069 |
** * Does not begin with "/" |
|
525cc35…
|
drh
|
1070 |
** * Does not contain any path element named "." or ".." |
|
39807cd…
|
jan.nijtmans
|
1071 |
** * Does not contain any of these characters in the path: "\" |
|
dbda8d6…
|
drh
|
1072 |
** * Does not end with "/". |
|
dbda8d6…
|
drh
|
1073 |
** * Does not contain two or more "/" characters in a row. |
|
dbda8d6…
|
drh
|
1074 |
** * Contains at least one character |
|
d48399b…
|
drh
|
1075 |
** |
|
d48399b…
|
drh
|
1076 |
** Invalid UTF8 characters result in a false return if bStrictUtf8 is |
|
d48399b…
|
drh
|
1077 |
** true. If bStrictUtf8 is false, invalid UTF8 characters are silently |
|
4d456c9…
|
drh
|
1078 |
** ignored. See http://en.wikipedia.org/wiki/UTF-8#Invalid_byte_sequences |
|
4d456c9…
|
drh
|
1079 |
** and http://en.wikipedia.org/wiki/Unicode (for the noncharacters) |
|
4d456c9…
|
drh
|
1080 |
** |
|
4d456c9…
|
drh
|
1081 |
** The bStrictUtf8 flag is true for new inputs, but is false when parsing |
|
4d456c9…
|
drh
|
1082 |
** legacy manifests, for backwards compatibility. |
|
dbda8d6…
|
drh
|
1083 |
*/ |
|
d48399b…
|
drh
|
1084 |
int file_is_simple_pathname(const char *z, int bStrictUtf8){ |
|
dbda8d6…
|
drh
|
1085 |
int i; |
|
4d456c9…
|
drh
|
1086 |
unsigned char c = (unsigned char) z[0]; |
|
d48399b…
|
drh
|
1087 |
char maskNonAscii = bStrictUtf8 ? 0x80 : 0x00; |
|
710a8ba…
|
drh
|
1088 |
if( c=='/' || c==0 ) return 0; |
|
710a8ba…
|
drh
|
1089 |
if( c=='.' ){ |
|
525cc35…
|
drh
|
1090 |
if( z[1]=='/' || z[1]==0 ) return 0; |
|
525cc35…
|
drh
|
1091 |
if( z[1]=='.' && (z[2]=='/' || z[2]==0) ) return 0; |
|
525cc35…
|
drh
|
1092 |
} |
|
4d456c9…
|
drh
|
1093 |
for(i=0; (c=(unsigned char)z[i])!=0; i++){ |
|
4d456c9…
|
drh
|
1094 |
if( c & maskNonAscii ){ |
|
a5cd2dd…
|
jan.nijtmans
|
1095 |
if( (z[++i]&0xc0)!=0x80 ){ |
|
a5cd2dd…
|
jan.nijtmans
|
1096 |
/* Invalid first continuation byte */ |
|
a5cd2dd…
|
jan.nijtmans
|
1097 |
return 0; |
|
a5cd2dd…
|
jan.nijtmans
|
1098 |
} |
|
4d456c9…
|
drh
|
1099 |
if( c<0xc2 ){ |
|
4d456c9…
|
drh
|
1100 |
/* Invalid 1-byte UTF-8 sequence, or 2-byte overlong form. */ |
|
4d456c9…
|
drh
|
1101 |
return 0; |
|
4d456c9…
|
drh
|
1102 |
}else if( (c&0xe0)==0xe0 ){ |
|
4d456c9…
|
drh
|
1103 |
/* 3-byte or more */ |
|
4d456c9…
|
drh
|
1104 |
int unicode; |
|
4d456c9…
|
drh
|
1105 |
if( c&0x10 ){ |
|
4d456c9…
|
drh
|
1106 |
/* Unicode characters > U+FFFF are not supported. |
|
4d456c9…
|
drh
|
1107 |
* Windows XP and earlier cannot handle them. |
|
4d456c9…
|
drh
|
1108 |
*/ |
|
4d456c9…
|
drh
|
1109 |
return 0; |
|
4d456c9…
|
drh
|
1110 |
} |
|
4d456c9…
|
drh
|
1111 |
/* This is a 3-byte UTF-8 character */ |
|
a5cd2dd…
|
jan.nijtmans
|
1112 |
unicode = ((c&0x0f)<<12) + ((z[i]&0x3f)<<6) + (z[i+1]&0x3f); |
|
4d456c9…
|
drh
|
1113 |
if( unicode <= 0x07ff ){ |
|
4d456c9…
|
drh
|
1114 |
/* overlong form */ |
|
4d456c9…
|
drh
|
1115 |
return 0; |
|
4d456c9…
|
drh
|
1116 |
}else if( unicode>=0xe000 ){ |
|
4d456c9…
|
drh
|
1117 |
/* U+E000..U+FFFF */ |
|
4d456c9…
|
drh
|
1118 |
if( (unicode<=0xf8ff) || (unicode>=0xfffe) ){ |
|
4d456c9…
|
drh
|
1119 |
/* U+E000..U+F8FF are for private use. |
|
4d456c9…
|
drh
|
1120 |
* U+FFFE..U+FFFF are noncharacters. */ |
|
4d456c9…
|
drh
|
1121 |
return 0; |
|
4d456c9…
|
drh
|
1122 |
} else if( (unicode>=0xfdd0) && (unicode<=0xfdef) ){ |
|
4d456c9…
|
drh
|
1123 |
/* U+FDD0..U+FDEF are noncharacters. */ |
|
4d456c9…
|
drh
|
1124 |
return 0; |
|
4d456c9…
|
drh
|
1125 |
} |
|
a5cd2dd…
|
jan.nijtmans
|
1126 |
}else if( (unicode>=0xd800) && (unicode<=0xdfff) ){ |
|
4d456c9…
|
drh
|
1127 |
/* U+D800..U+DFFF are for surrogate pairs. */ |
|
4d456c9…
|
drh
|
1128 |
return 0; |
|
4d456c9…
|
drh
|
1129 |
} |
|
a5cd2dd…
|
jan.nijtmans
|
1130 |
if( (z[++i]&0xc0)!=0x80 ){ |
|
a5cd2dd…
|
jan.nijtmans
|
1131 |
/* Invalid second continuation byte */ |
|
4d456c9…
|
drh
|
1132 |
return 0; |
|
4d456c9…
|
drh
|
1133 |
} |
|
a5cd2dd…
|
jan.nijtmans
|
1134 |
} |
|
b178bcb…
|
jan.nijtmans
|
1135 |
}else if( bStrictUtf8 && (c=='\\') ){ |
|
dbda8d6…
|
drh
|
1136 |
return 0; |
|
dbda8d6…
|
drh
|
1137 |
} |
|
710a8ba…
|
drh
|
1138 |
if( c=='/' ){ |
|
525cc35…
|
drh
|
1139 |
if( z[i+1]=='/' ) return 0; |
|
525cc35…
|
drh
|
1140 |
if( z[i+1]=='.' ){ |
|
525cc35…
|
drh
|
1141 |
if( z[i+2]=='/' || z[i+2]==0 ) return 0; |
|
525cc35…
|
drh
|
1142 |
if( z[i+2]=='.' && (z[i+3]=='/' || z[i+3]==0) ) return 0; |
|
dbda8d6…
|
drh
|
1143 |
} |
|
dbda8d6…
|
drh
|
1144 |
} |
|
dbda8d6…
|
drh
|
1145 |
} |
|
dbda8d6…
|
drh
|
1146 |
if( z[i-1]=='/' ) return 0; |
|
b178bcb…
|
jan.nijtmans
|
1147 |
return 1; |
|
b178bcb…
|
jan.nijtmans
|
1148 |
} |
|
b4aadf2…
|
drh
|
1149 |
int file_is_simple_pathname_nonstrict(const char *z){ |
|
b4aadf2…
|
drh
|
1150 |
unsigned char c = (unsigned char) z[0]; |
|
b4aadf2…
|
drh
|
1151 |
if( c=='/' || c==0 ) return 0; |
|
b4aadf2…
|
drh
|
1152 |
if( c=='.' ){ |
|
b4aadf2…
|
drh
|
1153 |
if( z[1]=='/' || z[1]==0 ) return 0; |
|
b4aadf2…
|
drh
|
1154 |
if( z[1]=='.' && (z[2]=='/' || z[2]==0) ) return 0; |
|
b4aadf2…
|
drh
|
1155 |
} |
|
b4aadf2…
|
drh
|
1156 |
while( (z = strchr(z+1, '/'))!=0 ){ |
|
b4aadf2…
|
drh
|
1157 |
if( z[1]=='/' ) return 0; |
|
b4aadf2…
|
drh
|
1158 |
if( z[1]==0 ) return 0; |
|
b4aadf2…
|
drh
|
1159 |
if( z[1]=='.' ){ |
|
b4aadf2…
|
drh
|
1160 |
if( z[2]=='/' || z[2]==0 ) return 0; |
|
b4aadf2…
|
drh
|
1161 |
if( z[2]=='.' && (z[3]=='/' || z[3]==0) ) return 0; |
|
b4aadf2…
|
drh
|
1162 |
} |
|
b4aadf2…
|
drh
|
1163 |
} |
|
b4aadf2…
|
drh
|
1164 |
return 1; |
|
b4aadf2…
|
drh
|
1165 |
} |
|
c637a8a…
|
drh
|
1166 |
|
|
c637a8a…
|
drh
|
1167 |
/* |
|
c637a8a…
|
drh
|
1168 |
** If the last component of the pathname in z[0]..z[j-1] is something |
|
c637a8a…
|
drh
|
1169 |
** other than ".." then back it out and return true. If the last |
|
c637a8a…
|
drh
|
1170 |
** component is empty or if it is ".." then return false. |
|
c637a8a…
|
drh
|
1171 |
*/ |
|
c637a8a…
|
drh
|
1172 |
static int backup_dir(const char *z, int *pJ){ |
|
c637a8a…
|
drh
|
1173 |
int j = *pJ; |
|
c637a8a…
|
drh
|
1174 |
int i; |
|
c637a8a…
|
drh
|
1175 |
if( j<=0 ) return 0; |
|
c637a8a…
|
drh
|
1176 |
for(i=j-1; i>0 && z[i-1]!='/'; i--){} |
|
c637a8a…
|
drh
|
1177 |
if( z[i]=='.' && i==j-2 && z[i+1]=='.' ) return 0; |
|
c637a8a…
|
drh
|
1178 |
*pJ = i-1; |
|
c637a8a…
|
drh
|
1179 |
return 1; |
|
c637a8a…
|
drh
|
1180 |
} |
|
dbda8d6…
|
drh
|
1181 |
|
|
dbda8d6…
|
drh
|
1182 |
/* |
|
dbda8d6…
|
drh
|
1183 |
** Simplify a filename by |
|
dbda8d6…
|
drh
|
1184 |
** |
|
95f004b…
|
jan.nijtmans
|
1185 |
** * Remove extended path prefix on windows and cygwin |
|
caf2eb2…
|
jan.nijtmans
|
1186 |
** * Convert all \ into / on windows and cygwin |
|
dbda8d6…
|
drh
|
1187 |
** * removing any trailing and duplicate / |
|
dbda8d6…
|
drh
|
1188 |
** * removing /./ |
|
d31c0f9…
|
drh
|
1189 |
** * removing /A/../ |
|
dbda8d6…
|
drh
|
1190 |
** |
|
dbda8d6…
|
drh
|
1191 |
** Changes are made in-place. Return the new name length. |
|
135ed93…
|
drh
|
1192 |
** If the slash parameter is non-zero, the trailing slash, if any, |
|
135ed93…
|
drh
|
1193 |
** is retained. |
|
dbda8d6…
|
drh
|
1194 |
*/ |
|
135ed93…
|
drh
|
1195 |
int file_simplify_name(char *z, int n, int slash){ |
|
95f004b…
|
jan.nijtmans
|
1196 |
int i = 1, j; |
|
1fff403…
|
andybradford
|
1197 |
assert( z!=0 ); |
|
14c19fb…
|
drh
|
1198 |
if( n<0 ) n = strlen(z); |
|
233a08f…
|
mistachkin
|
1199 |
if( n==0 ) return 0; |
|
c637a8a…
|
drh
|
1200 |
|
|
95f004b…
|
jan.nijtmans
|
1201 |
/* On windows and cygwin convert all \ characters to / |
|
95f004b…
|
jan.nijtmans
|
1202 |
* and remove extended path prefix if present */ |
|
9e60227…
|
mistachkin
|
1203 |
#if defined(_WIN32) || defined(__CYGWIN__) |
|
95f004b…
|
jan.nijtmans
|
1204 |
for(j=0; j<n; j++){ |
|
95f004b…
|
jan.nijtmans
|
1205 |
if( z[j]=='\\' ) z[j] = '/'; |
|
95f004b…
|
jan.nijtmans
|
1206 |
} |
|
95f004b…
|
jan.nijtmans
|
1207 |
if( n>3 && !memcmp(z, "//?/", 4) ){ |
|
95f004b…
|
jan.nijtmans
|
1208 |
if( fossil_strnicmp(z+4,"UNC", 3) ){ |
|
95f004b…
|
jan.nijtmans
|
1209 |
i += 4; |
|
95f004b…
|
jan.nijtmans
|
1210 |
z[0] = z[4]; |
|
95f004b…
|
jan.nijtmans
|
1211 |
}else{ |
|
95f004b…
|
jan.nijtmans
|
1212 |
i += 6; |
|
95f004b…
|
jan.nijtmans
|
1213 |
z[0] = '/'; |
|
95f004b…
|
jan.nijtmans
|
1214 |
} |
|
d569515…
|
drh
|
1215 |
} |
|
d569515…
|
drh
|
1216 |
#endif |
|
c637a8a…
|
drh
|
1217 |
|
|
c637a8a…
|
drh
|
1218 |
/* Removing trailing "/" characters */ |
|
4d456c9…
|
drh
|
1219 |
if( !slash ){ |
|
135ed93…
|
drh
|
1220 |
while( n>1 && z[n-1]=='/' ){ n--; } |
|
135ed93…
|
drh
|
1221 |
} |
|
c637a8a…
|
drh
|
1222 |
|
|
f1173da…
|
drh
|
1223 |
/* Remove duplicate '/' characters. Except, two // at the beginning |
|
f1173da…
|
drh
|
1224 |
** of a pathname is allowed since this is important on windows. */ |
|
95f004b…
|
jan.nijtmans
|
1225 |
for(j=1; i<n; i++){ |
|
c637a8a…
|
drh
|
1226 |
z[j++] = z[i]; |
|
c637a8a…
|
drh
|
1227 |
while( z[i]=='/' && i<n-1 && z[i+1]=='/' ) i++; |
|
c637a8a…
|
drh
|
1228 |
} |
|
c637a8a…
|
drh
|
1229 |
n = j; |
|
c637a8a…
|
drh
|
1230 |
|
|
c637a8a…
|
drh
|
1231 |
/* Skip over zero or more initial "./" sequences */ |
|
c637a8a…
|
drh
|
1232 |
for(i=0; i<n-1 && z[i]=='.' && z[i+1]=='/'; i+=2){} |
|
c637a8a…
|
drh
|
1233 |
|
|
c637a8a…
|
drh
|
1234 |
/* Begin copying from z[i] back to z[j]... */ |
|
c637a8a…
|
drh
|
1235 |
for(j=0; i<n; i++){ |
|
dbda8d6…
|
drh
|
1236 |
if( z[i]=='/' ){ |
|
c637a8a…
|
drh
|
1237 |
/* Skip over internal "/." directory components */ |
|
a7822bc…
|
drh
|
1238 |
if( z[i+1]=='.' && (i+2==n || z[i+2]=='/') ){ |
|
dbda8d6…
|
drh
|
1239 |
i += 1; |
|
dbda8d6…
|
drh
|
1240 |
continue; |
|
dbda8d6…
|
drh
|
1241 |
} |
|
c637a8a…
|
drh
|
1242 |
|
|
c637a8a…
|
drh
|
1243 |
/* If this is a "/.." directory component then back out the |
|
c637a8a…
|
drh
|
1244 |
** previous term of the directory if it is something other than ".." |
|
c637a8a…
|
drh
|
1245 |
** or "." |
|
c637a8a…
|
drh
|
1246 |
*/ |
|
c637a8a…
|
drh
|
1247 |
if( z[i+1]=='.' && i+2<n && z[i+2]=='.' && (i+3==n || z[i+3]=='/') |
|
c637a8a…
|
drh
|
1248 |
&& backup_dir(z, &j) |
|
c637a8a…
|
drh
|
1249 |
){ |
|
d31c0f9…
|
drh
|
1250 |
i += 2; |
|
dbda8d6…
|
drh
|
1251 |
continue; |
|
dbda8d6…
|
drh
|
1252 |
} |
|
dbda8d6…
|
drh
|
1253 |
} |
|
c637a8a…
|
drh
|
1254 |
if( j>=0 ) z[j] = z[i]; |
|
c637a8a…
|
drh
|
1255 |
j++; |
|
dbda8d6…
|
drh
|
1256 |
} |
|
af194db…
|
mgagnon
|
1257 |
if( j==0 ) z[j++] = '/'; |
|
dbda8d6…
|
drh
|
1258 |
z[j] = 0; |
|
dbda8d6…
|
drh
|
1259 |
return j; |
|
dbda8d6…
|
drh
|
1260 |
} |
|
dbda8d6…
|
drh
|
1261 |
|
|
dbda8d6…
|
drh
|
1262 |
/* |
|
c637a8a…
|
drh
|
1263 |
** COMMAND: test-simplify-name |
|
c637a8a…
|
drh
|
1264 |
** |
|
8bfd995…
|
rberteig
|
1265 |
** Usage: %fossil test-simplify-name FILENAME... |
|
c637a8a…
|
drh
|
1266 |
** |
|
89a8588…
|
drh
|
1267 |
** Print the simplified versions of each FILENAME. This is used to test |
|
89a8588…
|
drh
|
1268 |
** the file_simplify_name() routine. |
|
89a8588…
|
drh
|
1269 |
** |
|
89a8588…
|
drh
|
1270 |
** If FILENAME is of the form "HOST:PATH" or "USER@HOST:PATH", then remove |
|
89a8588…
|
drh
|
1271 |
** and print the remote host prefix first. This is used to test the |
|
89a8588…
|
drh
|
1272 |
** file_skip_userhost() interface. |
|
c637a8a…
|
drh
|
1273 |
*/ |
|
c637a8a…
|
drh
|
1274 |
void cmd_test_simplify_name(void){ |
|
c637a8a…
|
drh
|
1275 |
int i; |
|
c637a8a…
|
drh
|
1276 |
char *z; |
|
89a8588…
|
drh
|
1277 |
const char *zTail; |
|
c637a8a…
|
drh
|
1278 |
for(i=2; i<g.argc; i++){ |
|
89a8588…
|
drh
|
1279 |
zTail = file_skip_userhost(g.argv[i]); |
|
89a8588…
|
drh
|
1280 |
if( zTail ){ |
|
89a8588…
|
drh
|
1281 |
fossil_print("... ON REMOTE: %.*s\n", (int)(zTail-g.argv[i]), g.argv[i]); |
|
4c3e172…
|
danield
|
1282 |
z = fossil_strdup(zTail); |
|
89a8588…
|
drh
|
1283 |
}else{ |
|
4c3e172…
|
danield
|
1284 |
z = fossil_strdup(g.argv[i]); |
|
89a8588…
|
drh
|
1285 |
} |
|
d8ec765…
|
drh
|
1286 |
fossil_print("[%s] -> ", z); |
|
135ed93…
|
drh
|
1287 |
file_simplify_name(z, -1, 0); |
|
d8ec765…
|
drh
|
1288 |
fossil_print("[%s]\n", z); |
|
c637a8a…
|
drh
|
1289 |
fossil_free(z); |
|
d8ec765…
|
drh
|
1290 |
} |
|
d8ec765…
|
drh
|
1291 |
} |
|
d8ec765…
|
drh
|
1292 |
|
|
d8ec765…
|
drh
|
1293 |
/* |
|
4b34254…
|
drh
|
1294 |
** Get the current working directory. |
|
4b34254…
|
drh
|
1295 |
** |
|
14733d1…
|
jan.nijtmans
|
1296 |
** On windows, the name is converted from unicode to UTF8 and all '\\' |
|
4b34254…
|
drh
|
1297 |
** characters are converted to '/'. No conversions are needed on |
|
4b34254…
|
drh
|
1298 |
** unix. |
|
f132f86…
|
drh
|
1299 |
** |
|
f132f86…
|
drh
|
1300 |
** Store the value of the CWD in zBuf which is nBuf bytes in size. |
|
f132f86…
|
drh
|
1301 |
** or if zBuf==0, allocate space to hold the result using fossil_malloc(). |
|
4b34254…
|
drh
|
1302 |
*/ |
|
f132f86…
|
drh
|
1303 |
char *file_getcwd(char *zBuf, int nBuf){ |
|
f132f86…
|
drh
|
1304 |
if( zBuf==0 ){ |
|
85b8320…
|
drh
|
1305 |
char zTemp[2000]; |
|
85b8320…
|
drh
|
1306 |
return fossil_strdup(file_getcwd(zTemp, sizeof(zTemp))); |
|
f132f86…
|
drh
|
1307 |
} |
|
4b34254…
|
drh
|
1308 |
#ifdef _WIN32 |
|
d9ff968…
|
mistachkin
|
1309 |
win32_getcwd(zBuf, nBuf); |
|
4b34254…
|
drh
|
1310 |
#else |
|
4b34254…
|
drh
|
1311 |
if( getcwd(zBuf, nBuf-1)==0 ){ |
|
89e6eb9…
|
drh
|
1312 |
if( errno==ERANGE ){ |
|
31c7bdb…
|
larrybr
|
1313 |
fossil_fatal("pwd too big: max %d", nBuf-1); |
|
89e6eb9…
|
drh
|
1314 |
}else{ |
|
31c7bdb…
|
larrybr
|
1315 |
fossil_fatal("cannot find current working directory; %s", |
|
89e6eb9…
|
drh
|
1316 |
strerror(errno)); |
|
89e6eb9…
|
drh
|
1317 |
} |
|
4b34254…
|
drh
|
1318 |
} |
|
89e6eb9…
|
drh
|
1319 |
#endif |
|
85b8320…
|
drh
|
1320 |
return zBuf; |
|
a7248d8…
|
drh
|
1321 |
} |
|
a7248d8…
|
drh
|
1322 |
|
|
a7248d8…
|
drh
|
1323 |
/* |
|
a7248d8…
|
drh
|
1324 |
** Return true if zPath is an absolute pathname. Return false |
|
a7248d8…
|
drh
|
1325 |
** if it is relative. |
|
a7248d8…
|
drh
|
1326 |
*/ |
|
a7248d8…
|
drh
|
1327 |
int file_is_absolute_path(const char *zPath){ |
|
b523bf0…
|
mistachkin
|
1328 |
if( fossil_isdirsep(zPath[0]) |
|
caf2eb2…
|
jan.nijtmans
|
1329 |
#if defined(_WIN32) || defined(__CYGWIN__) |
|
caf2eb2…
|
jan.nijtmans
|
1330 |
|| (fossil_isalpha(zPath[0]) && zPath[1]==':' |
|
b523bf0…
|
mistachkin
|
1331 |
&& (fossil_isdirsep(zPath[2]) || zPath[2]=='\0')) |
|
a7248d8…
|
drh
|
1332 |
#endif |
|
a7248d8…
|
drh
|
1333 |
){ |
|
a7248d8…
|
drh
|
1334 |
return 1; |
|
a7248d8…
|
drh
|
1335 |
}else{ |
|
a7248d8…
|
drh
|
1336 |
return 0; |
|
a7248d8…
|
drh
|
1337 |
} |
|
4b34254…
|
drh
|
1338 |
} |
|
4b34254…
|
drh
|
1339 |
|
|
4b34254…
|
drh
|
1340 |
/* |
|
4b34254…
|
drh
|
1341 |
** Compute a canonical pathname for a file or directory. |
|
40376ef…
|
drh
|
1342 |
** |
|
40376ef…
|
drh
|
1343 |
** * Make the name absolute if it is relative. |
|
40376ef…
|
drh
|
1344 |
** * Remove redundant / characters |
|
40376ef…
|
drh
|
1345 |
** * Remove all /./ path elements. |
|
40376ef…
|
drh
|
1346 |
** * Convert /A/../ to just / |
|
40376ef…
|
drh
|
1347 |
** * On windows, add the drive letter prefix. |
|
40376ef…
|
drh
|
1348 |
** |
|
135ed93…
|
drh
|
1349 |
** If the slash parameter is non-zero, the trailing slash, if any, |
|
135ed93…
|
drh
|
1350 |
** is retained. |
|
15a7b1f…
|
drh
|
1351 |
** |
|
15a7b1f…
|
drh
|
1352 |
** See also: file_canonical_name_dup() |
|
4b34254…
|
drh
|
1353 |
*/ |
|
135ed93…
|
drh
|
1354 |
void file_canonical_name(const char *zOrigName, Blob *pOut, int slash){ |
|
40376ef…
|
drh
|
1355 |
char zPwd[2000]; |
|
af194db…
|
mgagnon
|
1356 |
blob_zero(pOut); |
|
a7248d8…
|
drh
|
1357 |
if( file_is_absolute_path(zOrigName) ){ |
|
40376ef…
|
drh
|
1358 |
#if defined(_WIN32) |
|
40376ef…
|
drh
|
1359 |
if( fossil_isdirsep(zOrigName[0]) ){ |
|
40376ef…
|
drh
|
1360 |
/* Add the drive letter to the full pathname */ |
|
40376ef…
|
drh
|
1361 |
file_getcwd(zPwd, sizeof(zPwd)-strlen(zOrigName)); |
|
40376ef…
|
drh
|
1362 |
blob_appendf(pOut, "%.2s%/", zPwd, zOrigName); |
|
40376ef…
|
drh
|
1363 |
}else |
|
40376ef…
|
drh
|
1364 |
#endif |
|
40376ef…
|
drh
|
1365 |
{ |
|
40376ef…
|
drh
|
1366 |
blob_appendf(pOut, "%/", zOrigName); |
|
40376ef…
|
drh
|
1367 |
} |
|
af194db…
|
mgagnon
|
1368 |
}else{ |
|
af194db…
|
mgagnon
|
1369 |
file_getcwd(zPwd, sizeof(zPwd)-strlen(zOrigName)); |
|
af194db…
|
mgagnon
|
1370 |
if( zPwd[0]=='/' && strlen(zPwd)==1 ){ |
|
25950f8…
|
jan.nijtmans
|
1371 |
/* when on '/', don't add an extra '/' */ |
|
af194db…
|
mgagnon
|
1372 |
if( zOrigName[0]=='.' && strlen(zOrigName)==1 ){ |
|
25950f8…
|
jan.nijtmans
|
1373 |
/* '.' when on '/' mean '/' */ |
|
af194db…
|
mgagnon
|
1374 |
blob_appendf(pOut, "%/", zPwd); |
|
af194db…
|
mgagnon
|
1375 |
}else{ |
|
af194db…
|
mgagnon
|
1376 |
blob_appendf(pOut, "%/%/", zPwd, zOrigName); |
|
af194db…
|
mgagnon
|
1377 |
} |
|
af194db…
|
mgagnon
|
1378 |
}else{ |
|
af194db…
|
mgagnon
|
1379 |
blob_appendf(pOut, "%//%/", zPwd, zOrigName); |
|
af194db…
|
mgagnon
|
1380 |
} |
|
af194db…
|
mgagnon
|
1381 |
} |
|
caf2eb2…
|
jan.nijtmans
|
1382 |
#if defined(_WIN32) || defined(__CYGWIN__) |
|
af194db…
|
mgagnon
|
1383 |
{ |
|
af194db…
|
mgagnon
|
1384 |
char *zOut; |
|
135ed93…
|
drh
|
1385 |
/* |
|
caf2eb2…
|
jan.nijtmans
|
1386 |
** On Windows/cygwin, normalize the drive letter to upper case. |
|
135ed93…
|
drh
|
1387 |
*/ |
|
135ed93…
|
drh
|
1388 |
zOut = blob_str(pOut); |
|
af194db…
|
mgagnon
|
1389 |
if( fossil_islower(zOut[0]) && zOut[1]==':' && zOut[2]=='/' ){ |
|
135ed93…
|
drh
|
1390 |
zOut[0] = fossil_toupper(zOut[0]); |
|
135ed93…
|
drh
|
1391 |
} |
|
af194db…
|
mgagnon
|
1392 |
} |
|
af194db…
|
mgagnon
|
1393 |
#endif |
|
135ed93…
|
drh
|
1394 |
blob_resize(pOut, file_simplify_name(blob_buffer(pOut), |
|
135ed93…
|
drh
|
1395 |
blob_size(pOut), slash)); |
|
135ed93…
|
drh
|
1396 |
} |
|
135ed93…
|
drh
|
1397 |
|
|
135ed93…
|
drh
|
1398 |
/* |
|
15a7b1f…
|
drh
|
1399 |
** Compute the canonical name of a file. Store that name in |
|
15a7b1f…
|
drh
|
1400 |
** memory obtained from fossil_malloc() and return a pointer to the |
|
15a7b1f…
|
drh
|
1401 |
** name. |
|
15a7b1f…
|
drh
|
1402 |
** |
|
15a7b1f…
|
drh
|
1403 |
** See also: file_canonical_name() |
|
15a7b1f…
|
drh
|
1404 |
*/ |
|
15a7b1f…
|
drh
|
1405 |
char *file_canonical_name_dup(const char *zOrigName){ |
|
15a7b1f…
|
drh
|
1406 |
Blob x; |
|
15a7b1f…
|
drh
|
1407 |
if( zOrigName==0 ) return 0; |
|
15a7b1f…
|
drh
|
1408 |
blob_init(&x, 0, 0); |
|
15a7b1f…
|
drh
|
1409 |
file_canonical_name(zOrigName, &x, 0); |
|
15a7b1f…
|
drh
|
1410 |
return blob_str(&x); |
|
78e6399…
|
drh
|
1411 |
} |
|
78e6399…
|
drh
|
1412 |
|
|
78e6399…
|
drh
|
1413 |
/* |
|
78e6399…
|
drh
|
1414 |
** Convert zPath, which is a relative pathname rooted at zDir, into the |
|
78e6399…
|
drh
|
1415 |
** case preferred by the underlying filesystem. Return the a copy |
|
78e6399…
|
drh
|
1416 |
** of the converted path in memory obtained from fossil_malloc(). |
|
78e6399…
|
drh
|
1417 |
** |
|
78e6399…
|
drh
|
1418 |
** For case-sensitive filesystems, such as on Linux, this routine is |
|
78e6399…
|
drh
|
1419 |
** just fossil_strdup(). But for case-insenstiive but "case preserving" |
|
78e6399…
|
drh
|
1420 |
** filesystems, such as on MacOS or Windows, we want the filename to be |
|
78e6399…
|
drh
|
1421 |
** in the preserved casing. That's what this routine does. |
|
78e6399…
|
drh
|
1422 |
*/ |
|
78e6399…
|
drh
|
1423 |
char *file_case_preferred_name(const char *zDir, const char *zPath){ |
|
9919dfb…
|
florian
|
1424 |
#ifndef _WIN32 /* Call win32_file_case_preferred_name() on Windows. */ |
|
78e6399…
|
drh
|
1425 |
DIR *d; |
|
78e6399…
|
drh
|
1426 |
int i; |
|
78e6399…
|
drh
|
1427 |
char *zResult = 0; |
|
78e6399…
|
drh
|
1428 |
void *zNative = 0; |
|
78e6399…
|
drh
|
1429 |
|
|
78e6399…
|
drh
|
1430 |
if( filenames_are_case_sensitive() ){ |
|
78e6399…
|
drh
|
1431 |
return fossil_strdup(zPath); |
|
78e6399…
|
drh
|
1432 |
} |
|
78e6399…
|
drh
|
1433 |
for(i=0; zPath[i] && zPath[i]!='/' && zPath[i]!='\\'; i++){} |
|
78e6399…
|
drh
|
1434 |
zNative = fossil_utf8_to_path(zDir, 1); |
|
78e6399…
|
drh
|
1435 |
d = opendir(zNative); |
|
78e6399…
|
drh
|
1436 |
if( d ){ |
|
78e6399…
|
drh
|
1437 |
struct dirent *pEntry; |
|
78e6399…
|
drh
|
1438 |
while( (pEntry = readdir(d))!=0 ){ |
|
78e6399…
|
drh
|
1439 |
char *zUtf8 = fossil_path_to_utf8(pEntry->d_name); |
|
78e6399…
|
drh
|
1440 |
if( fossil_strnicmp(zUtf8, zPath, i)==0 && zUtf8[i]==0 ){ |
|
78e6399…
|
drh
|
1441 |
if( zPath[i]==0 ){ |
|
78e6399…
|
drh
|
1442 |
zResult = fossil_strdup(zUtf8); |
|
78e6399…
|
drh
|
1443 |
}else{ |
|
78e6399…
|
drh
|
1444 |
char *zSubDir = mprintf("%s/%s", zDir, zUtf8); |
|
78e6399…
|
drh
|
1445 |
char *zSubPath = file_case_preferred_name(zSubDir, &zPath[i+1]); |
|
78e6399…
|
drh
|
1446 |
zResult = mprintf("%s/%s", zUtf8, zSubPath); |
|
78e6399…
|
drh
|
1447 |
fossil_free(zSubPath); |
|
78e6399…
|
drh
|
1448 |
fossil_free(zSubDir); |
|
78e6399…
|
drh
|
1449 |
} |
|
78e6399…
|
drh
|
1450 |
fossil_path_free(zUtf8); |
|
78e6399…
|
drh
|
1451 |
break; |
|
78e6399…
|
drh
|
1452 |
} |
|
78e6399…
|
drh
|
1453 |
fossil_path_free(zUtf8); |
|
78e6399…
|
drh
|
1454 |
} |
|
78e6399…
|
drh
|
1455 |
closedir(d); |
|
78e6399…
|
drh
|
1456 |
} |
|
78e6399…
|
drh
|
1457 |
fossil_path_free(zNative); |
|
78e6399…
|
drh
|
1458 |
if( zResult==0 ) zResult = fossil_strdup(zPath); |
|
78e6399…
|
drh
|
1459 |
return zResult; |
|
6041e25…
|
florian
|
1460 |
#else /* !_WIN32 */ |
|
9919dfb…
|
florian
|
1461 |
return win32_file_case_preferred_name(zDir,zPath); |
|
6041e25…
|
florian
|
1462 |
#endif /* !_WIN32 */ |
|
78e6399…
|
drh
|
1463 |
} |
|
78e6399…
|
drh
|
1464 |
|
|
78e6399…
|
drh
|
1465 |
/* |
|
78e6399…
|
drh
|
1466 |
** COMMAND: test-case-filename |
|
78e6399…
|
drh
|
1467 |
** |
|
78e6399…
|
drh
|
1468 |
** Usage: fossil test-case-filename DIRECTORY PATH PATH PATH .... |
|
78e6399…
|
drh
|
1469 |
** |
|
78e6399…
|
drh
|
1470 |
** All the PATH arguments (there must be one at least one) are pathnames |
|
78e6399…
|
drh
|
1471 |
** relative to DIRECTORY. This test command prints the OS-preferred name |
|
78e6399…
|
drh
|
1472 |
** for each PATH in filesystems where case is not significant. |
|
78e6399…
|
drh
|
1473 |
*/ |
|
78e6399…
|
drh
|
1474 |
void test_preferred_fn(void){ |
|
78e6399…
|
drh
|
1475 |
int i; |
|
78e6399…
|
drh
|
1476 |
if( g.argc<4 ){ |
|
78e6399…
|
drh
|
1477 |
usage("DIRECTORY PATH ..."); |
|
78e6399…
|
drh
|
1478 |
} |
|
78e6399…
|
drh
|
1479 |
for(i=3; i<g.argc; i++){ |
|
78e6399…
|
drh
|
1480 |
char *z = file_case_preferred_name(g.argv[2], g.argv[i]); |
|
78e6399…
|
drh
|
1481 |
fossil_print("%s -> %s\n", g.argv[i], z); |
|
78e6399…
|
drh
|
1482 |
fossil_free(z); |
|
78e6399…
|
drh
|
1483 |
} |
|
491b986…
|
mgagnon
|
1484 |
} |
|
491b986…
|
mgagnon
|
1485 |
|
|
491b986…
|
mgagnon
|
1486 |
/* |
|
01fcc6a…
|
drh
|
1487 |
** The input is the name of an executable, such as one might |
|
01fcc6a…
|
drh
|
1488 |
** type on a command-line. This routine resolves that name into |
|
01fcc6a…
|
drh
|
1489 |
** a full pathname. The result is obtained from fossil_malloc() |
|
01fcc6a…
|
drh
|
1490 |
** and should be freed by the caller. |
|
01fcc6a…
|
drh
|
1491 |
*/ |
|
01fcc6a…
|
drh
|
1492 |
char *file_fullexename(const char *zCmd){ |
|
01fcc6a…
|
drh
|
1493 |
#ifdef _WIN32 |
|
d8c32eb…
|
drh
|
1494 |
char *zPath; |
|
d8c32eb…
|
drh
|
1495 |
char *z = 0; |
|
d8c32eb…
|
drh
|
1496 |
const char *zExe = ""; |
|
d8c32eb…
|
drh
|
1497 |
if( sqlite3_strlike("%.exe",zCmd,0)!=0 ) zExe = ".exe"; |
|
d8c32eb…
|
drh
|
1498 |
if( file_is_absolute_path(zCmd) ){ |
|
d8c32eb…
|
drh
|
1499 |
return mprintf("%s%s", zCmd, zExe); |
|
d8c32eb…
|
drh
|
1500 |
} |
|
d8c32eb…
|
drh
|
1501 |
if( strchr(zCmd,'\\')!=0 && strchr(zCmd,'/')!=0 ){ |
|
d8c32eb…
|
drh
|
1502 |
int i; |
|
d8c32eb…
|
drh
|
1503 |
Blob out = BLOB_INITIALIZER; |
|
d8c32eb…
|
drh
|
1504 |
file_canonical_name(zCmd, &out, 0); |
|
d8c32eb…
|
drh
|
1505 |
blob_append(&out, zExe, -1); |
|
d8c32eb…
|
drh
|
1506 |
z = fossil_strdup(blob_str(&out)); |
|
d8c32eb…
|
drh
|
1507 |
blob_reset(&out); |
|
d8c32eb…
|
drh
|
1508 |
for(i=0; z[i]; i++){ if( z[i]=='/' ) z[i] = '\\'; } |
|
d8c32eb…
|
drh
|
1509 |
return z; |
|
d8c32eb…
|
drh
|
1510 |
} |
|
d8c32eb…
|
drh
|
1511 |
z = mprintf(".\\%s%s", zCmd, zExe); |
|
d8c32eb…
|
drh
|
1512 |
if( file_isfile(z, ExtFILE) ){ |
|
d8c32eb…
|
drh
|
1513 |
int i; |
|
d8c32eb…
|
drh
|
1514 |
Blob out = BLOB_INITIALIZER; |
|
d8c32eb…
|
drh
|
1515 |
file_canonical_name(zCmd, &out, 0); |
|
d8c32eb…
|
drh
|
1516 |
blob_append(&out, zExe, -1); |
|
d8c32eb…
|
drh
|
1517 |
z = fossil_strdup(blob_str(&out)); |
|
d8c32eb…
|
drh
|
1518 |
blob_reset(&out); |
|
d8c32eb…
|
drh
|
1519 |
for(i=0; z[i]; i++){ if( z[i]=='/' ) z[i] = '\\'; } |
|
d8c32eb…
|
drh
|
1520 |
return z; |
|
d8c32eb…
|
drh
|
1521 |
} |
|
d8c32eb…
|
drh
|
1522 |
fossil_free(z); |
|
d8c32eb…
|
drh
|
1523 |
zPath = fossil_getenv("PATH"); |
|
d8c32eb…
|
drh
|
1524 |
while( zPath && zPath[0] ){ |
|
d8c32eb…
|
drh
|
1525 |
int n; |
|
d8c32eb…
|
drh
|
1526 |
char *zColon; |
|
d8c32eb…
|
drh
|
1527 |
zColon = strchr(zPath, ';'); |
|
d8c32eb…
|
drh
|
1528 |
n = zColon ? (int)(zColon-zPath) : (int)strlen(zPath); |
|
d8c32eb…
|
drh
|
1529 |
while( n>0 && zPath[n-1]=='\\' ){ n--; } |
|
d8c32eb…
|
drh
|
1530 |
z = mprintf("%.*s\\%s%s", n, zPath, zCmd, zExe); |
|
d8c32eb…
|
drh
|
1531 |
if( file_isfile(z, ExtFILE) ){ |
|
d8c32eb…
|
drh
|
1532 |
return z; |
|
d8c32eb…
|
drh
|
1533 |
} |
|
d8c32eb…
|
drh
|
1534 |
fossil_free(z); |
|
d8c32eb…
|
drh
|
1535 |
if( zColon==0 ) break; |
|
d8c32eb…
|
drh
|
1536 |
zPath = zColon+1; |
|
d8c32eb…
|
drh
|
1537 |
} |
|
01fcc6a…
|
drh
|
1538 |
return fossil_strdup(zCmd); |
|
01fcc6a…
|
drh
|
1539 |
#else |
|
01fcc6a…
|
drh
|
1540 |
char *zPath; |
|
01fcc6a…
|
drh
|
1541 |
char *z; |
|
01fcc6a…
|
drh
|
1542 |
if( zCmd[0]=='/' ){ |
|
01fcc6a…
|
drh
|
1543 |
return fossil_strdup(zCmd); |
|
01fcc6a…
|
drh
|
1544 |
} |
|
01fcc6a…
|
drh
|
1545 |
if( strchr(zCmd,'/')!=0 ){ |
|
01fcc6a…
|
drh
|
1546 |
Blob out = BLOB_INITIALIZER; |
|
01fcc6a…
|
drh
|
1547 |
file_canonical_name(zCmd, &out, 0); |
|
01fcc6a…
|
drh
|
1548 |
z = fossil_strdup(blob_str(&out)); |
|
01fcc6a…
|
drh
|
1549 |
blob_reset(&out); |
|
01fcc6a…
|
drh
|
1550 |
return z; |
|
01fcc6a…
|
drh
|
1551 |
} |
|
01fcc6a…
|
drh
|
1552 |
zPath = fossil_getenv("PATH"); |
|
01fcc6a…
|
drh
|
1553 |
while( zPath && zPath[0] ){ |
|
01fcc6a…
|
drh
|
1554 |
int n; |
|
01fcc6a…
|
drh
|
1555 |
char *zColon; |
|
01fcc6a…
|
drh
|
1556 |
zColon = strchr(zPath, ':'); |
|
01fcc6a…
|
drh
|
1557 |
n = zColon ? (int)(zColon-zPath) : (int)strlen(zPath); |
|
01fcc6a…
|
drh
|
1558 |
z = mprintf("%.*s/%s", n, zPath, zCmd); |
|
01fcc6a…
|
drh
|
1559 |
if( file_isexe(z, ExtFILE) ){ |
|
01fcc6a…
|
drh
|
1560 |
return z; |
|
01fcc6a…
|
drh
|
1561 |
} |
|
01fcc6a…
|
drh
|
1562 |
fossil_free(z); |
|
01fcc6a…
|
drh
|
1563 |
if( zColon==0 ) break; |
|
01fcc6a…
|
drh
|
1564 |
zPath = zColon+1; |
|
01fcc6a…
|
drh
|
1565 |
} |
|
01fcc6a…
|
drh
|
1566 |
return fossil_strdup(zCmd); |
|
01fcc6a…
|
drh
|
1567 |
#endif |
|
01fcc6a…
|
drh
|
1568 |
} |
|
01fcc6a…
|
drh
|
1569 |
|
|
01fcc6a…
|
drh
|
1570 |
/* |
|
01fcc6a…
|
drh
|
1571 |
** COMMAND: test-which |
|
01fcc6a…
|
drh
|
1572 |
** |
|
01fcc6a…
|
drh
|
1573 |
** Usage: %fossil test-which ARGS... |
|
01fcc6a…
|
drh
|
1574 |
** |
|
01fcc6a…
|
drh
|
1575 |
** For each argument, search the PATH for the executable with the name |
|
01fcc6a…
|
drh
|
1576 |
** and print its full pathname. |
|
b3f270f…
|
drh
|
1577 |
** |
|
b3f270f…
|
drh
|
1578 |
** See also the "which" command (without the "test-" prefix). The plain |
|
b3f270f…
|
drh
|
1579 |
** "which" command is more convenient to use since it provides the -a/-all |
|
b3f270f…
|
drh
|
1580 |
** option, and because it is shorter. The "fossil which" command without |
|
b3f270f…
|
drh
|
1581 |
** the "test-" prefix is recommended for day-to-day use. This command is |
|
b3f270f…
|
drh
|
1582 |
** retained because it tests the internal file_fullexename() function |
|
b3f270f…
|
drh
|
1583 |
** whereas plain "which" does not. |
|
01fcc6a…
|
drh
|
1584 |
*/ |
|
01fcc6a…
|
drh
|
1585 |
void test_which_cmd(void){ |
|
01fcc6a…
|
drh
|
1586 |
int i; |
|
01fcc6a…
|
drh
|
1587 |
for(i=2; i<g.argc; i++){ |
|
01fcc6a…
|
drh
|
1588 |
char *z = file_fullexename(g.argv[i]); |
|
01fcc6a…
|
drh
|
1589 |
fossil_print("%z\n", z); |
|
01fcc6a…
|
drh
|
1590 |
} |
|
01fcc6a…
|
drh
|
1591 |
} |
|
01fcc6a…
|
drh
|
1592 |
|
|
01fcc6a…
|
drh
|
1593 |
/* |
|
2375d6c…
|
mistachkin
|
1594 |
** Emits the effective or raw stat() information for the specified |
|
2375d6c…
|
mistachkin
|
1595 |
** file or directory, optionally preserving the trailing slash and |
|
2375d6c…
|
mistachkin
|
1596 |
** resetting the cached stat() information. |
|
2375d6c…
|
mistachkin
|
1597 |
*/ |
|
2375d6c…
|
mistachkin
|
1598 |
static void emitFileStat( |
|
2375d6c…
|
mistachkin
|
1599 |
const char *zPath, |
|
2375d6c…
|
mistachkin
|
1600 |
int slash, |
|
2375d6c…
|
mistachkin
|
1601 |
int reset |
|
2375d6c…
|
mistachkin
|
1602 |
){ |
|
fdc6af2…
|
drh
|
1603 |
char zBuf[200]; |
|
fdc6af2…
|
drh
|
1604 |
char *z; |
|
2375d6c…
|
mistachkin
|
1605 |
Blob x; |
|
750879e…
|
mistachkin
|
1606 |
char *zFull; |
|
8434738…
|
drh
|
1607 |
int rc; |
|
8434738…
|
drh
|
1608 |
sqlite3_int64 iMtime; |
|
8434738…
|
drh
|
1609 |
struct fossilStat testFileStat; |
|
2375d6c…
|
mistachkin
|
1610 |
memset(zBuf, 0, sizeof(zBuf)); |
|
2375d6c…
|
mistachkin
|
1611 |
blob_zero(&x); |
|
2375d6c…
|
mistachkin
|
1612 |
file_canonical_name(zPath, &x, slash); |
|
750879e…
|
mistachkin
|
1613 |
zFull = blob_str(&x); |
|
f741baa…
|
drh
|
1614 |
fossil_print("[%s] -> [%s]\n", zPath, zFull); |
|
8434738…
|
drh
|
1615 |
memset(&testFileStat, 0, sizeof(struct fossilStat)); |
|
8434738…
|
drh
|
1616 |
rc = fossil_stat(zPath, &testFileStat, 0); |
|
8434738…
|
drh
|
1617 |
fossil_print(" stat_rc = %d\n", rc); |
|
8434738…
|
drh
|
1618 |
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", testFileStat.st_size); |
|
8434738…
|
drh
|
1619 |
fossil_print(" stat_size = %s\n", zBuf); |
|
af6833b…
|
drh
|
1620 |
if( g.db==0 ) sqlite3_open(":memory:", &g.db); |
|
8434738…
|
drh
|
1621 |
z = db_text(0, "SELECT datetime(%lld, 'unixepoch')", testFileStat.st_mtime); |
|
8434738…
|
drh
|
1622 |
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld (%s)", testFileStat.st_mtime, z); |
|
8434738…
|
drh
|
1623 |
fossil_free(z); |
|
8434738…
|
drh
|
1624 |
fossil_print(" stat_mtime = %s\n", zBuf); |
|
8434738…
|
drh
|
1625 |
fossil_print(" stat_mode = 0%o\n", testFileStat.st_mode); |
|
8434738…
|
drh
|
1626 |
memset(&testFileStat, 0, sizeof(struct fossilStat)); |
|
8434738…
|
drh
|
1627 |
rc = fossil_stat(zPath, &testFileStat, 1); |
|
8434738…
|
drh
|
1628 |
fossil_print(" l_stat_rc = %d\n", rc); |
|
8434738…
|
drh
|
1629 |
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", testFileStat.st_size); |
|
8434738…
|
drh
|
1630 |
fossil_print(" l_stat_size = %s\n", zBuf); |
|
8434738…
|
drh
|
1631 |
z = db_text(0, "SELECT datetime(%lld, 'unixepoch')", testFileStat.st_mtime); |
|
8434738…
|
drh
|
1632 |
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld (%s)", testFileStat.st_mtime, z); |
|
8434738…
|
drh
|
1633 |
fossil_free(z); |
|
8434738…
|
drh
|
1634 |
fossil_print(" l_stat_mtime = %s\n", zBuf); |
|
8434738…
|
drh
|
1635 |
fossil_print(" l_stat_mode = 0%o\n", testFileStat.st_mode); |
|
8434738…
|
drh
|
1636 |
if( reset ) resetStat(); |
|
1772357…
|
drh
|
1637 |
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", file_size(zPath,ExtFILE)); |
|
1772357…
|
drh
|
1638 |
fossil_print(" file_size(ExtFILE) = %s\n", zBuf); |
|
1772357…
|
drh
|
1639 |
iMtime = file_mtime(zPath, ExtFILE); |
|
1772357…
|
drh
|
1640 |
z = db_text(0, "SELECT datetime(%lld, 'unixepoch')", iMtime); |
|
1772357…
|
drh
|
1641 |
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld (%s)", iMtime, z); |
|
1772357…
|
drh
|
1642 |
fossil_free(z); |
|
1772357…
|
drh
|
1643 |
fossil_print(" file_mtime(ExtFILE) = %s\n", zBuf); |
|
1772357…
|
drh
|
1644 |
fossil_print(" file_mode(ExtFILE) = 0%o\n", file_mode(zPath,ExtFILE)); |
|
1772357…
|
drh
|
1645 |
fossil_print(" file_isfile(ExtFILE) = %d\n", file_isfile(zPath,ExtFILE)); |
|
1772357…
|
drh
|
1646 |
fossil_print(" file_isdir(ExtFILE) = %d\n", file_isdir(zPath,ExtFILE)); |
|
effdada…
|
drh
|
1647 |
fossil_print(" file_issocket() = %d\n", file_issocket(zPath)); |
|
1772357…
|
drh
|
1648 |
if( reset ) resetStat(); |
|
1772357…
|
drh
|
1649 |
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", file_size(zPath,RepoFILE)); |
|
1772357…
|
drh
|
1650 |
fossil_print(" file_size(RepoFILE) = %s\n", zBuf); |
|
1772357…
|
drh
|
1651 |
iMtime = file_mtime(zPath,RepoFILE); |
|
1772357…
|
drh
|
1652 |
z = db_text(0, "SELECT datetime(%lld, 'unixepoch')", iMtime); |
|
1772357…
|
drh
|
1653 |
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld (%s)", iMtime, z); |
|
1772357…
|
drh
|
1654 |
fossil_free(z); |
|
1772357…
|
drh
|
1655 |
fossil_print(" file_mtime(RepoFILE) = %s\n", zBuf); |
|
1772357…
|
drh
|
1656 |
fossil_print(" file_mode(RepoFILE) = 0%o\n", file_mode(zPath,RepoFILE)); |
|
1772357…
|
drh
|
1657 |
fossil_print(" file_isfile(RepoFILE) = %d\n", file_isfile(zPath,RepoFILE)); |
|
1772357…
|
drh
|
1658 |
fossil_print(" file_isfile_or_link = %d\n", file_isfile_or_link(zPath)); |
|
1772357…
|
drh
|
1659 |
fossil_print(" file_islink = %d\n", file_islink(zPath)); |
|
1772357…
|
drh
|
1660 |
fossil_print(" file_isexe(RepoFILE) = %d\n", file_isexe(zPath,RepoFILE)); |
|
1772357…
|
drh
|
1661 |
fossil_print(" file_isdir(RepoFILE) = %d\n", file_isdir(zPath,RepoFILE)); |
|
aad4c24…
|
drh
|
1662 |
fossil_print(" file_is_repository = %d\n", file_is_repository(zPath)); |
|
f741baa…
|
drh
|
1663 |
fossil_print(" file_is_reserved_name = %d\n", |
|
f741baa…
|
drh
|
1664 |
file_is_reserved_name(zFull,-1)); |
|
61132ce…
|
drh
|
1665 |
fossil_print(" file_in_cwd = %d\n", file_in_cwd(zPath)); |
|
f741baa…
|
drh
|
1666 |
blob_reset(&x); |
|
8434738…
|
drh
|
1667 |
if( reset ) resetStat(); |
|
2375d6c…
|
mistachkin
|
1668 |
} |
|
2375d6c…
|
mistachkin
|
1669 |
|
|
2375d6c…
|
mistachkin
|
1670 |
/* |
|
2375d6c…
|
mistachkin
|
1671 |
** COMMAND: test-file-environment |
|
2375d6c…
|
mistachkin
|
1672 |
** |
|
2375d6c…
|
mistachkin
|
1673 |
** Usage: %fossil test-file-environment FILENAME... |
|
2375d6c…
|
mistachkin
|
1674 |
** |
|
2375d6c…
|
mistachkin
|
1675 |
** Display the effective file handling subsystem "settings" and then |
|
2375d6c…
|
mistachkin
|
1676 |
** display file system information about the files specified, if any. |
|
2375d6c…
|
mistachkin
|
1677 |
** |
|
2375d6c…
|
mistachkin
|
1678 |
** Options: |
|
cf1960e…
|
drh
|
1679 |
** --allow-symlinks BOOLEAN Temporarily turn allow-symlinks on/off |
|
2512d2d…
|
km
|
1680 |
** --open-config Open the configuration database first |
|
2512d2d…
|
km
|
1681 |
** --reset Reset cached stat() info for each file |
|
bc36fdc…
|
danield
|
1682 |
** --root ROOT Use ROOT as the root of the check-out |
|
2512d2d…
|
km
|
1683 |
** --slash Trailing slashes, if any, are retained |
|
2375d6c…
|
mistachkin
|
1684 |
*/ |
|
2375d6c…
|
mistachkin
|
1685 |
void cmd_test_file_environment(void){ |
|
2375d6c…
|
mistachkin
|
1686 |
int i; |
|
2375d6c…
|
mistachkin
|
1687 |
int slashFlag = find_option("slash",0,0)!=0; |
|
2375d6c…
|
mistachkin
|
1688 |
int resetFlag = find_option("reset",0,0)!=0; |
|
f741baa…
|
drh
|
1689 |
const char *zRoot = find_option("root",0,1); |
|
cf1960e…
|
drh
|
1690 |
const char *zAllow = find_option("allow-symlinks",0,1); |
|
2375d6c…
|
mistachkin
|
1691 |
if( find_option("open-config", 0, 0)!=0 ){ |
|
2375d6c…
|
mistachkin
|
1692 |
Th_OpenConfig(1); |
|
2375d6c…
|
mistachkin
|
1693 |
} |
|
af6833b…
|
drh
|
1694 |
db_find_and_open_repository(OPEN_ANY_SCHEMA|OPEN_OK_NOT_FOUND, 0); |
|
2375d6c…
|
mistachkin
|
1695 |
fossil_print("filenames_are_case_sensitive() = %d\n", |
|
2375d6c…
|
mistachkin
|
1696 |
filenames_are_case_sensitive()); |
|
cf1960e…
|
drh
|
1697 |
if( zAllow ){ |
|
cf1960e…
|
drh
|
1698 |
g.allowSymlinks = !is_false(zAllow); |
|
cf1960e…
|
drh
|
1699 |
} |
|
3bb9cbe…
|
florian
|
1700 |
if( zRoot==0 ) zRoot = g.zLocalRoot==0 ? "" : g.zLocalRoot; |
|
b727218…
|
drh
|
1701 |
fossil_print("db_allow_symlinks() = %d\n", db_allow_symlinks()); |
|
f741baa…
|
drh
|
1702 |
fossil_print("local-root = [%s]\n", zRoot); |
|
38930fb…
|
florian
|
1703 |
if( g.db==0 ) sqlite3_open(":memory:", &g.db); |
|
106de27…
|
drh
|
1704 |
sqlite3_create_function(g.db, "inode", 1, SQLITE_UTF8, 0, |
|
106de27…
|
drh
|
1705 |
file_inode_sql_func, 0, 0); |
|
2375d6c…
|
mistachkin
|
1706 |
for(i=2; i<g.argc; i++){ |
|
f741baa…
|
drh
|
1707 |
char *z; |
|
8434738…
|
drh
|
1708 |
emitFileStat(g.argv[i], slashFlag, resetFlag); |
|
f741baa…
|
drh
|
1709 |
z = file_canonical_name_dup(g.argv[i]); |
|
f741baa…
|
drh
|
1710 |
fossil_print(" file_canonical_name = %s\n", z); |
|
f741baa…
|
drh
|
1711 |
fossil_print(" file_nondir_path = "); |
|
f741baa…
|
drh
|
1712 |
if( fossil_strnicmp(zRoot,z,(int)strlen(zRoot))!=0 ){ |
|
f741baa…
|
drh
|
1713 |
fossil_print("(--root is not a prefix of this file)\n"); |
|
f741baa…
|
drh
|
1714 |
}else{ |
|
f741baa…
|
drh
|
1715 |
int n = file_nondir_objects_on_path(zRoot, z); |
|
f741baa…
|
drh
|
1716 |
fossil_print("%.*s\n", n, z); |
|
f741baa…
|
drh
|
1717 |
} |
|
106de27…
|
drh
|
1718 |
fossil_free(z); |
|
106de27…
|
drh
|
1719 |
z = db_text(0, "SELECT inode(%Q)", g.argv[i]); |
|
106de27…
|
drh
|
1720 |
fossil_print(" file_inode_sql_func = \"%s\"\n", z); |
|
f741baa…
|
drh
|
1721 |
fossil_free(z); |
|
2375d6c…
|
mistachkin
|
1722 |
} |
|
2375d6c…
|
mistachkin
|
1723 |
} |
|
2375d6c…
|
mistachkin
|
1724 |
|
|
2375d6c…
|
mistachkin
|
1725 |
/* |
|
26eef7f…
|
rberteig
|
1726 |
** COMMAND: test-canonical-name |
|
c3b5f1d…
|
jan.nijtmans
|
1727 |
** |
|
7fbaee7…
|
drh
|
1728 |
** Usage: %fossil test-canonical-name FILENAME... |
|
dbda8d6…
|
drh
|
1729 |
** |
|
dbda8d6…
|
drh
|
1730 |
** Test the operation of the canonical name generator. |
|
7fbaee7…
|
drh
|
1731 |
** Also test Fossil's ability to measure attributes of a file. |
|
dbda8d6…
|
drh
|
1732 |
*/ |
|
dbda8d6…
|
drh
|
1733 |
void cmd_test_canonical_name(void){ |
|
dbda8d6…
|
drh
|
1734 |
int i; |
|
b727218…
|
drh
|
1735 |
Blob x; |
|
e6ced76…
|
jan.nijtmans
|
1736 |
int slashFlag = find_option("slash",0,0)!=0; |
|
b727218…
|
drh
|
1737 |
blob_zero(&x); |
|
dbda8d6…
|
drh
|
1738 |
for(i=2; i<g.argc; i++){ |
|
b727218…
|
drh
|
1739 |
char zBuf[100]; |
|
b727218…
|
drh
|
1740 |
const char *zName = g.argv[i]; |
|
b727218…
|
drh
|
1741 |
file_canonical_name(zName, &x, slashFlag); |
|
b727218…
|
drh
|
1742 |
fossil_print("[%s] -> [%s]\n", zName, blob_buffer(&x)); |
|
b727218…
|
drh
|
1743 |
blob_reset(&x); |
|
1772357…
|
drh
|
1744 |
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", file_size(zName,RepoFILE)); |
|
1772357…
|
drh
|
1745 |
fossil_print(" file_size = %s\n", zBuf); |
|
1772357…
|
drh
|
1746 |
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", file_mtime(zName,RepoFILE)); |
|
1772357…
|
drh
|
1747 |
fossil_print(" file_mtime = %s\n", zBuf); |
|
1772357…
|
drh
|
1748 |
fossil_print(" file_isfile = %d\n", file_isfile(zName,RepoFILE)); |
|
1772357…
|
drh
|
1749 |
fossil_print(" file_isfile_or_link = %d\n", file_isfile_or_link(zName)); |
|
1772357…
|
drh
|
1750 |
fossil_print(" file_islink = %d\n", file_islink(zName)); |
|
1772357…
|
drh
|
1751 |
fossil_print(" file_isexe = %d\n", file_isexe(zName,RepoFILE)); |
|
1772357…
|
drh
|
1752 |
fossil_print(" file_isdir = %d\n", file_isdir(zName,RepoFILE)); |
|
d569515…
|
drh
|
1753 |
} |
|
d569515…
|
drh
|
1754 |
} |
|
d569515…
|
drh
|
1755 |
|
|
d569515…
|
drh
|
1756 |
/* |
|
d569515…
|
drh
|
1757 |
** Return TRUE if the given filename is canonical. |
|
d569515…
|
drh
|
1758 |
** |
|
d569515…
|
drh
|
1759 |
** Canonical names are full pathnames using "/" not "\" and which |
|
d569515…
|
drh
|
1760 |
** contain no "/./" or "/../" terms. |
|
d569515…
|
drh
|
1761 |
*/ |
|
d569515…
|
drh
|
1762 |
int file_is_canonical(const char *z){ |
|
d569515…
|
drh
|
1763 |
int i; |
|
f6ff25e…
|
drh
|
1764 |
if( |
|
caf2eb2…
|
jan.nijtmans
|
1765 |
#if defined(_WIN32) || defined(__CYGWIN__) |
|
f6ff25e…
|
drh
|
1766 |
!fossil_isupper(z[0]) || z[1]!=':' || !fossil_isdirsep(z[2]) |
|
f6ff25e…
|
drh
|
1767 |
#else |
|
f6ff25e…
|
drh
|
1768 |
z[0]!='/' |
|
d569515…
|
drh
|
1769 |
#endif |
|
d569515…
|
drh
|
1770 |
) return 0; |
|
d569515…
|
drh
|
1771 |
|
|
d569515…
|
drh
|
1772 |
for(i=0; z[i]; i++){ |
|
d569515…
|
drh
|
1773 |
if( z[i]=='\\' ) return 0; |
|
d569515…
|
drh
|
1774 |
if( z[i]=='/' ){ |
|
d569515…
|
drh
|
1775 |
if( z[i+1]=='.' ){ |
|
d569515…
|
drh
|
1776 |
if( z[i+2]=='/' || z[i+2]==0 ) return 0; |
|
d569515…
|
drh
|
1777 |
if( z[i+2]=='.' && (z[i+3]=='/' || z[i+3]==0) ) return 0; |
|
d569515…
|
drh
|
1778 |
} |
|
d569515…
|
drh
|
1779 |
} |
|
dbda8d6…
|
drh
|
1780 |
} |
|
d569515…
|
drh
|
1781 |
return 1; |
|
d569515…
|
drh
|
1782 |
} |
|
d569515…
|
drh
|
1783 |
|
|
f668ff4…
|
drh
|
1784 |
/* |
|
9a0c995…
|
drh
|
1785 |
** Return a pointer to the first character in a pathname past the |
|
9a0c995…
|
drh
|
1786 |
** drive letter. This routine is a no-op on unix. |
|
9a0c995…
|
drh
|
1787 |
*/ |
|
9a0c995…
|
drh
|
1788 |
char *file_without_drive_letter(char *zIn){ |
|
9a0c995…
|
drh
|
1789 |
#ifdef _WIN32 |
|
9a0c995…
|
drh
|
1790 |
if( fossil_isalpha(zIn[0]) && zIn[1]==':' ) zIn += 2; |
|
9a0c995…
|
drh
|
1791 |
#endif |
|
9a0c995…
|
drh
|
1792 |
return zIn; |
|
9a0c995…
|
drh
|
1793 |
} |
|
9a0c995…
|
drh
|
1794 |
|
|
9a0c995…
|
drh
|
1795 |
/* |
|
dbda8d6…
|
drh
|
1796 |
** Compute a pathname for a file or directory that is relative |
|
135ed93…
|
drh
|
1797 |
** to the current directory. If the slash parameter is non-zero, |
|
135ed93…
|
drh
|
1798 |
** the trailing slash, if any, is retained. |
|
dbda8d6…
|
drh
|
1799 |
*/ |
|
135ed93…
|
drh
|
1800 |
void file_relative_name(const char *zOrigName, Blob *pOut, int slash){ |
|
dbda8d6…
|
drh
|
1801 |
char *zPath; |
|
dbda8d6…
|
drh
|
1802 |
blob_set(pOut, zOrigName); |
|
135ed93…
|
drh
|
1803 |
blob_resize(pOut, file_simplify_name(blob_buffer(pOut), |
|
135ed93…
|
drh
|
1804 |
blob_size(pOut), slash)); |
|
9a0c995…
|
drh
|
1805 |
zPath = file_without_drive_letter(blob_buffer(pOut)); |
|
dbda8d6…
|
drh
|
1806 |
if( zPath[0]=='/' ){ |
|
dbda8d6…
|
drh
|
1807 |
int i, j; |
|
dbda8d6…
|
drh
|
1808 |
Blob tmp; |
|
9a0c995…
|
drh
|
1809 |
char *zPwd; |
|
9a0c995…
|
drh
|
1810 |
char zBuf[2000]; |
|
9a0c995…
|
drh
|
1811 |
zPwd = zBuf; |
|
9a0c995…
|
drh
|
1812 |
file_getcwd(zBuf, sizeof(zBuf)-20); |
|
9a0c995…
|
drh
|
1813 |
zPwd = file_without_drive_letter(zBuf); |
|
9a0c995…
|
drh
|
1814 |
i = 1; |
|
d4b3e1d…
|
jan.nijtmans
|
1815 |
#if defined(_WIN32) || defined(__CYGWIN__) |
|
9a0c995…
|
drh
|
1816 |
while( zPath[i] && fossil_tolower(zPwd[i])==fossil_tolower(zPath[i]) ) i++; |
|
9a0c995…
|
drh
|
1817 |
#else |
|
9a0c995…
|
drh
|
1818 |
while( zPath[i] && zPwd[i]==zPath[i] ) i++; |
|
9a0c995…
|
drh
|
1819 |
#endif |
|
dbda8d6…
|
drh
|
1820 |
if( zPath[i]==0 ){ |
|
add7524…
|
jan.nijtmans
|
1821 |
memcpy(&tmp, pOut, sizeof(tmp)); |
|
dbda8d6…
|
drh
|
1822 |
if( zPwd[i]==0 ){ |
|
add7524…
|
jan.nijtmans
|
1823 |
blob_set(pOut, "."); |
|
dbda8d6…
|
drh
|
1824 |
}else{ |
|
add7524…
|
jan.nijtmans
|
1825 |
blob_set(pOut, ".."); |
|
dbda8d6…
|
drh
|
1826 |
for(j=i+1; zPwd[j]; j++){ |
|
4d456c9…
|
drh
|
1827 |
if( zPwd[j]=='/' ){ |
|
dbda8d6…
|
drh
|
1828 |
blob_append(pOut, "/..", 3); |
|
dbda8d6…
|
drh
|
1829 |
} |
|
dbda8d6…
|
drh
|
1830 |
} |
|
565ba73…
|
jan.nijtmans
|
1831 |
while( i>0 && (zPwd[i]!='/')) --i; |
|
565ba73…
|
jan.nijtmans
|
1832 |
blob_append(pOut, zPath+i, j-i); |
|
add7524…
|
jan.nijtmans
|
1833 |
} |
|
add7524…
|
jan.nijtmans
|
1834 |
if( slash && i>0 && zPath[strlen(zPath)-1]=='/'){ |
|
add7524…
|
jan.nijtmans
|
1835 |
blob_append(pOut, "/", 1); |
|
dbda8d6…
|
drh
|
1836 |
} |
|
add7524…
|
jan.nijtmans
|
1837 |
blob_reset(&tmp); |
|
dbda8d6…
|
drh
|
1838 |
return; |
|
dbda8d6…
|
drh
|
1839 |
} |
|
dbda8d6…
|
drh
|
1840 |
if( zPwd[i]==0 && zPath[i]=='/' ){ |
|
dbda8d6…
|
drh
|
1841 |
memcpy(&tmp, pOut, sizeof(tmp)); |
|
dbda8d6…
|
drh
|
1842 |
blob_set(pOut, "./"); |
|
dbda8d6…
|
drh
|
1843 |
blob_append(pOut, &zPath[i+1], -1); |
|
dbda8d6…
|
drh
|
1844 |
blob_reset(&tmp); |
|
dbda8d6…
|
drh
|
1845 |
return; |
|
dbda8d6…
|
drh
|
1846 |
} |
|
dbda8d6…
|
drh
|
1847 |
while( zPath[i-1]!='/' ){ i--; } |
|
af194db…
|
mgagnon
|
1848 |
if( zPwd[0]=='/' && strlen(zPwd)==1 ){ |
|
25950f8…
|
jan.nijtmans
|
1849 |
/* If on '/', don't go to higher level */ |
|
af194db…
|
mgagnon
|
1850 |
blob_zero(&tmp); |
|
af194db…
|
mgagnon
|
1851 |
}else{ |
|
af194db…
|
mgagnon
|
1852 |
blob_set(&tmp, "../"); |
|
af194db…
|
mgagnon
|
1853 |
} |
|
dbda8d6…
|
drh
|
1854 |
for(j=i; zPwd[j]; j++){ |
|
4d456c9…
|
drh
|
1855 |
if( zPwd[j]=='/' ){ |
|
dbda8d6…
|
drh
|
1856 |
blob_append(&tmp, "../", 3); |
|
dbda8d6…
|
drh
|
1857 |
} |
|
dbda8d6…
|
drh
|
1858 |
} |
|
dbda8d6…
|
drh
|
1859 |
blob_append(&tmp, &zPath[i], -1); |
|
dbda8d6…
|
drh
|
1860 |
blob_reset(pOut); |
|
dbda8d6…
|
drh
|
1861 |
memcpy(pOut, &tmp, sizeof(tmp)); |
|
dbda8d6…
|
drh
|
1862 |
} |
|
dbda8d6…
|
drh
|
1863 |
} |
|
dbda8d6…
|
drh
|
1864 |
|
|
dbda8d6…
|
drh
|
1865 |
/* |
|
26eef7f…
|
rberteig
|
1866 |
** COMMAND: test-relative-name |
|
dbda8d6…
|
drh
|
1867 |
** |
|
dbda8d6…
|
drh
|
1868 |
** Test the operation of the relative name generator. |
|
dbda8d6…
|
drh
|
1869 |
*/ |
|
dbda8d6…
|
drh
|
1870 |
void cmd_test_relative_name(void){ |
|
dbda8d6…
|
drh
|
1871 |
int i; |
|
dbda8d6…
|
drh
|
1872 |
Blob x; |
|
6e1e4bb…
|
jan.nijtmans
|
1873 |
int slashFlag = find_option("slash",0,0)!=0; |
|
d8ec765…
|
drh
|
1874 |
blob_zero(&x); |
|
d8ec765…
|
drh
|
1875 |
for(i=2; i<g.argc; i++){ |
|
e6ced76…
|
jan.nijtmans
|
1876 |
file_relative_name(g.argv[i], &x, slashFlag); |
|
d8ec765…
|
drh
|
1877 |
fossil_print("%s\n", blob_buffer(&x)); |
|
d8ec765…
|
drh
|
1878 |
blob_reset(&x); |
|
d8ec765…
|
drh
|
1879 |
} |
|
d8ec765…
|
drh
|
1880 |
} |
|
d8ec765…
|
drh
|
1881 |
|
|
d8ec765…
|
drh
|
1882 |
/* |
|
c56a387…
|
mistachkin
|
1883 |
** Compute a full path name for a file in the local tree. If |
|
c56a387…
|
mistachkin
|
1884 |
** the absolute flag is non-zero, the computed path will be |
|
c56a387…
|
mistachkin
|
1885 |
** absolute, starting with the root path of the local tree; |
|
c56a387…
|
mistachkin
|
1886 |
** otherwise, it will be relative to the root of the local |
|
c56a387…
|
mistachkin
|
1887 |
** tree. In both cases, the root of the local tree is defined |
|
c56a387…
|
mistachkin
|
1888 |
** by the g.zLocalRoot variable. Return TRUE on success. On |
|
c56a387…
|
mistachkin
|
1889 |
** failure, print and error message and quit if the errFatal |
|
c56a387…
|
mistachkin
|
1890 |
** flag is true. If errFatal is false, then simply return 0. |
|
d8ec765…
|
drh
|
1891 |
*/ |
|
c56a387…
|
mistachkin
|
1892 |
int file_tree_name( |
|
c56a387…
|
mistachkin
|
1893 |
const char *zOrigName, |
|
c56a387…
|
mistachkin
|
1894 |
Blob *pOut, |
|
c56a387…
|
mistachkin
|
1895 |
int absolute, |
|
c56a387…
|
mistachkin
|
1896 |
int errFatal |
|
c56a387…
|
mistachkin
|
1897 |
){ |
|
135ed93…
|
drh
|
1898 |
Blob localRoot; |
|
135ed93…
|
drh
|
1899 |
int nLocalRoot; |
|
135ed93…
|
drh
|
1900 |
char *zLocalRoot; |
|
d8ec765…
|
drh
|
1901 |
Blob full; |
|
d8ec765…
|
drh
|
1902 |
int nFull; |
|
d8ec765…
|
drh
|
1903 |
char *zFull; |
|
0df0ce8…
|
jan.nijtmans
|
1904 |
int (*xCmp)(const char*,const char*,int); |
|
d8ec765…
|
drh
|
1905 |
|
|
d8ec765…
|
drh
|
1906 |
blob_zero(pOut); |
|
ebe97b7…
|
drh
|
1907 |
if( !g.localOpen ){ |
|
c56a387…
|
mistachkin
|
1908 |
if( absolute && !file_is_absolute_path(zOrigName) ){ |
|
c56a387…
|
mistachkin
|
1909 |
if( errFatal ){ |
|
bc36fdc…
|
danield
|
1910 |
fossil_fatal("relative to absolute needs open check-out tree: %s", |
|
c56a387…
|
mistachkin
|
1911 |
zOrigName); |
|
c56a387…
|
mistachkin
|
1912 |
} |
|
c56a387…
|
mistachkin
|
1913 |
return 0; |
|
c56a387…
|
mistachkin
|
1914 |
}else{ |
|
c56a387…
|
mistachkin
|
1915 |
/* |
|
c56a387…
|
mistachkin
|
1916 |
** The original path may be relative or absolute; however, without |
|
bc36fdc…
|
danield
|
1917 |
** an open check-out tree, the only things we can do at this point |
|
c56a387…
|
mistachkin
|
1918 |
** is return it verbatim or generate a fatal error. The caller is |
|
c56a387…
|
mistachkin
|
1919 |
** probably expecting a tree-relative path name will be returned; |
|
c56a387…
|
mistachkin
|
1920 |
** however, most places where this function is called already check |
|
bc36fdc…
|
danield
|
1921 |
** if the local check-out tree is open, either directly or indirectly, |
|
c56a387…
|
mistachkin
|
1922 |
** which would make this situation impossible. Alternatively, they |
|
c56a387…
|
mistachkin
|
1923 |
** could check the returned path using the file_is_absolute_path() |
|
c56a387…
|
mistachkin
|
1924 |
** function. |
|
c56a387…
|
mistachkin
|
1925 |
*/ |
|
c56a387…
|
mistachkin
|
1926 |
blob_appendf(pOut, "%s", zOrigName); |
|
c56a387…
|
mistachkin
|
1927 |
return 1; |
|
c56a387…
|
mistachkin
|
1928 |
} |
|
ebe97b7…
|
drh
|
1929 |
} |
|
135ed93…
|
drh
|
1930 |
file_canonical_name(g.zLocalRoot, &localRoot, 1); |
|
135ed93…
|
drh
|
1931 |
nLocalRoot = blob_size(&localRoot); |
|
135ed93…
|
drh
|
1932 |
zLocalRoot = blob_buffer(&localRoot); |
|
135ed93…
|
drh
|
1933 |
assert( nLocalRoot>0 && zLocalRoot[nLocalRoot-1]=='/' ); |
|
135ed93…
|
drh
|
1934 |
file_canonical_name(zOrigName, &full, 0); |
|
d8ec765…
|
drh
|
1935 |
nFull = blob_size(&full); |
|
d8ec765…
|
drh
|
1936 |
zFull = blob_buffer(&full); |
|
0df0ce8…
|
jan.nijtmans
|
1937 |
if( filenames_are_case_sensitive() ){ |
|
0df0ce8…
|
jan.nijtmans
|
1938 |
xCmp = fossil_strncmp; |
|
0df0ce8…
|
jan.nijtmans
|
1939 |
}else{ |
|
0df0ce8…
|
jan.nijtmans
|
1940 |
xCmp = fossil_strnicmp; |
|
0df0ce8…
|
jan.nijtmans
|
1941 |
} |
|
d8ec765…
|
drh
|
1942 |
|
|
d8ec765…
|
drh
|
1943 |
/* Special case. zOrigName refers to g.zLocalRoot directory. */ |
|
45f3516…
|
jan.nijtmans
|
1944 |
if( (nFull==nLocalRoot-1 && xCmp(zLocalRoot, zFull, nFull)==0) |
|
af194db…
|
mgagnon
|
1945 |
|| (nFull==1 && zFull[0]=='/' && nLocalRoot==1 && zLocalRoot[0]=='/') ){ |
|
c56a387…
|
mistachkin
|
1946 |
if( absolute ){ |
|
c56a387…
|
mistachkin
|
1947 |
blob_append(pOut, zLocalRoot, nLocalRoot); |
|
c56a387…
|
mistachkin
|
1948 |
}else{ |
|
c56a387…
|
mistachkin
|
1949 |
blob_append(pOut, ".", 1); |
|
c56a387…
|
mistachkin
|
1950 |
} |
|
135ed93…
|
drh
|
1951 |
blob_reset(&localRoot); |
|
135ed93…
|
drh
|
1952 |
blob_reset(&full); |
|
d8ec765…
|
drh
|
1953 |
return 1; |
|
d8ec765…
|
drh
|
1954 |
} |
|
d8ec765…
|
drh
|
1955 |
|
|
0df0ce8…
|
jan.nijtmans
|
1956 |
if( nFull<=nLocalRoot || xCmp(zLocalRoot, zFull, nLocalRoot) ){ |
|
135ed93…
|
drh
|
1957 |
blob_reset(&localRoot); |
|
d8ec765…
|
drh
|
1958 |
blob_reset(&full); |
|
d8ec765…
|
drh
|
1959 |
if( errFatal ){ |
|
bc36fdc…
|
danield
|
1960 |
fossil_fatal("file outside of check-out tree: %s", zOrigName); |
|
d8ec765…
|
drh
|
1961 |
} |
|
d8ec765…
|
drh
|
1962 |
return 0; |
|
d8ec765…
|
drh
|
1963 |
} |
|
c56a387…
|
mistachkin
|
1964 |
if( absolute ){ |
|
c56a387…
|
mistachkin
|
1965 |
if( !file_is_absolute_path(zOrigName) ){ |
|
c56a387…
|
mistachkin
|
1966 |
blob_append(pOut, zLocalRoot, nLocalRoot); |
|
c56a387…
|
mistachkin
|
1967 |
} |
|
c56a387…
|
mistachkin
|
1968 |
blob_append(pOut, zOrigName, -1); |
|
c56a387…
|
mistachkin
|
1969 |
blob_resize(pOut, file_simplify_name(blob_buffer(pOut), |
|
c56a387…
|
mistachkin
|
1970 |
blob_size(pOut), 0)); |
|
c56a387…
|
mistachkin
|
1971 |
}else{ |
|
c56a387…
|
mistachkin
|
1972 |
blob_append(pOut, &zFull[nLocalRoot], nFull-nLocalRoot); |
|
c56a387…
|
mistachkin
|
1973 |
} |
|
135ed93…
|
drh
|
1974 |
blob_reset(&localRoot); |
|
135ed93…
|
drh
|
1975 |
blob_reset(&full); |
|
d8ec765…
|
drh
|
1976 |
return 1; |
|
d8ec765…
|
drh
|
1977 |
} |
|
d8ec765…
|
drh
|
1978 |
|
|
d8ec765…
|
drh
|
1979 |
/* |
|
26eef7f…
|
rberteig
|
1980 |
** COMMAND: test-tree-name |
|
d8ec765…
|
drh
|
1981 |
** |
|
d8ec765…
|
drh
|
1982 |
** Test the operation of the tree name generator. |
|
0df0ce8…
|
jan.nijtmans
|
1983 |
** |
|
0df0ce8…
|
jan.nijtmans
|
1984 |
** Options: |
|
2512d2d…
|
km
|
1985 |
** --absolute Return an absolute path instead of a relative one |
|
0df0ce8…
|
jan.nijtmans
|
1986 |
** --case-sensitive B Enable or disable case-sensitive filenames. B is |
|
0df0ce8…
|
jan.nijtmans
|
1987 |
** a boolean: "yes", "no", "true", "false", etc. |
|
d8ec765…
|
drh
|
1988 |
*/ |
|
d8ec765…
|
drh
|
1989 |
void cmd_test_tree_name(void){ |
|
d8ec765…
|
drh
|
1990 |
int i; |
|
d8ec765…
|
drh
|
1991 |
Blob x; |
|
c56a387…
|
mistachkin
|
1992 |
int absoluteFlag = find_option("absolute",0,0)!=0; |
|
ebe97b7…
|
drh
|
1993 |
db_find_and_open_repository(0,0); |
|
d8ec765…
|
drh
|
1994 |
blob_zero(&x); |
|
d8ec765…
|
drh
|
1995 |
for(i=2; i<g.argc; i++){ |
|
c56a387…
|
mistachkin
|
1996 |
if( file_tree_name(g.argv[i], &x, absoluteFlag, 1) ){ |
|
d8ec765…
|
drh
|
1997 |
fossil_print("%s\n", blob_buffer(&x)); |
|
d8ec765…
|
drh
|
1998 |
blob_reset(&x); |
|
d8ec765…
|
drh
|
1999 |
} |
|
d8ec765…
|
drh
|
2000 |
} |
|
61132ce…
|
drh
|
2001 |
} |
|
61132ce…
|
drh
|
2002 |
|
|
61132ce…
|
drh
|
2003 |
/* |
|
61132ce…
|
drh
|
2004 |
** zFile is the name of a file. Return true if that file is in the |
|
61132ce…
|
drh
|
2005 |
** current working directory (the "pwd" or file_getcwd() directory). |
|
61132ce…
|
drh
|
2006 |
** Return false if the file is someplace else. |
|
61132ce…
|
drh
|
2007 |
*/ |
|
61132ce…
|
drh
|
2008 |
int file_in_cwd(const char *zFile){ |
|
61132ce…
|
drh
|
2009 |
char *zFull = file_canonical_name_dup(zFile); |
|
61132ce…
|
drh
|
2010 |
char *zCwd = file_getcwd(0,0); |
|
61132ce…
|
drh
|
2011 |
size_t nCwd = strlen(zCwd); |
|
61132ce…
|
drh
|
2012 |
size_t nFull = strlen(zFull); |
|
61132ce…
|
drh
|
2013 |
int rc = 1; |
|
61132ce…
|
drh
|
2014 |
int (*xCmp)(const char*,const char*,int); |
|
61132ce…
|
drh
|
2015 |
|
|
61132ce…
|
drh
|
2016 |
if( filenames_are_case_sensitive() ){ |
|
61132ce…
|
drh
|
2017 |
xCmp = fossil_strncmp; |
|
61132ce…
|
drh
|
2018 |
}else{ |
|
61132ce…
|
drh
|
2019 |
xCmp = fossil_strnicmp; |
|
61132ce…
|
drh
|
2020 |
} |
|
61132ce…
|
drh
|
2021 |
|
|
61132ce…
|
drh
|
2022 |
if( nFull>nCwd+1 |
|
61132ce…
|
drh
|
2023 |
&& xCmp(zFull,zCwd,nCwd)==0 |
|
61132ce…
|
drh
|
2024 |
&& zFull[nCwd]=='/' |
|
61132ce…
|
drh
|
2025 |
&& strchr(zFull+nCwd+1, '/')==0 |
|
61132ce…
|
drh
|
2026 |
){ |
|
61132ce…
|
drh
|
2027 |
rc = 1; |
|
61132ce…
|
drh
|
2028 |
}else{ |
|
61132ce…
|
drh
|
2029 |
rc = 0; |
|
61132ce…
|
drh
|
2030 |
} |
|
61132ce…
|
drh
|
2031 |
fossil_free(zFull); |
|
61132ce…
|
drh
|
2032 |
fossil_free(zCwd); |
|
61132ce…
|
drh
|
2033 |
return rc; |
|
dbda8d6…
|
drh
|
2034 |
} |
|
dbda8d6…
|
drh
|
2035 |
|
|
dbda8d6…
|
drh
|
2036 |
/* |
|
dbda8d6…
|
drh
|
2037 |
** Parse a URI into scheme, host, port, and path. |
|
dbda8d6…
|
drh
|
2038 |
*/ |
|
dbda8d6…
|
drh
|
2039 |
void file_parse_uri( |
|
dbda8d6…
|
drh
|
2040 |
const char *zUri, |
|
dbda8d6…
|
drh
|
2041 |
Blob *pScheme, |
|
dbda8d6…
|
drh
|
2042 |
Blob *pHost, |
|
dbda8d6…
|
drh
|
2043 |
int *pPort, |
|
dbda8d6…
|
drh
|
2044 |
Blob *pPath |
|
dbda8d6…
|
drh
|
2045 |
){ |
|
dbda8d6…
|
drh
|
2046 |
int i, j; |
|
dbda8d6…
|
drh
|
2047 |
|
|
dbda8d6…
|
drh
|
2048 |
for(i=0; zUri[i] && zUri[i]>='a' && zUri[i]<='z'; i++){} |
|
dbda8d6…
|
drh
|
2049 |
if( zUri[i]!=':' ){ |
|
dbda8d6…
|
drh
|
2050 |
blob_zero(pScheme); |
|
dbda8d6…
|
drh
|
2051 |
blob_zero(pHost); |
|
dbda8d6…
|
drh
|
2052 |
blob_set(pPath, zUri); |
|
dbda8d6…
|
drh
|
2053 |
return; |
|
dbda8d6…
|
drh
|
2054 |
} |
|
dbda8d6…
|
drh
|
2055 |
blob_init(pScheme, zUri, i); |
|
dbda8d6…
|
drh
|
2056 |
i++; |
|
dbda8d6…
|
drh
|
2057 |
if( zUri[i]=='/' && zUri[i+1]=='/' ){ |
|
dbda8d6…
|
drh
|
2058 |
i += 2; |
|
dbda8d6…
|
drh
|
2059 |
j = i; |
|
dbda8d6…
|
drh
|
2060 |
while( zUri[i] && zUri[i]!='/' && zUri[i]!=':' ){ i++; } |
|
dbda8d6…
|
drh
|
2061 |
blob_init(pHost, &zUri[j], i-j); |
|
dbda8d6…
|
drh
|
2062 |
if( zUri[i]==':' ){ |
|
dbda8d6…
|
drh
|
2063 |
i++; |
|
dbda8d6…
|
drh
|
2064 |
*pPort = atoi(&zUri[i]); |
|
dbda8d6…
|
drh
|
2065 |
while( zUri[i] && zUri[i]!='/' ){ i++; } |
|
dbda8d6…
|
drh
|
2066 |
} |
|
dbda8d6…
|
drh
|
2067 |
}else{ |
|
dbda8d6…
|
drh
|
2068 |
blob_zero(pHost); |
|
dbda8d6…
|
drh
|
2069 |
} |
|
dbda8d6…
|
drh
|
2070 |
if( zUri[i]=='/' ){ |
|
dbda8d6…
|
drh
|
2071 |
blob_set(pPath, &zUri[i]); |
|
dbda8d6…
|
drh
|
2072 |
}else{ |
|
dbda8d6…
|
drh
|
2073 |
blob_set(pPath, "/"); |
|
dbda8d6…
|
drh
|
2074 |
} |
|
797d680…
|
drh
|
2075 |
} |
|
797d680…
|
drh
|
2076 |
|
|
797d680…
|
drh
|
2077 |
/* |
|
a072be1…
|
drh
|
2078 |
** Construct a random temporary filename into pBuf where the name of |
|
a072be1…
|
drh
|
2079 |
** the temporary file is derived from zBasis. The suffix on the temp |
|
a072be1…
|
drh
|
2080 |
** file is the same as the suffix on zBasis, and the temp file has |
|
a072be1…
|
drh
|
2081 |
** the root of zBasis in its name. |
|
1dd2527…
|
drh
|
2082 |
** |
|
1dd2527…
|
drh
|
2083 |
** If zTag is not NULL, then try to create the temp-file using zTag |
|
1dd2527…
|
drh
|
2084 |
** as a differentiator. If that fails, or if zTag is NULL, then use |
|
1dd2527…
|
drh
|
2085 |
** a bunch of random characters as the tag. |
|
b3fab9b…
|
drh
|
2086 |
** |
|
b3fab9b…
|
drh
|
2087 |
** Dangerous characters in zBasis are changed. |
|
1aff463…
|
drh
|
2088 |
** |
|
1aff463…
|
drh
|
2089 |
** See also fossil_temp_filename() and file_time_tempname(); |
|
797d680…
|
drh
|
2090 |
*/ |
|
1dd2527…
|
drh
|
2091 |
void file_tempname(Blob *pBuf, const char *zBasis, const char *zTag){ |
|
135ed93…
|
drh
|
2092 |
#if defined(_WIN32) |
|
52fb2a7…
|
jan.nijtmans
|
2093 |
const char *azDirs[] = { |
|
135ed93…
|
drh
|
2094 |
0, /* GetTempPath */ |
|
135ed93…
|
drh
|
2095 |
0, /* TEMP */ |
|
135ed93…
|
drh
|
2096 |
0, /* TMP */ |
|
52fb2a7…
|
jan.nijtmans
|
2097 |
".", |
|
52fb2a7…
|
jan.nijtmans
|
2098 |
}; |
|
135ed93…
|
drh
|
2099 |
#else |
|
1fff403…
|
andybradford
|
2100 |
static const char *azDirs[] = { |
|
1fff403…
|
andybradford
|
2101 |
0, /* TMPDIR */ |
|
797d680…
|
drh
|
2102 |
"/var/tmp", |
|
797d680…
|
drh
|
2103 |
"/usr/tmp", |
|
797d680…
|
drh
|
2104 |
"/tmp", |
|
797d680…
|
drh
|
2105 |
"/temp", |
|
797d680…
|
drh
|
2106 |
".", |
|
797d680…
|
drh
|
2107 |
}; |
|
52fb2a7…
|
jan.nijtmans
|
2108 |
#endif |
|
797d680…
|
drh
|
2109 |
static const unsigned char zChars[] = |
|
797d680…
|
drh
|
2110 |
"abcdefghijklmnopqrstuvwxyz" |
|
797d680…
|
drh
|
2111 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
|
797d680…
|
drh
|
2112 |
"0123456789"; |
|
051d0ab…
|
drh
|
2113 |
unsigned int i; |
|
797d680…
|
drh
|
2114 |
const char *zDir = "."; |
|
d3a1663…
|
drh
|
2115 |
int cnt = 0; |
|
051d0ab…
|
drh
|
2116 |
char zRand[16]; |
|
a072be1…
|
drh
|
2117 |
int nBasis; |
|
a072be1…
|
drh
|
2118 |
const char *zSuffix; |
|
b3fab9b…
|
drh
|
2119 |
char *z; |
|
135ed93…
|
drh
|
2120 |
|
|
135ed93…
|
drh
|
2121 |
#if defined(_WIN32) |
|
14733d1…
|
jan.nijtmans
|
2122 |
wchar_t zTmpPath[MAX_PATH]; |
|
135ed93…
|
drh
|
2123 |
|
|
14733d1…
|
jan.nijtmans
|
2124 |
if( GetTempPathW(MAX_PATH, zTmpPath) ){ |
|
9571b68…
|
drh
|
2125 |
azDirs[0] = fossil_path_to_utf8(zTmpPath); |
|
b5f0d70…
|
drh
|
2126 |
/* Removing trailing \ from the temp path */ |
|
b5f0d70…
|
drh
|
2127 |
z = (char*)azDirs[0]; |
|
b5f0d70…
|
drh
|
2128 |
i = (int)strlen(z)-1; |
|
b5f0d70…
|
drh
|
2129 |
if( i>0 && z[i]=='\\' ) z[i] = 0; |
|
135ed93…
|
drh
|
2130 |
} |
|
135ed93…
|
drh
|
2131 |
|
|
135ed93…
|
drh
|
2132 |
azDirs[1] = fossil_getenv("TEMP"); |
|
135ed93…
|
drh
|
2133 |
azDirs[2] = fossil_getenv("TMP"); |
|
1fff403…
|
andybradford
|
2134 |
#else |
|
1fff403…
|
andybradford
|
2135 |
azDirs[0] = fossil_getenv("TMPDIR"); |
|
135ed93…
|
drh
|
2136 |
#endif |
|
135ed93…
|
drh
|
2137 |
|
|
3cb9ba4…
|
andygoth
|
2138 |
for(i=0; i<count(azDirs); i++){ |
|
135ed93…
|
drh
|
2139 |
if( azDirs[i]==0 ) continue; |
|
1772357…
|
drh
|
2140 |
if( !file_isdir(azDirs[i], ExtFILE) ) continue; |
|
797d680…
|
drh
|
2141 |
zDir = azDirs[i]; |
|
797d680…
|
drh
|
2142 |
break; |
|
797d680…
|
drh
|
2143 |
} |
|
797d680…
|
drh
|
2144 |
|
|
a072be1…
|
drh
|
2145 |
assert( zBasis!=0 ); |
|
a072be1…
|
drh
|
2146 |
zSuffix = 0; |
|
a072be1…
|
drh
|
2147 |
for(i=0; zBasis[i]; i++){ |
|
a072be1…
|
drh
|
2148 |
if( zBasis[i]=='/' || zBasis[i]=='\\' ){ |
|
a072be1…
|
drh
|
2149 |
zBasis += i+1; |
|
a072be1…
|
drh
|
2150 |
i = -1; |
|
a072be1…
|
drh
|
2151 |
}else if( zBasis[i]=='.' ){ |
|
a072be1…
|
drh
|
2152 |
zSuffix = zBasis + i; |
|
a072be1…
|
drh
|
2153 |
} |
|
a072be1…
|
drh
|
2154 |
} |
|
a072be1…
|
drh
|
2155 |
if( zSuffix==0 || zSuffix<=zBasis ){ |
|
a072be1…
|
drh
|
2156 |
zSuffix = ""; |
|
a072be1…
|
drh
|
2157 |
nBasis = i; |
|
a072be1…
|
drh
|
2158 |
}else{ |
|
a072be1…
|
drh
|
2159 |
nBasis = (int)(zSuffix - zBasis); |
|
a072be1…
|
drh
|
2160 |
} |
|
a072be1…
|
drh
|
2161 |
if( nBasis==0 ){ |
|
a072be1…
|
drh
|
2162 |
nBasis = 6; |
|
a072be1…
|
drh
|
2163 |
zBasis = "fossil"; |
|
a072be1…
|
drh
|
2164 |
} |
|
797d680…
|
drh
|
2165 |
do{ |
|
051d0ab…
|
drh
|
2166 |
blob_zero(pBuf); |
|
31c7bdb…
|
larrybr
|
2167 |
if( cnt++>20 ) fossil_fatal("cannot generate a temporary filename"); |
|
1dd2527…
|
drh
|
2168 |
if( zTag==0 ){ |
|
1aff463…
|
drh
|
2169 |
const int nRand = sizeof(zRand)-1; |
|
1aff463…
|
drh
|
2170 |
sqlite3_randomness(nRand, zRand); |
|
1aff463…
|
drh
|
2171 |
for(i=0; i<nRand; i++){ |
|
1dd2527…
|
drh
|
2172 |
zRand[i] = (char)zChars[ ((unsigned char)zRand[i])%(sizeof(zChars)-1) ]; |
|
1dd2527…
|
drh
|
2173 |
} |
|
1aff463…
|
drh
|
2174 |
zRand[nRand] = 0; |
|
1dd2527…
|
drh
|
2175 |
zTag = zRand; |
|
1772357…
|
drh
|
2176 |
} |
|
1dd2527…
|
drh
|
2177 |
blob_appendf(pBuf, "%s/%.*s~%s%s", zDir, nBasis, zBasis, zTag, zSuffix); |
|
1dd2527…
|
drh
|
2178 |
zTag = 0; |
|
b3fab9b…
|
drh
|
2179 |
for(z=blob_str(pBuf); z!=0 && (z=strpbrk(z,"'\"`;|$&"))!=0; z++){ |
|
b3fab9b…
|
drh
|
2180 |
z[0] = '_'; |
|
b3fab9b…
|
drh
|
2181 |
} |
|
1772357…
|
drh
|
2182 |
}while( file_size(blob_str(pBuf), ExtFILE)>=0 ); |
|
135ed93…
|
drh
|
2183 |
|
|
135ed93…
|
drh
|
2184 |
#if defined(_WIN32) |
|
9571b68…
|
drh
|
2185 |
fossil_path_free((char *)azDirs[0]); |
|
9571b68…
|
drh
|
2186 |
fossil_path_free((char *)azDirs[1]); |
|
9571b68…
|
drh
|
2187 |
fossil_path_free((char *)azDirs[2]); |
|
e474c17…
|
drh
|
2188 |
/* Change all \ characters in the windows path into / so that they can |
|
e474c17…
|
drh
|
2189 |
** be safely passed to a subcommand, such as by gdiff */ |
|
e474c17…
|
drh
|
2190 |
z = blob_buffer(pBuf); |
|
e474c17…
|
drh
|
2191 |
for(i=0; z[i]; i++) if( z[i]=='\\' ) z[i] = '/'; |
|
1fff403…
|
andybradford
|
2192 |
#else |
|
1fff403…
|
andybradford
|
2193 |
fossil_path_free((char *)azDirs[0]); |
|
135ed93…
|
drh
|
2194 |
#endif |
|
e474c17…
|
drh
|
2195 |
} |
|
e474c17…
|
drh
|
2196 |
|
|
38165f3…
|
drh
|
2197 |
/* |
|
38165f3…
|
drh
|
2198 |
** Compute a temporary filename in zDir. The filename is based on |
|
38165f3…
|
drh
|
2199 |
** the current time. |
|
1aff463…
|
drh
|
2200 |
** |
|
1aff463…
|
drh
|
2201 |
** See also fossil_temp_filename() and file_tempname(); |
|
38165f3…
|
drh
|
2202 |
*/ |
|
38165f3…
|
drh
|
2203 |
char *file_time_tempname(const char *zDir, const char *zSuffix){ |
|
38165f3…
|
drh
|
2204 |
struct tm *tm; |
|
38165f3…
|
drh
|
2205 |
unsigned int r; |
|
38165f3…
|
drh
|
2206 |
static unsigned int cnt = 0; |
|
38165f3…
|
drh
|
2207 |
time_t t; |
|
38165f3…
|
drh
|
2208 |
t = time(0); |
|
38165f3…
|
drh
|
2209 |
tm = gmtime(&t); |
|
38165f3…
|
drh
|
2210 |
sqlite3_randomness(sizeof(r), &r); |
|
38165f3…
|
drh
|
2211 |
return mprintf("%s/%04d%02d%02d%02d%02d%02d%04d%06d%s", |
|
38165f3…
|
drh
|
2212 |
zDir, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, |
|
38165f3…
|
drh
|
2213 |
tm->tm_hour, tm->tm_min, tm->tm_sec, cnt++, r%1000000, zSuffix); |
|
38165f3…
|
drh
|
2214 |
} |
|
38165f3…
|
drh
|
2215 |
|
|
f1d23f0…
|
drh
|
2216 |
|
|
f1d23f0…
|
drh
|
2217 |
/* |
|
f1d23f0…
|
drh
|
2218 |
** COMMAND: test-tempname |
|
1dd2527…
|
drh
|
2219 |
** Usage: fossil test-name [--time SUFFIX] [--tag NAME] BASENAME ... |
|
38165f3…
|
drh
|
2220 |
** |
|
38165f3…
|
drh
|
2221 |
** Generate temporary filenames derived from BASENAME. Use the --time |
|
1dd2527…
|
drh
|
2222 |
** option to generate temp names based on the time of day. If --tag NAME |
|
1dd2527…
|
drh
|
2223 |
** is specified, try to use NAME as the differentiator in the temp file. |
|
1aff463…
|
drh
|
2224 |
** |
|
1aff463…
|
drh
|
2225 |
** If --time is used, file_time_tempname() generates the filename. |
|
1aff463…
|
drh
|
2226 |
** If BASENAME is present, file_tempname() generates the filename. |
|
1aff463…
|
drh
|
2227 |
** Without --time or BASENAME, fossil_temp_filename() generates the filename. |
|
f1d23f0…
|
drh
|
2228 |
*/ |
|
f1d23f0…
|
drh
|
2229 |
void file_test_tempname(void){ |
|
f1d23f0…
|
drh
|
2230 |
int i; |
|
2332efb…
|
drh
|
2231 |
const char *zSuffix = find_option("time",0,1); |
|
f1d23f0…
|
drh
|
2232 |
Blob x = BLOB_INITIALIZER; |
|
38165f3…
|
drh
|
2233 |
char *z; |
|
1dd2527…
|
drh
|
2234 |
const char *zTag = find_option("tag",0,1); |
|
38165f3…
|
drh
|
2235 |
verify_all_options(); |
|
1aff463…
|
drh
|
2236 |
if( g.argc<=2 ){ |
|
1aff463…
|
drh
|
2237 |
z = fossil_temp_filename(); |
|
1aff463…
|
drh
|
2238 |
fossil_print("%s\n", z); |
|
1aff463…
|
drh
|
2239 |
sqlite3_free(z); |
|
1aff463…
|
drh
|
2240 |
} |
|
f1d23f0…
|
drh
|
2241 |
for(i=2; i<g.argc; i++){ |
|
38165f3…
|
drh
|
2242 |
if( zSuffix ){ |
|
38165f3…
|
drh
|
2243 |
z = file_time_tempname(g.argv[i], zSuffix); |
|
38165f3…
|
drh
|
2244 |
fossil_print("%s\n", z); |
|
38165f3…
|
drh
|
2245 |
fossil_free(z); |
|
38165f3…
|
drh
|
2246 |
}else{ |
|
1dd2527…
|
drh
|
2247 |
file_tempname(&x, g.argv[i], zTag); |
|
38165f3…
|
drh
|
2248 |
fossil_print("%s\n", blob_str(&x)); |
|
38165f3…
|
drh
|
2249 |
blob_reset(&x); |
|
38165f3…
|
drh
|
2250 |
} |
|
f1d23f0…
|
drh
|
2251 |
} |
|
f1d23f0…
|
drh
|
2252 |
} |
|
f1d23f0…
|
drh
|
2253 |
|
|
aa9ea79…
|
drh
|
2254 |
|
|
aa9ea79…
|
drh
|
2255 |
/* |
|
aa9ea79…
|
drh
|
2256 |
** Return true if a file named zName exists and has identical content |
|
aa9ea79…
|
drh
|
2257 |
** to the blob pContent. If zName does not exist or if the content is |
|
aa9ea79…
|
drh
|
2258 |
** different in any way, then return false. |
|
1772357…
|
drh
|
2259 |
** |
|
1772357…
|
drh
|
2260 |
** This routine assumes RepoFILE |
|
aa9ea79…
|
drh
|
2261 |
*/ |
|
aa9ea79…
|
drh
|
2262 |
int file_is_the_same(Blob *pContent, const char *zName){ |
|
aa9ea79…
|
drh
|
2263 |
i64 iSize; |
|
aa9ea79…
|
drh
|
2264 |
int rc; |
|
aa9ea79…
|
drh
|
2265 |
Blob onDisk; |
|
aa9ea79…
|
drh
|
2266 |
|
|
1772357…
|
drh
|
2267 |
iSize = file_size(zName, RepoFILE); |
|
aa9ea79…
|
drh
|
2268 |
if( iSize<0 ) return 0; |
|
aa9ea79…
|
drh
|
2269 |
if( iSize!=blob_size(pContent) ) return 0; |
|
1772357…
|
drh
|
2270 |
blob_read_from_file(&onDisk, zName, RepoFILE); |
|
aa9ea79…
|
drh
|
2271 |
rc = blob_compare(&onDisk, pContent); |
|
aa9ea79…
|
drh
|
2272 |
blob_reset(&onDisk); |
|
aa9ea79…
|
drh
|
2273 |
return rc==0; |
|
d8ec765…
|
drh
|
2274 |
} |
|
d8ec765…
|
drh
|
2275 |
|
|
5715208…
|
drh
|
2276 |
/* |
|
5715208…
|
drh
|
2277 |
** Return the value of an environment variable as UTF8. |
|
9571b68…
|
drh
|
2278 |
** Use fossil_path_free() to release resources. |
|
5715208…
|
drh
|
2279 |
*/ |
|
5715208…
|
drh
|
2280 |
char *fossil_getenv(const char *zName){ |
|
5715208…
|
drh
|
2281 |
#ifdef _WIN32 |
|
14733d1…
|
jan.nijtmans
|
2282 |
wchar_t *uName = fossil_utf8_to_unicode(zName); |
|
14733d1…
|
jan.nijtmans
|
2283 |
void *zValue = _wgetenv(uName); |
|
ca72844…
|
drh
|
2284 |
fossil_unicode_free(uName); |
|
14733d1…
|
jan.nijtmans
|
2285 |
#else |
|
14733d1…
|
jan.nijtmans
|
2286 |
char *zValue = getenv(zName); |
|
5715208…
|
drh
|
2287 |
#endif |
|
9571b68…
|
drh
|
2288 |
if( zValue ) zValue = fossil_path_to_utf8(zValue); |
|
5715208…
|
drh
|
2289 |
return zValue; |
|
5715208…
|
drh
|
2290 |
} |
|
5715208…
|
drh
|
2291 |
|
|
5715208…
|
drh
|
2292 |
/* |
|
0af7024…
|
mistachkin
|
2293 |
** Sets the value of an environment variable as UTF8. |
|
0af7024…
|
mistachkin
|
2294 |
*/ |
|
0af7024…
|
mistachkin
|
2295 |
int fossil_setenv(const char *zName, const char *zValue){ |
|
0af7024…
|
mistachkin
|
2296 |
int rc; |
|
0af7024…
|
mistachkin
|
2297 |
char *zString = mprintf("%s=%s", zName, zValue); |
|
0af7024…
|
mistachkin
|
2298 |
#ifdef _WIN32 |
|
0af7024…
|
mistachkin
|
2299 |
wchar_t *uString = fossil_utf8_to_unicode(zString); |
|
0af7024…
|
mistachkin
|
2300 |
rc = _wputenv(uString); |
|
0af7024…
|
mistachkin
|
2301 |
fossil_unicode_free(uString); |
|
0af7024…
|
mistachkin
|
2302 |
fossil_free(zString); |
|
0af7024…
|
mistachkin
|
2303 |
#else |
|
0af7024…
|
mistachkin
|
2304 |
rc = putenv(zString); |
|
0af7024…
|
mistachkin
|
2305 |
/* NOTE: Cannot free the string on POSIX. */ |
|
0af7024…
|
mistachkin
|
2306 |
/* fossil_free(zString); */ |
|
0af7024…
|
mistachkin
|
2307 |
#endif |
|
0af7024…
|
mistachkin
|
2308 |
return rc; |
|
0af7024…
|
mistachkin
|
2309 |
} |
|
0af7024…
|
mistachkin
|
2310 |
|
|
0af7024…
|
mistachkin
|
2311 |
/* |
|
ed63bdd…
|
drh
|
2312 |
** Clear all environment variables |
|
ed63bdd…
|
drh
|
2313 |
*/ |
|
ed63bdd…
|
drh
|
2314 |
int fossil_clearenv(void){ |
|
ed63bdd…
|
drh
|
2315 |
#ifdef _WIN32 |
|
61fd10e…
|
mistachkin
|
2316 |
int rc = 0; |
|
61fd10e…
|
mistachkin
|
2317 |
LPWCH zzEnv = GetEnvironmentStringsW(); |
|
61fd10e…
|
mistachkin
|
2318 |
if( zzEnv ){ |
|
61fd10e…
|
mistachkin
|
2319 |
LPCWSTR zEnv = zzEnv; /* read-only */ |
|
61fd10e…
|
mistachkin
|
2320 |
while( 1 ){ |
|
61fd10e…
|
mistachkin
|
2321 |
LPWSTR zNewEnv = _wcsdup(zEnv); /* writable */ |
|
61fd10e…
|
mistachkin
|
2322 |
if( zNewEnv ){ |
|
61fd10e…
|
mistachkin
|
2323 |
LPWSTR zEquals = wcsstr(zNewEnv, L"="); |
|
61fd10e…
|
mistachkin
|
2324 |
if( zEquals ){ |
|
61fd10e…
|
mistachkin
|
2325 |
zEquals[1] = 0; /* no value */ |
|
61fd10e…
|
mistachkin
|
2326 |
if( zNewEnv==zEquals || _wputenv(zNewEnv)==0 ){ /* via CRT */ |
|
61fd10e…
|
mistachkin
|
2327 |
/* do nothing */ |
|
61fd10e…
|
mistachkin
|
2328 |
}else{ |
|
61fd10e…
|
mistachkin
|
2329 |
zEquals[0] = 0; /* name only */ |
|
61fd10e…
|
mistachkin
|
2330 |
if( !SetEnvironmentVariableW(zNewEnv, NULL) ){ /* via Win32 */ |
|
61fd10e…
|
mistachkin
|
2331 |
rc = 1; |
|
61fd10e…
|
mistachkin
|
2332 |
} |
|
61fd10e…
|
mistachkin
|
2333 |
} |
|
61fd10e…
|
mistachkin
|
2334 |
if( rc==0 ){ |
|
61fd10e…
|
mistachkin
|
2335 |
zEnv += (lstrlenW(zEnv) + 1); /* double NUL term? */ |
|
61fd10e…
|
mistachkin
|
2336 |
if( zEnv[0]==0 ){ |
|
61fd10e…
|
mistachkin
|
2337 |
free(zNewEnv); |
|
61fd10e…
|
mistachkin
|
2338 |
break; /* no more vars */ |
|
61fd10e…
|
mistachkin
|
2339 |
} |
|
61fd10e…
|
mistachkin
|
2340 |
} |
|
61fd10e…
|
mistachkin
|
2341 |
}else{ |
|
61fd10e…
|
mistachkin
|
2342 |
rc = 1; |
|
61fd10e…
|
mistachkin
|
2343 |
} |
|
61fd10e…
|
mistachkin
|
2344 |
}else{ |
|
61fd10e…
|
mistachkin
|
2345 |
rc = 1; |
|
61fd10e…
|
mistachkin
|
2346 |
} |
|
61fd10e…
|
mistachkin
|
2347 |
free(zNewEnv); |
|
61fd10e…
|
mistachkin
|
2348 |
if( rc!=0 ) break; |
|
61fd10e…
|
mistachkin
|
2349 |
} |
|
61fd10e…
|
mistachkin
|
2350 |
if( !FreeEnvironmentStringsW(zzEnv) ){ |
|
61fd10e…
|
mistachkin
|
2351 |
rc = 2; |
|
61fd10e…
|
mistachkin
|
2352 |
} |
|
61fd10e…
|
mistachkin
|
2353 |
}else{ |
|
61fd10e…
|
mistachkin
|
2354 |
rc = 1; |
|
61fd10e…
|
mistachkin
|
2355 |
} |
|
61fd10e…
|
mistachkin
|
2356 |
return rc; |
|
ed63bdd…
|
drh
|
2357 |
#else |
|
d72aa24…
|
drh
|
2358 |
extern char **environ; |
|
6e7211a…
|
ashepilko
|
2359 |
environ[0] = 0; |
|
d72aa24…
|
drh
|
2360 |
return 0; |
|
ed63bdd…
|
drh
|
2361 |
#endif |
|
ed63bdd…
|
drh
|
2362 |
} |
|
ed63bdd…
|
drh
|
2363 |
|
|
ed63bdd…
|
drh
|
2364 |
/* |
|
d8ec765…
|
drh
|
2365 |
** Like fopen() but always takes a UTF8 argument. |
|
1772357…
|
drh
|
2366 |
** |
|
1772357…
|
drh
|
2367 |
** This function assumes ExtFILE. In other words, symbolic links |
|
1772357…
|
drh
|
2368 |
** are always followed. |
|
d8ec765…
|
drh
|
2369 |
*/ |
|
d8ec765…
|
drh
|
2370 |
FILE *fossil_fopen(const char *zName, const char *zMode){ |
|
14733d1…
|
jan.nijtmans
|
2371 |
#ifdef _WIN32 |
|
14733d1…
|
jan.nijtmans
|
2372 |
wchar_t *uMode = fossil_utf8_to_unicode(zMode); |
|
9571b68…
|
drh
|
2373 |
wchar_t *uName = fossil_utf8_to_path(zName, 0); |
|
14733d1…
|
jan.nijtmans
|
2374 |
FILE *f = _wfopen(uName, uMode); |
|
9571b68…
|
drh
|
2375 |
fossil_path_free(uName); |
|
ca72844…
|
drh
|
2376 |
fossil_unicode_free(uMode); |
|
14733d1…
|
jan.nijtmans
|
2377 |
#else |
|
14733d1…
|
jan.nijtmans
|
2378 |
FILE *f = fopen(zName, zMode); |
|
6e7211a…
|
ashepilko
|
2379 |
#endif |
|
6e7211a…
|
ashepilko
|
2380 |
return f; |
|
6e7211a…
|
ashepilko
|
2381 |
} |
|
6e7211a…
|
ashepilko
|
2382 |
|
|
6e7211a…
|
ashepilko
|
2383 |
/* |
|
f48e48f…
|
drh
|
2384 |
** Wrapper for freopen() that understands UTF8 arguments. |
|
f48e48f…
|
drh
|
2385 |
*/ |
|
f48e48f…
|
drh
|
2386 |
FILE *fossil_freopen(const char *zName, const char *zMode, FILE *stream){ |
|
f48e48f…
|
drh
|
2387 |
#ifdef _WIN32 |
|
f48e48f…
|
drh
|
2388 |
wchar_t *uMode = fossil_utf8_to_unicode(zMode); |
|
f48e48f…
|
drh
|
2389 |
wchar_t *uName = fossil_utf8_to_path(zName, 0); |
|
f48e48f…
|
drh
|
2390 |
FILE *f = _wfreopen(uName, uMode, stream); |
|
f48e48f…
|
drh
|
2391 |
fossil_path_free(uName); |
|
f48e48f…
|
drh
|
2392 |
fossil_unicode_free(uMode); |
|
f48e48f…
|
drh
|
2393 |
#else |
|
f48e48f…
|
drh
|
2394 |
FILE *f = freopen(zName, zMode, stream); |
|
f48e48f…
|
drh
|
2395 |
#endif |
|
f48e48f…
|
drh
|
2396 |
return f; |
|
f48e48f…
|
drh
|
2397 |
} |
|
f48e48f…
|
drh
|
2398 |
|
|
f48e48f…
|
drh
|
2399 |
/* |
|
7723a92…
|
stephan
|
2400 |
** Works like fclose() except that: |
|
7723a92…
|
stephan
|
2401 |
** |
|
7723a92…
|
stephan
|
2402 |
** 1) is a no-op if f is 0 or if it is stdin. |
|
7723a92…
|
stephan
|
2403 |
** |
|
7723a92…
|
stephan
|
2404 |
** 2) If f is one of (stdout, stderr), it is flushed but not closed. |
|
fe86954…
|
stephan
|
2405 |
*/ |
|
fe86954…
|
stephan
|
2406 |
void fossil_fclose(FILE *f){ |
|
fe86954…
|
stephan
|
2407 |
if(f!=0){ |
|
fe86954…
|
stephan
|
2408 |
if(stdout==f || stderr==f){ |
|
fe86954…
|
stephan
|
2409 |
fflush(f); |
|
7723a92…
|
stephan
|
2410 |
}else if(stdin!=f){ |
|
fe86954…
|
stephan
|
2411 |
fclose(f); |
|
fe86954…
|
stephan
|
2412 |
} |
|
fe86954…
|
stephan
|
2413 |
} |
|
fe86954…
|
stephan
|
2414 |
} |
|
fe86954…
|
stephan
|
2415 |
|
|
fe86954…
|
stephan
|
2416 |
/* |
|
fe86954…
|
stephan
|
2417 |
** Works like fopen(zName,"wb") except that: |
|
fe86954…
|
stephan
|
2418 |
** |
|
fe86954…
|
stephan
|
2419 |
** 1) If zName is "-", the stdout handle is returned. |
|
fe86954…
|
stephan
|
2420 |
** |
|
fe86954…
|
stephan
|
2421 |
** 2) Else file_mkfolder() is used to create all directories |
|
fe86954…
|
stephan
|
2422 |
** which lead up to the file before opening it. |
|
fe86954…
|
stephan
|
2423 |
** |
|
fe86954…
|
stephan
|
2424 |
** 3) It fails fatally if the file cannot be opened. |
|
fe86954…
|
stephan
|
2425 |
*/ |
|
fe86954…
|
stephan
|
2426 |
FILE *fossil_fopen_for_output(const char *zFilename){ |
|
fe86954…
|
stephan
|
2427 |
if(zFilename[0]=='-' && zFilename[1]==0){ |
|
fe86954…
|
stephan
|
2428 |
return stdout; |
|
fe86954…
|
stephan
|
2429 |
}else{ |
|
fe86954…
|
stephan
|
2430 |
FILE * p; |
|
fe86954…
|
stephan
|
2431 |
file_mkfolder(zFilename, ExtFILE, 1, 0); |
|
fe86954…
|
stephan
|
2432 |
p = fossil_fopen(zFilename, "wb"); |
|
fe86954…
|
stephan
|
2433 |
if( p==0 ){ |
|
556479b…
|
danield
|
2434 |
#if defined(_WIN32) |
|
fe86954…
|
stephan
|
2435 |
const char *zReserved = file_is_win_reserved(zFilename); |
|
fe86954…
|
stephan
|
2436 |
if( zReserved ){ |
|
fe86954…
|
stephan
|
2437 |
fossil_fatal("cannot open \"%s\" because \"%s\" is " |
|
fe86954…
|
stephan
|
2438 |
"a reserved name on Windows", zFilename, |
|
fe86954…
|
stephan
|
2439 |
zReserved); |
|
fe86954…
|
stephan
|
2440 |
} |
|
fe86954…
|
stephan
|
2441 |
#endif |
|
fe86954…
|
stephan
|
2442 |
fossil_fatal("unable to open file \"%s\" for writing", |
|
fe86954…
|
stephan
|
2443 |
zFilename); |
|
fe86954…
|
stephan
|
2444 |
} |
|
fe86954…
|
stephan
|
2445 |
return p; |
|
fe86954…
|
stephan
|
2446 |
} |
|
fe86954…
|
stephan
|
2447 |
} |
|
fe86954…
|
stephan
|
2448 |
|
|
fe86954…
|
stephan
|
2449 |
|
|
fe86954…
|
stephan
|
2450 |
/* |
|
12c6650…
|
drh
|
2451 |
** Return non-NULL if zFilename contains pathname elements that |
|
12c6650…
|
drh
|
2452 |
** are reserved on Windows. The returned string is the disallowed |
|
12c6650…
|
drh
|
2453 |
** path element. |
|
12c6650…
|
drh
|
2454 |
*/ |
|
12c6650…
|
drh
|
2455 |
const char *file_is_win_reserved(const char *zPath){ |
|
275da70…
|
danield
|
2456 |
static const char *const azRes[] = { "CON","PRN","AUX","NUL","COM","LPT" }; |
|
12c6650…
|
drh
|
2457 |
static char zReturn[5]; |
|
12c6650…
|
drh
|
2458 |
int i; |
|
12c6650…
|
drh
|
2459 |
while( zPath[0] ){ |
|
3cb9ba4…
|
andygoth
|
2460 |
for(i=0; i<count(azRes); i++){ |
|
12c6650…
|
drh
|
2461 |
if( sqlite3_strnicmp(zPath, azRes[i], 3)==0 |
|
12c6650…
|
drh
|
2462 |
&& ((i>=4 && fossil_isdigit(zPath[3]) |
|
12c6650…
|
drh
|
2463 |
&& (zPath[4]=='/' || zPath[4]=='.' || zPath[4]==0)) |
|
12c6650…
|
drh
|
2464 |
|| (i<4 && (zPath[3]=='/' || zPath[3]=='.' || zPath[3]==0))) |
|
12c6650…
|
drh
|
2465 |
){ |
|
12c6650…
|
drh
|
2466 |
sqlite3_snprintf(5,zReturn,"%.*s", i>=4 ? 4 : 3, zPath); |
|
12c6650…
|
drh
|
2467 |
return zReturn; |
|
12c6650…
|
drh
|
2468 |
} |
|
12c6650…
|
drh
|
2469 |
} |
|
12c6650…
|
drh
|
2470 |
while( zPath[0] && zPath[0]!='/' ) zPath++; |
|
12c6650…
|
drh
|
2471 |
while( zPath[0]=='/' ) zPath++; |
|
12c6650…
|
drh
|
2472 |
} |
|
12c6650…
|
drh
|
2473 |
return 0; |
|
12c6650…
|
drh
|
2474 |
} |
|
12c6650…
|
drh
|
2475 |
|
|
12c6650…
|
drh
|
2476 |
/* |
|
12c6650…
|
drh
|
2477 |
** COMMAND: test-valid-for-windows |
|
12c6650…
|
drh
|
2478 |
** Usage: fossil test-valid-for-windows FILENAME .... |
|
12c6650…
|
drh
|
2479 |
** |
|
12c6650…
|
drh
|
2480 |
** Show which filenames are not valid for Windows |
|
12c6650…
|
drh
|
2481 |
*/ |
|
12c6650…
|
drh
|
2482 |
void file_test_valid_for_windows(void){ |
|
12c6650…
|
drh
|
2483 |
int i; |
|
12c6650…
|
drh
|
2484 |
for(i=2; i<g.argc; i++){ |
|
12c6650…
|
drh
|
2485 |
fossil_print("%s %s\n", file_is_win_reserved(g.argv[i]), g.argv[i]); |
|
12c6650…
|
drh
|
2486 |
} |
|
239b4c1…
|
drh
|
2487 |
} |
|
239b4c1…
|
drh
|
2488 |
|
|
239b4c1…
|
drh
|
2489 |
/* |
|
f41cf03…
|
mistachkin
|
2490 |
** Returns non-zero if the specified file extension belongs to a Fossil |
|
f41cf03…
|
mistachkin
|
2491 |
** repository file. |
|
f41cf03…
|
mistachkin
|
2492 |
*/ |
|
f41cf03…
|
mistachkin
|
2493 |
int file_is_repository_extension(const char *zPath){ |
|
f41cf03…
|
mistachkin
|
2494 |
if( fossil_strcmp(zPath, ".fossil")==0 ) return 1; |
|
f41cf03…
|
mistachkin
|
2495 |
#if USE_SEE |
|
f41cf03…
|
mistachkin
|
2496 |
if( fossil_strcmp(zPath, ".efossil")==0 ) return 1; |
|
f41cf03…
|
mistachkin
|
2497 |
#endif |
|
f41cf03…
|
mistachkin
|
2498 |
return 0; |
|
f41cf03…
|
mistachkin
|
2499 |
} |
|
f41cf03…
|
mistachkin
|
2500 |
|
|
f41cf03…
|
mistachkin
|
2501 |
/* |
|
f41cf03…
|
mistachkin
|
2502 |
** Returns non-zero if the specified path appears to match a file extension |
|
f41cf03…
|
mistachkin
|
2503 |
** that should belong to a Fossil repository file. |
|
f41cf03…
|
mistachkin
|
2504 |
*/ |
|
f41cf03…
|
mistachkin
|
2505 |
int file_contains_repository_extension(const char *zPath){ |
|
f41cf03…
|
mistachkin
|
2506 |
if( sqlite3_strglob("*.fossil*",zPath)==0 ) return 1; |
|
f41cf03…
|
mistachkin
|
2507 |
#if USE_SEE |
|
f41cf03…
|
mistachkin
|
2508 |
if( sqlite3_strglob("*.efossil*",zPath)==0 ) return 1; |
|
f41cf03…
|
mistachkin
|
2509 |
#endif |
|
f41cf03…
|
mistachkin
|
2510 |
return 0; |
|
f41cf03…
|
mistachkin
|
2511 |
} |
|
f41cf03…
|
mistachkin
|
2512 |
|
|
f41cf03…
|
mistachkin
|
2513 |
/* |
|
f41cf03…
|
mistachkin
|
2514 |
** Returns non-zero if the specified path ends with a file extension that |
|
f41cf03…
|
mistachkin
|
2515 |
** should belong to a Fossil repository file. |
|
f41cf03…
|
mistachkin
|
2516 |
*/ |
|
f41cf03…
|
mistachkin
|
2517 |
int file_ends_with_repository_extension(const char *zPath, int bQual){ |
|
f41cf03…
|
mistachkin
|
2518 |
if( bQual ){ |
|
f41cf03…
|
mistachkin
|
2519 |
if( sqlite3_strglob("*/*.fossil", zPath)==0 ) return 1; |
|
f41cf03…
|
mistachkin
|
2520 |
#if USE_SEE |
|
f41cf03…
|
mistachkin
|
2521 |
if( sqlite3_strglob("*/*.efossil", zPath)==0 ) return 1; |
|
f41cf03…
|
mistachkin
|
2522 |
#endif |
|
f41cf03…
|
mistachkin
|
2523 |
}else{ |
|
f41cf03…
|
mistachkin
|
2524 |
if( sqlite3_strglob("*.fossil", zPath)==0 ) return 1; |
|
f41cf03…
|
mistachkin
|
2525 |
#if USE_SEE |
|
f41cf03…
|
mistachkin
|
2526 |
if( sqlite3_strglob("*.efossil", zPath)==0 ) return 1; |
|
f41cf03…
|
mistachkin
|
2527 |
#endif |
|
f41cf03…
|
mistachkin
|
2528 |
} |
|
f41cf03…
|
mistachkin
|
2529 |
return 0; |
|
f41cf03…
|
mistachkin
|
2530 |
} |
|
f41cf03…
|
mistachkin
|
2531 |
|
|
f41cf03…
|
mistachkin
|
2532 |
/* |
|
239b4c1…
|
drh
|
2533 |
** Remove surplus "/" characters from the beginning of a full pathname. |
|
239b4c1…
|
drh
|
2534 |
** Extra leading "/" characters are benign on unix. But on Windows |
|
239b4c1…
|
drh
|
2535 |
** machines, they must be removed. Example: Convert "/C:/fossil/xyx.fossil" |
|
eb9b231…
|
jan.nijtmans
|
2536 |
** into "C:/fossil/xyz.fossil". Cygwin should behave as Windows here. |
|
239b4c1…
|
drh
|
2537 |
*/ |
|
239b4c1…
|
drh
|
2538 |
const char *file_cleanup_fullpath(const char *z){ |
|
eb9b231…
|
jan.nijtmans
|
2539 |
#if defined(_WIN32) || defined(__CYGWIN__) |
|
239b4c1…
|
drh
|
2540 |
if( z[0]=='/' && fossil_isalpha(z[1]) && z[2]==':' && z[3]=='/' ) z++; |
|
239b4c1…
|
drh
|
2541 |
#else |
|
239b4c1…
|
drh
|
2542 |
while( z[0]=='/' && z[1]=='/' ) z++; |
|
239b4c1…
|
drh
|
2543 |
#endif |
|
239b4c1…
|
drh
|
2544 |
return z; |
|
94e846d…
|
drh
|
2545 |
} |
|
94e846d…
|
drh
|
2546 |
|
|
94e846d…
|
drh
|
2547 |
/* |
|
ff7fe39…
|
drh
|
2548 |
** Find the name of all objects (files and subdirectories) in a given |
|
ff7fe39…
|
drh
|
2549 |
** directory that match a GLOB pattern. If zGlob is NULL, then return |
|
ff7fe39…
|
drh
|
2550 |
** all objects. The list is written into *pazList and the number of |
|
ff7fe39…
|
drh
|
2551 |
** entries is returned. If pazList is NULL, then only the count is |
|
ff7fe39…
|
drh
|
2552 |
** returned. |
|
ff7fe39…
|
drh
|
2553 |
** |
|
ff7fe39…
|
drh
|
2554 |
** If zDir is not a directory, *pazList is unchanged and -1 is returned. |
|
ff7fe39…
|
drh
|
2555 |
** |
|
ff7fe39…
|
drh
|
2556 |
** Memory used to old *pazList should be freed using a subsequent call |
|
ff7fe39…
|
drh
|
2557 |
** to file_directory_list_free(). |
|
3cb7d39…
|
stephan
|
2558 |
** |
|
3cb7d39…
|
stephan
|
2559 |
** This routine never counts the two "." and ".." special directory |
|
3cb7d39…
|
stephan
|
2560 |
** entries, even if the provided glob would match them. |
|
94e846d…
|
drh
|
2561 |
*/ |
|
ff7fe39…
|
drh
|
2562 |
int file_directory_list( |
|
ff7fe39…
|
drh
|
2563 |
const char *zDir, /* Directory to get a listing of */ |
|
ff7fe39…
|
drh
|
2564 |
const char *zGlob, /* Only list objects matching this pattern */ |
|
f39852d…
|
drh
|
2565 |
int omitDotFiles, /* 0: skip "." and "..", 1: no .-files, 2: keep all */ |
|
ff7fe39…
|
drh
|
2566 |
int nLimit, /* Find at most this many files. 0 means "all" */ |
|
ff7fe39…
|
drh
|
2567 |
char ***pazList /* OUT: Write the list here, if not NULL */ |
|
ff7fe39…
|
drh
|
2568 |
){ |
|
6a7d2ad…
|
drh
|
2569 |
void *zNative; |
|
94e846d…
|
drh
|
2570 |
DIR *d; |
|
94e846d…
|
drh
|
2571 |
int n = -1; |
|
ff7fe39…
|
drh
|
2572 |
int nAlloc = 0; |
|
ff7fe39…
|
drh
|
2573 |
if( pazList ) *pazList = 0; |
|
94e846d…
|
drh
|
2574 |
zNative = fossil_utf8_to_path(zDir,1); |
|
94e846d…
|
drh
|
2575 |
d = opendir(zNative); |
|
94e846d…
|
drh
|
2576 |
if( d ){ |
|
94e846d…
|
drh
|
2577 |
struct dirent *pEntry; |
|
94e846d…
|
drh
|
2578 |
n = 0; |
|
94e846d…
|
drh
|
2579 |
while( (pEntry=readdir(d))!=0 ){ |
|
ff7fe39…
|
drh
|
2580 |
char *zUtf8 = 0; |
|
94e846d…
|
drh
|
2581 |
if( pEntry->d_name[0]==0 ) continue; |
|
f39852d…
|
drh
|
2582 |
if( pEntry->d_name[0]=='.' |
|
f39852d…
|
drh
|
2583 |
&& omitDotFiles<2 |
|
f39852d…
|
drh
|
2584 |
&& (omitDotFiles==1 |
|
f39852d…
|
drh
|
2585 |
/* Skip the special "." and ".." entries unless omitDotFiles>=2 */ |
|
3cb7d39…
|
stephan
|
2586 |
|| pEntry->d_name[1]==0 |
|
f39852d…
|
drh
|
2587 |
|| (pEntry->d_name[1]=='.' && pEntry->d_name[2]==0) |
|
f39852d…
|
drh
|
2588 |
) |
|
f39852d…
|
drh
|
2589 |
){ |
|
3cb7d39…
|
stephan
|
2590 |
continue; |
|
3cb7d39…
|
stephan
|
2591 |
} |
|
6a7d2ad…
|
drh
|
2592 |
if( zGlob ){ |
|
ff7fe39…
|
drh
|
2593 |
int rc; |
|
ff7fe39…
|
drh
|
2594 |
zUtf8 = fossil_path_to_utf8(pEntry->d_name); |
|
ff7fe39…
|
drh
|
2595 |
rc = sqlite3_strglob(zGlob, zUtf8); |
|
ff7fe39…
|
drh
|
2596 |
if( rc ){ |
|
ff7fe39…
|
drh
|
2597 |
fossil_path_free(zUtf8); |
|
ff7fe39…
|
drh
|
2598 |
continue; |
|
ff7fe39…
|
drh
|
2599 |
} |
|
ff7fe39…
|
drh
|
2600 |
} |
|
ff7fe39…
|
drh
|
2601 |
if( pazList ){ |
|
ff7fe39…
|
drh
|
2602 |
if( n+1 >= nAlloc ){ |
|
ff7fe39…
|
drh
|
2603 |
nAlloc = 100 + n; |
|
ff7fe39…
|
drh
|
2604 |
*pazList = fossil_realloc(*pazList, nAlloc*sizeof(char*)); |
|
ff7fe39…
|
drh
|
2605 |
} |
|
ff7fe39…
|
drh
|
2606 |
if( zUtf8==0 ){ |
|
ff7fe39…
|
drh
|
2607 |
zUtf8 = fossil_path_to_utf8(pEntry->d_name); |
|
ff7fe39…
|
drh
|
2608 |
} |
|
ff7fe39…
|
drh
|
2609 |
(*pazList)[n] = fossil_strdup(zUtf8); |
|
6a7d2ad…
|
drh
|
2610 |
} |
|
94e846d…
|
drh
|
2611 |
n++; |
|
ff7fe39…
|
drh
|
2612 |
if( zUtf8 ) fossil_path_free(zUtf8); |
|
ff7fe39…
|
drh
|
2613 |
if( nLimit>0 && n>=nLimit ) break; |
|
94e846d…
|
drh
|
2614 |
} |
|
94e846d…
|
drh
|
2615 |
closedir(d); |
|
94e846d…
|
drh
|
2616 |
} |
|
94e846d…
|
drh
|
2617 |
fossil_path_free(zNative); |
|
ff7fe39…
|
drh
|
2618 |
if( pazList ) (*pazList)[n] = 0; |
|
94e846d…
|
drh
|
2619 |
return n; |
|
94e846d…
|
drh
|
2620 |
} |
|
ff7fe39…
|
drh
|
2621 |
void file_directory_list_free(char **azList){ |
|
ff7fe39…
|
drh
|
2622 |
char **az; |
|
ff7fe39…
|
drh
|
2623 |
if( azList==0 ) return; |
|
ff7fe39…
|
drh
|
2624 |
az = azList; |
|
ff7fe39…
|
drh
|
2625 |
while( az[0] ){ |
|
ff7fe39…
|
drh
|
2626 |
fossil_free(az[0]); |
|
ff7fe39…
|
drh
|
2627 |
az++; |
|
ff7fe39…
|
drh
|
2628 |
} |
|
ff7fe39…
|
drh
|
2629 |
fossil_free(azList); |
|
ff7fe39…
|
drh
|
2630 |
} |
|
94e846d…
|
drh
|
2631 |
|
|
94e846d…
|
drh
|
2632 |
/* |
|
ff7fe39…
|
drh
|
2633 |
** COMMAND: test-dir-list |
|
ff7fe39…
|
drh
|
2634 |
** |
|
ff7fe39…
|
drh
|
2635 |
** Usage: %fossil test-dir-list NAME [GLOB] [OPTIONS] |
|
ff7fe39…
|
drh
|
2636 |
** |
|
ff7fe39…
|
drh
|
2637 |
** Return the names of up to N objects in the directory NAME. If GLOB is |
|
ff7fe39…
|
drh
|
2638 |
** provided, then only show objects that match the GLOB pattern. |
|
ff7fe39…
|
drh
|
2639 |
** |
|
ff7fe39…
|
drh
|
2640 |
** This command is intended for testing the file_directory_list() function. |
|
94e846d…
|
drh
|
2641 |
** |
|
ff7fe39…
|
drh
|
2642 |
** Options: |
|
94e846d…
|
drh
|
2643 |
** |
|
ff7fe39…
|
drh
|
2644 |
** --count Only count files, do not list them. |
|
ff7fe39…
|
drh
|
2645 |
** --limit N Only show the first N files seen |
|
ff7fe39…
|
drh
|
2646 |
** --nodots Do not show or count files that start with '.' |
|
94e846d…
|
drh
|
2647 |
*/ |
|
ff7fe39…
|
drh
|
2648 |
void test_dir_list_cmd(void){ |
|
94e846d…
|
drh
|
2649 |
int omitDotFiles = find_option("nodots",0,0)!=0; |
|
ff7fe39…
|
drh
|
2650 |
const char *zLimit = find_option("limit",0,1); |
|
ff7fe39…
|
drh
|
2651 |
int countOnly = find_option("count",0,0)!=0; |
|
94e846d…
|
drh
|
2652 |
const char *zGlob; |
|
94e846d…
|
drh
|
2653 |
const char *zDir; |
|
ff7fe39…
|
drh
|
2654 |
char **azList = 0; |
|
ff7fe39…
|
drh
|
2655 |
int nList; |
|
ff7fe39…
|
drh
|
2656 |
|
|
94e846d…
|
drh
|
2657 |
verify_all_options(); |
|
94e846d…
|
drh
|
2658 |
if( g.argc!=3 && g.argc!=4 ){ |
|
94e846d…
|
drh
|
2659 |
usage("NAME [GLOB] [-nodots]"); |
|
94e846d…
|
drh
|
2660 |
} |
|
94e846d…
|
drh
|
2661 |
zDir = g.argv[2]; |
|
94e846d…
|
drh
|
2662 |
zGlob = g.argc==4 ? g.argv[3] : 0; |
|
ff7fe39…
|
drh
|
2663 |
nList = file_directory_list(zDir, zGlob, omitDotFiles, |
|
ff7fe39…
|
drh
|
2664 |
zLimit ? atoi(zLimit) : 0, |
|
ff7fe39…
|
drh
|
2665 |
countOnly ? 0 : &azList); |
|
ff7fe39…
|
drh
|
2666 |
if( countOnly ){ |
|
ff7fe39…
|
drh
|
2667 |
fossil_print("%d\n", nList); |
|
ff7fe39…
|
drh
|
2668 |
}else{ |
|
ff7fe39…
|
drh
|
2669 |
int i; |
|
ff7fe39…
|
drh
|
2670 |
for(i=0; i<nList; i++){ |
|
ff7fe39…
|
drh
|
2671 |
fossil_print(" %s\n", azList[i]); |
|
ff7fe39…
|
drh
|
2672 |
} |
|
ff7fe39…
|
drh
|
2673 |
} |
|
ff7fe39…
|
drh
|
2674 |
file_directory_list_free(azList); |
|
1b49ab3…
|
stephan
|
2675 |
} |
|
1b49ab3…
|
stephan
|
2676 |
|
|
1b49ab3…
|
stephan
|
2677 |
/* |
|
1b49ab3…
|
stephan
|
2678 |
** Internal helper for touch_cmd(). zAbsName must be resolvable as-is |
|
adbbeac…
|
stephan
|
2679 |
** to an existing file - this function does not expand/normalize |
|
adbbeac…
|
stephan
|
2680 |
** it. i.e. it "really should" be an absolute path. zTreeName is |
|
29f450a…
|
stephan
|
2681 |
** strictly cosmetic: it is used when dryRunFlag, verboseFlag, or |
|
29f450a…
|
stephan
|
2682 |
** quietFlag generate output, and is assumed to be a repo-relative or |
|
29f450a…
|
stephan
|
2683 |
** or subdir-relative filename. |
|
1b49ab3…
|
stephan
|
2684 |
** |
|
1b49ab3…
|
stephan
|
2685 |
** newMTime is the file's new timestamp (Unix epoch). |
|
1b49ab3…
|
stephan
|
2686 |
** |
|
1b49ab3…
|
stephan
|
2687 |
** Returns 1 if it sets zAbsName's mtime, 0 if it does not (indicating |
|
56530e9…
|
stephan
|
2688 |
** that the file already has that timestamp or a warning was emitted |
|
56530e9…
|
stephan
|
2689 |
** or was not found). If dryRunFlag is true then it outputs the name |
|
56530e9…
|
stephan
|
2690 |
** of the file it would have timestamped but does not stamp the |
|
56530e9…
|
stephan
|
2691 |
** file. If verboseFlag is true, it outputs a message if the file's |
|
56530e9…
|
stephan
|
2692 |
** timestamp is actually modified. If quietFlag is true then the |
|
56530e9…
|
stephan
|
2693 |
** output of non-fatal warning messages is suppressed. |
|
29f450a…
|
stephan
|
2694 |
** |
|
29f450a…
|
stephan
|
2695 |
** As a special case, if newMTime is 0 then this function emits a |
|
29f450a…
|
stephan
|
2696 |
** warning (unless quietFlag is true), does NOT set the timestamp, and |
|
29f450a…
|
stephan
|
2697 |
** returns 0. The timestamp is known to be zero when |
|
29f450a…
|
stephan
|
2698 |
** mtime_of_manifest_file() is asked to provide the timestamp for a |
|
29f450a…
|
stephan
|
2699 |
** file which is currently undergoing an uncommitted merge (though |
|
29f450a…
|
stephan
|
2700 |
** this may depend on exactly where that merge is happening the |
|
29f450a…
|
stephan
|
2701 |
** history of the project). |
|
1b49ab3…
|
stephan
|
2702 |
*/ |
|
1b49ab3…
|
stephan
|
2703 |
static int touch_cmd_stamp_one_file(char const *zAbsName, |
|
1b49ab3…
|
stephan
|
2704 |
char const *zTreeName, |
|
1b49ab3…
|
stephan
|
2705 |
i64 newMtime, int dryRunFlag, |
|
29f450a…
|
stephan
|
2706 |
int verboseFlag, int quietFlag){ |
|
29f450a…
|
stephan
|
2707 |
i64 currentMtime; |
|
29f450a…
|
stephan
|
2708 |
if(newMtime==0){ |
|
29f450a…
|
stephan
|
2709 |
if( quietFlag==0 ){ |
|
29f450a…
|
stephan
|
2710 |
fossil_print("SKIPPING timestamp of 0: %s\n", zTreeName); |
|
29f450a…
|
stephan
|
2711 |
} |
|
29f450a…
|
stephan
|
2712 |
return 0; |
|
29f450a…
|
stephan
|
2713 |
} |
|
29f450a…
|
stephan
|
2714 |
currentMtime = file_mtime(zAbsName, 0); |
|
1b49ab3…
|
stephan
|
2715 |
if(currentMtime<0){ |
|
56530e9…
|
stephan
|
2716 |
fossil_print("SKIPPING: cannot stat file: %s\n", zAbsName); |
|
56530e9…
|
stephan
|
2717 |
return 0; |
|
1b49ab3…
|
stephan
|
2718 |
}else if(currentMtime==newMtime){ |
|
1b49ab3…
|
stephan
|
2719 |
return 0; |
|
1b49ab3…
|
stephan
|
2720 |
}else if( dryRunFlag!=0 ){ |
|
1b49ab3…
|
stephan
|
2721 |
fossil_print( "dry-run: %s\n", zTreeName ); |
|
1b49ab3…
|
stephan
|
2722 |
}else{ |
|
1b49ab3…
|
stephan
|
2723 |
file_set_mtime(zAbsName, newMtime); |
|
1b49ab3…
|
stephan
|
2724 |
if( verboseFlag!=0 ){ |
|
1b49ab3…
|
stephan
|
2725 |
fossil_print( "touched %s\n", zTreeName ); |
|
1b49ab3…
|
stephan
|
2726 |
} |
|
1b49ab3…
|
stephan
|
2727 |
} |
|
1b49ab3…
|
stephan
|
2728 |
return 1; |
|
a7e86f5…
|
drh
|
2729 |
} |
|
a7e86f5…
|
drh
|
2730 |
|
|
1b49ab3…
|
stephan
|
2731 |
/* |
|
adbbeac…
|
stephan
|
2732 |
** Internal helper for touch_cmd(). If the given file name is found in |
|
bc36fdc…
|
danield
|
2733 |
** the given check-out version, which MUST be the check-out version |
|
1b49ab3…
|
stephan
|
2734 |
** currently populating the vfile table, the vfile.mrid value for the |
|
1b49ab3…
|
stephan
|
2735 |
** file is returned, else 0 is returned. zName must be resolvable |
|
adbbeac…
|
stephan
|
2736 |
** as-is from the vfile table - this function neither expands nor |
|
adbbeac…
|
stephan
|
2737 |
** normalizes it, though it does compare using the repo's |
|
adbbeac…
|
stephan
|
2738 |
** filename_collation() preference. |
|
1b49ab3…
|
stephan
|
2739 |
*/ |
|
1b49ab3…
|
stephan
|
2740 |
static int touch_cmd_vfile_mrid( int vid, char const *zName ){ |
|
1b49ab3…
|
stephan
|
2741 |
int mrid = 0; |
|
1b49ab3…
|
stephan
|
2742 |
static Stmt q = empty_Stmt_m; |
|
adbbeac…
|
stephan
|
2743 |
db_static_prepare(&q, |
|
adbbeac…
|
stephan
|
2744 |
"SELECT vfile.mrid " |
|
1b49ab3…
|
stephan
|
2745 |
"FROM vfile LEFT JOIN blob ON vfile.mrid=blob.rid " |
|
1b49ab3…
|
stephan
|
2746 |
"WHERE vid=:vid AND pathname=:pathname %s", |
|
1b49ab3…
|
stephan
|
2747 |
filename_collation()); |
|
1b49ab3…
|
stephan
|
2748 |
db_bind_int(&q, ":vid", vid); |
|
1b49ab3…
|
stephan
|
2749 |
db_bind_text(&q, ":pathname", zName); |
|
1b49ab3…
|
stephan
|
2750 |
if(SQLITE_ROW==db_step(&q)){ |
|
1b49ab3…
|
stephan
|
2751 |
mrid = db_column_int(&q, 0); |
|
1b49ab3…
|
stephan
|
2752 |
} |
|
1b49ab3…
|
stephan
|
2753 |
db_reset(&q); |
|
1b49ab3…
|
stephan
|
2754 |
return mrid; |
|
1b49ab3…
|
stephan
|
2755 |
} |
|
a7e86f5…
|
drh
|
2756 |
|
|
a7e86f5…
|
drh
|
2757 |
/* |
|
a7e86f5…
|
drh
|
2758 |
** COMMAND: touch* |
|
a7e86f5…
|
drh
|
2759 |
** |
|
1b49ab3…
|
stephan
|
2760 |
** Usage: %fossil touch ?OPTIONS? ?FILENAME...? |
|
1b49ab3…
|
stephan
|
2761 |
** |
|
bc36fdc…
|
danield
|
2762 |
** For each file in the current check-out matching one of the provided |
|
1b49ab3…
|
stephan
|
2763 |
** list of glob patterns and/or file names, the file's mtime is |
|
1b49ab3…
|
stephan
|
2764 |
** updated to a value specified by one of the flags --checkout, |
|
1b49ab3…
|
stephan
|
2765 |
** --checkin, or --now. |
|
a7e86f5…
|
drh
|
2766 |
** |
|
1b49ab3…
|
stephan
|
2767 |
** If neither glob patterns nor filenames are provided, it operates on |
|
1b49ab3…
|
stephan
|
2768 |
** all files managed by the currently checked-out version. |
|
a7e86f5…
|
drh
|
2769 |
** |
|
a7e86f5…
|
drh
|
2770 |
** This command gets its name from the conventional Unix "touch" |
|
a7e86f5…
|
drh
|
2771 |
** command. |
|
a7e86f5…
|
drh
|
2772 |
** |
|
a7e86f5…
|
drh
|
2773 |
** Options: |
|
c3b4856…
|
stephan
|
2774 |
** --now Stamp each affected file with the current time. |
|
c3b4856…
|
stephan
|
2775 |
** This is the default behavior. |
|
c3b4856…
|
stephan
|
2776 |
** -c|--checkin Stamp each affected file with the time of the |
|
2e56ef4…
|
km
|
2777 |
** most recent check-in which modified that file |
|
1b49ab3…
|
stephan
|
2778 |
** -C|--checkout Stamp each affected file with the time of the |
|
2e56ef4…
|
km
|
2779 |
** currently checked-out version |
|
2e56ef4…
|
km
|
2780 |
** -g GLOBLIST Comma-separated list of glob patterns |
|
a7e86f5…
|
drh
|
2781 |
** -G GLOBFILE Similar to -g but reads its globs from a |
|
2e56ef4…
|
km
|
2782 |
** fossil-conventional glob list file |
|
ba7b431…
|
stephan
|
2783 |
** -v|--verbose Outputs extra information about its globs |
|
2e56ef4…
|
km
|
2784 |
** and each file it touches |
|
a7e86f5…
|
drh
|
2785 |
** -n|--dry-run Outputs which files would require touching, |
|
2e56ef4…
|
km
|
2786 |
** but does not touch them |
|
29f450a…
|
stephan
|
2787 |
** -q|--quiet Suppress warnings, e.g. when skipping unmanaged |
|
2e56ef4…
|
km
|
2788 |
** or out-of-tree files |
|
1b49ab3…
|
stephan
|
2789 |
** |
|
1b49ab3…
|
stephan
|
2790 |
** Only one of --now, --checkin, and --checkout may be used. The |
|
1b49ab3…
|
stephan
|
2791 |
** default is --now. |
|
c3b4856…
|
stephan
|
2792 |
** |
|
1b49ab3…
|
stephan
|
2793 |
** Only one of -g or -G may be used. If neither is provided and no |
|
1b49ab3…
|
stephan
|
2794 |
** additional filenames are provided, the effect is as if a glob of |
|
ad275f9…
|
stephan
|
2795 |
** '*' were provided, i.e. all files belonging to the |
|
bc36fdc…
|
danield
|
2796 |
** currently checked-out version. Note that all glob patterns provided |
|
ad275f9…
|
stephan
|
2797 |
** via these flags are always evaluated as if they are relative to the |
|
ad275f9…
|
stephan
|
2798 |
** top of the source tree, not the current working (sub)directory. |
|
ad275f9…
|
stephan
|
2799 |
** Filenames provided without these flags, on the other hand, are |
|
ad275f9…
|
stephan
|
2800 |
** treated as relative to the current directory. |
|
ad275f9…
|
stephan
|
2801 |
** |
|
29f450a…
|
stephan
|
2802 |
** As a special case, files currently undergoing an uncommitted merge |
|
29f450a…
|
stephan
|
2803 |
** might not get timestamped with --checkin because it may be |
|
29f450a…
|
stephan
|
2804 |
** impossible for fossil to choose between multiple potential |
|
29f450a…
|
stephan
|
2805 |
** timestamps. A non-fatal warning is emitted for such cases. |
|
29f450a…
|
stephan
|
2806 |
** |
|
a7e86f5…
|
drh
|
2807 |
*/ |
|
a7e86f5…
|
drh
|
2808 |
void touch_cmd(){ |
|
70a94d0…
|
jan.nijtmans
|
2809 |
const char * zGlobList; /* -g List of glob patterns */ |
|
a7e86f5…
|
drh
|
2810 |
const char * zGlobFile; /* -G File of glob patterns */ |
|
a7e86f5…
|
drh
|
2811 |
Glob * pGlob = 0; /* List of glob patterns */ |
|
a7e86f5…
|
drh
|
2812 |
int verboseFlag; |
|
a7e86f5…
|
drh
|
2813 |
int dryRunFlag; |
|
bc36fdc…
|
danield
|
2814 |
int vid; /* Check-out version */ |
|
a7e86f5…
|
drh
|
2815 |
int changeCount = 0; /* Number of files touched */ |
|
1b49ab3…
|
stephan
|
2816 |
int quietFlag = 0; /* -q|--quiet */ |
|
1b49ab3…
|
stephan
|
2817 |
int timeFlag; /* -1==--checkin, 1==--checkout, 0==--now */ |
|
1b49ab3…
|
stephan
|
2818 |
i64 nowTime = 0; /* Timestamp of --now or --checkout */ |
|
a7e86f5…
|
drh
|
2819 |
Stmt q; |
|
adbbeac…
|
stephan
|
2820 |
Blob absBuffer = empty_blob; /* Absolute filename buffer */ |
|
a7e86f5…
|
drh
|
2821 |
|
|
a7e86f5…
|
drh
|
2822 |
verboseFlag = find_option("verbose","v",0)!=0; |
|
74d5ce3…
|
florian
|
2823 |
quietFlag = g.fQuiet; |
|
4a720c2…
|
stephan
|
2824 |
dryRunFlag = find_option("dry-run","n",0)!=0; |
|
a7e86f5…
|
drh
|
2825 |
zGlobList = find_option("glob", "g",1); |
|
a7e86f5…
|
drh
|
2826 |
zGlobFile = find_option("globfile", "G",1); |
|
a7e86f5…
|
drh
|
2827 |
|
|
a7e86f5…
|
drh
|
2828 |
if(zGlobList && zGlobFile){ |
|
c3b4856…
|
stephan
|
2829 |
fossil_fatal("Options -g and -G may not be used together."); |
|
1b49ab3…
|
stephan
|
2830 |
} |
|
1b49ab3…
|
stephan
|
2831 |
|
|
1b49ab3…
|
stephan
|
2832 |
{ |
|
1b49ab3…
|
stephan
|
2833 |
int const ci = |
|
1b49ab3…
|
stephan
|
2834 |
(find_option("checkin","c",0) || find_option("check-in",0,0)) |
|
1b49ab3…
|
stephan
|
2835 |
? 1 : 0; |
|
1b49ab3…
|
stephan
|
2836 |
int const co = find_option("checkout","C",0) ? 1 : 0; |
|
1b49ab3…
|
stephan
|
2837 |
int const now = find_option("now",0,0) ? 1 : 0; |
|
1b49ab3…
|
stephan
|
2838 |
if(ci + co + now > 1){ |
|
1b49ab3…
|
stephan
|
2839 |
fossil_fatal("Options --checkin, --checkout, and --now may " |
|
1b49ab3…
|
stephan
|
2840 |
"not be used together."); |
|
1b49ab3…
|
stephan
|
2841 |
}else if(co){ |
|
1b49ab3…
|
stephan
|
2842 |
timeFlag = 1; |
|
1b49ab3…
|
stephan
|
2843 |
if(verboseFlag){ |
|
bc36fdc…
|
danield
|
2844 |
fossil_print("Timestamp = current check-out version.\n"); |
|
1b49ab3…
|
stephan
|
2845 |
} |
|
1b49ab3…
|
stephan
|
2846 |
}else if(ci){ |
|
1b49ab3…
|
stephan
|
2847 |
timeFlag = -1; |
|
1b49ab3…
|
stephan
|
2848 |
if(verboseFlag){ |
|
bc36fdc…
|
danield
|
2849 |
fossil_print("Timestamp = check-in in which each file was " |
|
1b49ab3…
|
stephan
|
2850 |
"most recently modified.\n"); |
|
1b49ab3…
|
stephan
|
2851 |
} |
|
1b49ab3…
|
stephan
|
2852 |
}else{ |
|
1b49ab3…
|
stephan
|
2853 |
timeFlag = 0; |
|
1b49ab3…
|
stephan
|
2854 |
if(verboseFlag){ |
|
1b49ab3…
|
stephan
|
2855 |
fossil_print("Timestamp = current system time.\n"); |
|
1b49ab3…
|
stephan
|
2856 |
} |
|
1b49ab3…
|
stephan
|
2857 |
} |
|
a7e86f5…
|
drh
|
2858 |
} |
|
c3b4856…
|
stephan
|
2859 |
|
|
c3b4856…
|
stephan
|
2860 |
verify_all_options(); |
|
a7e86f5…
|
drh
|
2861 |
|
|
a7e86f5…
|
drh
|
2862 |
db_must_be_within_tree(); |
|
a7e86f5…
|
drh
|
2863 |
vid = db_lget_int("checkout", 0); |
|
a7e86f5…
|
drh
|
2864 |
if(vid==0){ |
|
bc36fdc…
|
danield
|
2865 |
fossil_fatal("Cannot determine check-out version."); |
|
a7e86f5…
|
drh
|
2866 |
} |
|
1b49ab3…
|
stephan
|
2867 |
|
|
a7e86f5…
|
drh
|
2868 |
if(zGlobList){ |
|
a7e86f5…
|
drh
|
2869 |
pGlob = *zGlobList ? glob_create(zGlobList) : 0; |
|
a7e86f5…
|
drh
|
2870 |
}else if(zGlobFile){ |
|
1b49ab3…
|
stephan
|
2871 |
Blob globs = empty_blob; |
|
a7e86f5…
|
drh
|
2872 |
blob_read_from_file(&globs, zGlobFile, ExtFILE); |
|
a7e86f5…
|
drh
|
2873 |
pGlob = glob_create( globs.aData ); |
|
a7e86f5…
|
drh
|
2874 |
blob_reset(&globs); |
|
a7e86f5…
|
drh
|
2875 |
} |
|
a7e86f5…
|
drh
|
2876 |
if( pGlob && verboseFlag!=0 ){ |
|
a7e86f5…
|
drh
|
2877 |
int i; |
|
a7e86f5…
|
drh
|
2878 |
for(i=0; i<pGlob->nPattern; ++i){ |
|
a7e86f5…
|
drh
|
2879 |
fossil_print("glob: %s\n", pGlob->azPattern[i]); |
|
a7e86f5…
|
drh
|
2880 |
} |
|
a7e86f5…
|
drh
|
2881 |
} |
|
1b49ab3…
|
stephan
|
2882 |
|
|
1b49ab3…
|
stephan
|
2883 |
db_begin_transaction(); |
|
1b49ab3…
|
stephan
|
2884 |
if(timeFlag==0){/*--now*/ |
|
1b49ab3…
|
stephan
|
2885 |
nowTime = time(0); |
|
bc36fdc…
|
danield
|
2886 |
}else if(timeFlag>0){/*--checkout: get the check-out |
|
1b49ab3…
|
stephan
|
2887 |
manifest's timestamp*/ |
|
1b49ab3…
|
stephan
|
2888 |
assert(vid>0); |
|
1b49ab3…
|
stephan
|
2889 |
nowTime = db_int64(-1, |
|
1b49ab3…
|
stephan
|
2890 |
"SELECT CAST(strftime('%%s'," |
|
1b49ab3…
|
stephan
|
2891 |
"(SELECT mtime FROM event WHERE objid=%d)" |
|
1b49ab3…
|
stephan
|
2892 |
") AS INTEGER)", vid); |
|
1b49ab3…
|
stephan
|
2893 |
if(nowTime<0){ |
|
bc36fdc…
|
danield
|
2894 |
fossil_fatal("Could not determine check-out version's time!"); |
|
1b49ab3…
|
stephan
|
2895 |
} |
|
1b49ab3…
|
stephan
|
2896 |
}else{ /* --checkin */ |
|
1b49ab3…
|
stephan
|
2897 |
assert(0 == nowTime); |
|
1b49ab3…
|
stephan
|
2898 |
} |
|
adbbeac…
|
stephan
|
2899 |
if((pGlob && pGlob->nPattern>0) || g.argc<3){ |
|
1b49ab3…
|
stephan
|
2900 |
/* |
|
adbbeac…
|
stephan
|
2901 |
** We have either (1) globs or (2) no trailing filenames. If there |
|
adbbeac…
|
stephan
|
2902 |
** are neither globs nor filenames then we operate on all managed |
|
adbbeac…
|
stephan
|
2903 |
** files. |
|
1b49ab3…
|
stephan
|
2904 |
*/ |
|
1b49ab3…
|
stephan
|
2905 |
db_prepare(&q, |
|
1b49ab3…
|
stephan
|
2906 |
"SELECT vfile.mrid, pathname " |
|
1b49ab3…
|
stephan
|
2907 |
"FROM vfile LEFT JOIN blob ON vfile.mrid=blob.rid " |
|
1b49ab3…
|
stephan
|
2908 |
"WHERE vid=%d", vid); |
|
1b49ab3…
|
stephan
|
2909 |
while(SQLITE_ROW==db_step(&q)){ |
|
1b49ab3…
|
stephan
|
2910 |
int const fid = db_column_int(&q, 0); |
|
1b49ab3…
|
stephan
|
2911 |
const char * zName = db_column_text(&q, 1); |
|
1b49ab3…
|
stephan
|
2912 |
i64 newMtime = nowTime; |
|
1b49ab3…
|
stephan
|
2913 |
char const * zAbs = 0; /* absolute path */ |
|
1b49ab3…
|
stephan
|
2914 |
absBuffer.nUsed = 0; |
|
1b49ab3…
|
stephan
|
2915 |
assert(timeFlag<0 ? newMtime==0 : newMtime>0); |
|
1b49ab3…
|
stephan
|
2916 |
if(pGlob){ |
|
1b49ab3…
|
stephan
|
2917 |
if(glob_match(pGlob, zName)==0) continue; |
|
1b49ab3…
|
stephan
|
2918 |
} |
|
1b49ab3…
|
stephan
|
2919 |
blob_appendf( &absBuffer, "%s%s", g.zLocalRoot, zName ); |
|
1b49ab3…
|
stephan
|
2920 |
zAbs = blob_str(&absBuffer); |
|
1b49ab3…
|
stephan
|
2921 |
if( newMtime || mtime_of_manifest_file(vid, fid, &newMtime)==0 ){ |
|
1b49ab3…
|
stephan
|
2922 |
changeCount += |
|
29f450a…
|
stephan
|
2923 |
touch_cmd_stamp_one_file( zAbs, zName, newMtime, dryRunFlag, |
|
29f450a…
|
stephan
|
2924 |
verboseFlag, quietFlag ); |
|
1b49ab3…
|
stephan
|
2925 |
} |
|
1b49ab3…
|
stephan
|
2926 |
} |
|
1b49ab3…
|
stephan
|
2927 |
db_finalize(&q); |
|
1b49ab3…
|
stephan
|
2928 |
} |
|
1b49ab3…
|
stephan
|
2929 |
glob_free(pGlob); |
|
1b49ab3…
|
stephan
|
2930 |
pGlob = 0; |
|
1b49ab3…
|
stephan
|
2931 |
if(g.argc>2){ |
|
1b49ab3…
|
stephan
|
2932 |
/* |
|
1b49ab3…
|
stephan
|
2933 |
** Trailing filenames on the command line. These require extra |
|
1b49ab3…
|
stephan
|
2934 |
** care to avoid modifying unmanaged or out-of-tree files and |
|
1b49ab3…
|
stephan
|
2935 |
** finding an associated --checkin timestamp. |
|
1b49ab3…
|
stephan
|
2936 |
*/ |
|
1b49ab3…
|
stephan
|
2937 |
int i; |
|
adbbeac…
|
stephan
|
2938 |
Blob treeNameBuf = empty_blob; /* Buffer for file_tree_name(). */ |
|
1b49ab3…
|
stephan
|
2939 |
for( i = 2; i < g.argc; ++i, |
|
1b49ab3…
|
stephan
|
2940 |
blob_reset(&treeNameBuf) ){ |
|
1b49ab3…
|
stephan
|
2941 |
char const * zArg = g.argv[i]; |
|
1b49ab3…
|
stephan
|
2942 |
char const * zTreeFile; /* repo-relative filename */ |
|
1b49ab3…
|
stephan
|
2943 |
char const * zAbs; /* absolute filename */ |
|
1b49ab3…
|
stephan
|
2944 |
i64 newMtime = nowTime; |
|
1b49ab3…
|
stephan
|
2945 |
int nameCheck; |
|
1b49ab3…
|
stephan
|
2946 |
int fid; /* vfile.mrid of file */ |
|
1b49ab3…
|
stephan
|
2947 |
nameCheck = file_tree_name( zArg, &treeNameBuf, 0, 0 ); |
|
1b49ab3…
|
stephan
|
2948 |
if(nameCheck==0){ |
|
1b49ab3…
|
stephan
|
2949 |
if(quietFlag==0){ |
|
1b49ab3…
|
stephan
|
2950 |
fossil_print("SKIPPING out-of-tree file: %s\n", zArg); |
|
1b49ab3…
|
stephan
|
2951 |
} |
|
1b49ab3…
|
stephan
|
2952 |
continue; |
|
1b49ab3…
|
stephan
|
2953 |
} |
|
1b49ab3…
|
stephan
|
2954 |
zTreeFile = blob_str(&treeNameBuf); |
|
1b49ab3…
|
stephan
|
2955 |
fid = touch_cmd_vfile_mrid( vid, zTreeFile ); |
|
1b49ab3…
|
stephan
|
2956 |
if(fid==0){ |
|
1b49ab3…
|
stephan
|
2957 |
if(quietFlag==0){ |
|
1b49ab3…
|
stephan
|
2958 |
fossil_print("SKIPPING unmanaged file: %s\n", zArg); |
|
1b49ab3…
|
stephan
|
2959 |
} |
|
1b49ab3…
|
stephan
|
2960 |
continue; |
|
1b49ab3…
|
stephan
|
2961 |
} |
|
adbbeac…
|
stephan
|
2962 |
absBuffer.nUsed = 0; |
|
1b49ab3…
|
stephan
|
2963 |
blob_appendf(&absBuffer, "%s%s", g.zLocalRoot, zTreeFile); |
|
1b49ab3…
|
stephan
|
2964 |
zAbs = blob_str(&absBuffer); |
|
1b49ab3…
|
stephan
|
2965 |
if(timeFlag<0){/*--checkin*/ |
|
1b49ab3…
|
stephan
|
2966 |
if(mtime_of_manifest_file( vid, fid, &newMtime )!=0){ |
|
1b49ab3…
|
stephan
|
2967 |
fossil_fatal("Could not resolve --checkin mtime of %s", zTreeFile); |
|
1b49ab3…
|
stephan
|
2968 |
} |
|
1b49ab3…
|
stephan
|
2969 |
}else{ |
|
1b49ab3…
|
stephan
|
2970 |
assert(newMtime>0); |
|
1b49ab3…
|
stephan
|
2971 |
} |
|
1b49ab3…
|
stephan
|
2972 |
changeCount += |
|
29f450a…
|
stephan
|
2973 |
touch_cmd_stamp_one_file( zAbs, zArg, newMtime, dryRunFlag, |
|
29f450a…
|
stephan
|
2974 |
verboseFlag, quietFlag ); |
|
70a94d0…
|
jan.nijtmans
|
2975 |
} |
|
1b49ab3…
|
stephan
|
2976 |
} |
|
1b49ab3…
|
stephan
|
2977 |
db_end_transaction(0); |
|
1b49ab3…
|
stephan
|
2978 |
blob_reset(&absBuffer); |
|
a7e86f5…
|
drh
|
2979 |
if( dryRunFlag!=0 ){ |
|
a7e86f5…
|
drh
|
2980 |
fossil_print("dry-run: would have touched %d file(s)\n", |
|
a7e86f5…
|
drh
|
2981 |
changeCount); |
|
adbbeac…
|
stephan
|
2982 |
}else{ |
|
adbbeac…
|
stephan
|
2983 |
fossil_print("Touched %d file(s)\n", changeCount); |
|
adbbeac…
|
stephan
|
2984 |
} |
|
b699040…
|
drh
|
2985 |
} |
|
b699040…
|
drh
|
2986 |
|
|
b699040…
|
drh
|
2987 |
/* |
|
b699040…
|
drh
|
2988 |
** If zFileName is not NULL and contains a '.', this returns a pointer |
|
f95e47b…
|
stephan
|
2989 |
** to the position after the final '.', else it returns NULL. As a |
|
f95e47b…
|
stephan
|
2990 |
** special case, if it ends with a period then a pointer to the |
|
f95e47b…
|
stephan
|
2991 |
** terminating NUL byte is returned. |
|
b699040…
|
drh
|
2992 |
*/ |
|
b699040…
|
drh
|
2993 |
const char * file_extension(const char *zFileName){ |
|
f95e47b…
|
stephan
|
2994 |
const char * zExt = zFileName ? strrchr(zFileName, '.') : 0; |
|
b699040…
|
drh
|
2995 |
return zExt ? &zExt[1] : 0; |
|
f741baa…
|
drh
|
2996 |
} |
|
f741baa…
|
drh
|
2997 |
|
|
f741baa…
|
drh
|
2998 |
/* |
|
f741baa…
|
drh
|
2999 |
** Returns non-zero if the specified file name ends with any reserved name, |
|
f741baa…
|
drh
|
3000 |
** e.g.: _FOSSIL_ or .fslckout. Specifically, it returns 1 for exact match |
|
f741baa…
|
drh
|
3001 |
** or 2 for a tail match on a longer file name. |
|
f741baa…
|
drh
|
3002 |
** |
|
f741baa…
|
drh
|
3003 |
** For the sake of efficiency, zFilename must be a canonical name, e.g. an |
|
f741baa…
|
drh
|
3004 |
** absolute path using only forward slash ('/') as a directory separator. |
|
f741baa…
|
drh
|
3005 |
** |
|
f741baa…
|
drh
|
3006 |
** nFilename must be the length of zFilename. When negative, strlen() will |
|
f741baa…
|
drh
|
3007 |
** be used to calculate it. |
|
f741baa…
|
drh
|
3008 |
*/ |
|
f741baa…
|
drh
|
3009 |
int file_is_reserved_name(const char *zFilename, int nFilename){ |
|
f741baa…
|
drh
|
3010 |
const char *zEnd; /* one-after-the-end of zFilename */ |
|
f741baa…
|
drh
|
3011 |
int gotSuffix = 0; /* length of suffix (-wal, -shm, -journal) */ |
|
f741baa…
|
drh
|
3012 |
|
|
f741baa…
|
drh
|
3013 |
assert( zFilename && "API misuse" ); |
|
f741baa…
|
drh
|
3014 |
if( nFilename<0 ) nFilename = (int)strlen(zFilename); |
|
f741baa…
|
drh
|
3015 |
if( nFilename<8 ) return 0; /* strlen("_FOSSIL_") */ |
|
f741baa…
|
drh
|
3016 |
zEnd = zFilename + nFilename; |
|
f741baa…
|
drh
|
3017 |
if( nFilename>=12 ){ /* strlen("_FOSSIL_-(shm|wal)") */ |
|
f741baa…
|
drh
|
3018 |
/* Check for (-wal, -shm, -journal) suffixes, with an eye towards |
|
f741baa…
|
drh
|
3019 |
** runtime speed. */ |
|
f741baa…
|
drh
|
3020 |
if( zEnd[-4]=='-' ){ |
|
f741baa…
|
drh
|
3021 |
if( fossil_strnicmp("wal", &zEnd[-3], 3) |
|
f741baa…
|
drh
|
3022 |
&& fossil_strnicmp("shm", &zEnd[-3], 3) ){ |
|
f741baa…
|
drh
|
3023 |
return 0; |
|
f741baa…
|
drh
|
3024 |
} |
|
f741baa…
|
drh
|
3025 |
gotSuffix = 4; |
|
f741baa…
|
drh
|
3026 |
}else if( nFilename>=16 && zEnd[-8]=='-' ){ /*strlen(_FOSSIL_-journal) */ |
|
f741baa…
|
drh
|
3027 |
if( fossil_strnicmp("journal", &zEnd[-7], 7) ) return 0; |
|
f741baa…
|
drh
|
3028 |
gotSuffix = 8; |
|
f741baa…
|
drh
|
3029 |
} |
|
f741baa…
|
drh
|
3030 |
if( gotSuffix ){ |
|
f741baa…
|
drh
|
3031 |
assert( 4==gotSuffix || 8==gotSuffix ); |
|
f741baa…
|
drh
|
3032 |
zEnd -= gotSuffix; |
|
f741baa…
|
drh
|
3033 |
nFilename -= gotSuffix; |
|
f741baa…
|
drh
|
3034 |
gotSuffix = 1; |
|
f741baa…
|
drh
|
3035 |
} |
|
f741baa…
|
drh
|
3036 |
assert( nFilename>=8 && "strlen(_FOSSIL_)" ); |
|
f741baa…
|
drh
|
3037 |
assert( gotSuffix==0 || gotSuffix==1 ); |
|
f741baa…
|
drh
|
3038 |
} |
|
f741baa…
|
drh
|
3039 |
switch( zEnd[-1] ){ |
|
f741baa…
|
drh
|
3040 |
case '_':{ |
|
f741baa…
|
drh
|
3041 |
if( fossil_strnicmp("_FOSSIL_", &zEnd[-8], 8) ) return 0; |
|
f741baa…
|
drh
|
3042 |
if( 8==nFilename ) return 1; |
|
f741baa…
|
drh
|
3043 |
return zEnd[-9]=='/' ? 2 : gotSuffix; |
|
f741baa…
|
drh
|
3044 |
} |
|
f741baa…
|
drh
|
3045 |
case 'T': |
|
f741baa…
|
drh
|
3046 |
case 't':{ |
|
f741baa…
|
drh
|
3047 |
if( nFilename<9 || zEnd[-9]!='.' |
|
f741baa…
|
drh
|
3048 |
|| fossil_strnicmp(".fslckout", &zEnd[-9], 9) ){ |
|
0537925…
|
andygoth
|
3049 |
return 0; |
|
f741baa…
|
drh
|
3050 |
} |
|
f741baa…
|
drh
|
3051 |
if( 9==nFilename ) return 1; |
|
f741baa…
|
drh
|
3052 |
return zEnd[-10]=='/' ? 2 : gotSuffix; |
|
f741baa…
|
drh
|
3053 |
} |
|
f741baa…
|
drh
|
3054 |
default:{ |
|
f741baa…
|
drh
|
3055 |
return 0; |
|
f741baa…
|
drh
|
3056 |
} |
|
f741baa…
|
drh
|
3057 |
} |
|
84e2abe…
|
mistachkin
|
3058 |
} |
|
84e2abe…
|
mistachkin
|
3059 |
|
|
84e2abe…
|
mistachkin
|
3060 |
/* |
|
84e2abe…
|
mistachkin
|
3061 |
** COMMAND: test-is-reserved-name |
|
84e2abe…
|
mistachkin
|
3062 |
** |
|
84e2abe…
|
mistachkin
|
3063 |
** Usage: %fossil test-is-reserved-name FILENAMES... |
|
84e2abe…
|
mistachkin
|
3064 |
** |
|
84e2abe…
|
mistachkin
|
3065 |
** Passes each given name to file_is_reserved_name() and outputs one |
|
84e2abe…
|
mistachkin
|
3066 |
** line per file: the result value of that function followed by the |
|
84e2abe…
|
mistachkin
|
3067 |
** name. |
|
84e2abe…
|
mistachkin
|
3068 |
*/ |
|
84e2abe…
|
mistachkin
|
3069 |
void test_is_reserved_name_cmd(void){ |
|
84e2abe…
|
mistachkin
|
3070 |
int i; |
|
84e2abe…
|
mistachkin
|
3071 |
|
|
84e2abe…
|
mistachkin
|
3072 |
if(g.argc<3){ |
|
84e2abe…
|
mistachkin
|
3073 |
usage("FILENAME_1 [...FILENAME_N]"); |
|
84e2abe…
|
mistachkin
|
3074 |
} |
|
84e2abe…
|
mistachkin
|
3075 |
for( i = 2; i < g.argc; ++i ){ |
|
84e2abe…
|
mistachkin
|
3076 |
const int check = file_is_reserved_name(g.argv[i], -1); |
|
84e2abe…
|
mistachkin
|
3077 |
fossil_print("%d %s\n", check, g.argv[i]); |
|
84e2abe…
|
mistachkin
|
3078 |
} |
|
3946ff8…
|
stephan
|
3079 |
} |
|
3946ff8…
|
stephan
|
3080 |
|
|
3946ff8…
|
stephan
|
3081 |
|
|
3946ff8…
|
stephan
|
3082 |
/* |
|
3946ff8…
|
stephan
|
3083 |
** Returns 1 if the given directory contains a file named .fslckout, 2 |
|
3946ff8…
|
stephan
|
3084 |
** if it contains a file named _FOSSIL_, else returns 0. |
|
3946ff8…
|
stephan
|
3085 |
*/ |
|
3946ff8…
|
stephan
|
3086 |
int dir_has_ckout_db(const char *zDir){ |
|
3946ff8…
|
stephan
|
3087 |
int rc = 0; |
|
36f916c…
|
drh
|
3088 |
i64 sz; |
|
3946ff8…
|
stephan
|
3089 |
char * zCkoutDb = mprintf("%//.fslckout", zDir); |
|
3946ff8…
|
stephan
|
3090 |
if(file_isfile(zCkoutDb, ExtFILE)){ |
|
3946ff8…
|
stephan
|
3091 |
rc = 1; |
|
3946ff8…
|
stephan
|
3092 |
}else{ |
|
3946ff8…
|
stephan
|
3093 |
fossil_free(zCkoutDb); |
|
3946ff8…
|
stephan
|
3094 |
zCkoutDb = mprintf("%//_FOSSIL_", zDir); |
|
3946ff8…
|
stephan
|
3095 |
if(file_isfile(zCkoutDb, ExtFILE)){ |
|
3946ff8…
|
stephan
|
3096 |
rc = 2; |
|
3946ff8…
|
stephan
|
3097 |
} |
|
36f916c…
|
drh
|
3098 |
} |
|
36f916c…
|
drh
|
3099 |
if( rc && ((sz = file_size(zCkoutDb, ExtFILE))<1024 || (sz%512)!=0) ){ |
|
36f916c…
|
drh
|
3100 |
rc = 0; |
|
106de27…
|
drh
|
3101 |
} |
|
3946ff8…
|
stephan
|
3102 |
fossil_free(zCkoutDb); |
|
3946ff8…
|
stephan
|
3103 |
return rc; |
|
106de27…
|
drh
|
3104 |
} |
|
106de27…
|
drh
|
3105 |
|
|
106de27…
|
drh
|
3106 |
/* |
|
106de27…
|
drh
|
3107 |
** This is the implementation of inode(FILENAME) SQL function. |
|
106de27…
|
drh
|
3108 |
** |
|
106de27…
|
drh
|
3109 |
** dev_inode(FILENAME) returns a string. If FILENAME exists and is |
|
106de27…
|
drh
|
3110 |
** a regular file, then the return string is of the form: |
|
106de27…
|
drh
|
3111 |
** |
|
106de27…
|
drh
|
3112 |
** DEV/INODE |
|
106de27…
|
drh
|
3113 |
** |
|
106de27…
|
drh
|
3114 |
** Where DEV and INODE are the device number and inode number for |
|
5233364…
|
florian
|
3115 |
** the file. On Windows, the volume serial number (DEV) and file |
|
5233364…
|
florian
|
3116 |
** identifier (INODE) are used to compute the value, see comments |
|
5233364…
|
florian
|
3117 |
** on the win32_file_id() function. |
|
106de27…
|
drh
|
3118 |
** |
|
106de27…
|
drh
|
3119 |
** If FILENAME does not exist, then the return is an empty string. |
|
106de27…
|
drh
|
3120 |
** |
|
106de27…
|
drh
|
3121 |
** The value of inode() can be used to eliminate files from a list |
|
106de27…
|
drh
|
3122 |
** that have duplicates because they have differing names due to links. |
|
106de27…
|
drh
|
3123 |
** |
|
106de27…
|
drh
|
3124 |
** Code that wants to use this SQL function needs to first register |
|
106de27…
|
drh
|
3125 |
** it using a call such as the following: |
|
106de27…
|
drh
|
3126 |
** |
|
106de27…
|
drh
|
3127 |
** sqlite3_create_function(g.db, "inode", 1, SQLITE_UTF8, 0, |
|
106de27…
|
drh
|
3128 |
** file_inode_sql_func, 0, 0); |
|
106de27…
|
drh
|
3129 |
*/ |
|
106de27…
|
drh
|
3130 |
void file_inode_sql_func( |
|
106de27…
|
drh
|
3131 |
sqlite3_context *context, |
|
106de27…
|
drh
|
3132 |
int argc, |
|
106de27…
|
drh
|
3133 |
sqlite3_value **argv |
|
106de27…
|
drh
|
3134 |
){ |
|
106de27…
|
drh
|
3135 |
const char *zFilename; |
|
106de27…
|
drh
|
3136 |
assert( argc==1 ); |
|
106de27…
|
drh
|
3137 |
zFilename = (const char*)sqlite3_value_text(argv[0]); |
|
106de27…
|
drh
|
3138 |
if( zFilename==0 || zFilename[0]==0 || file_access(zFilename,F_OK) ){ |
|
106de27…
|
drh
|
3139 |
sqlite3_result_text(context, "", 0, SQLITE_STATIC); |
|
106de27…
|
drh
|
3140 |
return; |
|
106de27…
|
drh
|
3141 |
} |
|
106de27…
|
drh
|
3142 |
#if defined(_WIN32) |
|
106de27…
|
drh
|
3143 |
{ |
|
5233364…
|
florian
|
3144 |
char *zFileId = win32_file_id(zFilename); |
|
5233364…
|
florian
|
3145 |
if( zFileId ){ |
|
5233364…
|
florian
|
3146 |
sqlite3_result_text(context, zFileId, -1, fossil_free); |
|
5233364…
|
florian
|
3147 |
}else{ |
|
5233364…
|
florian
|
3148 |
sqlite3_result_text(context, "", 0, SQLITE_STATIC); |
|
5233364…
|
florian
|
3149 |
} |
|
106de27…
|
drh
|
3150 |
} |
|
106de27…
|
drh
|
3151 |
#else |
|
106de27…
|
drh
|
3152 |
{ |
|
106de27…
|
drh
|
3153 |
struct stat buf; |
|
106de27…
|
drh
|
3154 |
int rc; |
|
106de27…
|
drh
|
3155 |
memset(&buf, 0, sizeof(buf)); |
|
106de27…
|
drh
|
3156 |
rc = stat(zFilename, &buf); |
|
106de27…
|
drh
|
3157 |
if( rc ){ |
|
106de27…
|
drh
|
3158 |
sqlite3_result_text(context, "", 0, SQLITE_STATIC); |
|
106de27…
|
drh
|
3159 |
}else{ |
|
106de27…
|
drh
|
3160 |
sqlite3_result_text(context, |
|
106de27…
|
drh
|
3161 |
mprintf("%lld/%lld", (i64)buf.st_dev, (i64)buf.st_ino), -1, |
|
106de27…
|
drh
|
3162 |
fossil_free); |
|
106de27…
|
drh
|
3163 |
} |
|
106de27…
|
drh
|
3164 |
} |
|
106de27…
|
drh
|
3165 |
#endif |
|
dbda8d6…
|
drh
|
3166 |
} |