This source file includes following definitions.
- _vfs_translate_path
- vfs_get_openfile
- vfs_test_current_dir
- vfs_free_handle
- vfs_class_find_by_handle
- vfs_new_handle
- vfs_ferrno
- vfs_register_class
- vfs_unregister_class
- vfs_strip_suffix_from_filename
- vfs_translate_path
- vfs_translate_path_n
- vfs_get_current_dir
- vfs_get_current_dir_n
- vfs_get_raw_current_dir
- vfs_set_raw_current_dir
- vfs_current_is_local
- vfs_file_class_flags
- vfs_init
- vfs_setup_work_dir
- vfs_shut
- vfs_dirent_init
- vfs_dirent_assign
- vfs_dirent_free
- vfs_fill_names
- vfs_file_is_local
- vfs_print_message
- vfs_setup_cwd
- vfs_get_cwd
- vfs_preallocate
- vfs_clone_file
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 #include <config.h>
45
46 #include <errno.h>
47 #include <stdlib.h>
48
49 #ifdef __linux__
50 #ifdef HAVE_LINUX_FS_H
51 #include <linux/fs.h>
52 #endif
53 #ifdef HAVE_SYS_IOCTL_H
54 #include <sys/ioctl.h>
55 #endif
56 #endif
57
58 #include "lib/global.h"
59 #include "lib/strutil.h"
60 #include "lib/util.h"
61 #include "lib/widget.h"
62 #include "lib/event.h"
63 #include "lib/charsets.h"
64
65 #include "vfs.h"
66 #include "utilvfs.h"
67 #include "gc.h"
68
69
70 extern struct vfs_dirent *mc_readdir_result;
71 extern GPtrArray *vfs__classes_list;
72 extern GString *vfs_str_buffer;
73 extern vfs_class *current_vfs;
74
75
76
77 MC_MOCKABLE struct vfs_dirent *mc_readdir_result = NULL;
78 MC_MOCKABLE GPtrArray *vfs__classes_list = NULL;
79 MC_MOCKABLE GString *vfs_str_buffer = NULL;
80 MC_MOCKABLE vfs_class *current_vfs = NULL;
81
82
83
84 #define VFS_FIRST_HANDLE 100
85
86
87
88 struct vfs_openfile
89 {
90 int handle;
91 vfs_class *vclass;
92 void *fsinfo;
93 };
94
95
96
97
98
99
100 static vfs_path_t *current_path = NULL;
101
102 static GPtrArray *vfs_openfiles = NULL;
103 static long vfs_free_handle_list = -1;
104
105
106
107
108
109
110
111
112
113
114
115
116
117 static estr_t
118 _vfs_translate_path (const char *path, int size, GIConv defcnv, GString *buffer)
119 {
120 estr_t state = ESTR_SUCCESS;
121 const char *semi;
122
123 if (size == 0)
124 return ESTR_SUCCESS;
125
126 size = (size > 0) ? size : (signed int) strlen (path);
127
128
129 semi = g_strrstr_len (path, size, VFS_ENCODING_PREFIX);
130 if (semi != NULL && (semi == path || IS_PATH_SEP (semi[-1])))
131 {
132 char encoding[16];
133 const char *slash;
134 GIConv coder = INVALID_CONV;
135 int ms;
136
137
138 ms = semi - path;
139
140 state = _vfs_translate_path (path, ms, defcnv, buffer);
141
142 if (state != ESTR_SUCCESS)
143 return state;
144
145
146 semi += strlen (VFS_ENCODING_PREFIX);
147 slash = strchr (semi, PATH_SEP);
148
149 if (slash - path >= size)
150 slash = NULL;
151
152 ms = (slash != NULL) ? slash - semi : (int) strlen (semi);
153 ms = MIN ((unsigned int) ms, sizeof (encoding) - 1);
154
155 if (semi + ms > path + size)
156 ms = path + size - semi;
157 memcpy (encoding, semi, ms);
158 encoding[ms] = '\0';
159
160 if (is_supported_encoding (encoding))
161 coder = str_crt_conv_to (encoding);
162
163 if (coder != INVALID_CONV)
164 {
165 if (slash != NULL)
166 state = str_vfs_convert_to (coder, slash + 1, path + size - slash - 1, buffer);
167 str_close_conv (coder);
168 return state;
169 }
170
171 errno = EINVAL;
172 state = ESTR_FAILURE;
173 }
174 else
175 {
176
177 state = str_vfs_convert_to (defcnv, path, size, buffer);
178 }
179
180 return state;
181 }
182
183
184
185 static struct vfs_openfile *
186 vfs_get_openfile (int handle)
187 {
188 struct vfs_openfile *h;
189
190 if (handle < VFS_FIRST_HANDLE || (guint) (handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
191 return NULL;
192
193 h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE);
194 if (h != NULL)
195 g_assert (h->handle == handle);
196
197 return h;
198 }
199
200
201
202 static gboolean
203 vfs_test_current_dir (const vfs_path_t *vpath)
204 {
205 struct stat my_stat, my_stat2;
206
207 return (mc_global.vfs.cd_symlinks && mc_stat (vpath, &my_stat) == 0
208 && mc_stat (vfs_get_raw_current_dir (), &my_stat2) == 0
209 && my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev);
210 }
211
212
213
214
215
216
217 void
218 vfs_free_handle (int handle)
219 {
220 const int idx = handle - VFS_FIRST_HANDLE;
221
222 if (handle >= VFS_FIRST_HANDLE && (guint) idx < vfs_openfiles->len)
223 {
224 struct vfs_openfile *h;
225
226 h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, idx);
227 g_free (h);
228 g_ptr_array_index (vfs_openfiles, idx) = (void *) vfs_free_handle_list;
229 vfs_free_handle_list = idx;
230 }
231 }
232
233
234
235
236 struct vfs_class *
237 vfs_class_find_by_handle (int handle, void **fsinfo)
238 {
239 struct vfs_openfile *h;
240
241 h = vfs_get_openfile (handle);
242
243 if (h == NULL)
244 return NULL;
245
246 if (fsinfo != NULL)
247 *fsinfo = h->fsinfo;
248
249 return h->vclass;
250 }
251
252
253
254
255
256
257
258 int
259 vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
260 {
261 struct vfs_openfile *h;
262
263 h = g_new (struct vfs_openfile, 1);
264 h->fsinfo = fsinfo;
265 h->vclass = vclass;
266
267
268 h->handle = vfs_free_handle_list;
269 if (h->handle == -1)
270 {
271
272 h->handle = vfs_openfiles->len;
273 g_ptr_array_add (vfs_openfiles, h);
274 }
275 else
276 {
277 vfs_free_handle_list = (long) g_ptr_array_index (vfs_openfiles, vfs_free_handle_list);
278 g_ptr_array_index (vfs_openfiles, h->handle) = h;
279 }
280
281 h->handle += VFS_FIRST_HANDLE;
282 return h->handle;
283 }
284
285
286
287 int
288 vfs_ferrno (struct vfs_class *vfs)
289 {
290 return vfs->ferrno != NULL ? vfs->ferrno (vfs) : E_UNKNOWN;
291
292 }
293
294
295
296 gboolean
297 vfs_register_class (struct vfs_class *vfs)
298 {
299 if (vfs->init != NULL)
300 if (vfs->init (vfs) == 0)
301 return FALSE;
302
303 g_ptr_array_add (vfs__classes_list, vfs);
304
305 return TRUE;
306 }
307
308
309
310 void
311 vfs_unregister_class (struct vfs_class *vfs)
312 {
313 if (vfs->done != NULL)
314 vfs->done (vfs);
315
316 g_ptr_array_remove (vfs__classes_list, vfs);
317 }
318
319
320
321
322
323
324
325 char *
326 vfs_strip_suffix_from_filename (const char *filename)
327 {
328 char *semi, *p;
329
330 if (filename == NULL)
331 vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
332
333 p = g_strdup (filename);
334 semi = g_strrstr (p, VFS_PATH_URL_DELIMITER);
335 if (semi != NULL)
336 {
337 char *vfs_prefix;
338
339 *semi = '\0';
340 vfs_prefix = strrchr (p, PATH_SEP);
341 if (vfs_prefix == NULL)
342 *semi = *VFS_PATH_URL_DELIMITER;
343 else
344 *vfs_prefix = '\0';
345 }
346
347 return p;
348 }
349
350
351
352 const char *
353 vfs_translate_path (const char *path)
354 {
355 estr_t state;
356
357 g_string_set_size (vfs_str_buffer, 0);
358 state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
359 return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
360 }
361
362
363
364 char *
365 vfs_translate_path_n (const char *path)
366 {
367 const char *result;
368
369 result = vfs_translate_path (path);
370 return g_strdup (result);
371 }
372
373
374
375
376
377
378
379
380 const char *
381 vfs_get_current_dir (void)
382 {
383 return current_path->str;
384 }
385
386
387
388
389
390
391
392
393 char *
394 vfs_get_current_dir_n (void)
395 {
396 return g_strdup (current_path->str);
397 }
398
399
400
401
402
403
404
405
406 const vfs_path_t *
407 vfs_get_raw_current_dir (void)
408 {
409 return current_path;
410 }
411
412
413
414
415
416
417
418 void
419 vfs_set_raw_current_dir (const vfs_path_t *vpath)
420 {
421 vfs_path_free (current_path, TRUE);
422 current_path = (vfs_path_t *) vpath;
423 }
424
425
426
427
428 gboolean
429 vfs_current_is_local (void)
430 {
431 return (current_vfs->flags & VFSF_LOCAL) != 0;
432 }
433
434
435
436
437 vfs_flags_t
438 vfs_file_class_flags (const vfs_path_t *vpath)
439 {
440 const vfs_path_element_t *path_element;
441
442 path_element = vfs_path_get_by_index (vpath, -1);
443 if (!vfs_path_element_valid (path_element))
444 return VFSF_UNKNOWN;
445
446 return path_element->class->flags;
447 }
448
449
450
451 void
452 vfs_init (void)
453 {
454
455 vfs__classes_list = g_ptr_array_new ();
456
457
458 vfs_openfiles = g_ptr_array_new ();
459
460 vfs_str_buffer = g_string_new ("");
461
462 mc_readdir_result = vfs_dirent_init (NULL, "", -1);
463 }
464
465
466
467 void
468 vfs_setup_work_dir (void)
469 {
470 vfs_setup_cwd ();
471
472
473
474
475
476
477
478 current_vfs = VFS_CLASS (vfs_path_get_last_path_vfs (current_path));
479 }
480
481
482
483 void
484 vfs_shut (void)
485 {
486 guint i;
487
488 vfs_gc_done ();
489
490 vfs_set_raw_current_dir (NULL);
491
492 for (i = 0; i < vfs__classes_list->len; i++)
493 {
494 struct vfs_class *vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
495
496 if (vfs->done != NULL)
497 vfs->done (vfs);
498 }
499
500
501 g_ptr_array_free (vfs_openfiles, TRUE);
502 vfs_openfiles = NULL;
503 g_ptr_array_free (vfs__classes_list, TRUE);
504 vfs__classes_list = NULL;
505 g_string_free (vfs_str_buffer, TRUE);
506 vfs_str_buffer = NULL;
507 current_vfs = NULL;
508 vfs_free_handle_list = -1;
509 vfs_dirent_free (mc_readdir_result);
510 mc_readdir_result = NULL;
511 }
512
513
514
515
516
517
518
519
520
521
522
523
524 struct vfs_dirent *
525 vfs_dirent_init (struct vfs_dirent *d, const char *fname, ino_t ino)
526 {
527 struct vfs_dirent *ret = d;
528
529 if (ret == NULL)
530 ret = g_new0 (struct vfs_dirent, 1);
531
532 if (ret->d_name_str == NULL)
533 ret->d_name_str = g_string_sized_new (MC_MAXFILENAMELEN);
534
535 vfs_dirent_assign (ret, fname, ino);
536
537 return ret;
538 }
539
540
541
542
543
544
545
546
547
548
549 void
550 vfs_dirent_assign (struct vfs_dirent *d, const char *fname, ino_t ino)
551 {
552 g_string_assign (d->d_name_str, fname);
553 d->d_name = d->d_name_str->str;
554 d->d_len = d->d_name_str->len;
555 d->d_ino = ino;
556 }
557
558
559
560
561
562
563
564
565 void
566 vfs_dirent_free (struct vfs_dirent *d)
567 {
568 g_string_free (d->d_name_str, TRUE);
569 g_free (d);
570 }
571
572
573
574
575
576
577
578
579 void
580 vfs_fill_names (fill_names_f func)
581 {
582 guint i;
583
584 for (i = 0; i < vfs__classes_list->len; i++)
585 {
586 struct vfs_class *vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
587
588 if (vfs->fill_names != NULL)
589 vfs->fill_names (vfs, func);
590 }
591 }
592
593
594
595 gboolean
596 vfs_file_is_local (const vfs_path_t *vpath)
597 {
598 return (vfs_file_class_flags (vpath) & VFSF_LOCAL) != 0;
599 }
600
601
602
603 void
604 vfs_print_message (const char *msg, ...)
605 {
606 ev_vfs_print_message_t event_data;
607 va_list ap;
608
609 va_start (ap, msg);
610 event_data.msg = g_strdup_vprintf (msg, ap);
611 va_end (ap);
612
613 mc_event_raise (MCEVENT_GROUP_CORE, "vfs_print_message", (gpointer) &event_data);
614 }
615
616
617
618
619
620
621
622 void
623 vfs_setup_cwd (void)
624 {
625 char *current_dir;
626 vfs_path_t *tmp_vpath;
627 const struct vfs_class *me;
628
629 if (vfs_get_raw_current_dir () == NULL)
630 {
631 current_dir = my_get_current_dir ();
632 vfs_set_raw_current_dir (vfs_path_from_str (current_dir));
633 g_free (current_dir);
634
635 current_dir = getenv ("PWD");
636 tmp_vpath = vfs_path_from_str (current_dir);
637
638 if (tmp_vpath != NULL)
639 {
640 if (vfs_test_current_dir (tmp_vpath))
641 vfs_set_raw_current_dir (tmp_vpath);
642 else
643 vfs_path_free (tmp_vpath, TRUE);
644 }
645 }
646
647 me = vfs_path_get_last_path_vfs (vfs_get_raw_current_dir ());
648 if ((me->flags & VFSF_LOCAL) != 0)
649 {
650 current_dir = my_get_current_dir ();
651 tmp_vpath = vfs_path_from_str (current_dir);
652 g_free (current_dir);
653
654 if (tmp_vpath != NULL)
655 {
656
657
658
659 if (!vfs_test_current_dir (tmp_vpath))
660 vfs_set_raw_current_dir (tmp_vpath);
661 else
662 vfs_path_free (tmp_vpath, TRUE);
663 }
664 }
665 }
666
667
668
669
670
671
672
673 char *
674 vfs_get_cwd (void)
675 {
676 vfs_setup_cwd ();
677 return vfs_get_current_dir_n ();
678 }
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693 int
694 vfs_preallocate (int dest_vfs_fd, off_t src_fsize, off_t dest_fsize)
695 {
696 #ifndef HAVE_POSIX_FALLOCATE
697 (void) dest_vfs_fd;
698 (void) src_fsize;
699 (void) dest_fsize;
700 return 0;
701
702 #else
703 void *dest_fd = NULL;
704 struct vfs_class *dest_class;
705
706 if (src_fsize == 0)
707 return 0;
708
709 dest_class = vfs_class_find_by_handle (dest_vfs_fd, &dest_fd);
710 if ((dest_class->flags & VFSF_LOCAL) == 0 || dest_fd == NULL)
711 return 0;
712
713 return posix_fallocate (*(int *) dest_fd, dest_fsize, src_fsize - dest_fsize);
714
715 #endif
716 }
717
718
719
720 int
721 vfs_clone_file (int dest_vfs_fd, int src_vfs_fd)
722 {
723 #ifdef FICLONE
724 void *dest_fd = NULL;
725 void *src_fd = NULL;
726 struct vfs_class *dest_class;
727 struct vfs_class *src_class;
728
729 dest_class = vfs_class_find_by_handle (dest_vfs_fd, &dest_fd);
730 if ((dest_class->flags & VFSF_LOCAL) == 0)
731 {
732 errno = ENOTSUP;
733 return (-1);
734 }
735 if (dest_fd == NULL)
736 {
737 errno = EBADF;
738 return (-1);
739 }
740
741 src_class = vfs_class_find_by_handle (src_vfs_fd, &src_fd);
742 if ((src_class->flags & VFSF_LOCAL) == 0)
743 {
744 errno = ENOTSUP;
745 return (-1);
746 }
747 if (src_fd == NULL)
748 {
749 errno = EBADF;
750 return (-1);
751 }
752
753 return ioctl (*(int *) dest_fd, FICLONE, *(int *) src_fd);
754 #else
755 (void) dest_vfs_fd;
756 (void) src_vfs_fd;
757 errno = ENOTSUP;
758 return (-1);
759 #endif
760 }
761
762