Move all stuff from lib to contrib
[midnight-commander.git] / vfs / local.c
blob26731ac123ddfa8c59b56010baa711b0fd44b19c
2 /**
3 * \file
4 * \brief Source: local FS
5 */
7 #include <config.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <string.h>
14 #include "../src/global.h"
15 #include "../src/tty.h" /* enable/disable interrupt key */
16 #include "../src/wtools.h" /* message() */
17 #include "../src/main.h" /* print_vfs_message */
18 #include "utilvfs.h"
19 #include "vfs.h"
20 #include "local.h"
22 /**
23 * Note: Some of this functions are not static. This has rather good
24 * reason: exactly same functions would have to appear in sfs.c. This
25 * saves both computer's memory and my work. <pavel@ucw.cz>
28 static struct vfs_class vfs_local_ops;
30 static void *
31 local_open (struct vfs_class *me, const char *file, int flags, int mode)
33 int *local_info;
34 int fd;
36 (void) me;
38 fd = open (file, NO_LINEAR (flags), mode);
39 if (fd == -1)
40 return 0;
42 local_info = g_new (int, 1);
43 *local_info = fd;
45 return local_info;
48 ssize_t
49 local_read (void *data, char *buffer, int count)
51 int n;
53 if (!data)
54 return -1;
56 while ((n = read (*((int *) data), buffer, count)) == -1){
57 #ifdef EAGAIN
58 if (errno == EAGAIN) continue;
59 #endif
60 #ifdef EINTR
61 if (errno == EINTR) continue;
62 #endif
63 return -1;
65 return n;
68 int
69 local_close (void *data)
71 int fd;
73 if (!data)
74 return -1;
76 fd = *(int *) data;
77 g_free (data);
78 return close (fd);
81 int
82 local_errno (struct vfs_class *me)
84 (void) me;
85 return errno;
88 static void *
89 local_opendir (struct vfs_class *me, const char *dirname)
91 DIR **local_info;
92 DIR *dir;
94 (void) me;
96 dir = opendir (dirname);
97 if (!dir)
98 return 0;
100 local_info = (DIR **) g_new (DIR *, 1);
101 *local_info = dir;
103 return local_info;
106 static void *
107 local_readdir (void *data)
109 return readdir (*(DIR **) data);
112 static int
113 local_closedir (void *data)
115 int i;
117 i = closedir (* (DIR **) data);
118 g_free (data);
119 return i;
122 static int
123 local_stat (struct vfs_class *me, const char *path, struct stat *buf)
125 (void) me;
127 return stat (path, buf);
130 static int
131 local_lstat (struct vfs_class *me, const char *path, struct stat *buf)
133 (void) me;
135 #ifndef HAVE_STATLSTAT
136 return lstat (path,buf);
137 #else
138 return statlstat (path, buf);
139 #endif
143 local_fstat (void *data, struct stat *buf)
145 /* FIXME: avoid type cast */
146 return fstat (*((int *) data), buf);
149 static int
150 local_chmod (struct vfs_class *me, const char *path, int mode)
152 (void) me;
154 return chmod (path, mode);
157 static int
158 local_chown (struct vfs_class *me, const char *path, int owner, int group)
160 (void) me;
162 return chown (path, owner, group);
165 static int
166 local_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
168 (void) me;
170 return utime (path, times);
173 static int
174 local_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
176 (void) me;
178 return readlink (path, buf, size);
181 static int
182 local_unlink (struct vfs_class *me, const char *path)
184 (void) me;
186 return unlink (path);
189 static int
190 local_symlink (struct vfs_class *me, const char *n1, const char *n2)
192 (void) me;
194 return symlink (n1, n2);
197 static ssize_t
198 local_write (void *data, const char *buf, int nbyte)
200 int fd;
201 int n;
203 if (!data)
204 return -1;
206 fd = * (int *) data;
207 while ((n = write (fd, buf, nbyte)) == -1){
208 #ifdef EAGAIN
209 if (errno == EAGAIN) continue;
210 #endif
211 #ifdef EINTR
212 if (errno == EINTR) continue;
213 #endif
214 break;
216 return n;
219 static int
220 local_rename (struct vfs_class *me, const char *a, const char *b)
222 (void) me;
224 return rename (a, b);
227 static int
228 local_chdir (struct vfs_class *me, const char *path)
230 (void) me;
232 return chdir (path);
235 off_t
236 local_lseek (void *data, off_t offset, int whence)
238 int fd = * (int *) data;
240 return lseek (fd, offset, whence);
243 static int
244 local_mknod (struct vfs_class *me, const char *path, int mode, int dev)
246 (void) me;
248 return mknod (path, mode, dev);
251 static int
252 local_link (struct vfs_class *me, const char *p1, const char *p2)
254 (void) me;
256 return link (p1, p2);
259 static int
260 local_mkdir (struct vfs_class *me, const char *path, mode_t mode)
262 (void) me;
264 return mkdir (path, mode);
267 static int
268 local_rmdir (struct vfs_class *me, const char *path)
270 (void) me;
272 return rmdir (path);
275 static char *
276 local_getlocalcopy (struct vfs_class *me, const char *path)
278 (void) me;
280 return g_strdup (path);
283 static int
284 local_ungetlocalcopy (struct vfs_class *me, const char *path,
285 const char *local, int has_changed)
287 (void) me;
288 (void) path;
289 (void) local;
290 (void) has_changed;
292 return 0;
295 static int
296 local_which (struct vfs_class *me, const char *path)
298 (void) me;
299 (void) path;
301 return 0; /* Every path which other systems do not like is expected to be ours */
304 void
305 init_localfs (void)
307 vfs_local_ops.name = "localfs";
308 vfs_local_ops.flags = VFSF_LOCAL;
309 vfs_local_ops.which = local_which;
310 vfs_local_ops.open = local_open;
311 vfs_local_ops.close = local_close;
312 vfs_local_ops.read = local_read;
313 vfs_local_ops.write = local_write;
314 vfs_local_ops.opendir = local_opendir;
315 vfs_local_ops.readdir = local_readdir;
316 vfs_local_ops.closedir = local_closedir;
317 vfs_local_ops.stat = local_stat;
318 vfs_local_ops.lstat = local_lstat;
319 vfs_local_ops.fstat = local_fstat;
320 vfs_local_ops.chmod = local_chmod;
321 vfs_local_ops.chown = local_chown;
322 vfs_local_ops.utime = local_utime;
323 vfs_local_ops.readlink = local_readlink;
324 vfs_local_ops.symlink = local_symlink;
325 vfs_local_ops.link = local_link;
326 vfs_local_ops.unlink = local_unlink;
327 vfs_local_ops.rename = local_rename;
328 vfs_local_ops.chdir = local_chdir;
329 vfs_local_ops.ferrno = local_errno;
330 vfs_local_ops.lseek = local_lseek;
331 vfs_local_ops.mknod = local_mknod;
332 vfs_local_ops.getlocalcopy = local_getlocalcopy;
333 vfs_local_ops.ungetlocalcopy = local_ungetlocalcopy;
334 vfs_local_ops.mkdir = local_mkdir;
335 vfs_local_ops.rmdir = local_rmdir;
336 vfs_register_class (&vfs_local_ops);