catalinux / rgfs (public) (License: GPLv3) (since 2020-11-11) (hash sha1)
Allows mapping of RocketGit storage area into a local directory
List of commits:
Subject Hash Author Date (UTC)
Now we can pass parameters by env 8352d8d09714a3c2af7550ad55517bfa8590a309 Catalin(ux) M. BOIE 2020-11-12 04:48:19
Inistial commit e51779259596117c80f6e7961f4387fdd48f820a Catalin(ux) M. BOIE 2020-11-11 08:18:38
Commit 8352d8d09714a3c2af7550ad55517bfa8590a309 - Now we can pass parameters by env
Author: Catalin(ux) M. BOIE
Author date (UTC): 2020-11-12 04:48
Committer name: Catalin(ux) M. BOIE
Committer date (UTC): 2020-11-12 04:48
Parent(s): e51779259596117c80f6e7961f4387fdd48f820a
Signing key:
Tree: 0c706dde08da15a04e42c99e1d2b92c9eae170c0
File Lines added Lines deleted
rgfs.c 194 112
File rgfs.c changed (mode: 100644) (index 67e54d0..5e30c14)
8 8 #include <sys/types.h> #include <sys/types.h>
9 9 #include <sys/socket.h> #include <sys/socket.h>
10 10 #include <sys/stat.h> #include <sys/stat.h>
11 #include <sys/time.h>
11 12 #include <netdb.h> #include <netdb.h>
12 13 #include <unistd.h> #include <unistd.h>
13 14 #include <stdio.h> #include <stdio.h>
 
