-
Notifications
You must be signed in to change notification settings - Fork 13
/
ftpfs.c
1885 lines (1577 loc) · 55.4 KB
/
ftpfs.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
/*
FTP file system
Copyright (C) 2006 Robson Braga Araujo <[email protected]>
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
*/
#include "config.h"
#include <stdlib.h>
#include <bsd/readpassphrase.h>
#include <stdio.h>
#include <ctype.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <netinet/in.h>
#include <fuse.h>
#include <fuse_opt.h>
#include <glib.h>
#include <semaphore.h>
#include <assert.h>
#include "charset_utils.h"
#include "path_utils.h"
#include "ftpfs-ls.h"
#include "cache.h"
#include "ftpfs.h"
#define CURLFTPFS_BAD_NOBODY 0x070f02
#define CURLFTPFS_BAD_SSL 0x070f03
#define CURLFTPFS_BAD_READ ((size_t)-1)
#define MAX_BUFFER_LEN (300*1024)
struct ftpfs ftpfs;
static char error_buf[CURL_ERROR_SIZE];
struct buffer {
uint8_t* p;
size_t len;
size_t size;
off_t begin_offset;
};
static void usage(const char* progname);
static void buf_init(struct buffer* buf)
{
buf->p = NULL;
buf->begin_offset = 0;
buf->len = 0;
buf->size = 0;
}
static inline void buf_free(struct buffer* buf)
{
free(buf->p);
}
static inline void buf_clear(struct buffer *buf)
{
buf_free(buf);
buf_init(buf);
}
static int buf_resize(struct buffer *buf, size_t len)
{
buf->size = (buf->len + len + 63) & ~31;
buf->p = (uint8_t *) realloc(buf->p, buf->size);
if (!buf->p) {
fprintf(stderr, "ftpfs: memory allocation failed\n");
return -1;
}
return 0;
}
static int buf_add_mem(struct buffer *buf, const void *data, size_t len)
{
if (buf->len + len > buf->size && buf_resize(buf, len) == -1)
return -1;
memcpy(buf->p + buf->len, data, len);
buf->len += len;
return 0;
}
static void buf_null_terminate(struct buffer *buf)
{
if (buf_add_mem(buf, "\0", 1) == -1)
exit(1);
}
struct ftpfs_file {
struct buffer buf;
int dirty;
int copied;
off_t last_offset;
int can_shrink;
pthread_t thread_id;
mode_t mode;
char * open_path;
char * full_path;
struct buffer stream_buf;
CURL *write_conn;
sem_t data_avail;
sem_t data_need;
sem_t data_written;
sem_t ready;
int isready;
int eof;
int written_flag;
int write_fail_cause;
int write_may_start;
char curl_error_buffer[CURL_ERROR_SIZE];
off_t pos;
};
enum {
KEY_HELP,
KEY_VERBOSE,
KEY_VERSION,
};
#define FTPFS_OPT(t, p, v) { t, offsetof(struct ftpfs, p), v }
static struct fuse_opt ftpfs_opts[] = {
FTPFS_OPT("ftpfs_debug=%u", debug, 0),
FTPFS_OPT("transform_symlinks", transform_symlinks, 1),
FTPFS_OPT("disable_epsv", disable_epsv, 1),
FTPFS_OPT("enable_epsv", disable_epsv, 0),
FTPFS_OPT("skip_pasv_ip", skip_pasv_ip, 1),
FTPFS_OPT("ftp_port=%s", ftp_port, 0),
FTPFS_OPT("disable_eprt", disable_eprt, 1),
FTPFS_OPT("ftp_method=%s", ftp_method, 0),
FTPFS_OPT("custom_list=%s", custom_list, 0),
FTPFS_OPT("tcp_nodelay", tcp_nodelay, 1),
FTPFS_OPT("connect_timeout=%u", connect_timeout, 0),
FTPFS_OPT("ssl", use_ssl, CURLFTPSSL_ALL),
FTPFS_OPT("ssl_control", use_ssl, CURLFTPSSL_CONTROL),
FTPFS_OPT("ssl_try", use_ssl, CURLFTPSSL_TRY),
FTPFS_OPT("no_verify_hostname", no_verify_hostname, 1),
FTPFS_OPT("no_verify_peer", no_verify_peer, 1),
FTPFS_OPT("cert=%s", cert, 0),
FTPFS_OPT("cert_type=%s", cert_type, 0),
FTPFS_OPT("key=%s", key, 0),
FTPFS_OPT("key_type=%s", key_type, 0),
FTPFS_OPT("pass=%s", key_password, 0),
FTPFS_OPT("engine=%s", engine, 0),
FTPFS_OPT("cacert=%s", cacert, 0),
FTPFS_OPT("capath=%s", capath, 0),
FTPFS_OPT("ciphers=%s", ciphers, 0),
FTPFS_OPT("interface=%s", interface, 0),
FTPFS_OPT("krb4=%s", krb4, 0),
FTPFS_OPT("proxy=%s", proxy, 0),
FTPFS_OPT("proxytunnel", proxytunnel, 1),
FTPFS_OPT("proxy_anyauth", proxyanyauth, 1),
FTPFS_OPT("proxy_basic", proxybasic, 1),
FTPFS_OPT("proxy_digest", proxydigest, 1),
FTPFS_OPT("proxy_ntlm", proxyntlm, 1),
FTPFS_OPT("httpproxy", proxytype, CURLPROXY_HTTP),
FTPFS_OPT("socks4", proxytype, CURLPROXY_SOCKS4),
FTPFS_OPT("socks5", proxytype, CURLPROXY_SOCKS5),
FTPFS_OPT("user=%s", user, 0),
FTPFS_OPT("proxy_user=%s", proxy_user, 0),
FTPFS_OPT("tlsv1", ssl_version, CURL_SSLVERSION_TLSv1),
FTPFS_OPT("sslv3", ssl_version, CURL_SSLVERSION_SSLv3),
FTPFS_OPT("ipv4", ip_version, CURL_IPRESOLVE_V4),
FTPFS_OPT("ipv6", ip_version, CURL_IPRESOLVE_V6),
FTPFS_OPT("utf8", tryutf8, 1),
FTPFS_OPT("codepage=%s", codepage, 0),
FTPFS_OPT("iocharset=%s", iocharset, 0),
FTPFS_OPT("nomulticonn", multiconn, 0),
FUSE_OPT_KEY("-h", KEY_HELP),
FUSE_OPT_KEY("--help", KEY_HELP),
FUSE_OPT_KEY("-v", KEY_VERBOSE),
FUSE_OPT_KEY("--verbose", KEY_VERBOSE),
FUSE_OPT_KEY("-V", KEY_VERSION),
FUSE_OPT_KEY("--version", KEY_VERSION),
FUSE_OPT_END
};
static struct ftpfs_file *get_ftpfs_file(struct fuse_file_info *fi)
{
return (struct ftpfs_file *) (uintptr_t) fi->fh;
}
static void cancel_previous_multi()
{
//curl_multi_cleanup(ftpfs.multi);
if (!ftpfs.attached_to_multi) return;
DEBUG(1, "cancel previous multi\n");
CURLMcode curlMCode = curl_multi_remove_handle(ftpfs.multi, ftpfs.connection);
if (curlMCode != CURLE_OK)
{
fprintf(stderr, "curl_multi_remove_handle problem: %d\n", curlMCode);
exit(1);
}
ftpfs.attached_to_multi = 0;
}
//Code from stackoverflow
char* replace(
char const * const original,
char const * const pattern,
char const * const replacement
){
size_t const replen = strlen(replacement);
size_t const patlen = strlen(pattern);
size_t const orilen = strlen(original);
size_t patcnt = 0;
const char * oriptr;
const char * patloc;
// find how many times the pattern occurs in the original string
for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen)
patcnt++;
{
// allocate memory for the new string
size_t const retlen = orilen + patcnt * (replen - patlen);
char * const returned = (char *) malloc( sizeof(char) * (retlen + 1) );
if (returned != NULL){
// copy the original string,
// replacing all the instances of the pattern
char * retptr = returned;
for (oriptr = original; patloc = strstr(oriptr, pattern); oriptr = patloc + patlen){
size_t const skplen = patloc - oriptr;
// copy the section until the occurence of the pattern
strncpy(retptr, oriptr, skplen);
retptr += skplen;
// copy the replacement
strncpy(retptr, replacement, replen);
retptr += replen;
}
// copy the rest of the string.
strcpy(retptr, oriptr);
}
return returned;
}
}
char* urlencode(char const * const original){
//Always process % first
char* tmp_percent = replace(original, "%", "%25");
char* tmpsharp = replace(tmp_percent, "#", "%23");
free(tmp_percent);
char* tmpspace = replace(tmpsharp, " ", "%20");
free(tmpsharp);
return(tmpspace);
}
static int op_return(int err, char * operation)
{
if(!err)
{
DEBUG(2, "%s successful\n", operation);
return 0;
}
fprintf(stderr, "ftpfs: operation %s failed because %s\n", operation, strerror(-err));
return err;
}
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *data) {
struct ftpfs_file* fh = (struct ftpfs_file*)data;
if (fh == NULL) return 0;
size_t to_copy = size * nmemb;
if (to_copy > fh->buf.len - fh->copied) {
to_copy = fh->buf.len - fh->copied;
}
DEBUG(2, "write_data: %zu\n", to_copy);
DEBUG(3, "%*s\n", (int)to_copy, (char*)ptr);
memcpy(ptr, fh->buf.p + fh->copied, to_copy);
fh->copied += to_copy;
return to_copy;
}
static size_t read_data(void *ptr, size_t size, size_t nmemb, void *data) {
struct buffer* buf = (struct buffer*)data;
if (buf == NULL) return size * nmemb;
if (buf_add_mem(buf, ptr, size * nmemb) == -1)
return 0;
DEBUG(2, "read_data: %zu\n", size * nmemb);
DEBUG(3, "%*s\n", (int)(size * nmemb), (char*)ptr);
return size * nmemb;
}
#define curl_easy_setopt_or_die(handle, option, ...) \
do {\
CURLcode res = curl_easy_setopt(handle, option, __VA_ARGS__);\
if (res != CURLE_OK) {\
fprintf(stderr, "Error setting curl: %s\n", error_buf);\
exit(1);\
}\
}while(0)
static int ftpfs_getdir(const char* path_tmp, fuse_cache_dirh_t h,
fuse_cache_dirfil_t filler) {
char * const path = urlencode(path_tmp);
int err = 0;
CURLcode curl_res;
char* dir_path = get_fulldir_path(path);
DEBUG(1, "ftpfs_getdir: %s\n", dir_path);
struct buffer buf;
buf_init(&buf);
pthread_mutex_lock(&ftpfs.lock);
cancel_previous_multi();
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_URL, dir_path);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_WRITEDATA, &buf);
curl_res = curl_easy_perform(ftpfs.connection);
pthread_mutex_unlock(&ftpfs.lock);
if (curl_res != 0) {
DEBUG(1, "%s\n", error_buf);
err = -EIO;
} else {
buf_null_terminate(&buf);
parse_dir((char*)buf.p, dir_path + strlen(ftpfs.host) - 1,
NULL, NULL, NULL, 0, h, filler);
}
free(dir_path);
buf_free(&buf);
free(path);
return op_return(err, "ftpfs_getdir");
}
static int ftpfs_getattr(const char* path_tmp, struct stat* sbuf) {
char * const path = urlencode(path_tmp);
int err;
CURLcode curl_res;
char* dir_path = get_dir_path(path);
DEBUG(2, "ftpfs_getattr: %s dir_path=%s\n", path, dir_path);
struct buffer buf;
buf_init(&buf);
pthread_mutex_lock(&ftpfs.lock);
cancel_previous_multi();
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_URL, dir_path);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_WRITEDATA, &buf);
curl_res = curl_easy_perform(ftpfs.connection);
pthread_mutex_unlock(&ftpfs.lock);
if (curl_res != 0) {
DEBUG(1, "%s\n", error_buf);
}
buf_null_terminate(&buf);
char* name = strrchr(path_tmp, '/');
++name;
err = parse_dir((char*)buf.p, dir_path + strlen(ftpfs.host) - 1,
name, sbuf, NULL, 0, NULL, NULL);
free(dir_path);
buf_free(&buf);
free(path);
if (err) return op_return(-ENOENT, "ftpfs_getattr");
return 0;
}
static int check_running() {
int running_handles = 0;
curl_multi_perform(ftpfs.multi, &running_handles);
return running_handles;
}
static size_t ftpfs_read_chunk(const char* full_path, char* rbuf,
size_t size, off_t offset,
struct fuse_file_info* fi,
int update_offset) {
int running_handles = 0;
int err = 0;
struct ftpfs_file* fh = get_ftpfs_file(fi);
DEBUG(2, "ftpfs_read_chunk: %s %p %zu %lld %p %p\n",
full_path, rbuf, size, offset, fi, fh);
pthread_mutex_lock(&ftpfs.lock);
DEBUG(2, "buffer size: %zu %lld\n", fh->buf.len, fh->buf.begin_offset);
if ((fh->buf.len < size + offset - fh->buf.begin_offset) ||
offset < fh->buf.begin_offset ||
offset > fh->buf.begin_offset + fh->buf.len) {
// We can't answer this from cache
if (ftpfs.current_fh != fh ||
offset < fh->buf.begin_offset ||
offset > fh->buf.begin_offset + fh->buf.len ||
!check_running()) {
DEBUG(1, "We need to restart the connection %p\n", ftpfs.connection);
DEBUG(2, "current_fh=%p fh=%p\n", ftpfs.current_fh, fh);
DEBUG(2, "buf.begin_offset=%lld offset=%lld\n", fh->buf.begin_offset, offset);
buf_clear(&fh->buf);
fh->buf.begin_offset = offset;
ftpfs.current_fh = fh;
cancel_previous_multi();
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_URL, full_path);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_WRITEDATA, &fh->buf);
if (offset) {
char range[15];
snprintf(range, 15, "%lld-", (long long) offset);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_RANGE, range);
}
CURLMcode curlMCode = curl_multi_add_handle(ftpfs.multi, ftpfs.connection);
if (curlMCode != CURLE_OK)
{
fprintf(stderr, "curl_multi_add_handle problem: %d\n", curlMCode);
exit(1);
}
ftpfs.attached_to_multi = 1;
}
while(CURLM_CALL_MULTI_PERFORM ==
curl_multi_perform(ftpfs.multi, &running_handles));
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_RANGE, NULL);
while ((fh->buf.len < size + offset - fh->buf.begin_offset) &&
running_handles) {
struct timeval timeout;
int rc; /* select() return code */
fd_set fdread;
fd_set fdwrite;
fd_set fdexcep;
int maxfd;
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
/* set a suitable timeout to play around with */
timeout.tv_sec = 1;
timeout.tv_usec = 0;
/* get file descriptors from the transfers */
curl_multi_fdset(ftpfs.multi, &fdread, &fdwrite, &fdexcep, &maxfd);
rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
if (rc == -1) {
err = 1;
break;
}
while(CURLM_CALL_MULTI_PERFORM ==
curl_multi_perform(ftpfs.multi, &running_handles));
}
if (running_handles == 0) {
int msgs_left = 1;
while (msgs_left) {
CURLMsg* msg = curl_multi_info_read(ftpfs.multi, &msgs_left);
if (msg == NULL ||
msg->msg != CURLMSG_DONE ||
msg->data.result != CURLE_OK) {
DEBUG(1, "error: curl_multi_info %d\n", msg->msg);
err = 1;
}
}
}
}
size_t to_copy = fh->buf.len + fh->buf.begin_offset - offset;
size = size > to_copy ? to_copy : size;
if (rbuf) {
memcpy(rbuf, fh->buf.p + offset - fh->buf.begin_offset, size);
}
if (update_offset) {
fh->last_offset = offset + size;
}
// Check if the buffer is growing and we can delete a part of it
if (fh->can_shrink && fh->buf.len > MAX_BUFFER_LEN) {
DEBUG(2, "Shrinking buffer from %zu to %zu bytes\n",
fh->buf.len, to_copy - size);
memmove(fh->buf.p,
fh->buf.p + offset - fh->buf.begin_offset + size,
to_copy - size);
fh->buf.len = to_copy - size;
fh->buf.begin_offset = offset + size;
}
pthread_mutex_unlock(&ftpfs.lock);
if (err) return CURLFTPFS_BAD_READ;
return size;
}
static void set_common_curl_stuff(CURL* easy);
static size_t write_data_bg(void *ptr, size_t size, size_t nmemb, void *data) {
struct ftpfs_file *fh = data;
unsigned to_copy = size * nmemb;
if (!fh->isready) {
sem_post(&fh->ready);
fh->isready = 1;
}
if (fh->stream_buf.len == 0 && fh->written_flag) {
sem_post(&fh->data_written); /* ftpfs_write can return */
}
sem_wait(&fh->data_avail);
DEBUG(2, "write_data_bg: data_avail eof=%d\n", fh->eof);
if (fh->eof)
return 0;
DEBUG(2, "write_data_bg: %d %zd\n", to_copy, fh->stream_buf.len);
if (to_copy > fh->stream_buf.len)
to_copy = fh->stream_buf.len;
memcpy(ptr, fh->stream_buf.p, to_copy);
if (fh->stream_buf.len > to_copy) {
size_t newlen = fh->stream_buf.len - to_copy;
memmove(fh->stream_buf.p, fh->stream_buf.p + to_copy, newlen);
fh->stream_buf.len = newlen;
sem_post(&fh->data_avail);
DEBUG(2, "write_data_bg: data_avail\n");
} else {
fh->stream_buf.len = 0;
fh->written_flag = 1;
sem_post(&fh->data_need);
DEBUG(2, "write_data_bg: data_need\n");
}
return to_copy;
}
int write_thread_ctr = 0;
static void *ftpfs_write_thread(void *data) {
struct ftpfs_file *fh = data;
char range[15];
DEBUG(2, "enter streaming write thread #%d path=%s pos=%lld\n", ++write_thread_ctr, fh->full_path, fh->pos);
curl_easy_setopt_or_die(fh->write_conn, CURLOPT_URL, fh->full_path);
curl_easy_setopt_or_die(fh->write_conn, CURLOPT_UPLOAD, 1);
curl_easy_setopt_or_die(fh->write_conn, CURLOPT_READFUNCTION, write_data_bg);
curl_easy_setopt_or_die(fh->write_conn, CURLOPT_READDATA, fh);
curl_easy_setopt_or_die(fh->write_conn, CURLOPT_LOW_SPEED_LIMIT, 1);
curl_easy_setopt_or_die(fh->write_conn, CURLOPT_LOW_SPEED_TIME, 60);
fh->curl_error_buffer[0] = '\0';
curl_easy_setopt_or_die(fh->write_conn, CURLOPT_ERRORBUFFER, fh->curl_error_buffer);
if (fh->pos > 0) {
/* resuming a streaming write */
//snprintf(range, 15, "%lld-", (long long) fh->pos);
//curl_easy_setopt_or_die(fh->write_conn, CURLOPT_RANGE, range);
curl_easy_setopt_or_die(fh->write_conn, CURLOPT_APPEND, 1);
//curl_easy_setopt_or_die(fh->write_conn, CURLOPT_RESUME_FROM_LARGE, (curl_off_t)fh->pos);
}
CURLcode curl_res = curl_easy_perform(fh->write_conn);
curl_easy_setopt_or_die(fh->write_conn, CURLOPT_UPLOAD, 0);
if (!fh->isready)
sem_post(&fh->ready);
if (curl_res != CURLE_OK)
{
DEBUG(1, "write problem: %d(%s) text=%s\n", curl_res, curl_easy_strerror(curl_res), fh->curl_error_buffer);
fh->write_fail_cause = curl_res;
/* problem - let ftpfs_write continue to avoid hang */
sem_post(&fh->data_need);
}
DEBUG(2, "leaving streaming write thread #%d curl_res=%d\n", write_thread_ctr--, curl_res);
sem_post(&fh->data_written); /* ftpfs_write may return */
return NULL;
}
/* returns 1 on success, 0 on failure */
static int start_write_thread(struct ftpfs_file *fh)
{
if (fh->write_conn != NULL)
{
fprintf(stderr, "assert fh->write_conn == NULL failed!\n");
exit(1);
}
fh->written_flag=0;
fh->isready=0;
fh->eof=0;
sem_init(&fh->data_avail, 0, 0);
sem_init(&fh->data_need, 0, 0);
sem_init(&fh->data_written, 0, 0);
sem_init(&fh->ready, 0, 0);
fh->write_conn = curl_easy_init();
if (fh->write_conn == NULL) {
fprintf(stderr, "Error initializing libcurl\n");
return 0;
} else {
int err;
set_common_curl_stuff(fh->write_conn);
err = pthread_create(&fh->thread_id, NULL, ftpfs_write_thread, fh);
if (err) {
fprintf(stderr, "failed to create thread: %s\n", strerror(err));
/* FIXME: destroy curl_easy */
return 0;
}
}
return 1;
}
static int finish_write_thread(struct ftpfs_file *fh)
{
if (fh->write_fail_cause == CURLE_OK)
{
sem_wait(&fh->data_need); /* only wait when there has been no error */
}
sem_post(&fh->data_avail);
fh->eof = 1;
pthread_join(fh->thread_id, NULL);
DEBUG(2, "finish_write_thread after pthread_join. write_fail_cause=%d\n", fh->write_fail_cause);
curl_easy_cleanup(fh->write_conn);
fh->write_conn = NULL;
sem_destroy(&fh->data_avail);
sem_destroy(&fh->data_need);
sem_destroy(&fh->data_written);
sem_destroy(&fh->ready);
if (fh->write_fail_cause != CURLE_OK)
{
return -EIO;
}
return 0;
}
static void free_ftpfs_file(struct ftpfs_file *fh) {
if (fh->write_conn)
curl_easy_cleanup(fh->write_conn);
g_free(fh->full_path);
g_free(fh->open_path);
sem_destroy(&fh->data_avail);
sem_destroy(&fh->data_need);
sem_destroy(&fh->data_written);
sem_destroy(&fh->ready);
if (fh->buf.size) { buf_free(&fh->buf); }
if (fh->stream_buf.size) { buf_free(&fh->stream_buf); }
free(fh);
}
static int buffer_file(struct ftpfs_file *fh) {
// If we want to write to the file, we have to load it all at once,
// modify it in memory and then upload it as a whole as most FTP servers
// don't support resume for uploads.
pthread_mutex_lock(&ftpfs.lock);
cancel_previous_multi();
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_URL, fh->full_path);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_WRITEDATA, &fh->buf);
CURLcode curl_res = curl_easy_perform(ftpfs.connection);
pthread_mutex_unlock(&ftpfs.lock);
if (curl_res != 0) {
return -EACCES;
}
return 0;
}
static int create_empty_file(const char * path)
{
int err = 0;
char *full_path = get_full_path(path);
pthread_mutex_lock(&ftpfs.lock);
cancel_previous_multi();
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_URL, full_path);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_INFILESIZE, 0);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_UPLOAD, 1);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_READDATA, NULL);
CURLcode curl_res = curl_easy_perform(ftpfs.connection);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_UPLOAD, 0);
pthread_mutex_unlock(&ftpfs.lock);
if (curl_res != 0) {
err = -EPERM;
}
free(full_path);
return err;
}
static int ftpfs_mknod(const char* path, mode_t mode, dev_t rdev);
static int ftpfs_chmod(const char* path, mode_t mode);
static char * flags_to_string(int flags)
{
char * access_mode_str = NULL;
if ((flags & O_ACCMODE) == O_WRONLY)
access_mode_str = "O_WRONLY";
else if ((flags & O_ACCMODE) == O_RDWR)
access_mode_str = "O_RDWR";
else if ((flags & O_ACCMODE) == O_RDONLY)
access_mode_str = "O_RDONLY";
return g_strdup_printf("access_mode=%s, flags=%s%s%s%s",
access_mode_str,
(flags & O_CREAT) ? "O_CREAT " : "",
(flags & O_TRUNC) ? "O_TRUNC " : "",
(flags & O_EXCL) ? "O_EXCL " : "",
(flags & O_APPEND) ? "O_APPEND " : "");
}
static int test_exists(const char* path)
{
struct stat sbuf;
return ftpfs_getattr(path, &sbuf);
}
static __off_t test_size(const char* path)
{
struct stat sbuf;
int err = ftpfs_getattr(path, &sbuf);
if (err)
return err;
return sbuf.st_size;
}
static int ftpfs_open_common(const char* path_tmp, mode_t mode,
struct fuse_file_info* fi) {
char * const path = urlencode(path_tmp);
char * flagsAsStr = flags_to_string(fi->flags);
DEBUG(2, "ftpfs_open_common: %s\n", flagsAsStr);
int err = 0;
struct ftpfs_file* fh =
(struct ftpfs_file*) malloc(sizeof(struct ftpfs_file));
memset(fh, 0, sizeof(*fh));
buf_init(&fh->buf);
fh->mode = mode;
fh->dirty = 0;
fh->copied = 0;
fh->last_offset = 0;
fh->can_shrink = 0;
buf_init(&fh->stream_buf);
/* sem_init(&fh->data_avail, 0, 0);
sem_init(&fh->data_need, 0, 0);
sem_init(&fh->data_written, 0, 0);
sem_init(&fh->ready, 0, 0); */
fh->open_path = strdup(path);
fh->full_path = get_full_path(path);
fh->written_flag = 0;
fh->write_fail_cause = CURLE_OK;
fh->curl_error_buffer[0] = '\0';
fh->write_may_start = 0;
fi->fh = (unsigned long) fh;
if ((fi->flags & O_ACCMODE) == O_RDONLY) {
if (fi->flags & O_CREAT) {
err = ftpfs_mknod(path, (mode & 07777) | S_IFREG, 0);
} else {
// If it's read-only, we can load the file a bit at a time, as necessary.
DEBUG(1, "opening %s O_RDONLY\n", path);
fh->can_shrink = 1;
size_t size = ftpfs_read_chunk(fh->full_path, NULL, 1, 0, fi, 0);
if (size == CURLFTPFS_BAD_READ) {
DEBUG(1, "initial read failed size=%d\n", size);
err = -EACCES;
}
}
}
else if ((fi->flags & O_ACCMODE) == O_RDWR || (fi->flags & O_ACCMODE) == O_WRONLY)
{
#ifndef CURLFTPFS_O_RW_WORKAROUND
if ((fi->flags & O_ACCMODE) == O_RDWR)
{
err = -ENOTSUP;
goto fin;
}
#endif
if ((fi->flags & O_APPEND))
{
DEBUG(1, "opening %s with O_APPEND - not supported!\n", path);
err = -ENOTSUP;
}
if ((fi->flags & O_EXCL))
{
DEBUG(1, "opening %s with O_EXCL - testing existence\n", path);
int exists_r = test_exists(path);
if (exists_r != -ENOENT)
err = -EACCES;
}
if (!err)
{
if ((fi->flags & O_CREAT) || (fi->flags & O_TRUNC))
{
DEBUG(1, "opening %s for writing with O_CREAT or O_TRUNC. write thread will start now\n", path);
fh->write_may_start=1;
if (start_write_thread(fh))
{
sem_wait(&fh->ready);
/* chmod makes only sense on O_CREAT */
if (fi->flags & O_CREAT) ftpfs_chmod(path, mode);
sem_post(&fh->data_need);
}
else
{
err = -EIO;
}
}
else
{
/* in this case we have to start writing later */
DEBUG(1, "opening %s for writing without O_CREAT or O_TRUNC. write thread will start after ftruncate\n", path);
/* expecting ftruncate */
fh->write_may_start=0;
}
}
} else {
err = -EIO;
}
fin:
if (err)
free_ftpfs_file(fh);
free(path);
g_free(flagsAsStr);
return op_return(err, "ftpfs_open");
}
static int ftpfs_open(const char* path, struct fuse_file_info* fi) {
return ftpfs_open_common(path, 0, fi);
}
#if FUSE_VERSION >= 25
static int ftpfs_create(const char* path, mode_t mode,
struct fuse_file_info* fi) {
return ftpfs_open_common(path, mode, fi);
}
#endif
static int ftpfs_read(const char* path_tmp, char* rbuf, size_t size, off_t offset,
struct fuse_file_info* fi) {
char * const path = urlencode(path_tmp);
int ret;
struct ftpfs_file *fh = get_ftpfs_file(fi);
DEBUG(1, "ftpfs_read: %s size=%zu offset=%lld has_write_conn=%d pos=%lld\n", path, size, (long long) offset, fh->write_conn!=0, fh->pos);
if (fh->pos>0 || fh->write_conn!=NULL)
{
fprintf(stderr, "in read/write mode we cannot read from a file that has already been written to\n");
return op_return(-EIO, "ftpfs_read");
}
char *full_path = get_full_path(path);
size_t size_read = ftpfs_read_chunk(full_path, rbuf, size, offset, fi, 1);
free(full_path);
if (size_read == CURLFTPFS_BAD_READ) {
ret = -EIO;
} else {
ret = size_read;
}
free(path);
if (ret<0) op_return(ret, "ftpfs_read");
return ret;
}
static int ftpfs_mknod(const char* path, mode_t mode, dev_t rdev) {
(void) rdev;
int err = 0;
DEBUG(1, "ftpfs_mknode: mode=%d\n", (int)mode);
if ((mode & S_IFMT) != S_IFREG)
return -EPERM;
err = create_empty_file(path);
if (!err)
ftpfs_chmod(path, mode);
return op_return(err, "ftpfs_mknod");
}
static int ftpfs_chmod(const char* path, mode_t mode) {
int err = 0;
// We can only process a subset of the mode - so strip
// to supported subset
int mode_c = mode - (mode / 0x1000 * 0x1000);
struct curl_slist* header = NULL;
char* full_path = get_dir_path(path);
char* filename = get_file_name(path);
char* cmd = g_strdup_printf("SITE CHMOD %.3o %s", mode_c, filename);
struct buffer buf;
buf_init(&buf);
header = curl_slist_append(header, cmd);
pthread_mutex_lock(&ftpfs.lock);
cancel_previous_multi();
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_POSTQUOTE, header);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_URL, full_path);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_WRITEDATA, &buf);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_NOBODY, ftpfs.safe_nobody);
CURLcode curl_res = curl_easy_perform(ftpfs.connection);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_POSTQUOTE, NULL);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_NOBODY, 0);
pthread_mutex_unlock(&ftpfs.lock);
if (curl_res != 0) {
err = -EPERM;
}
buf_free(&buf);
curl_slist_free_all(header);
free(full_path);
free(filename);
free(cmd);
return op_return(err, "ftpfs_chmod");
}
static int ftpfs_chown(const char* path, uid_t uid, gid_t gid) {
int err = 0;
DEBUG(1, "ftpfs_chown: %d %d\n", (int)uid, (int)gid);
struct curl_slist* header = NULL;
char* full_path = get_dir_path(path);
char* filename = get_file_name(path);
char* cmd = g_strdup_printf("SITE CHUID %i %s", uid, filename);
char* cmd2 = g_strdup_printf("SITE CHGID %i %s", gid, filename);
struct buffer buf;
buf_init(&buf);
header = curl_slist_append(header, cmd);
header = curl_slist_append(header, cmd2);
pthread_mutex_lock(&ftpfs.lock);
cancel_previous_multi();
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_POSTQUOTE, header);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_URL, full_path);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_WRITEDATA, &buf);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_NOBODY, ftpfs.safe_nobody);
CURLcode curl_res = curl_easy_perform(ftpfs.connection);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_POSTQUOTE, NULL);
curl_easy_setopt_or_die(ftpfs.connection, CURLOPT_NOBODY, 0);
pthread_mutex_unlock(&ftpfs.lock);
if (curl_res != 0) {
err = -EPERM;
}
buf_free(&buf);
curl_slist_free_all(header);