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