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