Return values of following functions are constants now:
[midnight-commander.git] / src / vfs / local / local.c
blobaeea45bc861a64c737268ce964e518db456874d7
1 /*
2 Virtual File System: local file system.
4 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2006, 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /**
25 * \file
26 * \brief Source: local FS
29 #include <config.h>
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <fcntl.h>
37 #include "lib/global.h"
39 #include "lib/vfs/utilvfs.h"
41 #include "local.h"
43 /*** global variables ****************************************************************************/
45 /*** file scope macro definitions ****************************************************************/
47 /*** file scope type declarations ****************************************************************/
49 /*** file scope variables ************************************************************************/
51 static struct vfs_class vfs_local_ops;
53 /*** file scope functions ************************************************************************/
54 /* --------------------------------------------------------------------------------------------- */
56 /**
57 * Note: Some of this functions are not static. This has rather good
58 * reason: exactly same functions would have to appear in sfs.c. This
59 * saves both computer's memory and my work. <pavel@ucw.cz>
63 /* --------------------------------------------------------------------------------------------- */
65 static void *
66 local_open (const vfs_path_t * vpath, int flags, mode_t mode)
68 int *local_info;
69 int fd;
70 const vfs_path_element_t *path_element;
72 path_element = vfs_path_get_by_index (vpath, -1);
73 fd = open (path_element->path, NO_LINEAR (flags), mode);
74 if (fd == -1)
75 return 0;
77 local_info = g_new (int, 1);
78 *local_info = fd;
80 return local_info;
83 /* --------------------------------------------------------------------------------------------- */
85 static void *
86 local_opendir (const vfs_path_t * vpath)
88 DIR **local_info;
89 DIR *dir;
90 const vfs_path_element_t *path_element;
92 path_element = vfs_path_get_by_index (vpath, -1);
93 dir = opendir (path_element->path);
94 if (!dir)
95 return 0;
97 local_info = (DIR **) g_new (DIR *, 1);
98 *local_info = dir;
100 return local_info;
103 /* --------------------------------------------------------------------------------------------- */
105 static void *
106 local_readdir (void *data)
108 return readdir (*(DIR **) data);
111 /* --------------------------------------------------------------------------------------------- */
113 static int
114 local_closedir (void *data)
116 int i;
118 i = closedir (*(DIR **) data);
119 g_free (data);
120 return i;
123 /* --------------------------------------------------------------------------------------------- */
125 static int
126 local_stat (const vfs_path_t * vpath, struct stat *buf)
128 const vfs_path_element_t *path_element;
130 path_element = vfs_path_get_by_index (vpath, -1);
131 return stat (path_element->path, buf);
134 /* --------------------------------------------------------------------------------------------- */
136 static int
137 local_lstat (const vfs_path_t * vpath, struct stat *buf)
139 const vfs_path_element_t *path_element;
141 path_element = vfs_path_get_by_index (vpath, -1);
142 #ifndef HAVE_STATLSTAT
143 return lstat (path_element->path, buf);
144 #else
145 return statlstat (path_element->path, buf);
146 #endif
149 /* --------------------------------------------------------------------------------------------- */
151 static int
152 local_chmod (const vfs_path_t * vpath, mode_t mode)
154 const vfs_path_element_t *path_element;
156 path_element = vfs_path_get_by_index (vpath, -1);
157 return chmod (path_element->path, mode);
160 /* --------------------------------------------------------------------------------------------- */
162 static int
163 local_chown (const vfs_path_t * vpath, uid_t owner, gid_t group)
165 const vfs_path_element_t *path_element;
167 path_element = vfs_path_get_by_index (vpath, -1);
168 return chown (path_element->path, owner, group);
171 /* --------------------------------------------------------------------------------------------- */
173 static int
174 local_utime (const vfs_path_t * vpath, struct utimbuf *times)
176 const vfs_path_element_t *path_element;
178 path_element = vfs_path_get_by_index (vpath, -1);
179 return utime (path_element->path, times);
182 /* --------------------------------------------------------------------------------------------- */
184 static int
185 local_readlink (const vfs_path_t * vpath, char *buf, size_t size)
187 const vfs_path_element_t *path_element;
189 path_element = vfs_path_get_by_index (vpath, -1);
190 return readlink (path_element->path, buf, size);
193 /* --------------------------------------------------------------------------------------------- */
195 static int
196 local_unlink (const vfs_path_t * vpath)
198 const vfs_path_element_t *path_element;
200 path_element = vfs_path_get_by_index (vpath, -1);
201 return unlink (path_element->path);
204 /* --------------------------------------------------------------------------------------------- */
206 static int
207 local_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
209 const vfs_path_element_t *path_element1;
210 const vfs_path_element_t *path_element2;
212 path_element1 = vfs_path_get_by_index (vpath1, -1);
213 path_element2 = vfs_path_get_by_index (vpath2, -1);
214 return symlink (path_element1->path, path_element2->path);
217 /* --------------------------------------------------------------------------------------------- */
219 static ssize_t
220 local_write (void *data, const char *buf, size_t nbyte)
222 int fd;
223 int n;
225 if (!data)
226 return -1;
228 fd = *(int *) data;
229 while ((n = write (fd, buf, nbyte)) == -1)
231 #ifdef EAGAIN
232 if (errno == EAGAIN)
233 continue;
234 #endif
235 #ifdef EINTR
236 if (errno == EINTR)
237 continue;
238 #endif
239 break;
241 return n;
244 /* --------------------------------------------------------------------------------------------- */
246 static int
247 local_rename (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
249 const vfs_path_element_t *path_element1;
250 const vfs_path_element_t *path_element2;
252 path_element1 = vfs_path_get_by_index (vpath1, -1);
253 path_element2 = vfs_path_get_by_index (vpath2, -1);
254 return rename (path_element1->path, path_element2->path);
257 /* --------------------------------------------------------------------------------------------- */
259 static int
260 local_chdir (const vfs_path_t * vpath)
262 const vfs_path_element_t *path_element;
264 path_element = vfs_path_get_by_index (vpath, -1);
265 return chdir (path_element->path);
268 /* --------------------------------------------------------------------------------------------- */
270 static int
271 local_mknod (const vfs_path_t * vpath, mode_t mode, dev_t dev)
273 const vfs_path_element_t *path_element;
275 path_element = vfs_path_get_by_index (vpath, -1);
276 return mknod (path_element->path, mode, dev);
279 /* --------------------------------------------------------------------------------------------- */
281 static int
282 local_link (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
284 const vfs_path_element_t *path_element1;
285 const vfs_path_element_t *path_element2;
287 path_element1 = vfs_path_get_by_index (vpath1, -1);
288 path_element2 = vfs_path_get_by_index (vpath2, -1);
289 return link (path_element1->path, path_element2->path);
292 /* --------------------------------------------------------------------------------------------- */
294 static int
295 local_mkdir (const vfs_path_t * vpath, mode_t mode)
297 const vfs_path_element_t *path_element;
299 path_element = vfs_path_get_by_index (vpath, -1);
300 return mkdir (path_element->path, mode);
303 /* --------------------------------------------------------------------------------------------- */
305 static int
306 local_rmdir (const vfs_path_t * vpath)
308 const vfs_path_element_t *path_element;
310 path_element = vfs_path_get_by_index (vpath, -1);
311 return rmdir (path_element->path);
314 /* --------------------------------------------------------------------------------------------- */
316 static vfs_path_t *
317 local_getlocalcopy (const vfs_path_t * vpath)
319 return vfs_path_clone (vpath);
322 /* --------------------------------------------------------------------------------------------- */
324 static int
325 local_ungetlocalcopy (const vfs_path_t * vpath, const vfs_path_t * local, gboolean has_changed)
327 (void) vpath;
328 (void) local;
329 (void) has_changed;
331 return 0;
334 /* --------------------------------------------------------------------------------------------- */
336 static int
337 local_which (struct vfs_class *me, const char *path)
339 (void) me;
340 (void) path;
342 return 0; /* Every path which other systems do not like is expected to be ours */
345 /* --------------------------------------------------------------------------------------------- */
346 /*** public functions ****************************************************************************/
347 /* --------------------------------------------------------------------------------------------- */
349 ssize_t
350 local_read (void *data, char *buffer, size_t count)
352 int n;
354 if (!data)
355 return -1;
357 while ((n = read (*((int *) data), buffer, count)) == -1)
359 #ifdef EAGAIN
360 if (errno == EAGAIN)
361 continue;
362 #endif
363 #ifdef EINTR
364 if (errno == EINTR)
365 continue;
366 #endif
367 return -1;
369 return n;
372 /* --------------------------------------------------------------------------------------------- */
375 local_close (void *data)
377 int fd;
379 if (!data)
380 return -1;
382 fd = *(int *) data;
383 g_free (data);
384 return close (fd);
387 /* --------------------------------------------------------------------------------------------- */
390 local_errno (struct vfs_class *me)
392 (void) me;
393 return errno;
396 /* --------------------------------------------------------------------------------------------- */
399 local_fstat (void *data, struct stat *buf)
401 /* FIXME: avoid type cast */
402 return fstat (*((int *) data), buf);
405 /* --------------------------------------------------------------------------------------------- */
407 off_t
408 local_lseek (void *data, off_t offset, int whence)
410 int fd = *(int *) data;
412 return lseek (fd, offset, whence);
415 /* --------------------------------------------------------------------------------------------- */
417 void
418 init_localfs (void)
420 vfs_local_ops.name = "localfs";
421 vfs_local_ops.flags = VFSF_LOCAL;
422 vfs_local_ops.which = local_which;
423 vfs_local_ops.open = local_open;
424 vfs_local_ops.close = local_close;
425 vfs_local_ops.read = local_read;
426 vfs_local_ops.write = local_write;
427 vfs_local_ops.opendir = local_opendir;
428 vfs_local_ops.readdir = local_readdir;
429 vfs_local_ops.closedir = local_closedir;
430 vfs_local_ops.stat = local_stat;
431 vfs_local_ops.lstat = local_lstat;
432 vfs_local_ops.fstat = local_fstat;
433 vfs_local_ops.chmod = local_chmod;
434 vfs_local_ops.chown = local_chown;
435 vfs_local_ops.utime = local_utime;
436 vfs_local_ops.readlink = local_readlink;
437 vfs_local_ops.symlink = local_symlink;
438 vfs_local_ops.link = local_link;
439 vfs_local_ops.unlink = local_unlink;
440 vfs_local_ops.rename = local_rename;
441 vfs_local_ops.chdir = local_chdir;
442 vfs_local_ops.ferrno = local_errno;
443 vfs_local_ops.lseek = local_lseek;
444 vfs_local_ops.mknod = local_mknod;
445 vfs_local_ops.getlocalcopy = local_getlocalcopy;
446 vfs_local_ops.ungetlocalcopy = local_ungetlocalcopy;
447 vfs_local_ops.mkdir = local_mkdir;
448 vfs_local_ops.rmdir = local_rmdir;
449 vfs_register_class (&vfs_local_ops);
452 /* --------------------------------------------------------------------------------------------- */