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, const long 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     const long sz = size > 0 ? size : (long) strlen (path);
 127 
 128     // try to find /#enc:
 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         // first must be translated part before #enc:
 138         ms = semi - path;
 139         state = _vfs_translate_path (path, ms, defcnv, buffer);
 140         if (state != ESTR_SUCCESS)
 141             return state;
 142 
 143         // now can be translated part after #enc:
 144         semi += strlen (VFS_ENCODING_PREFIX);  // skip "#enc:"
 145         slash = strchr (semi, PATH_SEP);
 146         // ignore slashes after sz
 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         // limit encoding size (ms) to path size (sz)
 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         // path can be translated whole at once
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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 /*** public functions ****************************************************************************/
 213 /* --------------------------------------------------------------------------------------------- */
 214 /** Free open file data for given file handle */
 215 
 216 void
 217 vfs_free_handle (int handle)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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 /** Find VFS class by file handle */
 234 
 235 struct vfs_class *
 236 vfs_class_find_by_handle (int handle, void **fsinfo)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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  * Create new VFS handle and put it to the list
 255  */
 256 
 257 int
 258 vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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     // Allocate the first free handle
 267     h->handle = vfs_free_handle_list;
 268     if (h->handle == -1)
 269     {
 270         // No free allocated handles, allocate one
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 288 {
 289     return vfs->ferrno != NULL ? vfs->ferrno (vfs) : E_UNKNOWN;
 290     // Hope that error message is obscure enough ;-)
 291 }
 292 
 293 /* --------------------------------------------------------------------------------------------- */
 294 
 295 gboolean
 296 vfs_register_class (struct vfs_class *vfs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 297 {
 298     if (vfs->init != NULL)         // vfs has own initialization function
 299         if (vfs->init (vfs) == 0)  // but it failed
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 311 {
 312     if (vfs->done != NULL)
 313         vfs->done (vfs);
 314 
 315     g_ptr_array_remove (vfs__classes_list, vfs);
 316 }
 317 
 318 /* --------------------------------------------------------------------------------------------- */
 319 /** Strip known vfs suffixes from a filename (possible improvement: strip
 320  *  suffix from last path component).
 321  *  \return a malloced string which has to be freed.
 322  */
 323 
 324 char *
 325 vfs_strip_suffix_from_filename (const char *filename)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 365 {
 366     const char *result;
 367 
 368     result = vfs_translate_path (path);
 369     return g_strdup (result);
 370 }
 371 
 372 /* --------------------------------------------------------------------------------------------- */
 373 /**
 374  * Get current directory without any OS calls.
 375  *
 376  * @return string contains current path
 377  */
 378 
 379 const char *
 380 vfs_get_current_dir (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 381 {
 382     return current_path->str;
 383 }
 384 
 385 /* --------------------------------------------------------------------------------------------- */
 386 /**
 387  * Get current directory without any OS calls.
 388  *
 389  * @return newly allocated string contains current path
 390  */
 391 
 392 char *
 393 vfs_get_current_dir_n (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 394 {
 395     return g_strdup (current_path->str);
 396 }
 397 
 398 /* --------------------------------------------------------------------------------------------- */
 399 /**
 400  * Get raw current directory object without any OS calls.
 401  *
 402  * @return object contain current path
 403  */
 404 
 405 const vfs_path_t *
 406 vfs_get_raw_current_dir (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 407 {
 408     return current_path;
 409 }
 410 
 411 /* --------------------------------------------------------------------------------------------- */
 412 /**
 413  * Set current directory object.
 414  *
 415  * @param vpath new path
 416  */
 417 void
 418 vfs_set_raw_current_dir (const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 419 {
 420     vfs_path_free (current_path, TRUE);
 421     current_path = (vfs_path_t *) vpath;
 422 }
 423 
 424 /* --------------------------------------------------------------------------------------------- */
 425 /* Return TRUE is the current VFS class is local */
 426 
 427 gboolean
 428 vfs_current_is_local (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 429 {
 430     return (current_vfs->flags & VFSF_LOCAL) != 0;
 431 }
 432 
 433 /* --------------------------------------------------------------------------------------------- */
 434 /* Return flags of the VFS class of the given filename */
 435 
 436 vfs_flags_t
 437 vfs_file_class_flags (const vfs_path_t *vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 452 {
 453     // create the VFS handle arrays
 454     vfs__classes_list = g_ptr_array_new ();
 455 
 456     // create the VFS handle array
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 468 {
 469     vfs_setup_cwd ();
 470 
 471     // FIXME: is we really need for this check?
 472     /*
 473        if (strlen (current_dir) > MC_MAXPATHLEN - 2)
 474        vfs_die ("Current dir too long.\n");
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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     // NULL-ize pointers to make unit tests happy
 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  * Init or create vfs_dirent structure
 515  *
 516  * @d vfs_dirent structure to init. If NULL, new structure is created.
 517  * @fname file name
 518  * @ino file inode number
 519  *
 520  * @return pointer to d if d isn't NULL, or pointer to newly created structure.
 521  */
 522 
 523 struct vfs_dirent *
 524 vfs_dirent_init (struct vfs_dirent *d, const char *fname, ino_t ino)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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  * Assign members of vfs_dirent structure
 542  *
 543  * @d vfs_dirent structure for assignment
 544  * @fname file name
 545  * @ino file inode number
 546  */
 547 
 548 void
 549 vfs_dirent_assign (struct vfs_dirent *d, const char *fname, ino_t ino)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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  * Destroy vfs_dirent structure
 560  *
 561  * @d vfs_dirent structure to destroy.
 562  */
 563 
 564 void
 565 vfs_dirent_free (struct vfs_dirent *d)
     /* [previous][next][first][last][top][bottom][index][help]  */
 566 {
 567     g_string_free (d->d_name_str, TRUE);
 568     g_free (d);
 569 }
 570 
 571 /* --------------------------------------------------------------------------------------------- */
 572 
 573 /**
 574  * These ones grab information from the VFS
 575  *  and handles them to an upper layer
 576  */
 577 
 578 void
 579 vfs_fill_names (fill_names_f func)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 596 {
 597     return (vfs_file_class_flags (vpath) & VFSF_LOCAL) != 0;
 598 }
 599 
 600 /* --------------------------------------------------------------------------------------------- */
 601 
 602 void
 603 vfs_print_message (const char *msg, ...)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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  * If it's local, reread the current directory
 618  * from the OS.
 619  */
 620 
 621 void
 622 vfs_setup_cwd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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             // One of directories in the path is not readable
 656 
 657             // Check if it is O.K. to use the current_dir
 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  * Return current directory.  If it's local, reread the current directory
 669  * from the OS.
 670  */
 671 
 672 char *
 673 vfs_get_cwd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 674 {
 675     vfs_setup_cwd ();
 676     return vfs_get_current_dir_n ();
 677 }
 678 
 679 /* --------------------------------------------------------------------------------------------- */
 680 /**
 681  * Preallocate space for file in new place for ensure that file
 682  * will be fully copied with less fragmentation.
 683  *
 684  * @param dest_vfs_fd mc VFS file handler
 685  * @param src_fsize source file size
 686  * @param dest_fsize destination file size (if destination exists, otherwise should be 0)
 687  *
 688  * @return 0 if success and non-zero otherwise.
 689  * Note: function doesn't touch errno global variable.
 690  */
 691 
 692 int
 693 vfs_preallocate (int dest_vfs_fd, off_t src_fsize, off_t dest_fsize)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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  // HAVE_POSIX_FALLOCATE
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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 /* --------------------------------------------------------------------------------------------- */

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