Manual pages: mcmcdiffmceditmcview

root/lib/vfs/vfs.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. _vfs_translate_path
  2. vfs_get_openfile
  3. vfs_test_current_dir
  4. vfs_free_handle
  5. vfs_class_find_by_handle
  6. vfs_new_handle
  7. vfs_ferrno
  8. vfs_register_class
  9. vfs_unregister_class
  10. vfs_strip_suffix_from_filename
  11. vfs_translate_path
  12. vfs_translate_path_n
  13. vfs_get_current_dir
  14. vfs_get_current_dir_n
  15. vfs_get_raw_current_dir
  16. vfs_set_raw_current_dir
  17. vfs_current_is_local
  18. vfs_file_class_flags
  19. vfs_init
  20. vfs_setup_work_dir
  21. vfs_shut
  22. vfs_dirent_init
  23. vfs_dirent_assign
  24. vfs_dirent_free
  25. vfs_fill_names
  26. vfs_file_is_local
  27. vfs_print_message
  28. vfs_setup_cwd
  29. vfs_get_cwd
  30. vfs_preallocate
  31. vfs_clone_file

   1 /*
   2    Virtual File System switch code
   3 
   4    Copyright (C) 1995-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by: 1995 Miguel de Icaza
   8    Jakub Jelinek, 1995
   9    Pavel Machek, 1998
  10    Slava Zanko <slavazanko@gmail.com>, 2011-2013
  11    Andrew Borodin <aborodin@vmail.ru>, 2011-2022
  12 
  13    This file is part of the Midnight Commander.
  14 
  15    The Midnight Commander is free software: you can redistribute it
  16    and/or modify it under the terms of the GNU General Public License as
  17    published by the Free Software Foundation, either version 3 of the License,
  18    or (at your option) any later version.
  19 
  20    The Midnight Commander is distributed in the hope that it will be useful,
  21    but WITHOUT ANY WARRANTY; without even the implied warranty of
  22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23    GNU General Public License for more details.
  24 
  25    You should have received a copy of the GNU General Public License
  26    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  27  */
  28 
  29 /**
  30  * \file
  31  * \brief Source: Virtual File System switch code
  32  * \author Miguel de Icaza
  33  * \author Jakub Jelinek
  34  * \author Pavel Machek
  35  * \date 1995, 1998
  36  * \warning functions like extfs_lstat() have right to destroy any
  37  * strings you pass to them. This is actually ok as you g_strdup what
  38  * you are passing to them, anyway; still, beware.
  39  *
  40  * Namespace: exports *many* functions with vfs_ prefix; exports
  41  * parse_ls_lga and friends which do not have that prefix.
  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"  // message()
  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 /* TODO: move it to the separate .h */
  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 /*** global variables ****************************************************************************/
  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 /*** file scope macro definitions ****************************************************************/
  83 
  84 #define VFS_FIRST_HANDLE 100
  85 
  86 /*** file scope type declarations ****************************************************************/
  87 
  88 struct vfs_openfile
  89 {
  90     int handle;
  91     vfs_class *vclass;
  92     void *fsinfo;
  93 };
  94 
  95 /*** forward declarations (file scope functions) *************************************************/
  96 
  97 /*** file scope variables ************************************************************************/
  98 
  99 /** They keep track of the current directory */
 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 /*** file scope functions ************************************************************************/
 107 /* --------------------------------------------------------------------------------------------- */
 108 /* now used only by vfs_translate_path, but could be used in other vfs
 109  * plugin to automatic detect encoding
 110  * path - path to translate
 111  * size - how many bytes from path translate
 112  * defcnv - converter, that is used as default, when path does not contain any
 113  *          #enc: substring
 114  * buffer - used to store result of translation
 115  */
 116 
 117 static estr_t
 118 _vfs_translate_path (const char *path, int size, GIConv defcnv, GString *buffer)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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     // try to find /#enc:
 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         // first must be translated part before #enc:
 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         // now can be translated part after #enc:
 146         semi += strlen (VFS_ENCODING_PREFIX);  // skip "#enc:"
 147         slash = strchr (semi, PATH_SEP);
 148         // ignore slashes after size;
 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         // limit encoding size (ms) to path size (size)
 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         // path can be translated whole at once
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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 /*** public functions ****************************************************************************/
 214 /* --------------------------------------------------------------------------------------------- */
 215 /** Free open file data for given file handle */
 216 
 217 void
 218 vfs_free_handle (int handle)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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 /** Find VFS class by file handle */
 235 
 236 struct vfs_class *
 237 vfs_class_find_by_handle (int handle, void **fsinfo)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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  * Create new VFS handle and put it to the list
 256  */
 257 
 258 int
 259 vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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     // Allocate the first free handle
 268     h->handle = vfs_free_handle_list;
 269     if (h->handle == -1)
 270     {
 271         // No free allocated handles, allocate one
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 289 {
 290     return vfs->ferrno != NULL ? vfs->ferrno (vfs) : E_UNKNOWN;
 291     // Hope that error message is obscure enough ;-)
 292 }
 293 
 294 /* --------------------------------------------------------------------------------------------- */
 295 
 296 gboolean
 297 vfs_register_class (struct vfs_class *vfs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 298 {
 299     if (vfs->init != NULL)         // vfs has own initialization function
 300         if (vfs->init (vfs) == 0)  // but it failed
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 312 {
 313     if (vfs->done != NULL)
 314         vfs->done (vfs);
 315 
 316     g_ptr_array_remove (vfs__classes_list, vfs);
 317 }
 318 
 319 /* --------------------------------------------------------------------------------------------- */
 320 /** Strip known vfs suffixes from a filename (possible improvement: strip
 321  *  suffix from last path component).
 322  *  \return a malloced string which has to be freed.
 323  */
 324 
 325 char *
 326 vfs_strip_suffix_from_filename (const char *filename)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 366 {
 367     const char *result;
 368 
 369     result = vfs_translate_path (path);
 370     return g_strdup (result);
 371 }
 372 
 373 /* --------------------------------------------------------------------------------------------- */
 374 /**
 375  * Get current directory without any OS calls.
 376  *
 377  * @return string contains current path
 378  */
 379 
 380 const char *
 381 vfs_get_current_dir (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 382 {
 383     return current_path->str;
 384 }
 385 
 386 /* --------------------------------------------------------------------------------------------- */
 387 /**
 388  * Get current directory without any OS calls.
 389  *
 390  * @return newly allocated string contains current path
 391  */
 392 
 393 char *
 394 vfs_get_current_dir_n (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 395 {
 396     return g_strdup (current_path->str);
 397 }
 398 
 399 /* --------------------------------------------------------------------------------------------- */
 400 /**
 401  * Get raw current directory object without any OS calls.
 402  *
 403  * @return object contain current path
 404  */
 405 
 406 const vfs_path_t *
 407 vfs_get_raw_current_dir (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 408 {
 409     return current_path;
 410 }
 411 
 412 /* --------------------------------------------------------------------------------------------- */
 413 /**
 414  * Set current directory object.
 415  *
 416  * @param vpath new path
 417  */
 418 void
 419 vfs_set_raw_current_dir (const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 420 {
 421     vfs_path_free (current_path, TRUE);
 422     current_path = (vfs_path_t *) vpath;
 423 }
 424 
 425 /* --------------------------------------------------------------------------------------------- */
 426 /* Return TRUE is the current VFS class is local */
 427 
 428 gboolean
 429 vfs_current_is_local (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 430 {
 431     return (current_vfs->flags & VFSF_LOCAL) != 0;
 432 }
 433 
 434 /* --------------------------------------------------------------------------------------------- */
 435 /* Return flags of the VFS class of the given filename */
 436 
 437 vfs_flags_t
 438 vfs_file_class_flags (const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 453 {
 454     // create the VFS handle arrays
 455     vfs__classes_list = g_ptr_array_new ();
 456 
 457     // create the VFS handle array
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 469 {
 470     vfs_setup_cwd ();
 471 
 472     // FIXME: is we really need for this check?
 473     /*
 474        if (strlen (current_dir) > MC_MAXPATHLEN - 2)
 475        vfs_die ("Current dir too long.\n");
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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     // NULL-ize pointers to make unit tests happy
 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  * Init or create vfs_dirent structure
 516  *
 517  * @d vfs_dirent structure to init. If NULL, new structure is created.
 518  * @fname file name
 519  * @ino file inode number
 520  *
 521  * @return pointer to d if d isn't NULL, or pointer to newly created structure.
 522  */
 523 
 524 struct vfs_dirent *
 525 vfs_dirent_init (struct vfs_dirent *d, const char *fname, ino_t ino)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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  * Assign members of vfs_dirent structure
 543  *
 544  * @d vfs_dirent structure for assignment
 545  * @fname file name
 546  * @ino file inode number
 547  */
 548 
 549 void
 550 vfs_dirent_assign (struct vfs_dirent *d, const char *fname, ino_t ino)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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  * Destroy vfs_dirent structure
 561  *
 562  * @d vfs_dirent structure to destroy.
 563  */
 564 
 565 void
 566 vfs_dirent_free (struct vfs_dirent *d)
     /* [previous][next][first][last][top][bottom][index][help]  */
 567 {
 568     g_string_free (d->d_name_str, TRUE);
 569     g_free (d);
 570 }
 571 
 572 /* --------------------------------------------------------------------------------------------- */
 573 
 574 /**
 575  * These ones grab information from the VFS
 576  *  and handles them to an upper layer
 577  */
 578 
 579 void
 580 vfs_fill_names (fill_names_f func)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 597 {
 598     return (vfs_file_class_flags (vpath) & VFSF_LOCAL) != 0;
 599 }
 600 
 601 /* --------------------------------------------------------------------------------------------- */
 602 
 603 void
 604 vfs_print_message (const char *msg, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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  * If it's local, reread the current directory
 619  * from the OS.
 620  */
 621 
 622 void
 623 vfs_setup_cwd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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             // One of directories in the path is not readable
 657 
 658             // Check if it is O.K. to use the current_dir
 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  * Return current directory.  If it's local, reread the current directory
 670  * from the OS.
 671  */
 672 
 673 char *
 674 vfs_get_cwd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 675 {
 676     vfs_setup_cwd ();
 677     return vfs_get_current_dir_n ();
 678 }
 679 
 680 /* --------------------------------------------------------------------------------------------- */
 681 /**
 682  * Preallocate space for file in new place for ensure that file
 683  * will be fully copied with less fragmentation.
 684  *
 685  * @param dest_vfs_fd mc VFS file handler
 686  * @param src_fsize source file size
 687  * @param dest_fsize destination file size (if destination exists, otherwise should be 0)
 688  *
 689  * @return 0 if success and non-zero otherwise.
 690  * Note: function doesn't touch errno global variable.
 691  */
 692 
 693 int
 694 vfs_preallocate (int dest_vfs_fd, off_t src_fsize, off_t dest_fsize)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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  // HAVE_POSIX_FALLOCATE
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */