2 Virtual File System: local file system.
4 Copyright (C) 1995-2017
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
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.
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.
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/>.
25 * \brief Source: local FS
30 #include <sys/types.h>
35 #include "lib/global.h"
37 #include "lib/vfs/utilvfs.h"
41 /*** global variables ****************************************************************************/
43 /*** file scope macro definitions ****************************************************************/
45 /*** file scope type declarations ****************************************************************/
47 /*** file scope variables ************************************************************************/
49 static struct vfs_class vfs_local_ops
;
51 /*** file scope functions ************************************************************************/
52 /* --------------------------------------------------------------------------------------------- */
55 * Note: Some of this functions are not static. This has rather good
56 * reason: exactly same functions would have to appear in sfs.c. This
57 * saves both computer's memory and my work. <pavel@ucw.cz>
61 /* --------------------------------------------------------------------------------------------- */
64 local_open (const vfs_path_t
* vpath
, int flags
, mode_t mode
)
68 const vfs_path_element_t
*path_element
;
70 path_element
= vfs_path_get_by_index (vpath
, -1);
71 fd
= open (path_element
->path
, NO_LINEAR (flags
), mode
);
75 local_info
= g_new (int, 1);
81 /* --------------------------------------------------------------------------------------------- */
84 local_opendir (const vfs_path_t
* vpath
)
88 const vfs_path_element_t
*path_element
;
90 path_element
= vfs_path_get_by_index (vpath
, -1);
91 dir
= opendir (path_element
->path
);
95 local_info
= (DIR **) g_new (DIR *, 1);
101 /* --------------------------------------------------------------------------------------------- */
104 local_readdir (void *data
)
106 return readdir (*(DIR **) data
);
109 /* --------------------------------------------------------------------------------------------- */
112 local_closedir (void *data
)
116 i
= closedir (*(DIR **) data
);
121 /* --------------------------------------------------------------------------------------------- */
124 local_stat (const vfs_path_t
* vpath
, struct stat
*buf
)
126 const vfs_path_element_t
*path_element
;
128 path_element
= vfs_path_get_by_index (vpath
, -1);
129 return stat (path_element
->path
, buf
);
132 /* --------------------------------------------------------------------------------------------- */
135 local_lstat (const vfs_path_t
* vpath
, struct stat
*buf
)
137 const vfs_path_element_t
*path_element
;
139 path_element
= vfs_path_get_by_index (vpath
, -1);
140 #ifndef HAVE_STATLSTAT
141 return lstat (path_element
->path
, buf
);
143 return statlstat (path_element
->path
, buf
);
147 /* --------------------------------------------------------------------------------------------- */
150 local_chmod (const vfs_path_t
* vpath
, mode_t mode
)
152 const vfs_path_element_t
*path_element
;
154 path_element
= vfs_path_get_by_index (vpath
, -1);
155 return chmod (path_element
->path
, mode
);
158 /* --------------------------------------------------------------------------------------------- */
161 local_chown (const vfs_path_t
* vpath
, uid_t owner
, gid_t group
)
163 const vfs_path_element_t
*path_element
;
165 path_element
= vfs_path_get_by_index (vpath
, -1);
166 return chown (path_element
->path
, owner
, group
);
169 /* --------------------------------------------------------------------------------------------- */
172 local_utime (const vfs_path_t
* vpath
, mc_timesbuf_t
* times
)
175 const vfs_path_element_t
*path_element
;
177 path_element
= vfs_path_get_by_index (vpath
, -1);
178 #ifdef HAVE_UTIMENSAT
179 ret
= utimensat (AT_FDCWD
, path_element
->path
, *times
, 0);
181 ret
= utime (path_element
->path
, times
);
186 /* --------------------------------------------------------------------------------------------- */
189 local_readlink (const vfs_path_t
* vpath
, char *buf
, size_t size
)
191 const vfs_path_element_t
*path_element
;
193 path_element
= vfs_path_get_by_index (vpath
, -1);
194 return readlink (path_element
->path
, buf
, size
);
197 /* --------------------------------------------------------------------------------------------- */
200 local_unlink (const vfs_path_t
* vpath
)
202 const vfs_path_element_t
*path_element
;
204 path_element
= vfs_path_get_by_index (vpath
, -1);
205 return unlink (path_element
->path
);
208 /* --------------------------------------------------------------------------------------------- */
211 local_symlink (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
213 const vfs_path_element_t
*path_element1
;
214 const vfs_path_element_t
*path_element2
;
216 path_element1
= vfs_path_get_by_index (vpath1
, -1);
217 path_element2
= vfs_path_get_by_index (vpath2
, -1);
218 return symlink (path_element1
->path
, path_element2
->path
);
221 /* --------------------------------------------------------------------------------------------- */
224 local_write (void *data
, const char *buf
, size_t nbyte
)
233 while ((n
= write (fd
, buf
, nbyte
)) == -1)
248 /* --------------------------------------------------------------------------------------------- */
251 local_rename (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
253 const vfs_path_element_t
*path_element1
;
254 const vfs_path_element_t
*path_element2
;
256 path_element1
= vfs_path_get_by_index (vpath1
, -1);
257 path_element2
= vfs_path_get_by_index (vpath2
, -1);
258 return rename (path_element1
->path
, path_element2
->path
);
261 /* --------------------------------------------------------------------------------------------- */
264 local_chdir (const vfs_path_t
* vpath
)
266 const vfs_path_element_t
*path_element
;
268 path_element
= vfs_path_get_by_index (vpath
, -1);
269 return chdir (path_element
->path
);
272 /* --------------------------------------------------------------------------------------------- */
275 local_mknod (const vfs_path_t
* vpath
, mode_t mode
, dev_t dev
)
277 const vfs_path_element_t
*path_element
;
279 path_element
= vfs_path_get_by_index (vpath
, -1);
280 return mknod (path_element
->path
, mode
, dev
);
283 /* --------------------------------------------------------------------------------------------- */
286 local_link (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
288 const vfs_path_element_t
*path_element1
;
289 const vfs_path_element_t
*path_element2
;
291 path_element1
= vfs_path_get_by_index (vpath1
, -1);
292 path_element2
= vfs_path_get_by_index (vpath2
, -1);
293 return link (path_element1
->path
, path_element2
->path
);
296 /* --------------------------------------------------------------------------------------------- */
299 local_mkdir (const vfs_path_t
* vpath
, mode_t mode
)
301 const vfs_path_element_t
*path_element
;
303 path_element
= vfs_path_get_by_index (vpath
, -1);
304 return mkdir (path_element
->path
, mode
);
307 /* --------------------------------------------------------------------------------------------- */
310 local_rmdir (const vfs_path_t
* vpath
)
312 const vfs_path_element_t
*path_element
;
314 path_element
= vfs_path_get_by_index (vpath
, -1);
315 return rmdir (path_element
->path
);
318 /* --------------------------------------------------------------------------------------------- */
321 local_getlocalcopy (const vfs_path_t
* vpath
)
323 return vfs_path_clone (vpath
);
326 /* --------------------------------------------------------------------------------------------- */
329 local_ungetlocalcopy (const vfs_path_t
* vpath
, const vfs_path_t
* local
, gboolean has_changed
)
338 /* --------------------------------------------------------------------------------------------- */
341 local_which (struct vfs_class
*me
, const char *path
)
346 return 0; /* Every path which other systems do not like is expected to be ours */
349 /* --------------------------------------------------------------------------------------------- */
350 /*** public functions ****************************************************************************/
351 /* --------------------------------------------------------------------------------------------- */
354 local_read (void *data
, char *buffer
, size_t count
)
361 while ((n
= read (*((int *) data
), buffer
, count
)) == -1)
376 /* --------------------------------------------------------------------------------------------- */
379 local_close (void *data
)
391 /* --------------------------------------------------------------------------------------------- */
394 local_errno (struct vfs_class
*me
)
400 /* --------------------------------------------------------------------------------------------- */
403 local_fstat (void *data
, struct stat
*buf
)
405 /* FIXME: avoid type cast */
406 return fstat (*((int *) data
), buf
);
409 /* --------------------------------------------------------------------------------------------- */
412 local_lseek (void *data
, off_t offset
, int whence
)
414 int fd
= *(int *) data
;
416 return lseek (fd
, offset
, whence
);
419 /* --------------------------------------------------------------------------------------------- */
424 vfs_local_ops
.name
= "localfs";
425 vfs_local_ops
.flags
= VFSF_LOCAL
;
426 vfs_local_ops
.which
= local_which
;
427 vfs_local_ops
.open
= local_open
;
428 vfs_local_ops
.close
= local_close
;
429 vfs_local_ops
.read
= local_read
;
430 vfs_local_ops
.write
= local_write
;
431 vfs_local_ops
.opendir
= local_opendir
;
432 vfs_local_ops
.readdir
= local_readdir
;
433 vfs_local_ops
.closedir
= local_closedir
;
434 vfs_local_ops
.stat
= local_stat
;
435 vfs_local_ops
.lstat
= local_lstat
;
436 vfs_local_ops
.fstat
= local_fstat
;
437 vfs_local_ops
.chmod
= local_chmod
;
438 vfs_local_ops
.chown
= local_chown
;
439 vfs_local_ops
.utime
= local_utime
;
440 vfs_local_ops
.readlink
= local_readlink
;
441 vfs_local_ops
.symlink
= local_symlink
;
442 vfs_local_ops
.link
= local_link
;
443 vfs_local_ops
.unlink
= local_unlink
;
444 vfs_local_ops
.rename
= local_rename
;
445 vfs_local_ops
.chdir
= local_chdir
;
446 vfs_local_ops
.ferrno
= local_errno
;
447 vfs_local_ops
.lseek
= local_lseek
;
448 vfs_local_ops
.mknod
= local_mknod
;
449 vfs_local_ops
.getlocalcopy
= local_getlocalcopy
;
450 vfs_local_ops
.ungetlocalcopy
= local_ungetlocalcopy
;
451 vfs_local_ops
.mkdir
= local_mkdir
;
452 vfs_local_ops
.rmdir
= local_rmdir
;
453 vfs_register_class (&vfs_local_ops
);
456 /* --------------------------------------------------------------------------------------------- */