15 16 #include <string.h> #include <string.h>
16 17 #include <errno.h> #include <errno.h>
17 18 #include <endian.h> #include <endian.h>
19 #include <stdarg.h>
18 20
19 21 #include <fuse.h> #include <fuse.h>
20 22
21 23 #include <gnutls/gnutls.h> #include <gnutls/gnutls.h>
22 24 #include <gnutls/x509.h> #include <gnutls/x509.h>
23 25
24 #if 0
26 static char *rgfs_key = "";
25 27 static int rgfs_port = 443; static int rgfs_port = 443;
26 static const char *rgfs_server = "rocketgit.com"; // TODO: switch to rgfs.rocketgit.com
28 static char *rgfs_server = "rgfs.rocketgit.com";
27 29 static char *rgfs_url = "/rgfs"; static char *rgfs_url = "/rgfs";
28 #else
29 static int rgfs_port = 8443;
30 static const char *rgfs_server = "cloud.embedromix.ro"; // TODO: switch to rgfs.rocketgit.com
31 static char *rgfs_url = "/rgfs";
32 #endif
30 static int rgfs_debug = 0;
31 static char *rgfs_log = "rgfs.log";
32 static int rgfs_log_fd = 2;
33 33
34 34 static gnutls_session_t session; static gnutls_session_t session;
35 35 static unsigned char connected = 0; static unsigned char connected = 0;
36 36 static int sd = -1; static int sd = -1;
37 37 static gnutls_certificate_credentials_t xcred; static gnutls_certificate_credentials_t xcred;
38 38
39 static void xlog(char *format, ...)
40 {
41 va_list ap;
42 size_t len, len2;
43 char line[4096];
44 struct timeval tv;
45
46 if (rgfs_debug == 0)
47 return;
48
49 if (rgfs_log_fd == -1)
50 return;
51
52 gettimeofday(&tv, NULL);
53 len = snprintf(line, sizeof(line),
54 "%ld.%03ld ", tv.tv_sec, tv.tv_usec);
55
56 va_start(ap, format);
57 len2 = vsnprintf(line + len, sizeof(line) - len, format, ap);
58 if (len2 >= sizeof(line) - len)
59 len2 = sizeof(line);
60 else
61 len2 += len;
62 va_end(ap);
63
64 write(rgfs_log_fd, line, len2);
65 }
39 66
40 67 /* /*
41 68 * Receiving data * Receiving data
 
... ... static ssize_t rgfs_recv(void *buf, const size_t buf_max)
49 76 } while ((r == GNUTLS_E_AGAIN) || (r == GNUTLS_E_INTERRUPTED)); } while ((r == GNUTLS_E_AGAIN) || (r == GNUTLS_E_INTERRUPTED));
50 77
51 78 if (r < 0) if (r < 0)
52 fprintf(stderr, "Cannot receive [%zd]!\n", r);
79 xlog("Cannot receive [%zd]!\n", r);
53 80
54 81 return r; return r;
55 82 } }
 
... ... static ssize_t rgfs_send(const void *buf, size_t buf_len)
66 93 } while ((r == GNUTLS_E_AGAIN) || (r == GNUTLS_E_INTERRUPTED)); } while ((r == GNUTLS_E_AGAIN) || (r == GNUTLS_E_INTERRUPTED));
67 94
68 95 if (r < 0) { if (r < 0) {
69 fprintf(stderr, "Cannot send [%zd]!\n", r);
96 xlog("Cannot send [%zd]!\n", r);
70 97 exit(EXIT_FAILURE); exit(EXIT_FAILURE);
71 98 } else if ((size_t) r < buf_len) { } else if ((size_t) r < buf_len) {
72 fprintf(stderr, "Invalid send r=%ld < buf_len=%zu!\n", r, buf_len);
99 xlog("Invalid send r=%ld < buf_len=%zu!\n", r, buf_len);
73 100 exit(EXIT_FAILURE); exit(EXIT_FAILURE);
74 101 } }
75 102
 
... ... static int rgfs_connect(void)
93 120 hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
94 121 r = getaddrinfo(rgfs_server, port, &hints, &result); r = getaddrinfo(rgfs_server, port, &hints, &result);
95 122 if (r != 0) { if (r != 0) {
96 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(r));
123 xlog("getaddrinfo: %s\n", gai_strerror(r));
97 124 return -1; return -1;
98 125 } }
99 126
 
... ... static int rgfs_connect(void)
110 137 ip, sizeof(ip), service, sizeof(service), ip, sizeof(ip), service, sizeof(service),
111 138 NI_NUMERICHOST | NI_NUMERICSERV); NI_NUMERICHOST | NI_NUMERICSERV);
112 139 if (r == 0) if (r == 0)
113 fprintf(stderr, "Connected to %s/%s!\n", ip, service);
140 xlog("Connected to %s/%s!\n", ip, service);
114 141 break; break;
115 142 } }
116 143
117 fprintf(stderr, "connect error: %m\n");
144 xlog("connect error: %m\n");
118 145 } }
119 146 freeaddrinfo(result); freeaddrinfo(result);
120 147 if (rp == NULL) { if (rp == NULL) {
121 fprintf(stderr, "Could not connect!\n");
148 xlog("Could not connect!\n");
122 149 return -1; return -1;
123 150 } }
124 151
 
... ... static ssize_t rgfs_ws1(void)
143 170 "\r\n", "\r\n",
144 171 rgfs_url, rgfs_server, rgfs_port); rgfs_url, rgfs_server, rgfs_port);
145 172
146 //fprintf(stderr, "Sending:\n%s", out);
173 //xlog("Sending:\n%s", out);
147 174 return rgfs_send(out, len); return rgfs_send(out, len);
148 175 } }
149 176
177 /*
178 * Returns 0 if ok, 1 if we should abort the program else => retry.
179 */
150 180 static int rgfs_tls(void) static int rgfs_tls(void)
151 181 { {
152 182 int r, ret = -1, off; int r, ret = -1, off;
153 183 char buf[8192], *desc; char buf[8192], *desc;
154 184 int type; int type;
155 unsigned status;
185 unsigned status, len;
156 186
157 187 while (1) { while (1) {
158 188 /* Destroy session */ /* Destroy session */
159 fprintf(stderr, "session=%p sd=%d\n", session, sd);
189 xlog("session=%p sd=%d\n", session, sd);
160 190 if (session) { if (session) {
161 191 r = gnutls_bye(session, GNUTLS_SHUT_RDWR); r = gnutls_bye(session, GNUTLS_SHUT_RDWR);
162 192 if (r != GNUTLS_E_SUCCESS) if (r != GNUTLS_E_SUCCESS)
163 fprintf(stderr, "gnutls error: cannot say goodbye!\n");
193 xlog("gnutls error: cannot say goodbye [%d]!\n", r);
164 194 } }
165 195
166 196 if (sd != -1) if (sd != -1)
 
... ... static int rgfs_tls(void)
171 201 /* Initialize TLS session */ /* Initialize TLS session */
172 202 r = gnutls_init(&session, GNUTLS_CLIENT); r = gnutls_init(&session, GNUTLS_CLIENT);
173 203 if (r != GNUTLS_E_SUCCESS) { if (r != GNUTLS_E_SUCCESS) {
174 fprintf(stderr, "gnutls error: cannot init session!\n");
204 xlog("gnutls error: cannot init session!\n");
175 205 break; break;
176 206 } }
177 207
178 208 r = gnutls_server_name_set(session, GNUTLS_NAME_DNS, r = gnutls_server_name_set(session, GNUTLS_NAME_DNS,
179 209 rgfs_server, strlen(rgfs_server)); rgfs_server, strlen(rgfs_server));
180 210 if (r != GNUTLS_E_SUCCESS) { if (r != GNUTLS_E_SUCCESS) {
181 fprintf(stderr, "gnutls error: cannot set name [%s]!\n", rgfs_server);
211 xlog("gnutls error: cannot set name [%s]!\n", rgfs_server);
182 212 break; break;
183 213 } }
184 214
185 215 /* It is recommended to use the default priorities */ /* It is recommended to use the default priorities */
186 216 r = gnutls_set_default_priority(session); r = gnutls_set_default_priority(session);
187 217 if (r != GNUTLS_E_SUCCESS) { if (r != GNUTLS_E_SUCCESS) {
188 fprintf(stderr, "gnutls error: cannot set default priority!\n");
218 xlog("gnutls error: cannot set default priority!\n");
189 219 break; break;
190 220 } }
191 221
192 222 /* put the x509 credentials to the current session */ /* put the x509 credentials to the current session */
193 223 r = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred); r = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
194 224 if (r != GNUTLS_E_SUCCESS) { if (r != GNUTLS_E_SUCCESS) {
195 fprintf(stderr, "gnutls error: cannot set credentials!\n");
225 xlog("gnutls error: cannot set credentials!\n");
196 226 break; break;
197 227 } }
198 228 gnutls_session_set_verify_cert(session, rgfs_server, 0); gnutls_session_set_verify_cert(session, rgfs_server, 0);
 
... ... static int rgfs_tls(void)
218 248 status = gnutls_session_get_verify_cert_status(session); status = gnutls_session_get_verify_cert_status(session);
219 249 r = gnutls_certificate_verification_status_print(status, type, &out, 0); r = gnutls_certificate_verification_status_print(status, type, &out, 0);
220 250 if (r == GNUTLS_E_SUCCESS) { if (r == GNUTLS_E_SUCCESS) {
221 fprintf(stderr, "cert verify output: %s\n", out.data);
251 xlog("cert verify output: %s\n", out.data);
222 252 gnutls_free(out.data); gnutls_free(out.data);
223 253 } }
224 254 } }
225 fprintf(stderr, "Handshake failed: %s\n", gnutls_strerror(r));
255 xlog("Handshake failed: %s\n", gnutls_strerror(r));
226 256 break; break;
227 257 } }
228 258
229 259 desc = gnutls_session_get_desc(session); desc = gnutls_session_get_desc(session);
230 fprintf(stderr, "Session info: %s\n", desc);
260 xlog("Session info: %s\n", desc);
231 261 gnutls_free(desc); gnutls_free(desc);
232 262
233 263 r = rgfs_ws1(); r = rgfs_ws1();
234 264 if (r == -1) if (r == -1)
235 265 break; break;
236 //fprintf(stderr, "ws1 returned %d.\n", r);
266 //xlog("ws1 returned %d.\n", r);
237 267
238 268 off = 0; off = 0;
239 269 while (1) { while (1) {
 
... ... static int rgfs_tls(void)
242 272 break; break;
243 273
244 274 buf[off + r] = '\0'; buf[off + r] = '\0';
245 //fprintf(stderr, "Received[%d + %d]:\n%s\n", off, r, buf);
275 //xlog("Received[%d + %d]:\n%s\n", off, r, buf);
246 276 if (strstr(buf, "\r\n\r\n")) if (strstr(buf, "\r\n\r\n"))
247 277 break; break;
248 278 } }
249 279 if (r == -1) if (r == -1)
250 280 break; break;
251 281 if (strncmp(buf, "HTTP/1.1 101 Switching Protocols", 32) != 0) { if (strncmp(buf, "HTTP/1.1 101 Switching Protocols", 32) != 0) {
252 fprintf(stderr, "Invalid HTTP answer:\n%s\n", buf);
282 xlog("Invalid HTTP answer:\n%s\n", buf);
253 283 break; break;
254 284 } }
255 285
256 fprintf(stderr, "Sending version...\n");
286 xlog("Sending version...\n");
257 287 buf[0] = 0x01; // type=SEND_VERSION buf[0] = 0x01; // type=SEND_VERSION
258 buf[1] = 0; // len
259 buf[2] = 1; // len
288 buf[1] = 0; // len H
289 buf[2] = 1; // len L
260 290 buf[3] = RGFS_PROTOCOL_VERSION; buf[3] = RGFS_PROTOCOL_VERSION;
261 291 r = rgfs_send(buf, 4); r = rgfs_send(buf, 4);
262 292 if (r <= 0) if (r <= 0)
263 293 break; break;
264 294
265 fprintf(stderr, "Receiving version...\n");
295 xlog("Receiving version...\n");
296 r = rgfs_recv(buf, sizeof(buf));
297 if (r <= 0)
298 break;
299 xlog("Server version: %hhu.\n", buf[4]);
300
301 xlog("Sending key...\n");
302 len = strlen(rgfs_key);
303 buf[0] = 0xFF; // type=SEND_KEY
304 buf[1] = 0; // len H
305 buf[2] = len; // len L
306 memcpy(buf + 3, rgfs_key, len);
307 r = rgfs_send(buf, 3 + len);
308 if (r <= 0)
309 break;
310
311 xlog("Receiving send_key confirmation...\n");
266 312 r = rgfs_recv(buf, sizeof(buf)); r = rgfs_recv(buf, sizeof(buf));
267 313 if (r <= 0) if (r <= 0)
268 314 break; break;
269 fprintf(stderr, "Server version: %hhu.\n", buf[4]);
315 if ((r < 5) || (buf[4] != 0x00)) {
316 ret = 1;
317 break;
318 }
270 319
271 320 ret = 0; ret = 0;
272 321 break; break;
 
