1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*
** Copyright (c) 2014 D. Richard Hipp
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the Simplified BSD License (also
** known as the "2-Clause License" or "FreeBSD License".)
**
** This program is distributed in the hope that it will be useful,
** but without any warranty; without even the implied warranty of
** merchantability or fitness for a particular purpose.
**
** Author contact information:
** drh@sqlite.org
** http://www.hwaci.com/drh/
**
*******************************************************************************
**
** This module implements the userspace side of a Fuse Filesystem that
** contains all check-ins for a fossil repository.
**
** This module is a mostly a no-op unless compiled with -DFOSSIL_HAVE_FUSEFS.
** The FOSSIL_HAVE_FUSEFS should be omitted on systems that lack support for
** the Fuse Filesystem, of course.
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#ifdef FOSSIL_HAVE_FUSEFS
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "fusefs.h"
#define FUSE_USE_VERSION 26
#include <fuse.h>
/*
** Global state information about the archive
*/
static struct sGlobal {
/* A cache of a single check-in manifest */
int rid; /* rid for the cached manifest */
char *zSymName; /* Symbolic name corresponding to rid */
Manifest *pMan; /* The cached manifest */
/* A cache of a single file within a single check-in */
int iFileRid; /* Check-in ID for the cached file */
ManifestFile *pFile; /* Name of a cached file */
Blob content; /* Content of the cached file */
/* Parsed path */
char *az[3]; /* 0=type, 1=id, 2=path */
} fusefs;
/*
** Clear the fusefs.az[] array.
*/
static void fusefs_clear_path(void){
int i;
for(i=0; i<count(fusefs.az); i++){
fossil_free(fusefs.az[i]);
fusefs.az[i] = 0;
}
}
/*
** Split of the input path into 0, 1, 2, or 3 elements in fusefs.az[].
** Return the number of elements.
**
** Any prior path parse is deleted.
*/
static int fusefs_parse_path(const char *zPath){
int i, j;
fusefs_clear_path();
if( strcmp(zPath,"/")==0 ) return 0;
for(i=0, j=1; i<2 && zPath[j]; i++){
int jStart = j;
while( zPath[j] && zPath[j]!='/' ){ j++; }
fusefs.az[i] = mprintf("%.*s", j-jStart, &zPath[jStart]);
if( zPath[j] ) j++;
}
if( zPath[j] ) fusefs.az[i++] = fossil_strdup(&zPath[j]);
return i;
}
/*
** Reclaim memory used by the fusefs local variable.
*/
static void fusefs_reset(void){
blob_reset(&fusefs.content);
manifest_destroy(fusefs.pMan);
fusefs.pMan = 0;
fossil_free(fusefs.zSymName);
fusefs.zSymName = 0;
fusefs.pFile = 0;
}
/*
** Load manifest rid into the cache.
*/
static void fusefs_load_rid(int rid, const char *zSymName){
if( fusefs.rid==rid && fusefs.pMan!=0 ) return;
fusefs_reset();
fusefs.zSymName = fossil_strdup(zSymName);
fusefs.pMan = manifest_get(rid, CFTYPE_MANIFEST, 0);
fusefs.rid = rid;
}
/*
** Locate the rid corresponding to a symbolic name
*/
static int fusefs_name_to_rid(const char *zSymName){
if( fusefs.rid>0 && strcmp(zSymName, fusefs.zSymName)==0 ){
return fusefs.rid;
}else{
return symbolic_name_to_rid(zSymName, "ci");
}
}
/*
** Implementation of stat()
*/
static int fusefs_getattr(const char *zPath, struct stat *stbuf){
int n, rid;
ManifestFile *pFile;
char *zDir;
stbuf->st_uid = getuid();
stbuf->st_gid = getgid();
n = fusefs_parse_path(zPath);
if( n==0 ){
stbuf->st_mode = S_IFDIR | 0555;
stbuf->st_nlink = 2;
return 0;
}
if( strcmp(fusefs.az[0],"checkins")!=0 ) return -ENOENT;
if( n==1 ){
stbuf->st_mode = S_IFDIR | 0111;
stbuf->st_nlink = 2;
return 0;
}
rid = fusefs_name_to_rid(fusefs.az[1]);
if( rid<=0 ) return -ENOENT;
if( n==2 ){
stbuf->st_mode = S_IFDIR | 0555;
stbuf->st_nlink = 2;
return 0;
}
fusefs_load_rid(rid, fusefs.az[1]);
if( fusefs.pMan==0 ) return -ENOENT;
stbuf->st_mtime = (fusefs.pMan->rDate - 2440587.5)*86400.0;
pFile = manifest_file_seek(fusefs.pMan, fusefs.az[2], 0);
if( pFile ){
static Stmt q;
stbuf->st_mode = S_IFREG |
(manifest_file_mperm(pFile)==PERM_EXE ? 0555 : 0444);
stbuf->st_nlink = 1;
db_static_prepare(&q, "SELECT size FROM blob WHERE uuid=$uuid");
db_bind_text(&q, "$uuid", pFile->zUuid);
if( db_step(&q)==SQLITE_ROW ){
stbuf->st_size = db_column_int(&q, 0);
}
db_reset(&q);
return 0;
}
zDir = mprintf("%s/", fusefs.az[2]);
pFile = manifest_file_seek(fusefs.pMan, zDir, 1);
fossil_free(zDir);
if( pFile==0 ) return -ENOENT;
n = (int)strlen(fusefs.az[2]);
if( strncmp(fusefs.az[2], pFile->zName, n)!=0 ) return -ENOENT;
if( pFile->zName[n]!='/' ) return -ENOENT;
stbuf->st_mode = S_IFDIR | 0555;
stbuf->st_nlink = 2;
return 0;
}
/*
** Implementation of readdir()
*/
static int fusefs_readdir(
const char *zPath,
void *buf,
fuse_fill_dir_t filler,
off_t offset,
struct fuse_file_info *fi
){
int n, rid;
ManifestFile *pFile;
const char *zPrev = "";
int nPrev = 0;
char *z;
int cnt = 0;
n = fusefs_parse_path(zPath);
if( n==0 ){
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
filler(buf, "checkins", NULL, 0);
return 0;
}
if( strcmp(fusefs.az[0],"checkins")!=0 ) return -ENOENT;
if( n==1 ) return -ENOENT;
rid = fusefs_name_to_rid(fusefs.az[1]);
if( rid<=0 ) return -ENOENT;
fusefs_load_rid(rid, fusefs.az[1]);
if( fusefs.pMan==0 ) return -ENOENT;
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
manifest_file_rewind(fusefs.pMan);
if( n==2 ){
while( (pFile = manifest_file_next(fusefs.pMan, 0))!=0 ){
if( nPrev>0 && strncmp(pFile->zName, zPrev, nPrev)==0
&& pFile->zName[nPrev]=='/' ) continue;
zPrev = pFile->zName;
for(nPrev=0; zPrev[nPrev] && zPrev[nPrev]!='/'; nPrev++){}
z = mprintf("%.*s", nPrev, zPrev);
filler(buf, z, NULL, 0);
fossil_free(z);
cnt++;
}
}else{
char *zBase = mprintf("%s/", fusefs.az[2]);
int nBase = (int)strlen(zBase);
while( (pFile = manifest_file_next(fusefs.pMan, 0))!=0 ){
if( strcmp(pFile->zName, zBase)>=0 ) break;
}
while( pFile && strncmp(zBase, pFile->zName, nBase)==0 ){
if( nPrev==0 || strncmp(pFile->zName+nBase, zPrev, nPrev)!=0 ){
zPrev = pFile->zName+nBase;
for(nPrev=0; zPrev[nPrev] && zPrev[nPrev]!='/'; nPrev++){}
if( zPrev[nPrev]=='/' ){
z = mprintf("%.*s", nPrev, zPrev);
filler(buf, z, NULL, 0);
fossil_free(z);
}else{
filler(buf, zPrev, NULL, 0);
nPrev = 0;
}
cnt++;
}
pFile = manifest_file_next(fusefs.pMan, 0);
}
fossil_free(zBase);
}
return cnt>0 ? 0 : -ENOENT;
}
/*
** Implementation of read()
*/
static int fusefs_read(
const char *zPath,
char *buf,
size_t size,
off_t offset,
struct fuse_file_info *fi
){
int n, rid;
n = fusefs_parse_path(zPath);
if( n<3 ) return -ENOENT;
if( strcmp(fusefs.az[0], "checkins")!=0 ) return -ENOENT;
rid = fusefs_name_to_rid(fusefs.az[1]);
if( rid<=0 ) return -ENOENT;
fusefs_load_rid(rid, fusefs.az[1]);
if( fusefs.pFile!=0 && strcmp(fusefs.az[2], fusefs.pFile->zName)!=0 ){
fusefs.pFile = 0;
blob_reset(&fusefs.content);
}
fusefs.pFile = manifest_file_seek(fusefs.pMan, fusefs.az[2], 0);
if( fusefs.pFile==0 ) return -ENOENT;
rid = uuid_to_rid(fusefs.pFile->zUuid, 0);
blob_reset(&fusefs.content);
content_get(rid, &fusefs.content);
if( offset>blob_size(&fusefs.content) ) return 0;
if( offset+size>blob_size(&fusefs.content) ){
size = blob_size(&fusefs.content) - offset;
}
memcpy(buf, blob_buffer(&fusefs.content)+offset, size);
return size;
}
static struct fuse_operations fusefs_methods = {
.getattr = fusefs_getattr,
.readdir = fusefs_readdir,
.read = fusefs_read,
};
#endif /* FOSSIL_HAVE_FUSEFS */
/*
** COMMAND: fusefs*
**
** Usage: %fossil fusefs [--debug] DIRECTORY
**
** This command uses the Fuse Filesystem (FuseFS) to mount a directory
** at DIRECTORY that contains the content of all check-ins in the
** repository. The names of files are DIRECTORY/checkins/VERSION/PATH
** where DIRECTORY is the root of the mount, VERSION is any valid
** check-in name (examples: "trunk" or "tip" or a tag or any unique
** prefix of an artifact hash, etc) and PATH is the pathname of the file in
** the check-in. If DIRECTORY does not exist, then an attempt is made
** to create it.
**
** The DIRECTORY/checkins directory is not searchable so one cannot
** do "ls DIRECTORY/checkins" to get a listing of all possible check-in
** names. There are countless variations on check-in names and it is
** impractical to list them all. But all other directories are searchable
** and so the "ls" command will work everywhere else in the fusefs
** file hierarchy.
**
** The FuseFS typically only works on Linux, and then only on Linux
** systems that have the right kernel drivers and have installed the
** appropriate support libraries.
**
** After stopping the "fossil fusefs" command, it might also be necessary
** to run "fusermount -u DIRECTORY" to reset the FuseFS before using it
** again.
*/
void fusefs_cmd(void){
#ifdef FOSSIL_HAVE_FUSEFS
char *zMountPoint;
char *azNewArgv[5];
int doDebug = find_option("debug","d",0)!=0;
db_find_and_open_repository(0,0);
verify_all_options();
blob_init(&fusefs.content, 0, 0);
if( g.argc!=3 ) usage("DIRECTORY");
zMountPoint = g.argv[2];
if( file_mkdir(zMountPoint, ExtFILE, 0) ){
fossil_fatal("cannot make directory [%s]", zMountPoint);
}
azNewArgv[0] = g.argv[0];
azNewArgv[1] = doDebug ? "-d" : "-f";
azNewArgv[2] = "-s";
azNewArgv[3] = zMountPoint;
azNewArgv[4] = 0;
g.localOpen = 0; /* Prevent tags like "current" and "prev" */
fuse_main(4, azNewArgv, &fusefs_methods, NULL);
fusefs_reset();
fusefs_clear_path();
#else
fprintf(stderr, "The FuseFS is not available in this build.\n");
exit(1);
#endif /* FOSSIL_HAVE_FUSEFS */
}
/*
** Return version numbers for the FUSE header that was used at compile-time
** and/or the FUSE library that was loaded at runtime.
*/
const char *fusefs_lib_version(void){
#if defined(FOSSIL_HAVE_FUSEFS) && FUSE_MAJOR_VERSION>=3
return fuse_pkgversion();
#else
return "unknown";
#endif
}
const char *fusefs_inc_version(void){
#ifdef FOSSIL_HAVE_FUSEFS
return COMPILER_STRINGIFY(FUSE_MAJOR_VERSION) "."
COMPILER_STRINGIFY(FUSE_MINOR_VERSION);
#else
return "unknown";
#endif
}
|