*** empty log message ***
[midnight-commander.git] / vfs / local.c
blobd2348375287c1934fa5a9b916ee258c26acda258
1 #include <config.h>
2 #include <errno.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <string.h>
8 #include <fcntl.h>
10 #include "utilvfs.h"
12 #include "vfs.h"
13 #include "local.h"
15 /* Note: Some of this functions are not static. This has rather good
16 * reason: exactly same functions would have to appear in sfs.c. This
17 * saves both computer's memory and my work. <pavel@ucw.cz>
18 * */
20 static void *
21 local_open (vfs *me, char *file, int flags, int mode)
23 int *local_info;
24 int fd;
26 fd = open (file, NO_LINEAR(flags), mode);
27 if (fd == -1)
28 return 0;
30 local_info = g_new (int, 1);
31 *local_info = fd;
33 return local_info;
36 int
37 local_read (void *data, char *buffer, int count)
39 int n;
41 if (!data)
42 return -1;
44 while ((n = read (*((int *) data), buffer, count)) == -1){
45 #ifdef EAGAIN
46 if (errno == EAGAIN) continue;
47 #endif
48 #ifdef EINTR
49 if (errno == EINTR) continue;
50 #endif
51 return -1;
53 return n;
56 int
57 local_close (void *data)
59 int fd;
61 if (!data)
62 return -1;
64 fd = *(int *) data;
65 g_free (data);
66 return close (fd);
69 int
70 local_errno (vfs *me)
72 return errno;
75 static void *
76 local_opendir (vfs *me, char *dirname)
78 DIR **local_info;
79 DIR *dir;
81 dir = opendir (dirname);
82 if (!dir)
83 return 0;
85 local_info = (DIR **) g_new (DIR *, 1);
86 *local_info = dir;
88 return local_info;
91 static int
92 local_telldir (void *data)
94 #ifdef HAVE_TELLDIR
95 return telldir( *(DIR **) data );
96 #else
97 #warning "Native telldir() not available, emulation not implemented"
98 abort();
99 return 0; /* for dumb compilers */
100 #endif /* !HAVE_TELLDIR */
103 static void
104 local_seekdir (void *data, int offset)
106 #ifdef HAVE_SEEKDIR
107 seekdir( *(DIR **) data, offset );
108 #else
109 #warning "Native seekdir() not available, emulation not implemented"
110 abort();
111 #endif /* !HAVE_SEEKDIR */
114 static void *
115 local_readdir (void *data)
117 return readdir (*(DIR **) data);
120 static int
121 local_closedir (void *data)
123 int i;
125 i = closedir (* (DIR **) data);
126 if (data)
127 g_free (data);
128 return i;
131 static int
132 local_stat (vfs *me, char *path, struct stat *buf)
134 return stat (path, buf);
137 static int
138 local_lstat (vfs *me, char *path, struct stat *buf)
140 #ifndef HAVE_STATLSTAT
141 return lstat (path,buf);
142 #else
143 return statlstat (path, buf);
144 #endif
148 local_fstat (void *data, struct stat *buf)
150 return fstat (*((int *) data), buf);
153 static int
154 local_chmod (vfs *me, char *path, int mode)
156 return chmod (path, mode);
159 static int
160 local_chown (vfs *me, char *path, int owner, int group)
162 return chown (path, owner, group);
165 static int
166 local_utime (vfs *me, char *path, struct utimbuf *times)
168 return utime (path, times);
171 static int
172 local_readlink (vfs *me, char *path, char *buf, int size)
174 return readlink (path, buf, size);
177 static int
178 local_unlink (vfs *me, char *path)
180 return unlink (path);
183 static int
184 local_symlink (vfs *me, char *n1, char *n2)
186 return symlink (n1, n2);
189 static int
190 local_write (void *data, char *buf, int nbyte)
192 int fd;
193 int n;
195 if (!data)
196 return -1;
198 fd = * (int *) data;
199 while ((n = write (fd, buf, nbyte)) == -1){
200 #ifdef EAGAIN
201 if (errno == EAGAIN) continue;
202 #endif
203 #ifdef EINTR
204 if (errno == EINTR) continue;
205 #endif
206 break;
208 return n;
211 static int
212 local_rename (vfs *me, char *a, char *b)
214 return rename (a, b);
217 static int
218 local_chdir (vfs *me, char *path)
220 return chdir (path);
224 local_lseek (void *data, off_t offset, int whence)
226 int fd = * (int *) data;
228 return lseek (fd, offset, whence);
231 static int
232 local_mknod (vfs *me, char *path, int mode, int dev)
234 return mknod (path, mode, dev);
237 static int
238 local_link (vfs *me, char *p1, char *p2)
240 return link (p1, p2);
243 static int
244 local_mkdir (vfs *me, char *path, mode_t mode)
246 return mkdir (path, mode);
249 static int
250 local_rmdir (vfs *me, char *path)
252 return rmdir (path);
255 static vfsid
256 local_getid (vfs *me, char *path, struct vfs_stamping **parent)
258 *parent = NULL;
259 return (vfsid) -1; /* We do not free local fs stuff at all */
262 static int
263 local_nothingisopen (vfsid id)
265 return 0;
268 static void
269 local_free (vfsid id)
273 static char *
274 local_getlocalcopy (vfs *me, char *path)
276 return g_strdup (path);
279 static int
280 local_ungetlocalcopy (vfs *me, char *path, char *local, int has_changed)
282 return 0;
285 #ifdef HAVE_MMAP
286 caddr_t
287 local_mmap (vfs *me, caddr_t addr, size_t len, int prot, int flags, void *data, off_t offset)
289 int fd = * (int *)data;
291 return mmap (addr, len, prot, flags, fd, offset);
295 local_munmap (vfs *me, caddr_t addr, size_t len, void *data)
297 return munmap (addr, len);
299 #endif
301 static int
302 local_which (vfs *me, char *path)
304 return 0; /* Every path which other systems do not like is expected to be ours */
307 vfs vfs_local_ops = {
308 NULL, /* This is place of next pointer */
309 "localfs",
310 0, /* flags */
311 NULL, /* prefix */
312 NULL, /* data */
313 0, /* errno */
314 NULL,
315 NULL,
316 NULL,
317 local_which,
319 local_open,
320 local_close,
321 local_read,
322 local_write,
324 local_opendir,
325 local_readdir,
326 local_closedir,
327 local_telldir,
328 local_seekdir,
330 local_stat,
331 local_lstat,
332 local_fstat,
334 local_chmod,
335 local_chown,
336 local_utime,
338 local_readlink,
339 local_symlink,
340 local_link,
341 local_unlink,
343 local_rename,
344 local_chdir,
345 local_errno,
346 local_lseek,
347 local_mknod,
349 local_getid,
350 local_nothingisopen,
351 local_free,
353 local_getlocalcopy,
354 local_ungetlocalcopy,
356 local_mkdir,
357 local_rmdir,
359 NULL,
360 NULL
361 #ifdef HAVE_MMAP
362 ,local_mmap,
363 local_munmap
364 #endif
367 vfs vfs_nil_ops = {
368 NULL, /* This is place of next pointer */
369 "nullfs",
370 0, /* flags */
371 NULL, /* prefix */
372 NULL, /* data */
373 0, /* errno */
374 NULL,
375 NULL,
376 NULL,
377 NULL,
379 NULL,
380 NULL,
381 NULL,
382 NULL,
384 NULL,
385 NULL,
386 NULL,
387 NULL,
388 NULL,
390 NULL,
391 NULL,
392 NULL,
394 NULL,
395 NULL,
396 NULL,
398 NULL,
399 NULL,
400 NULL,
401 NULL,
403 NULL,
404 NULL,
405 NULL,
406 NULL,
407 NULL,
409 local_getid,
410 local_nothingisopen,
411 local_free,
413 NULL,
414 NULL,
416 NULL,
417 NULL,
419 NULL,
420 NULL
421 #ifdef HAVE_MMAP
422 , NULL,
423 NULL
424 #endif