... ... static void rgfs_reconnect(void)
280 329 int r; int r;
281 330
282 331 while (1) { while (1) {
283 fprintf(stderr, "Reconnecting...\n");
332 xlog("Reconnecting...\n");
284 333 r = rgfs_tls(); r = rgfs_tls();
285 334 if (r == 0) { if (r == 0) {
286 fprintf(stderr, " Reconnecting OK!\n");
335 xlog("Reconnecting OK!\n");
287 336 connected = 1; connected = 1;
288 337 break; break;
289 338 } }
339 if (r == 1) {
340 xlog("Exiting!");
341 exit(1);
342 }
290 343
291 fprintf(stderr, "Sleeping...\n");
344 xlog("Sleeping...\n");
292 345 sleep(1); sleep(1);
293 346 } }
294 347 } }
 
... ... static int rgfs_recv_hl(void *buf, size_t buf_size)
300 353
301 354 off = 0; off = 0;
302 355 while (1) { while (1) {
303 //fprintf(stderr, " Waiting for data (off=%d to_read=%u)\n", off, to_read);
356 //xlog(" Waiting for data (off=%d to_read=%u)\n", off, to_read);
304 357 if (off == buf_size) { if (off == buf_size) {
305 fprintf(stderr, "Buffer is too small!\n");
358 xlog("Buffer is too small!\n");
306 359 exit(EXIT_FAILURE); exit(EXIT_FAILURE);
307 360 } }
308 361 r = rgfs_recv(buf + off, buf_size - off); r = rgfs_recv(buf + off, buf_size - off);
 
... ... static int rgfs_recv_hl(void *buf, size_t buf_size)
314 367 to_read = be32toh(*(uint32_t *) buf); to_read = be32toh(*(uint32_t *) buf);
315 368
316 369 #if 0 #if 0
317 fprintf(stderr, " RECV: ");
370 xlog(" RECV: ");
318 371 for (unsigned int i = 0; i < off; i++) for (unsigned int i = 0; i < off; i++)
319 fprintf(stderr, "%02hhx", * (unsigned char *) (buf + i));
320 fprintf(stderr, "\n");
372 xlog("%02hhx", * (unsigned char *) (buf + i));
373 xlog("\n");
321 374 #endif #endif
322 375
323 376 if (off == 4 + to_read) { if (off == 4 + to_read) {
324 //fprintf(stderr, " Readed 4 + %u\n", to_read);
377 //xlog(" Readed 4 + %u\n", to_read);
325 378 return off; return off;
326 379 } }
327 380 } }
 
