replaced calls to g_strdup() by mhl_str_dup()
[midnight-commander.git] / vfs / local.c
blob29ff36b5fa979157364ed5094dd1ad876b886796
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 <mhl/string.h>
10 #include "../src/global.h"
11 #include "../src/tty.h" /* enable/disable interrupt key */
12 #include "../src/wtools.h" /* message() */
13 #include "../src/main.h" /* print_vfs_message */
14 #include "utilvfs.h"
15 #include "vfs.h"
16 #include "local.h"
18 /* Note: Some of this functions are not static. This has rather good
19 * reason: exactly same functions would have to appear in sfs.c. This
20 * saves both computer's memory and my work. <pavel@ucw.cz>
21 * */
23 static struct vfs_class vfs_local_ops;
25 static void *
26 local_open (struct vfs_class *me, const char *file, int flags, int mode)
28 int *local_info;
29 int fd;
31 (void) me;
33 fd = open (file, NO_LINEAR (flags), mode);
34 if (fd == -1)
35 return 0;
37 local_info = g_new (int, 1);
38 *local_info = fd;
40 return local_info;
43 ssize_t
44 local_read (void *data, char *buffer, int count)
46 int n;
48 if (!data)
49 return -1;
51 while ((n = read (*((int *) data), buffer, count)) == -1){
52 #ifdef EAGAIN
53 if (errno == EAGAIN) continue;
54 #endif
55 #ifdef EINTR
56 if (errno == EINTR) continue;
57 #endif
58 return -1;
60 return n;
63 int
64 local_close (void *data)
66 int fd;
68 if (!data)
69 return -1;
71 fd = *(int *) data;
72 g_free (data);
73 return close (fd);
76 int
77 local_errno (struct vfs_class *me)
79 (void) me;
80 return errno;
83 static void *
84 local_opendir (struct vfs_class *me, const char *dirname)
86 DIR **local_info;
87 DIR *dir;
89 (void) me;
91 dir = opendir (dirname);
92 if (!dir)
93 return 0;
95 local_info = (DIR **) g_new (DIR *, 1);
96 *local_info = dir;
98 return local_info;
101 static void *
102 local_readdir (void *data)
104 return readdir (*(DIR **) data);
107 static int
108 local_closedir (void *data)
110 int i;
112 i = closedir (* (DIR **) data);
113 g_free (data);
114 return i;
117 static int
118 local_stat (struct vfs_class *me, const char *path, struct stat *buf)
120 (void) me;
122 return stat (path, buf);
125 static int
126 local_lstat (struct vfs_class *me, const char *path, struct stat *buf)
128 (void) me;
130 #ifndef HAVE_STATLSTAT
131 return lstat (path,buf);
132 #else
133 return statlstat (path, buf);
134 #endif
138 local_fstat (void *data, struct stat *buf)
140 /* FIXME: avoid type cast */
141 return fstat (*((int *) data), buf);
144 static int
145 local_chmod (struct vfs_class *me, const char *path, int mode)
147 (void) me;
149 return chmod (path, mode);
152 static int
153 local_chown (struct vfs_class *me, const char *path, int owner, int group)
155 (void) me;
157 return chown (path, owner, group);
160 static int
161 local_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
163 (void) me;
165 return utime (path, times);
168 static int
169 local_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
171 (void) me;
173 return readlink (path, buf, size);
176 static int
177 local_unlink (struct vfs_class *me, const char *path)
179 (void) me;
181 return unlink (path);
184 static int
185 local_symlink (struct vfs_class *me, const char *n1, const char *n2)
187 (void) me;
189 return symlink (n1, n2);
192 static ssize_t
193 local_write (void *data, const char *buf, int nbyte)
195 int fd;
196 int n;
198 if (!data)
199 return -1;
201 fd = * (int *) data;
202 while ((n = write (fd, buf, nbyte)) == -1){
203 #ifdef EAGAIN
204 if (errno == EAGAIN) continue;
205 #endif
206 #ifdef EINTR
207 if (errno == EINTR) continue;
208 #endif
209 break;
211 return n;
214 static int
215 local_rename (struct vfs_class *me, const char *a, const char *b)
217 (void) me;
219 return rename (a, b);
222 static int
223 local_chdir (struct vfs_class *me, const char *path)
225 (void) me;
227 return chdir (path);
230 off_t
231 local_lseek (void *data, off_t offset, int whence)
233 int fd = * (int *) data;
235 return lseek (fd, offset, whence);
238 static int
239 local_mknod (struct vfs_class *me, const char *path, int mode, int dev)
241 (void) me;
243 return mknod (path, mode, dev);
246 static int
247 local_link (struct vfs_class *me, const char *p1, const char *p2)
249 (void) me;
251 return link (p1, p2);
254 static int
255 local_mkdir (struct vfs_class *me, const char *path, mode_t mode)
257 (void) me;
259 return mkdir (path, mode);
262 static int
263 local_rmdir (struct vfs_class *me, const char *path)
265 (void) me;
267 return rmdir (path);
270 static char *
271 local_getlocalcopy (struct vfs_class *me, const char *path)
273 (void) me;
275 return mhl_str_dup (path);
278 static int
279 local_ungetlocalcopy (struct vfs_class *me, const char *path,
280 const char *local, int has_changed)
282 (void) me;
283 (void) path;
284 (void) local;
285 (void) has_changed;
287 return 0;
290 static int
291 local_which (struct vfs_class *me, const char *path)
293 (void) me;
294 (void) path;
296 return 0; /* Every path which other systems do not like is expected to be ours */
299 void
300 init_localfs (void)
302 vfs_local_ops.name = "localfs";
303 vfs_local_ops.flags = VFSF_LOCAL;
304 vfs_local_ops.which = local_which;
305 vfs_local_ops.open = local_open;
306 vfs_local_ops.close = local_close;
307 vfs_local_ops.read = local_read;
308 vfs_local_ops.write = local_write;
309 vfs_local_ops.opendir = local_opendir;
310 vfs_local_ops.readdir = local_readdir;
311 vfs_local_ops.closedir = local_closedir;
312 vfs_local_ops.stat = local_stat;
313 vfs_local_ops.lstat = local_lstat;
314 vfs_local_ops.fstat = local_fstat;
315 vfs_local_ops.chmod = local_chmod;
316 vfs_local_ops.chown = local_chown;
317 vfs_local_ops.utime = local_utime;
318 vfs_local_ops.readlink = local_readlink;
319 vfs_local_ops.symlink = local_symlink;
320 vfs_local_ops.link = local_link;
321 vfs_local_ops.unlink = local_unlink;
322 vfs_local_ops.rename = local_rename;
323 vfs_local_ops.chdir = local_chdir;
324 vfs_local_ops.ferrno = local_errno;
325 vfs_local_ops.lseek = local_lseek;
326 vfs_local_ops.mknod = local_mknod;
327 vfs_local_ops.getlocalcopy = local_getlocalcopy;
328 vfs_local_ops.ungetlocalcopy = local_ungetlocalcopy;
329 vfs_local_ops.mkdir = local_mkdir;
330 vfs_local_ops.rmdir = local_rmdir;
331 vfs_register_class (&vfs_local_ops);