root/src/vfs/local/local.c

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

DEFINITIONS

This source file includes following definitions.
  1. local_open
  2. local_opendir
  3. local_readdir
  4. local_closedir
  5. local_stat
  6. local_lstat
  7. local_chmod
  8. local_chown
  9. local_utime
  10. local_readlink
  11. local_unlink
  12. local_symlink
  13. local_write
  14. local_rename
  15. local_chdir
  16. local_mknod
  17. local_link
  18. local_mkdir
  19. local_rmdir
  20. local_getlocalcopy
  21. local_ungetlocalcopy
  22. local_which
  23. local_read
  24. local_close
  25. local_errno
  26. local_fstat
  27. local_lseek
  28. local_nothingisopen
  29. vfs_init_localfs

   1 /*
   2    Virtual File System: local file system.
   3 
   4    Copyright (C) 1995-2019
   5    Free Software Foundation, Inc.
   6 
   7    This file is part of the Midnight Commander.
   8 
   9    The Midnight Commander is free software: you can redistribute it
  10    and/or modify it under the terms of the GNU General Public License as
  11    published by the Free Software Foundation, either version 3 of the License,
  12    or (at your option) any later version.
  13 
  14    The Midnight Commander is distributed in the hope that it will be useful,
  15    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17    GNU General Public License for more details.
  18 
  19    You should have received a copy of the GNU General Public License
  20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21  */
  22 
  23 /**
  24  * \file
  25  * \brief Source: local FS
  26  */
  27 
  28 #include <config.h>
  29 #include <errno.h>
  30 #include <sys/types.h>
  31 #include <unistd.h>
  32 #include <stdio.h>
  33 #include <string.h>
  34 
  35 #include "lib/global.h"
  36 
  37 #include "lib/vfs/xdirentry.h"  /* vfs_s_subclass */
  38 #include "lib/vfs/utilvfs.h"
  39 
  40 #include "local.h"
  41 
  42 /*** global variables ****************************************************************************/
  43 
  44 /*** file scope macro definitions ****************************************************************/
  45 
  46 /*** file scope type declarations ****************************************************************/
  47 
  48 /*** file scope variables ************************************************************************/
  49 
  50 static struct vfs_s_subclass local_subclass;
  51 static struct vfs_class *vfs_local_ops = VFS_CLASS (&local_subclass);
  52 
  53 /* --------------------------------------------------------------------------------------------- */
  54 /*** file scope functions ************************************************************************/
  55 /* --------------------------------------------------------------------------------------------- */
  56 
  57 /**
  58  * Note: Some of this functions are not static. This has rather good
  59  * reason: exactly same functions would have to appear in sfs.c. This
  60  * saves both computer's memory and my work.  <pavel@ucw.cz>
  61  */
  62 
  63 
  64 /* --------------------------------------------------------------------------------------------- */
  65 
  66 static void *
  67 local_open (const vfs_path_t * vpath, int flags, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
  68 {
  69     int *local_info;
  70     int fd;
  71     const vfs_path_element_t *path_element;
  72 
  73     path_element = vfs_path_get_by_index (vpath, -1);
  74     fd = open (path_element->path, NO_LINEAR (flags), mode);
  75     if (fd == -1)
  76         return 0;
  77 
  78     local_info = g_new (int, 1);
  79     *local_info = fd;
  80 
  81     return local_info;
  82 }
  83 
  84 /* --------------------------------------------------------------------------------------------- */
  85 
  86 static void *
  87 local_opendir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
  88 {
  89     DIR **local_info;
  90     DIR *dir;
  91     const vfs_path_element_t *path_element;
  92 
  93     path_element = vfs_path_get_by_index (vpath, -1);
  94     dir = opendir (path_element->path);
  95     if (dir == NULL)
  96         return 0;
  97 
  98     local_info = (DIR **) g_new (DIR *, 1);
  99     *local_info = dir;
 100 
 101     return local_info;
 102 }
 103 
 104 /* --------------------------------------------------------------------------------------------- */
 105 
 106 static void *
 107 local_readdir (void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 108 {
 109     return readdir (*(DIR **) data);
 110 }
 111 
 112 /* --------------------------------------------------------------------------------------------- */
 113 
 114 static int
 115 local_closedir (void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 116 {
 117     int i;
 118 
 119     i = closedir (*(DIR **) data);
 120     g_free (data);
 121     return i;
 122 }
 123 
 124 /* --------------------------------------------------------------------------------------------- */
 125 
 126 static int
 127 local_stat (const vfs_path_t * vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
 128 {
 129     const vfs_path_element_t *path_element;
 130 
 131     path_element = vfs_path_get_by_index (vpath, -1);
 132     return stat (path_element->path, buf);
 133 }
 134 
 135 /* --------------------------------------------------------------------------------------------- */
 136 
 137 static int
 138 local_lstat (const vfs_path_t * vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
 139 {
 140     const vfs_path_element_t *path_element;
 141 
 142     path_element = vfs_path_get_by_index (vpath, -1);
 143 #ifndef HAVE_STATLSTAT
 144     return lstat (path_element->path, buf);
 145 #else
 146     return statlstat (path_element->path, buf);
 147 #endif
 148 }
 149 
 150 /* --------------------------------------------------------------------------------------------- */
 151 
 152 static int
 153 local_chmod (const vfs_path_t * vpath, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
 154 {
 155     const vfs_path_element_t *path_element;
 156 
 157     path_element = vfs_path_get_by_index (vpath, -1);
 158     return chmod (path_element->path, mode);
 159 }
 160 
 161 /* --------------------------------------------------------------------------------------------- */
 162 
 163 static int
 164 local_chown (const vfs_path_t * vpath, uid_t owner, gid_t group)
     /* [previous][next][first][last][top][bottom][index][help]  */
 165 {
 166     const vfs_path_element_t *path_element;
 167 
 168     path_element = vfs_path_get_by_index (vpath, -1);
 169     return chown (path_element->path, owner, group);
 170 }
 171 
 172 /* --------------------------------------------------------------------------------------------- */
 173 
 174 static int
 175 local_utime (const vfs_path_t * vpath, mc_timesbuf_t * times)
     /* [previous][next][first][last][top][bottom][index][help]  */
 176 {
 177     int ret;
 178     const vfs_path_element_t *path_element;
 179 
 180     path_element = vfs_path_get_by_index (vpath, -1);
 181 #ifdef HAVE_UTIMENSAT
 182     ret = utimensat (AT_FDCWD, path_element->path, *times, 0);
 183 #else
 184     ret = utime (path_element->path, times);
 185 #endif
 186     return ret;
 187 }
 188 
 189 /* --------------------------------------------------------------------------------------------- */
 190 
 191 static int
 192 local_readlink (const vfs_path_t * vpath, char *buf, size_t size)
     /* [previous][next][first][last][top][bottom][index][help]  */
 193 {
 194     const vfs_path_element_t *path_element;
 195 
 196     path_element = vfs_path_get_by_index (vpath, -1);
 197     return readlink (path_element->path, buf, size);
 198 }
 199 
 200 /* --------------------------------------------------------------------------------------------- */
 201 
 202 static int
 203 local_unlink (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 204 {
 205     const vfs_path_element_t *path_element;
 206 
 207     path_element = vfs_path_get_by_index (vpath, -1);
 208     return unlink (path_element->path);
 209 }
 210 
 211 /* --------------------------------------------------------------------------------------------- */
 212 
 213 static int
 214 local_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
     /* [previous][next][first][last][top][bottom][index][help]  */
 215 {
 216     const vfs_path_element_t *path_element1, *path_element2;
 217 
 218     path_element1 = vfs_path_get_by_index (vpath1, -1);
 219     path_element2 = vfs_path_get_by_index (vpath2, -1);
 220     return symlink (path_element1->path, path_element2->path);
 221 }
 222 
 223 /* --------------------------------------------------------------------------------------------- */
 224 
 225 static ssize_t
 226 local_write (void *data, const char *buf, size_t nbyte)
     /* [previous][next][first][last][top][bottom][index][help]  */
 227 {
 228     int fd;
 229     int n;
 230 
 231     if (data == NULL)
 232         return (-1);
 233 
 234     fd = *(int *) data;
 235 
 236     while ((n = write (fd, buf, nbyte)) == -1)
 237     {
 238 #ifdef EAGAIN
 239         if (errno == EAGAIN)
 240             continue;
 241 #endif
 242 #ifdef EINTR
 243         if (errno == EINTR)
 244             continue;
 245 #endif
 246         break;
 247     }
 248 
 249     return n;
 250 }
 251 
 252 /* --------------------------------------------------------------------------------------------- */
 253 
 254 static int
 255 local_rename (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
     /* [previous][next][first][last][top][bottom][index][help]  */
 256 {
 257     const vfs_path_element_t *path_element1, *path_element2;
 258 
 259     path_element1 = vfs_path_get_by_index (vpath1, -1);
 260     path_element2 = vfs_path_get_by_index (vpath2, -1);
 261     return rename (path_element1->path, path_element2->path);
 262 }
 263 
 264 /* --------------------------------------------------------------------------------------------- */
 265 
 266 static int
 267 local_chdir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 268 {
 269     const vfs_path_element_t *path_element;
 270 
 271     path_element = vfs_path_get_by_index (vpath, -1);
 272     return chdir (path_element->path);
 273 }
 274 
 275 /* --------------------------------------------------------------------------------------------- */
 276 
 277 static int
 278 local_mknod (const vfs_path_t * vpath, mode_t mode, dev_t dev)
     /* [previous][next][first][last][top][bottom][index][help]  */
 279 {
 280     const vfs_path_element_t *path_element;
 281 
 282     path_element = vfs_path_get_by_index (vpath, -1);
 283     return mknod (path_element->path, mode, dev);
 284 }
 285 
 286 /* --------------------------------------------------------------------------------------------- */
 287 
 288 static int
 289 local_link (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
     /* [previous][next][first][last][top][bottom][index][help]  */
 290 {
 291     const vfs_path_element_t *path_element1, *path_element2;
 292 
 293     path_element1 = vfs_path_get_by_index (vpath1, -1);
 294     path_element2 = vfs_path_get_by_index (vpath2, -1);
 295     return link (path_element1->path, path_element2->path);
 296 }
 297 
 298 /* --------------------------------------------------------------------------------------------- */
 299 
 300 static int
 301 local_mkdir (const vfs_path_t * vpath, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
 302 {
 303     const vfs_path_element_t *path_element;
 304 
 305     path_element = vfs_path_get_by_index (vpath, -1);
 306     return mkdir (path_element->path, mode);
 307 }
 308 
 309 /* --------------------------------------------------------------------------------------------- */
 310 
 311 static int
 312 local_rmdir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 313 {
 314     const vfs_path_element_t *path_element;
 315 
 316     path_element = vfs_path_get_by_index (vpath, -1);
 317     return rmdir (path_element->path);
 318 }
 319 
 320 /* --------------------------------------------------------------------------------------------- */
 321 
 322 static vfs_path_t *
 323 local_getlocalcopy (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
 324 {
 325     return vfs_path_clone (vpath);
 326 }
 327 
 328 /* --------------------------------------------------------------------------------------------- */
 329 
 330 static int
 331 local_ungetlocalcopy (const vfs_path_t * vpath, const vfs_path_t * local, gboolean has_changed)
     /* [previous][next][first][last][top][bottom][index][help]  */
 332 {
 333     (void) vpath;
 334     (void) local;
 335     (void) has_changed;
 336 
 337     return 0;
 338 }
 339 
 340 /* --------------------------------------------------------------------------------------------- */
 341 
 342 static int
 343 local_which (struct vfs_class *me, const char *path)
     /* [previous][next][first][last][top][bottom][index][help]  */
 344 {
 345     (void) me;
 346     (void) path;
 347 
 348     return 0;                   /* Every path which other systems do not like is expected to be ours */
 349 }
 350 
 351 /* --------------------------------------------------------------------------------------------- */
 352 /*** public functions ****************************************************************************/
 353 /* --------------------------------------------------------------------------------------------- */
 354 
 355 ssize_t
 356 local_read (void *data, char *buffer, size_t count)
     /* [previous][next][first][last][top][bottom][index][help]  */
 357 {
 358     int n;
 359     int fd;
 360 
 361     if (data == NULL)
 362         return (-1);
 363 
 364     fd = *(int *) data;
 365 
 366     while ((n = read (fd, buffer, count)) == -1)
 367     {
 368 #ifdef EAGAIN
 369         if (errno == EAGAIN)
 370             continue;
 371 #endif
 372 #ifdef EINTR
 373         if (errno == EINTR)
 374             continue;
 375 #endif
 376         return (-1);
 377     }
 378 
 379     return n;
 380 }
 381 
 382 /* --------------------------------------------------------------------------------------------- */
 383 
 384 int
 385 local_close (void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
 386 {
 387     int fd;
 388 
 389     if (data == NULL)
 390         return (-1);
 391 
 392     fd = *(int *) data;
 393     g_free (data);
 394     return close (fd);
 395 }
 396 
 397 /* --------------------------------------------------------------------------------------------- */
 398 
 399 int
 400 local_errno (struct vfs_class *me)
     /* [previous][next][first][last][top][bottom][index][help]  */
 401 {
 402     (void) me;
 403     return errno;
 404 }
 405 
 406 /* --------------------------------------------------------------------------------------------- */
 407 
 408 int
 409 local_fstat (void *data, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
 410 {
 411     int fd = *(int *) data;
 412 
 413     return fstat (fd, buf);
 414 }
 415 
 416 /* --------------------------------------------------------------------------------------------- */
 417 
 418 off_t
 419 local_lseek (void *data, off_t offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help]  */
 420 {
 421     int fd = *(int *) data;
 422 
 423     return lseek (fd, offset, whence);
 424 }
 425 
 426 /* --------------------------------------------------------------------------------------------- */
 427 
 428 static gboolean
 429 local_nothingisopen (vfsid id)
     /* [previous][next][first][last][top][bottom][index][help]  */
 430 {
 431     (void) id;
 432 
 433     return TRUE;
 434 }
 435 
 436 /* --------------------------------------------------------------------------------------------- */
 437 
 438 void
 439 vfs_init_localfs (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 440 {
 441     /* NULLize vfs_s_subclass members */
 442     memset (&local_subclass, 0, sizeof (local_subclass));
 443 
 444     vfs_init_class (vfs_local_ops, "localfs", VFS_LOCAL, NULL);
 445     vfs_local_ops->which = local_which;
 446     vfs_local_ops->open = local_open;
 447     vfs_local_ops->close = local_close;
 448     vfs_local_ops->read = local_read;
 449     vfs_local_ops->write = local_write;
 450     vfs_local_ops->opendir = local_opendir;
 451     vfs_local_ops->readdir = local_readdir;
 452     vfs_local_ops->closedir = local_closedir;
 453     vfs_local_ops->stat = local_stat;
 454     vfs_local_ops->lstat = local_lstat;
 455     vfs_local_ops->fstat = local_fstat;
 456     vfs_local_ops->chmod = local_chmod;
 457     vfs_local_ops->chown = local_chown;
 458     vfs_local_ops->utime = local_utime;
 459     vfs_local_ops->readlink = local_readlink;
 460     vfs_local_ops->symlink = local_symlink;
 461     vfs_local_ops->link = local_link;
 462     vfs_local_ops->unlink = local_unlink;
 463     vfs_local_ops->rename = local_rename;
 464     vfs_local_ops->chdir = local_chdir;
 465     vfs_local_ops->ferrno = local_errno;
 466     vfs_local_ops->lseek = local_lseek;
 467     vfs_local_ops->mknod = local_mknod;
 468     vfs_local_ops->getlocalcopy = local_getlocalcopy;
 469     vfs_local_ops->ungetlocalcopy = local_ungetlocalcopy;
 470     vfs_local_ops->mkdir = local_mkdir;
 471     vfs_local_ops->rmdir = local_rmdir;
 472     vfs_local_ops->nothingisopen = local_nothingisopen;
 473     vfs_register_class (vfs_local_ops);
 474 }
 475 
 476 /* --------------------------------------------------------------------------------------------- */

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