... ... static ssize_t rgfs_send_recv(const void *out_buf, size_t out_buf_len,
336 389 rgfs_reconnect(); rgfs_reconnect();
337 390
338 391 while (1) { while (1) {
339 //fprintf(stderr, " Sending %zu bytes\n", out_buf_len);
392 //xlog(" Sending %zu bytes\n", out_buf_len);
340 393 r = rgfs_send(out_buf, out_buf_len); r = rgfs_send(out_buf, out_buf_len);
341 394 if (r <= 0) { if (r <= 0) {
342 395 rgfs_reconnect(); rgfs_reconnect();
 
... ... static int rgfs_fuse_getattr(const char *path, struct stat *s)
362 415 unsigned char buf[4 * 4096], cmd; unsigned char buf[4 * 4096], cmd;
363 416 uint16_t path_len, len; uint16_t path_len, len;
364 417
365 //fprintf(stderr, "getattr: path=%s\n", path);
418 //xlog("getattr: path=%s\n", path);
366 419
367 420 path_len = strlen(path); path_len = strlen(path);
368 421 len = 1 + 2 + path_len; len = 1 + 2 + path_len;
369 422 if (len > sizeof(buf)) { if (len > sizeof(buf)) {
370 fprintf(stderr, " buffer too small\n");
423 xlog(" buffer too small\n");
371 424 return -EIO; return -EIO;
372 425 } }
373 426
 
... ... static int rgfs_fuse_getattr(const char *path, struct stat *s)
382 435 i = 4; i = 4;
383 436 while (i < r) { while (i < r) {
384 437 cmd = buf[i++]; cmd = buf[i++];
385 //fprintf(stderr, " cmd=0x%02hhx\n", cmd);
438 //xlog(" cmd=0x%02hhx\n", cmd);
386 439 switch (cmd) { switch (cmd) {
387 440 case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break;
388 441 case 0x01: s->st_mode = be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x01: s->st_mode = be32toh(*(uint32_t *) (buf + i)); i += 4; break;
 
... ... static int rgfs_fuse_getattr(const char *path, struct stat *s)
391 444 case 0x04: s->st_atime = be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x04: s->st_atime = be32toh(*(uint32_t *) (buf + i)); i += 4; break;
392 445 case 0x05: s->st_mtime = be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x05: s->st_mtime = be32toh(*(uint32_t *) (buf + i)); i += 4; break;
393 446 case 0x06: s->st_ctime = be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x06: s->st_ctime = be32toh(*(uint32_t *) (buf + i)); i += 4; break;
394 default: fprintf(stderr, "Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
447 default: xlog("Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
395 448 } }
396 449 } }
397 450
398 451 if (err != 0) { if (err != 0) {
399 //fprintf(stderr, " server returned error %d!\n", err);
452 //xlog(" server returned error %d!\n", err);
400 453 return err; return err;
401 454 } }
402 455
403 //fprintf(stderr, " mode=%o nlink=%lu size=%lu\n",
456 //xlog(" mode=%o nlink=%lu size=%lu\n",
404 457 // s->st_mode, s->st_nlink, s->st_size); // s->st_mode, s->st_nlink, s->st_size);
405 458 return 0; return 0;
406 459 } }
 
... ... static int rgfs_fuse_readdir(const char *dir, void *out,
416 469
417 470 (void) offset; (void) offset;
418 471 (void) fi; (void) fi;
419 //fprintf(stderr, "readdir: dir=%s offset=%zd\n", dir, offset);
472 //xlog("readdir: dir=%s offset=%zd\n", dir, offset);
420 473
421 474 path_len = strlen(dir); path_len = strlen(dir);
422 475 len = 1 + 2 + 8 + path_len; len = 1 + 2 + 8 + path_len;
423 476 if (len > sizeof(buf)) { if (len > sizeof(buf)) {
424 fprintf(stderr, " buffer too small\n");
477 xlog(" buffer too small\n");
425 478 return -EIO; return -EIO;
426 479 } }
427 480
 
... ... static int rgfs_fuse_readdir(const char *dir, void *out,
440 493 i = 4; i = 4;
441 494 while (i < r) { while (i < r) {
442 495 cmd = buf[i++]; cmd = buf[i++];
443 //fprintf(stderr, " cmd=0x%02hhx\n", cmd);
496 //xlog(" cmd=0x%02hhx\n", cmd);
444 497 switch (cmd) { switch (cmd) {
445 498 case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break;
446 499 case 0x01: /* entry */ case 0x01: /* entry */
447 500 u16 = be16toh(*(uint16_t *) (buf + i)); i += 2; u16 = be16toh(*(uint16_t *) (buf + i)); i += 2;
448 501 if (u16 > sizeof(path) - 1) { if (u16 > sizeof(path) - 1) {
449 fprintf(stderr, "path len bigger than buf!\n");
502 xlog("path len bigger than buf!\n");
450 503 i += u16; i += u16;
451 504 break; break;
452 505 } }
453 506 memcpy(path, buf + i, u16); i += u16; memcpy(path, buf + i, u16); i += u16;
454 507 path[u16] = '\0'; path[u16] = '\0';
455 //fprintf(stderr, " received path %s\n", path);
508 //xlog(" received path %s\n", path);
456 509 ret = filler(out, path, NULL, 0); ret = filler(out, path, NULL, 0);
457 510 if (ret != 0) if (ret != 0)
458 fprintf(stderr, " FILLTER RETURNS 1!\n");
511 xlog(" FILLTER RETURNS 1!\n");
459 512 break; break;
460 default: fprintf(stderr, "Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
513 default: xlog("Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
461 514 } }
462 515 } }
463 516
464 517 if (err != 0) { if (err != 0) {
465 //fprintf(stderr, " server returned error %d!\n", err);
518 //xlog(" server returned error %d!\n", err);
466 519 return err; return err;
467 520 } }
468 521
 
... ... static int rgfs_fuse_open(const char *path, struct fuse_file_info *fi)
473 526 { {
474 527 (void) path; (void) path;
475 528 (void) fi; (void) fi;
476 //fprintf(stderr, "open: path=%s\n", path);
529 //xlog("open: path=%s\n", path);
477 530
478 531 //if ((fi->flags & O_ACCMODE) != O_RDONLY) //if ((fi->flags & O_ACCMODE) != O_RDONLY)
479 532 // return -EACCES; // return -EACCES;
 
... ... static int rgfs_fuse_create(const char *path, mode_t mode,
490 543
491 544 (void) mode; (void) mode;
492 545 (void) fi; (void) fi;
493 //fprintf(stderr, "create: path=%s\n", path);
546 //xlog("create: path=%s\n", path);
494 547
495 548 path_len = strlen(path); path_len = strlen(path);
496 549 len = 1 + 2 + 2 + path_len; len = 1 + 2 + 2 + path_len;
497 550 if (len > sizeof(buf)) { if (len > sizeof(buf)) {
498 fprintf(stderr, " buffer too small\n");
551 xlog(" buffer too small\n");
499 552 return -EIO; return -EIO;
500 553 } }
501 554
 
... ... static int rgfs_fuse_create(const char *path, mode_t mode,
510 563 i = 4; i = 4;
511 564 while (i < r) { while (i < r) {
512 565 cmd = buf[i++]; cmd = buf[i++];
513 //fprintf(stderr, " cmd=0x%02hhx\n", cmd);
566 //xlog(" cmd=0x%02hhx\n", cmd);
514 567 switch (cmd) { switch (cmd) {
515 568 case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break;
516 default: fprintf(stderr, "Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
569 default: xlog("Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
517 570 } }
518 571 } }
519 572
520 573 if (err != 0) { if (err != 0) {
521 //fprintf(stderr, " server returned error %d!\n", err);
574 //xlog(" server returned error %d!\n", err);
522 575 return err; return err;
523 576 } }
524 577
 
... ... static int rgfs_fuse_mkdir(const char *path, mode_t mode)
532 585 unsigned char buf[4 * 4096], cmd; unsigned char buf[4 * 4096], cmd;
533 586
534 587 (void) mode; (void) mode;
535 //fprintf(stderr, "mkdir: path=%s\n", path);
588 //xlog("mkdir: path=%s\n", path);
536 589
537 590 path_len = strlen(path); path_len = strlen(path);
538 591 len = 1 + 2 + 2 + path_len; len = 1 + 2 + 2 + path_len;
539 592 if (len > sizeof(buf)) { if (len > sizeof(buf)) {
540 fprintf(stderr, " buffer too small\n");
593 xlog(" buffer too small\n");
541 594 return -EIO; return -EIO;
542 595 } }
543 596
 
... ... static int rgfs_fuse_mkdir(const char *path, mode_t mode)
552 605 i = 4; i = 4;
553 606 while (i < r) { while (i < r) {
554 607 cmd = buf[i++]; cmd = buf[i++];
555 //fprintf(stderr, " cmd=0x%02hhx\n", cmd);
608 //xlog(" cmd=0x%02hhx\n", cmd);
556 609 switch (cmd) { switch (cmd) {
557 610 case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break;
558 default: fprintf(stderr, "Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
611 default: xlog("Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
559 612 } }
560 613 } }
561 614
562 615 if (err != 0) { if (err != 0) {
563 //fprintf(stderr, " server returned error %d!\n", err);
616 //xlog(" server returned error %d!\n", err);
564 617 return err; return err;
565 618 } }
566 619
 
... ... static int rgfs_fuse_unlink(const char *path)
573 626 uint16_t u16, path_len, len; uint16_t u16, path_len, len;
574 627 unsigned char buf[4 * 4096], cmd; unsigned char buf[4 * 4096], cmd;
575 628
576 //fprintf(stderr, "unlink: path=%s\n", path);
629 //xlog("unlink: path=%s\n", path);
577 630
578 631 path_len = strlen(path); path_len = strlen(path);
579 632 len = 1 + 2 + 2 + path_len; len = 1 + 2 + 2 + path_len;
580 633 if (len > sizeof(buf)) { if (len > sizeof(buf)) {
581 fprintf(stderr, " buffer too small\n");
634 xlog(" buffer too small\n");
582 635 return -EIO; return -EIO;
583 636 } }
584 637
 
... ... static int rgfs_fuse_unlink(const char *path)
593 646 i = 4; i = 4;
594 647 while (i < r) { while (i < r) {
595 648 cmd = buf[i++]; cmd = buf[i++];
596 //fprintf(stderr, " cmd=0x%02hhx\n", cmd);
649 //xlog(" cmd=0x%02hhx\n", cmd);
597 650 switch (cmd) { switch (cmd) {
598 651 case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break;
599 default: fprintf(stderr, "Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
652 default: xlog("Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
600 653 } }
601 654 } }
602 655
603 656 if (err != 0) { if (err != 0) {
604 //fprintf(stderr, " server returned error %d!\n", err);
657 //xlog(" server returned error %d!\n", err);
605 658 return err; return err;
606 659 } }
607 660
 
... ... static int rgfs_fuse_rmdir(const char *path)
614 667 uint16_t u16, path_len, len; uint16_t u16, path_len, len;
615 668 unsigned char buf[4096], cmd; unsigned char buf[4096], cmd;
616 669
617 //fprintf(stderr, "rmdir: path=%s\n", path);
670 //xlog("rmdir: path=%s\n", path);
618 671
619 672 path_len = strlen(path); path_len = strlen(path);
620 673 len = 1 + 2 + 2 + path_len; len = 1 + 2 + 2 + path_len;
621 674 if (len > sizeof(buf)) { if (len > sizeof(buf)) {
622 fprintf(stderr, " buffer too small\n");
675 xlog(" buffer too small\n");
623 676 return -EIO; return -EIO;
624 677 } }
625 678
 
... ... static int rgfs_fuse_rmdir(const char *path)
634 687 i = 4; i = 4;
635 688 while (i < r) { while (i < r) {
636 689 cmd = buf[i++]; cmd = buf[i++];
637 //fprintf(stderr, " cmd=0x%02hhx\n", cmd);
690 //xlog(" cmd=0x%02hhx\n", cmd);
638 691 switch (cmd) { switch (cmd) {
639 692 case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break;
640 default: fprintf(stderr, "Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
693 default: xlog("Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
641 694 } }
642 695 } }
643 696
644 697 if (err != 0) { if (err != 0) {
645 //fprintf(stderr, " server returned error %d!\n", err);
698 //xlog(" server returned error %d!\n", err);
646 699 return err; return err;
647 700 } }
648 701
 
... ... static int rgfs_fuse_truncate(const char *path, off_t off)
656 709 uint64_t u64; uint64_t u64;
657 710 unsigned char buf[4096], cmd; unsigned char buf[4096], cmd;
658 711
659 //fprintf(stderr, "truncate: path=%s off=%ld\n", path, off);
712 //xlog("truncate: path=%s off=%ld\n", path, off);
660 713
661 714 path_len = strlen(path); path_len = strlen(path);
662 715 len = 1 + 2 + 2 + 8 + path_len; len = 1 + 2 + 2 + 8 + path_len;
663 716 if (len > sizeof(buf)) { if (len > sizeof(buf)) {
664 fprintf(stderr, " buffer too small\n");
717 xlog(" buffer too small\n");
665 718 return -EIO; return -EIO;
666 719 } }
667 720
 
... ... static int rgfs_fuse_truncate(const char *path, off_t off)
677 730 i = 4; i = 4;
678 731 while (i < r) { while (i < r) {
679 732 cmd = buf[i++]; cmd = buf[i++];
680 fprintf(stderr, " cmd=0x%02hhx\n", cmd);
733 xlog(" cmd=0x%02hhx\n", cmd);
681 734 switch (cmd) { switch (cmd) {
682 735 case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break;
683 default: fprintf(stderr, "Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
736 default: xlog("Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
684 737 } }
685 738 } }
686 739
687 740 if (err != 0) { if (err != 0) {
688 //fprintf(stderr, " server returned error %d!\n", err);
741 //xlog(" server returned error %d!\n", err);
689 742 return err; return err;
690 743 } }
691 744
 
... ... static int rgfs_fuse_read(const char *path, char *out, size_t size,
701 754 uint64_t u64; uint64_t u64;
702 755
703 756 (void) fi; (void) fi;
704 //fprintf(stderr, "read: path=%s size=%zu offset=%zd\n",
757 //xlog("read: path=%s size=%zu offset=%zd\n",
705 758 // path, size, offset); // path, size, offset);
706 759
707 760 if (size > sizeof(buf) - 1 - 4 - 1 - 8) if (size > sizeof(buf) - 1 - 4 - 1 - 8)
 
... ... static int rgfs_fuse_read(const char *path, char *out, size_t size,
710 763 path_len = strlen(path); path_len = strlen(path);
711 764 len = 1 + 2 + 8 + 8 + path_len; len = 1 + 2 + 8 + 8 + path_len;
712 765 if (len > sizeof(buf)) { if (len > sizeof(buf)) {
713 fprintf(stderr, " buffer too small\n");
766 xlog(" buffer too small\n");
714 767 return -EIO; return -EIO;
715 768 } }
716 769
 
... ... static int rgfs_fuse_read(const char *path, char *out, size_t size,
726 779 i = 4; i = 4;
727 780 while (i < r) { while (i < r) {
728 781 cmd = buf[i++]; cmd = buf[i++];
729 //fprintf(stderr, " cmd=0x%02hhx\n", cmd);
782 //xlog(" cmd=0x%02hhx\n", cmd);
730 783 switch (cmd) { switch (cmd) {
731 784 case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break;
732 785 case 0x01: /* block */ case 0x01: /* block */
733 786 u64 = be64toh(*(uint64_t *) (buf + i)); i += 8; u64 = be64toh(*(uint64_t *) (buf + i)); i += 8;
734 //fprintf(stderr, " received %lu bytes block\n", u64);
787 //xlog(" received %lu bytes block\n", u64);
735 788 if (i + u64 > (unsigned int) r) { if (i + u64 > (unsigned int) r) {
736 fprintf(stderr, " i=%d + u64=%lu > r=%d\n",
789 xlog(" i=%d + u64=%lu > r=%d\n",
737 790 i, u64, r); i, u64, r);
738 791 i += u64; i += u64;
739 792 break; break;
740 793 } }
741 794 memcpy(out, buf + i, u64); i += u64; memcpy(out, buf + i, u64); i += u64;
742 795 break; break;
743 default: fprintf(stderr, "Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
796 default: xlog("Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
744 797 } }
745 798 } }
746 799
747 800 if (err != 0) { if (err != 0) {
748 //fprintf(stderr, " server returned error %d!\n", err);
801 //xlog(" server returned error %d!\n", err);
749 802 return err; return err;
750 803 } }
751 804
 
... ... static int rgfs_fuse_write(const char *path, const char *in, size_t size,
763 816 (void) fi; (void) fi;
764 817
765 818 #if 0 #if 0
766 fprintf(stderr, "write: path=%s size=%zu offset=%zd: ", path, size, offset);
819 xlog("write: path=%s size=%zu offset=%zd: ", path, size, offset);
767 820 for (unsigned int j = 0; j < 32; j++) for (unsigned int j = 0; j < 32; j++)
768 fprintf(stderr, "%02hhx", in[j]);
769 fprintf(stderr, "\n");
821 xlog("%02hhx", in[j]);
822 xlog("\n");
770 823 #endif #endif
771 824
772 825 path_len = strlen(path); path_len = strlen(path);
773 826 len = 1 + 2 + 2 + 8 + path_len + size; len = 1 + 2 + 2 + 8 + path_len + size;
774 827 if (len > sizeof(buf)) { if (len > sizeof(buf)) {
775 fprintf(stderr, " buffer too small\n");
828 xlog(" buffer too small\n");
776 829 return -EIO; return -EIO;
777 830 } }
778 831
 
... ... static int rgfs_fuse_write(const char *path, const char *in, size_t size,
789 842 i = 4; i = 4;
790 843 while (i < r) { while (i < r) {
791 844 cmd = buf[i++]; cmd = buf[i++];
792 //fprintf(stderr, " cmd=0x%02hhx\n", cmd);
845 //xlog(" cmd=0x%02hhx\n", cmd);
793 846 switch (cmd) { switch (cmd) {
794 847 case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break;
795 848 case 0x01: /* block */ case 0x01: /* block */
796 849 u64 = be64toh(*(uint64_t *) (buf + i)); i += 8; u64 = be64toh(*(uint64_t *) (buf + i)); i += 8;
797 850 if (u64 != size) if (u64 != size)
798 fprintf(stderr, " wrote only %lu bytes\n", u64);
851 xlog(" wrote only %lu bytes\n", u64);
799 852 break; break;
800 default: fprintf(stderr, "Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
853 default: xlog("Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
801 854 } }
802 855 } }
803 856
804 857 if (err != 0) { if (err != 0) {
805 //fprintf(stderr, " server returned error %d!\n", err);
858 //xlog(" server returned error %d!\n", err);
806 859 return err; return err;
807 860 } }
808 861
 
... ... static int rgfs_fuse_rename(const char *old, const char *new)
815 868 unsigned char buf[4 * 4096], cmd; unsigned char buf[4 * 4096], cmd;
816 869 uint16_t u16, old_len, new_len, len; uint16_t u16, old_len, new_len, len;
817 870
818 //fprintf(stderr, "rename: old=%s new=%s\n", old, new);
871 //xlog("rename: old=%s new=%s\n", old, new);
819 872
820 873 old_len = strlen(old); old_len = strlen(old);
821 874 new_len = strlen(new); new_len = strlen(new);
822 875 len = 1 + 2 + 2 + 2 + old_len + new_len; len = 1 + 2 + 2 + 2 + old_len + new_len;
823 876 if (len > sizeof(buf)) { if (len > sizeof(buf)) {
824 fprintf(stderr, " buffer too small\n");
877 xlog(" buffer too small\n");
825 878 return -EIO; return -EIO;
826 879 } }
827 880
 
... ... static int rgfs_fuse_rename(const char *old, const char *new)
838 891 i = 4; i = 4;
839 892 while (i < r) { while (i < r) {
840 893 cmd = buf[i++]; cmd = buf[i++];
841 //fprintf(stderr, " cmd=0x%02hhx\n", cmd);
894 //xlog(" cmd=0x%02hhx\n", cmd);
842 895 switch (cmd) { switch (cmd) {
843 896 case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break; case 0x00: err = - be32toh(*(uint32_t *) (buf + i)); i += 4; break;
844 default: fprintf(stderr, "Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
897 default: xlog("Unexpected subcode 0x%02hhx!\n", cmd); exit(EXIT_FAILURE);
845 898 } }
846 899 } }
847 900
848 901 if (err != 0) { if (err != 0) {
849 //fprintf(stderr, " server returned error %d!\n", err);
902 //xlog(" server returned error %d!\n", err);
850 903 return err; return err;
851 904 } }
852 905
 
... ... static const struct fuse_operations fuse_rgfs = {
870 923 int main(int argc, char *argv[]) int main(int argc, char *argv[])
871 924 { {
872 925 int r; int r;
926 char *s;
927
928 s = getenv("RGFS_KEY");
929 if (s)
930 rgfs_key = s;
931
932 s = getenv("RGFS_DEBUG");
933 if (s)
934 rgfs_debug = atoi(s);
935
936 s = getenv("RGFS_LOG");
937 if (s)
938 rgfs_log = s;
939 rgfs_log_fd = open(rgfs_log, O_CREAT | O_WRONLY | O_APPEND, 0600);
940
941 s = getenv("RGFS_PORT");
942 if (s)
943 rgfs_port = atoi(s);
944
945 s = getenv("RGFS_SERVER");
946 if (s)
947 rgfs_server = s;
948
949 s = getenv("RGFS_URL");
950 if (s)
951 rgfs_url = s;
952
953 xlog("server=%s port=%d url=%s\n",
954 rgfs_server, rgfs_port, rgfs_url);
873 955
874 956 if (gnutls_check_version("3.4.6") == NULL) { if (gnutls_check_version("3.4.6") == NULL) {
875 fprintf(stderr, "GnuTLS 3.4.6 or later is required!\n");
957 xlog("GnuTLS 3.4.6 or later is required!\n");
876 958 exit(EXIT_FAILURE); exit(EXIT_FAILURE);
877 959 } }
878 960
879 961 /* for backwards compatibility with gnutls < 3.3.0 */ /* for backwards compatibility with gnutls < 3.3.0 */
880 962 r = gnutls_global_init(); r = gnutls_global_init();
881 963 if (r != GNUTLS_E_SUCCESS) { if (r != GNUTLS_E_SUCCESS) {
882 fprintf(stderr, "gnutls error: cannot init!\n");
964 xlog("gnutls error: cannot init!\n");
883 965 exit(EXIT_FAILURE); exit(EXIT_FAILURE);
884 966 } }
885 967
886 968 /* X509 stuff */ /* X509 stuff */
887 969 r = gnutls_certificate_allocate_credentials(&xcred); r = gnutls_certificate_allocate_credentials(&xcred);
888 970 if (r != GNUTLS_E_SUCCESS) { if (r != GNUTLS_E_SUCCESS) {
889 fprintf(stderr, "gnutls error: cannot allocate credentials!\n");
971 xlog("gnutls error: cannot allocate credentials!\n");
890 972 exit(EXIT_FAILURE); exit(EXIT_FAILURE);
891 973 } }
892 974
893 975 /* sets the system trusted CAs for Internet PKI */ /* sets the system trusted CAs for Internet PKI */
894 976 r = gnutls_certificate_set_x509_system_trust(xcred); r = gnutls_certificate_set_x509_system_trust(xcred);
895 977 if (r < 0) { if (r < 0) {
896 fprintf(stderr, "gnutls error: cannot set system trust [%d]!\n", r);
978 xlog("gnutls error: cannot set system trust [%d]!\n", r);
897 979 exit(EXIT_FAILURE); exit(EXIT_FAILURE);
898 980 } }
899 981
900 982 /* If client holds a certificate it can be set using the following: /* If client holds a certificate it can be set using the following:
901 983 gnutls_certificate_set_x509_key_file (xcred, "cert.pem", "key.pem", gnutls_certificate_set_x509_key_file (xcred, "cert.pem", "key.pem",
902 GNUTLS_X509_FMT_PEM); */
984 GNUTLS_X509_FMT_PEM);
985 */
903 986
904 fprintf(stderr, "Running fuse...\n");
987 xlog("Running fuse...\n");
905 988 return fuse_main(argc, argv, &fuse_rgfs, NULL); return fuse_main(argc, argv, &fuse_rgfs, NULL);
906 989
907 990 #if 0 #if 0
908 991 if (r < 0 && gnutls_error_is_fatal(r) == 0) { if (r < 0 && gnutls_error_is_fatal(r) == 0) {
909 fprintf(stderr, "Warning: %s\n", gnutls_strerror(r));
992 xlog("Warning: %s\n", gnutls_strerror(r));
910 993 } else if (r < 0) { } else if (r < 0) {
911 fprintf(stderr, "Error: %s\n", gnutls_strerror(r));
994 xlog("Error: %s\n", gnutls_strerror(r));
912 995 exit(EXIT_FAILURE); exit(EXIT_FAILURE);
913 996 } }
914 997 #endif #endif
915
916 998 } }
917 999
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/catalinux/rgfs

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/catalinux/rgfs

Clone this repository using git:
git clone git://git.rocketgit.com/user/catalinux/rgfs

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main