/
obsfs.c
1488 lines (1329 loc) · 51.1 KB
/
obsfs.c
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* obsfs.c
* (c) 2010 Ulrich Hecht, SuSE Linux Products GmbH <uli@suse.de>
*
* This file is part of obsfs.
*
* obsfs is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 2 or version 3 of the License.
*
* obsfs 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. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with obsfs. If not, see <http://www.gnu.org/licenses/>.
*
*/
#define FUSE_USE_VERSION 26
#define DEBUG_OBSFS
#include <fuse.h>
#include <fuse/fuse_opt.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <curl/curl.h>
#include <expat.h>
#include <unistd.h>
#include <regex.h>
#include "obsfs.h"
#include "cache.h"
#include "util.h"
#include "status.h"
#include "rc.h"
#ifdef DEBUG_OBSFS
#define DEBUG(x...) fprintf(stderr, x)
#else
#define DEBUG(x...)
#endif
const char *root_dir[] = {
"/build",
"/source",
"/published",
"/request",
"/statistics",
NULL
};
/* package status APIs */
const char const *status_api[] = {
"_history", "_reason", "_status", "_log", NULL
};
regex_t build_project;
regex_t build_project_failed;
regex_t build_project_failed_foo;
regex_t build_project_failed_foo_bar;
regex_t build_project_repo_arch;
regex_t build_project_repo_arch_foo;
regex_t build_project_repo_arch_failed;
regex_t source_project;
regex_t source_project_package;
regex_t source_project_package_rev;
regex_t source_project_package_rev_num;
regex_t source_project_package_unexpanded;
regex_t source_myprojectpackages;
char *file_cache_dir = NULL; /* directory to keep cached file contents in */
int file_cache_count = 1; /* used to make up names for cached files */
char *url_prefix = NULL; /* prefix for API calls (https://...) */
/* filesystem options */
struct options {
char *api_username; /* API user name */
char *api_password; /* API user password */
char *api_hostname; /* API server name */
} options;
/* lifted from the Hello, World with options example */
#define OBSFS_OPT_KEY(t, p, v) { t, offsetof(struct options, p), v }
enum {
KEY_HELP,
KEY_VERSION
};
static struct fuse_opt obsfs_opts[] =
{
OBSFS_OPT_KEY("user=%s", api_username, 0),
OBSFS_OPT_KEY("pass=%s", api_password, 0),
OBSFS_OPT_KEY("host=%s", api_hostname, 0),
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_KEY("-V", KEY_VERSION),
FUSE_OPT_KEY("--version", KEY_VERSION),
FUSE_OPT_END
};
static int obsfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi);
static int is_in_root_dir(const char *path)
{
const char **d = root_dir;
for(; *d; d++) {
if (!strcmp(*d, path))
return 1;
}
return 0;
}
static int obsfs_getattr(const char *path, struct stat *stbuf)
{
/* initialize the stat buffer we are going to fill in */
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/") == 0 || is_in_root_dir(path)) {
/* root and the stuff inside it cannot be deduced because the server
returns a human-readable info page for "/", so they are hardcoded
here. */
stat_make_dir(stbuf);
} else {
/* actual API files and directories */
attr_t *ret;
DEBUG("getattr: looking for %s\n", path);
/* let's see if we have that cached already */
ret = attr_cache_find(path);
if (ret) {
DEBUG("found it!\n");
*stbuf = ret->st;
}
else {
/* Cache miss, we are going to retrieve the directory "path" is in.
The reason for that is that the only way to find out about
a directory entry is to retrieve the entire directory from the
server. We thus have obsfs_readdir() obtain the directory
without giving it a filler function, so it will only cache
the entries it finds in the attribute cache, where we can
subsequently retrieve the one we're looking for. */
char *dir = dirname_c(path, NULL);
DEBUG("not found, trying to get directory\n");
/* call with buf and filler NULL for cache-only operation */
obsfs_readdir(dir, NULL, NULL, 0, NULL);
free(dir);
/* now the attributes are in the attr cache (if it exists at all) */
ret = attr_cache_find(path);
if (ret) {
DEBUG("found it after all\n");
*stbuf = ret->st;
}
else {
/* file not found */
return -ENOENT;
}
}
}
return 0;
}
static int obsfs_readlink(const char *path, char *buf, size_t buflen)
{
attr_t *ret = attr_cache_find(path);
if (ret) {
found:
if (!ret->symlink)
return -ENOENT;
strncpy(buf, ret->symlink, buflen - 1);
buf[buflen-1] = 0;
return 0;
}
char *dir = dirname_c(path, NULL);
DEBUG("link not found, trying to get directory\n");
/* call with buf and filler NULL for cache-only operation */
obsfs_readdir(dir, NULL, NULL, 0, NULL);
free(dir);
/* now the attributes are in the attr cache (if it exists at all) */
ret = attr_cache_find(path);
if (ret)
goto found;
return -ENOENT;
}
/* data we need in the expat callbacks to save the directory entries */
struct filbuf {
void *buf; /* directory entry buffer, provided by FUSE */
fuse_fill_dir_t filler; /* buffer filler function */
const char *fs_path; /* directory to read... */
const char *api_path; /* ... and where to get it from */
const char *mangled_path; /* canonical FS path if fs_path is an alias */
dir_t *cdir; /* dir cache entry to fill in */
int in_dir; /* flag set when inside a <directory> or <binarylist> */
int in_collection; /* flag set when inside a <collection> */
int in_latest; /* flag set when inside a <latest_*> (statistics) */
int in_revisionlist; /* flag set when inside a <revisionlist> */
const char *filter_attr;
const char *filter_value;
};
/* add a node to a FUSE directory buffer, a directory cache entry, and the attribute cache */
static void add_dir_node(void *buf, fuse_fill_dir_t filler, dir_t *newdir, const char *path, const char *node_name, struct stat *st, const char *symlink, const char *hardlink)
{
char *full_path;
/* add node to the directory buffer (if any) */
if (filler)
filler(buf, node_name, st, 0);
/* compose a full path and add node to the attribute cache */
full_path = malloc(strlen(path) + 1 /* slash */ + strlen(node_name) + 1 /* null */);
sprintf(full_path, "%s/%s", path, node_name);
/* Tricky problem: Apparently, FUSE does a LOOKUP (using the getattr
method) before every open(), but it only does a GETATTR (also using the
getattr method) the first time a file is opened. That means that our
preferred method of updating the file stats in obsfs_open() generally
works, but if a directory expires and is retrieved from the server
again, we set the size back to size 0. When the file is opened now,
FUSE only does the LOOKUP before open and remembers the wrong file
size. The subsequent obsfs_open() call rectifies it for us, but FUSE
doesn't ask us again and won't permit programs to read any data. The
next time the file is opened things are fine again, because the
previous obsfs_open() run has set the stats correctly, and when FUSE
does a LOOKUP, it gets the right data and will allow programs to read
the file.
To work around this problem, we simply check if we have a cached copy
already and use its size if so. */
/* check if we have a local copy that we can use to get the size */
struct stat local_st;
if (!lstat(full_path + 1 /* skip leading slash */, &local_st)) {
st->st_size = local_st.st_size;
}
attr_cache_add(full_path, st, symlink, hardlink, newdir->rev);
/* add node to the directory cache entry */
dir_cache_add(newdir, node_name, S_ISDIR(st->st_mode) ? 1 : 0);
if (S_ISDIR(st->st_mode)) {
attr_t *parent = attr_cache_find(path);
if (parent)
parent->st.st_nlink++;
}
free(full_path);
}
/* expat tag start callback for reading API directories */
static void expat_api_dir_start(void *ud, const XML_Char *name, const XML_Char **atts)
{
struct stat st;
struct filbuf *fb = (struct filbuf *)ud;
stat_default_file(&st);
/* start of directory */
if (!strcmp(name, "directory") || !strcmp(name, "binarylist") || !strcmp(name, "result") ||
!strcmp(name, "collection") || !strcmp(name, "latest_added") || !strcmp(name, "latest_updated") ||
!strcmp(name, "revisionlist")) {
fb->in_dir = 1;
if (!strcmp(name, "collection"))
fb->in_collection = 1;
else if (!strcmp(name, "latest_added") || !strcmp(name, "latest_updated"))
fb->in_latest = 1;
else if (!strcmp(name, "revisionlist"))
fb->in_revisionlist = 1;
while (*atts) {
if (!strcmp(atts[0], "rev")) {
/* when working on expanded sources, we need to specify the revision when GETting
files, so we remember it here */
fb->cdir->rev = strdup(atts[1]);
DEBUG("source dir rev %s\n", fb->cdir->rev);
}
atts += 2;
}
return;
}
if (fb->in_dir && !strcmp(name, "linkinfo") && !endswith(fb->fs_path, "/" NODE_UNEXPANDED)) {
/* add an "_unexpanded" directory entry to allow access to the unmerged sources */
stat_make_dir(&st);
add_dir_node(fb->buf, fb->filler, fb->cdir, fb->fs_path, NODE_UNEXPANDED, &st, NULL, NULL);
}
/* directory entry */
if (fb->in_dir && (!strcmp(name, "entry") || !strcmp(name, "binary") || !strcmp(name, "project") || !strcmp(name, "package"))) {
const char *filename = NULL;
char *symlink = NULL;
char *hardlink = NULL;
char *relink = NULL;
stat_make_dir(&st); /* assume it's a directory until we know better */
/* process all attributes */
while (*atts) {
/* key/value filtering */
if (fb->filter_attr && !strcmp(atts[0], fb->filter_attr) && strcmp(atts[1], fb->filter_value)) {
/* entry doesn't match the filter, skip it */
filename = NULL;
break;
}
/* "name" attribute occurs in "directory" "entry"s and "collection" "project"s and "package"s */
if (!strcmp(atts[0], "name")) {
if (fb->in_collection) {
/* this is a collection, so we assume we're dealing with a package or project list for _my_p* */
if (!strcmp(name, "package")) {
if (endswith(fb->fs_path, "/_my_packages")) {
/* nothing to do, we're trying to list projects, so we wait for the "project" attribute */
}
else {
/* it's a file below _my_packages/<project>, it should symlink to the package in the /source
tree */
stat_make_symlink(&st);
filename = atts[1];
char *project = rindex(fb->fs_path, '/') + 1;
symlink = malloc(strlen("../../") + strlen(project) + 1 /* slash */ + strlen(filename) + 1);
sprintf(symlink, "../../%s/%s", project, filename);
}
}
else {
/* project list, all entries symlink to /build/<project> or /source/<project> */
filename = atts[1];
stat_make_symlink(&st);
symlink = malloc(strlen("../") + strlen(filename) + 1);
sprintf(symlink, "../%s", filename);
}
}
else if (fb->in_latest) {
filename = atts[1];
}
else {
regmatch_t matches[10];
/* entry in a "directory" directory; we assume it is itself a directory */
filename = atts[1];
if (endswith(fb->fs_path, "/" NODE_UNEXPANDED)) {
hardlink = malloc(strlen(fb->api_path) + 1 + strlen(filename) + 1);
sprintf(hardlink, "%s/%s", fb->api_path, filename);
}
else if (!regexec(&source_project_package_rev_num, fb->fs_path, 10, matches, 0)) {
/* a revision subdirectory entry; when creating this directory, we
saved the revision number already, so all we have to do here is
to hardlink to the regular source file; "&rev=..." will be added
automatically */
char *package_path = get_match(matches[1], fb->fs_path);
hardlink = malloc(strlen(package_path) + 1 + strlen(filename) + 1);
sprintf(hardlink, "%s/%s", package_path, filename);
free(package_path);
st.st_mode &= ~S_IWUSR; /* FIXME: overwritten by stat_make_*() */
}
/* Muddy waters:
- There are entries in the /published tree that don't
have a size, but are files anyway.
- Everything in /request is a file. */
if (is_a_file(fb->api_path, filename) || endswith(fb->api_path, "/request"))
stat_make_file(&st);
}
}
else if (!strcmp(atts[0], "filename")) {
filename = atts[1];
/* entry in a "binarylist" directory, this is always a regular file */
stat_make_file(&st);
}
else if (!strcmp(atts[0], "size")) {
/* file size */
st.st_size = atoi(atts[1]);
/* an entry with a size is always a regular file */
stat_make_file(&st);
}
else if (!strcmp(atts[0], "mtime")) {
st.st_mtime = atoi(atts[1]);
}
else if (!strcmp(atts[0], "project")) {
if (fb->in_latest) {
relink = malloc(strlen("../../source/") + strlen(atts[1]) + strlen("/%s") + 1);
sprintf(relink, "../../source/%s/%%s", atts[1]);
}
/* "project" attributes are exclusive to "package" entries
We are interested in this attribute when we try to make a list of projects
for the user's packages. */
else if (endswith(fb->fs_path, "/_my_packages")) {
char *full_path = malloc(strlen(fb->fs_path) + 1 /* slash */ + strlen(atts[1]) + 1);
sprintf(full_path, "%s/%s", fb->fs_path, atts[1]);
/* Only add this project if it isn't already there.
(We are processing a list of packages here, several of which can come from
the same project.) */
if (!attr_cache_find(full_path))
filename = atts[1];
free(full_path);
}
}
atts += 2; /* expat hands us a string array with name/value pairs */
}
if (filename) {
if (relink) {
/* have this entry symlink to a file with the same name in a different directory */
symlink = malloc(strlen(relink) + strlen(filename) + 1);
sprintf(symlink, relink, filename);
//DEBUG("YYYYYYYYYY relinking to %s from %s\n", symlink, fb->fs_path);
st.st_mode = S_IFLNK;
free(relink);
}
add_dir_node(fb->buf, fb->filler, fb->cdir, fb->fs_path, filename, &st, symlink, hardlink);
if (symlink)
free(symlink);
if (hardlink)
free(hardlink);
}
}
stat_default_file(&st);
/* "status" entries in "result" lists, used to build the _failed dirs */
if (fb->in_dir && !strcmp(name, "status")) {
const char *packagename = NULL;
for (; *atts; atts += 2) {
/* key/value filtering */
if (fb->filter_attr && !strcmp(atts[0], fb->filter_attr) && strcmp(atts[1], fb->filter_value)) {
packagename = NULL;
break;
}
/* package name */
if (!strcmp(atts[0], "package")) {
packagename = atts[1];
stat_make_file(&st);
}
/* FIXME: parse endtime */
}
if (packagename) {
/* hardlink to the log file in the package directory */
char *hardlink = malloc(strlen(fb->mangled_path) + strlen(packagename) + 10);
/* we could either be at build/<project>/_failed/<repo>/<arch> or at
build/<project>/<repo>/<arch>/_failed, so we use the canonical
path, which is always the latter */
strcpy(hardlink, fb->mangled_path);
*(strrchr(hardlink, '/')+1) = 0; /* strip last path element ("_failed") */
strcat(hardlink, packagename);
strcat(hardlink, "/_log");
add_dir_node(fb->buf, fb->filler, fb->cdir, fb->fs_path, packagename, &st, NULL, hardlink);
free(hardlink);
}
}
/* "revision" entries in "revisionlist" lists, used to build source revision
subdirectories */
if (fb->in_revisionlist && !strcmp(name, "revision")) {
stat_make_dir(&st);
for (; *atts; atts += 2) {
if (!strcmp(atts[0], "rev")) {
add_dir_node(fb->buf, fb->filler, fb->cdir, fb->fs_path, atts[1], &st, NULL, NULL);
}
}
}
}
/* expat tag end handler for reading API directories */
static void expat_api_dir_end(void *ud, const XML_Char *name)
{
struct filbuf *fb = (struct filbuf *)ud;
/* end of API directory */
if (!strcmp(name, "directory") || !strcmp(name, "binarylist") || !strcmp(name, "result") || !strcmp(name, "collection")) {
fb->in_dir = 0;
fb->in_collection = 0;
fb->in_revisionlist = 0;
}
}
/* expat expects an fwrite()-style callback, so we need an adapter for XML_Parse() */
static size_t write_adapter(void *ptr, size_t size, size_t nmemb, void *userdata)
{
XML_Parse((XML_Parser)userdata, ptr, size * nmemb, 0);
return size * nmemb;
}
/* initialize curl and set API user name and password, writer function and user data */
static CURL *curl_open_file(const char *url, void *read_fun, void *read_data, void *write_fun, void *write_data)
{
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_USERNAME, options.api_username);
curl_easy_setopt(curl, CURLOPT_PASSWORD, options.api_password);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_fun);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_fun);
curl_easy_setopt(curl, CURLOPT_READDATA, read_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, write_data);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookies"); /* start cookie engine */
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookies");
return curl;
}
static void parse_dir(void *buf, fuse_fill_dir_t filler, dir_t *newdir, const char *fs_path, const char *api_path,
const char *mangled_path, const char *filter_attr, const char *filter_value)
{
char *urlbuf; /* used to compose the full API URL */
CURL *curl;
CURLcode ret;
XML_Parser xp;
struct filbuf fb; /* data the expat callbacks need */
DEBUG("parsing directory %s (API %s)\n", fs_path, api_path);
xp = XML_ParserCreate(NULL); /* create an expat parser */
if (!xp)
abort();
/* copy some data that the parser callbacks will need */
memset(&fb, 0, sizeof(fb));
fb.filler = filler;
fb.buf = buf;
fb.fs_path = fs_path;
fb.api_path = api_path;
fb.mangled_path = mangled_path;
fb.cdir = newdir;
fb.filter_attr = filter_attr;
fb.filter_value = filter_value;
XML_SetUserData(xp, (void *)&fb); /* pass the data to the parser */
/* set handlers for start and end tags */
XML_SetElementHandler(xp, expat_api_dir_start, expat_api_dir_end);
/* construct the full API URL for this directory */
urlbuf = make_url(url_prefix, api_path, NULL);
/* open the URL and set up CURL options */
curl = curl_open_file(urlbuf, NULL, NULL, write_adapter, xp);
//DEBUG("username %s pw %s\n", options.api_username, options.api_password);
/* perform the actual retrieval; this will instruct curl to get the data from
the API server and call the write_adapter() for each hunk of data, which will
in turn call XML_Parse() which will funnel the invidiual components through
the start and end tag handlers expat_api_dir_start() and expat_api_dir_end() */
if ((ret = curl_easy_perform(curl))) {
fprintf(stderr,"curl error %d\n", ret);
}
/* clean up stuff */
curl_easy_cleanup(curl);
XML_ParserFree(xp);
free(urlbuf);
}
/* string appendectomy: remove "appendix" by copying the non-"appendix"
parts of "patient" to a new string and returning it */
static char *strstripcpy(char *patient, const char *appendix)
{
char *ret;
char *apploc = strstr(patient, appendix);
if (!apploc)
return NULL; /* nothing found */
ret = malloc(strlen(patient) + 1); /* the new string is not likely to be larger than the old one */
strncpy(ret, patient, apploc - patient); /* copy everything up to "appendix" */
ret[apploc - patient] = 0; /* terminate string (in case the following strcat() is a NOP */
strcat(ret + (apploc - patient), apploc + strlen(appendix)); /* copy stuff after "appendix" */
return ret;
}
/* read an API directory and fill in the FUSE directory buffer, the directory
cache, and the attribute cache */
static int get_api_dir(const char *path, void *buf, fuse_fill_dir_t filler)
{
dirent_t *cached_dirents;
int cached_dirents_size;
int mangled_path = 0;
regmatch_t matches[10];
if (filler) {
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
}
/* see if we have this directory cached already */
dir_t *dir = dir_cache_find(path);
if (dir) {
cached_dirents = dir->entries;
cached_dirents_size = dir->num_entries;
/* cache hit */
int i;
struct stat st;
/* since this dir is already cached, we are done if we don't have a filler */
if (!filler)
return 0;
/* fill the FUSE dir buffer with our cached entries */
stat_default_file(&st);
for (i = 0; i < cached_dirents_size; i++) {
if (cached_dirents[i].is_dir)
stat_make_dir(&st);
else
stat_make_file(&st);
filler(buf, cached_dirents[i].name, &st, 0);
}
}
else {
/* not in cache, we have to retrieve it from the API server */
dir_t *newdir = dir_cache_new(path); /* get directory cache handle */
char *canon_path = strdup(path);
char *fpath;
/* handle the build/<project>/_failed/... tree
This tree collects all the fail logs to make it easier to get
an overview of failing packages using, for instance, find. */
if ((fpath = strstr(canon_path, "/" NODE_FAILED))) {
char *opath = canon_path; /* original path requested */
if (!regexec(&build_project_failed_foo_bar, canon_path, 0, matches, 0)) {
/* build/<project>/_failed/<foo>/<bar> is equivalent to
build/<project>/<foo>/<bar>/_failed */
canon_path = strstripcpy(opath, "/" NODE_FAILED); /* remove "/_failed" */
free(opath);
strcat(canon_path, "/" NODE_FAILED); /* ...and add it again at the end */
mangled_path = 1;
}
else if (!regexec(&build_project_failed_foo, canon_path, 0, matches, 0) ||
!regexec(&build_project_failed, canon_path, 5, matches, 0)) {
/* build/<project>/_failed and build/<project>/_failed/<foo> are
equivalent to build/<project> and build/<project>/<foo>, respectively */
canon_path = strstripcpy(opath, "/" NODE_FAILED); /* remove the "/_failed" */
free(opath);
mangled_path = 1; /* remember that we messed with the path so we don't add
another "_failed" entry to this directory */
}
}
/* Is this the (canonical) "_failed" directory? */
if (!regexec(&build_project_repo_arch_failed, canon_path, 10, matches, 0)) {
/* dissect path to find project, repo, and architecture */
int i;
for (i = 0; matches[i].rm_so != -1; i++) {
DEBUG("REGEX match %d to %d\n", matches[i].rm_so, matches[i].rm_eo);
}
char *project = get_match(matches[1], canon_path);
char *repo = get_match(matches[2], canon_path);
char *arch = get_match(matches[3], canon_path);
DEBUG("REGEX project %s repo %s arch %s\n", project, repo, arch);
/* construct the API server path for "failed" results */
char *respath = malloc(strlen(project) + strlen(repo) + strlen(arch) + 100 /* too lazy to count right now */);
sprintf(respath, "/build/%s/_result?repository=%s&arch=%s", project, repo, arch);
/* parse only those entries that have attribute "code" with value "failed" */
parse_dir(buf, filler, newdir, path, respath, canon_path, "code", "failed");
free(respath);
free(project);
free(repo);
free(arch);
}
/* Or is it "/source/_my_{project,package}s"? */
else if (!regexec(&source_myprojectpackages, canon_path, 10, matches, 0)) {
char *projectpackage = get_match(matches[1], canon_path); /* "project" or "package" */
char *project = get_match(matches[2], canon_path); /* project name */
DEBUG("REGEX projectpackage %s project %s\n", projectpackage, project);
const char *my_p_path_format;
char *my_p_path;
if (!strcmp(projectpackage, "project") || strlen(project) == 0) {
/* /source/_my_projects or /source/_my_packages */
my_p_path_format = "/search/%s_id?match=person/@userid+=+'%s'";
my_p_path = malloc(strlen(my_p_path_format) + strlen(options.api_username) + strlen(projectpackage));
sprintf(my_p_path, my_p_path_format, projectpackage, options.api_username);
}
else {
/* /source/_my_packages/<project> */
my_p_path_format = "/search/package_id?match=person/@userid+=+'%s'+and+@project+=+'%s'";
my_p_path = malloc(strlen(my_p_path_format) + strlen(options.api_username) + strlen(projectpackage) + strlen(project));
sprintf(my_p_path, my_p_path_format, options.api_username, project + 1 /* skip leading slash */);
}
parse_dir(buf, filler, newdir, path, my_p_path, canon_path, NULL, NULL);
free(my_p_path);
free(projectpackage);
free(project);
}
/* It doesn't make sense to have a /build/_my_packages dir because the
/build tree adds the architecture level, meaning that there is more
than one directory for each package. /build/_my_projects maps fine,
though, and that's why it is handled here. */
else if (!strcmp("/build/_my_projects", canon_path)) {
const char *my_p_path_format = "/search/project_id?match=person/@userid+=+'%s'";
char *my_p_path = malloc(strlen(my_p_path_format) + strlen(options.api_username));
sprintf(my_p_path, my_p_path_format, options.api_username);
parse_dir(buf, filler, newdir, path, my_p_path, canon_path, NULL, NULL);
free(my_p_path);
}
else if (!strcmp("/statistics", canon_path)) {
struct stat st;
stat_default_dir(&st);
add_dir_node(buf, filler, newdir, path, "latest_added", &st, NULL, NULL);
add_dir_node(buf, filler, newdir, path, "latest_updated", &st, NULL, NULL);
}
else if (!regexec(&source_project_package, canon_path, 10, matches, 0)) {
/* source directories are expanded by default */
char *expandpath = malloc(strlen(canon_path) + strlen("?expand=1") + 1);
sprintf(expandpath, "%s?expand=1", canon_path);
parse_dir(buf, filler, newdir, path, expandpath, canon_path, NULL, NULL);
free(expandpath);
}
else if (!regexec(&source_project_package_unexpanded, canon_path, 10, matches, 0)) {
/* subdirectory containing unexpanded sources */
parse_dir(buf, filler, newdir, path, get_match(matches[1], canon_path), canon_path, NULL, NULL);
}
else if (!regexec(&source_project_package_rev, canon_path, 10, matches, 0)) {
/* revisions directory containg all revisions of a package's sources */
char *package_path = get_match(matches[1], canon_path);
char *revpath = malloc(strlen(package_path) + strlen("/_history"));
sprintf(revpath, "%s/_history", package_path);
parse_dir(buf, filler, newdir, path, revpath, canon_path, NULL, NULL);
free(revpath);
free(package_path);
}
else if (!regexec(&source_project_package_rev_num, canon_path, 10, matches, 0)) {
/* a specific source revision's directory */
char *package_path = get_match(matches[1], canon_path);
char *revision = get_match(matches[2], canon_path);
/* source directories are expanded by default */
char *expandpath = malloc(strlen(package_path) + strlen("?expand=1&rev=") + strlen(revision) + 1);
sprintf(expandpath, "%s?expand=1&rev=%s", package_path, revision);
parse_dir(buf, filler, newdir, path, expandpath, canon_path, NULL, NULL);
free(expandpath);
free(revision);
free(package_path);
}
else {
/* regular directory, no special handling */
parse_dir(buf, filler, newdir, path, canon_path, canon_path, NULL, NULL);
}
free(canon_path);
/* check if we need to add additional nodes */
/* Most of the available API is not exposed through directories. We have to know
about it and add them ourselves at the appropriate places. */
/* special entries for /build tree */
if (!mangled_path /* no additional nodes if we have messed with the path */
&& !strncmp("/build", path, 6)
) {
/* "_failed" directories */
if (!regexec(&build_project_repo_arch, path, 0, matches, 0) ||
!regexec(&build_project, path, 0, matches, 0)) {
/* build/<project>/<repo>/<arch>/_failed and build/<project>/_failed */
struct stat st;
stat_default_dir(&st);
add_dir_node(buf, filler, newdir, path, NODE_FAILED, &st, NULL, NULL);
}
/* log, history, status, and reason for packages */
if (regexec(&build_project_repo_arch_failed, path, 0, matches, 0)
&& !regexec(&build_project_repo_arch_foo, path, 0, matches, 0)) {
int i;
struct stat st;
stat_default_file(&st);
/* st.st_size = 4096; not sure if this is a good idea;
this entry is corrected to reflect the actual size
when the file is treated by obsfs_open() */
/* package status APIs */
for (i = 0; status_api[i]; i++) {
add_dir_node(buf, filler, newdir, path, status_api[i], &st, NULL, NULL);
}
}
}
/* "_activity", "_rating" special nodes (statistics), "_meta", "_history" */
else if (!regexec(&source_project_package, path, 3, matches, 0)) {
struct stat st;
stat_default_file(&st);
const char *sf = "/statistics/%s/%s/%s"; /* hardlink to statistics tree */
char *project = get_match(matches[1], path);
char *package = get_match(matches[2], path);
char *hardlink = malloc(strlen(sf) + strlen("activity") + strlen(project) + strlen(package));
sprintf(hardlink, sf, "activity", project, package);
add_dir_node(buf, filler, newdir, path, "_activity", &st, NULL, hardlink);
sprintf(hardlink, sf, "rating", project, package);
add_dir_node(buf, filler, newdir, path, "_rating", &st, NULL, hardlink);
free(hardlink);
add_dir_node(buf, filler, newdir, path, "_meta", &st, NULL, NULL);
add_dir_node(buf, filler, newdir, path, "_history", &st, NULL, NULL);
/* revisions subdirectory */
stat_make_dir(&st);
add_dir_node(buf, filler, newdir, path, "_rev", &st, NULL, NULL);
}
/* add _my_packages to /source and _my_packages and _my_projects to /source */
else if (!strcmp("/source", path) || !strcmp("/build", path)) {
struct stat st;
stat_default_dir(&st);
add_dir_node(buf, filler, newdir, path, "_my_projects", &st, NULL, NULL);
if (!strcmp("/source", path))
add_dir_node(buf, filler, newdir, path, "_my_packages", &st, NULL, NULL);
}
else if (!regexec(&source_project, path, 2, matches, 0)) {
/* /source/<project>/_meta */
struct stat st;
stat_default_file(&st);
const char *nn[] = {"_meta", "_config", "_pubkey", NULL};
const char **n;
for (n = nn; *n; n++) {
add_dir_node(buf, filler, newdir, path, *n, &st, NULL, NULL);
}
}
}
return 0;
}
static int obsfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
(void)offset;
(void)fi;
struct stat st;
stat_default_file(&st);
DEBUG("readdir path %s\n", path);
/* The API server does not provide us with a root directory; retrieving
"/" only yields a human-readable info page. We therefore have to
construct the root directory manually. */
if (!strcmp(path, "/") && filler && buf) {
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
st.st_mode = S_IFDIR;
const char **d;
/* fill in the root directory entries */
for(d = root_dir; *d; d++) {
filler(buf, (*d)+1 /* skip slash */, &st, 0);
}
return 0;
}
else {
/* If it's not the root directory, we get it from the API server (or dir cache). */
return get_api_dir(path, buf, filler);
}
}
/* retrieve a file, store it in our local file cache, and return a descriptor
to the local copy */
static int obsfs_open(const char *path, struct fuse_file_info *fi)
{
char *urlbuf;
CURL *curl;
FILE *fp;
CURLcode ret;
struct stat st;
const char *relpath = path + 1; /* skip leading slash */
attr_t *at = attr_cache_find(path);
/* discard unmodified cached files that have expired */
if (!lstat(relpath, &st)) {
if (at && !at->modified && (time(NULL) - st.st_mtime) > FILE_CACHE_TIMEOUT) {
DEBUG("OPEN: expiring cached file %s\n", path);
unlink(relpath);
}
}
fp = fopen(relpath, "r+");
if (!fp) {
/* create the cache file */
if (mkdirp(relpath, 0755))
return -errno;
fp = fopen(relpath, "w+");
if (!fp)
return -EIO;
/* find out if this file is supposed to hardlink somewhere */
const char *effective_path = path;
if (at && at->hardlink) {
effective_path = at->hardlink;
}
/* In an expanded source directory, all files are retrieved with a
"rev=..." parameter (the revision is stored in the dir cache
entry and is added by make_url()); this is no good for the status
APIs, particularly _history, which would only display one revision
then. We therefore have to make an exception for these nodes and
not specify a revision when retrieving them. */
const char *rev;
if (at) {
const char **s;
rev = at->rev;
for (s = status_api; *s; s++) {
if (strstr(effective_path, *s)) {
rev = NULL;
break;
}
}
}
else
rev = NULL;
/* compose the full URL */
urlbuf = make_url(url_prefix, effective_path, rev);
/* retrieve the file from the API server */
DEBUG("getting URL %s\n", urlbuf);
curl = curl_open_file(urlbuf, NULL, NULL, fwrite, fp);
ret = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (ret) {
fprintf(stderr,"curl error %d\n", ret);
}
free(urlbuf);
}
/* create a new file handle for the cache file, we need it later to retrieve
the contents */
fi->fh = dup(fileno(fp));
fclose(fp);
/* now that we have the actual size, update the stat cache; this is necessary
for the special nodes, the sizes of which we don't know when constructing
their directory entries */
if (fstat(fi->fh, &st)) {
perror("fstat");
}
attr_cache_add(path, &st, at? at->symlink : NULL, at? at->hardlink : NULL, at? at->rev : NULL);
return 0;
}
static int obsfs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
/* read from the cache file */
int ret = pread(fi->fh, buf, size, offset);
if (ret < 0)
return -errno;
else
return ret;
}
static int obsfs_write(const char *path, const char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
attr_t *at = attr_cache_find(path);
if (!at) {
DEBUG("WRITE: internal error writing to %s\n", path);
return -EIO;
}
if (!at->modified) {
at->modified = 1;
char *dn = dirname_c(path, NULL);
dir_t *dir = dir_cache_find(dn);
free(dn);
if (dir) {
dir->modified++;
}
}
if (offset + size > at->st.st_size)
at->st.st_size = offset + size;
return pwrite(fi->fh, buf, size, offset);
}
static int obsfs_truncate(const char *path, off_t offset)
{
return truncate(path + 1, offset);
}
/* Giving it a NULL pointer as the reader function doesn't deter curl from
using fwrite() anyway, so we need this expceptionally useless function. */
static size_t write_null(void *ptr, size_t size, size_t nmemb, void *userdata)
{
(void)ptr;
(void)userdata;
return size * nmemb;
}
static int obsfs_flush(const char *path, struct fuse_file_info *fi)
{
int ret;
FILE *fp;
DEBUG("FLUSH: flushing %s\n", path);
/* If the file is being flushed, we have seen it before, so it's in the attr cache. */
/* FIXME: What if it has expired there? */
attr_t *at = attr_cache_find(path);
if (!at) {
DEBUG("FLUSH: internal error flushing %s\n", path);
return -EIO;
}
/* If it has been modified, we need to write it back to the API server. */
if (at->modified) {
/* where to PUT it */
/* find out if this file is supposed to hardlink somewhere */
const char *effective_path = path;
if (at && at->hardlink) {
effective_path = at->hardlink;