extfs: tester: fix indentation.
[midnight-commander.git] / src / vfs / local / local.c
blob2f55d3dbf41430fd15bf847fc2308372def050ba
1 /*
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/>.
23 /**
24 * \file
25 * \brief Source: local FS
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>
35 #include "lib/global.h"
37 #include "lib/vfs/utilvfs.h"
39 #include "local.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 /* --------------------------------------------------------------------------------------------- */
54 /**
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 /* --------------------------------------------------------------------------------------------- */
63 static void *
64 local_open (const vfs_path_t * vpath, int flags, mode_t mode)
66 int *local_info;
67 int fd;
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);
72 if (fd == -1)
73 return 0;
75 local_info = g_new (int, 1);
76 *local_info = fd;
78 return local_info;
81 /* --------------------------------------------------------------------------------------------- */
83 static void *
84 local_opendir (const vfs_path_t * vpath)
86 DIR **local_info;
87 DIR *dir;
88 const vfs_path_element_t *path_element;
90 path_element = vfs_path_get_by_index (vpath, -1);
91 dir = opendir (path_element->path);
92 if (!dir)
93 return 0;
95 local_info = (DIR **) g_new (DIR *, 1);
96 *local_info = dir;
98 return local_info;
101 /* --------------------------------------------------------------------------------------------- */
103 static void *
104 local_readdir (void *data)
106 return readdir (*(DIR **) data);
109 /* --------------------------------------------------------------------------------------------- */
111 static int
112 local_closedir (void *data)
114 int i;
116 i = closedir (*(DIR **) data);
117 g_free (data);
118 return i;
121 /* --------------------------------------------------------------------------------------------- */
123 static int
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 /* --------------------------------------------------------------------------------------------- */
134 static int
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);
142 #else
143 return statlstat (path_element->path, buf);
144 #endif
147 /* --------------------------------------------------------------------------------------------- */
149 static int
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 /* --------------------------------------------------------------------------------------------- */
160 static int
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 /* --------------------------------------------------------------------------------------------- */
171 static int
172 local_utime (const vfs_path_t * vpath, mc_timesbuf_t * times)
174 int ret;
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);
180 #else
181 ret = utime (path_element->path, times);
182 #endif
183 return ret;
186 /* --------------------------------------------------------------------------------------------- */
188 static int
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 /* --------------------------------------------------------------------------------------------- */
199 static int
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 /* --------------------------------------------------------------------------------------------- */
210 static int
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 /* --------------------------------------------------------------------------------------------- */
223 static ssize_t
224 local_write (void *data, const char *buf, size_t nbyte)
226 int fd;
227 int n;
229 if (!data)
230 return -1;
232 fd = *(int *) data;
233 while ((n = write (fd, buf, nbyte)) == -1)
235 #ifdef EAGAIN
236 if (errno == EAGAIN)
237 continue;
238 #endif
239 #ifdef EINTR
240 if (errno == EINTR)
241 continue;
242 #endif
243 break;
245 return n;
248 /* --------------------------------------------------------------------------------------------- */
250 static int
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 /* --------------------------------------------------------------------------------------------- */
263 static int
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 /* --------------------------------------------------------------------------------------------- */
274 static int
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 /* --------------------------------------------------------------------------------------------- */
285 static int
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 /* --------------------------------------------------------------------------------------------- */
298 static int
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 /* --------------------------------------------------------------------------------------------- */
309 static int
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 /* --------------------------------------------------------------------------------------------- */
320 static vfs_path_t *
321 local_getlocalcopy (const vfs_path_t * vpath)
323 return vfs_path_clone (vpath);
326 /* --------------------------------------------------------------------------------------------- */
328 static int
329 local_ungetlocalcopy (const vfs_path_t * vpath, const vfs_path_t * local, gboolean has_changed)
331 (void) vpath;
332 (void) local;
333 (void) has_changed;
335 return 0;
338 /* --------------------------------------------------------------------------------------------- */
340 static int
341 local_which (struct vfs_class *me, const char *path)
343 (void) me;
344 (void) path;
346 return 0; /* Every path which other systems do not like is expected to be ours */
349 /* --------------------------------------------------------------------------------------------- */
350 /*** public functions ****************************************************************************/
351 /* --------------------------------------------------------------------------------------------- */
353 ssize_t
354 local_read (void *data, char *buffer, size_t count)
356 int n;
358 if (!data)
359 return -1;
361 while ((n = read (*((int *) data), buffer, count)) == -1)
363 #ifdef EAGAIN
364 if (errno == EAGAIN)
365 continue;
366 #endif
367 #ifdef EINTR
368 if (errno == EINTR)
369 continue;
370 #endif
371 return -1;
373 return n;
376 /* --------------------------------------------------------------------------------------------- */
379 local_close (void *data)
381 int fd;
383 if (!data)
384 return -1;
386 fd = *(int *) data;
387 g_free (data);
388 return close (fd);
391 /* --------------------------------------------------------------------------------------------- */
394 local_errno (struct vfs_class *me)
396 (void) me;
397 return errno;
400 /* --------------------------------------------------------------------------------------------- */
403 local_fstat (void *data, struct stat *buf)
405 /* FIXME: avoid type cast */
406 return fstat (*((int *) data), buf);
409 /* --------------------------------------------------------------------------------------------- */
411 off_t
412 local_lseek (void *data, off_t offset, int whence)
414 int fd = *(int *) data;
416 return lseek (fd, offset, whence);
419 /* --------------------------------------------------------------------------------------------- */
421 void
422 init_localfs (void)
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 /* --------------------------------------------------------------------------------------------- */