Removed tabs.
[usmb.git] / usmb_dir.c
blob506673086ee1fff7d9374158483bfe86a5d0115b
1 /* usmb - mount SMB shares via FUSE and Samba
2 * Copyright (C) 2006-2009 Geoff Johnstone
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "config.h"
18 #include <sys/time.h> // struct timeval needed by libsmbclient.h
19 #include <libsmbclient.h>
20 #include "samba3x-compat.h"
21 #include <fuse.h>
22 #include <dirent.h>
23 #include <errno.h>
24 #include <stdarg.h>
25 #include <stddef.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "usmb_dir.h"
30 #include "usmb.h"
31 #include "utils.h"
34 int usmb_mkdir (const char *dirname, mode_t mode)
36 char *url = make_url (dirname);
37 if (NULL == url)
38 return -ENOMEM;
40 DEBUG (fprintf (stderr, "mkdir (%s)\n", url));
41 int ret = smbc_getFunctionMkdir (ctx) (ctx, url, mode) ? -errno : 0;
42 free (url);
43 return ret;
47 int usmb_rmdir (const char *dirname)
49 char *url = make_url (dirname);
50 if (NULL == url)
51 return -ENOMEM;
53 DEBUG (fprintf (stderr, "rmdir (%s)\n", url));
54 int ret = smbc_getFunctionRmdir (ctx) (ctx, url) ? -errno : 0;
55 free (url);
57 return ret;
61 int usmb_opendir (const char *dirname, struct fuse_file_info *fi)
63 char *url = make_url (dirname);
64 if (NULL == url)
65 return -ENOMEM;
67 DEBUG (fprintf (stderr, "opendir (%s)", url));
68 SMBCFILE *file = smbc_getFunctionOpendir (ctx) (ctx, url);
69 DEBUG (fprintf (stderr, " = %p\n", (void *)file));
71 int ret = (NULL == file) ? -errno : 0;
72 free (url);
73 fi->fh = smbcfile_to_fd (file);
75 return ret;
79 int usmb_readdir (const char *path, void *h, fuse_fill_dir_t filler,
80 off_t offset UNUSED, struct fuse_file_info *fi UNUSED)
82 SMBCCTX *ctx = NULL;
83 SMBCFILE *file = NULL;
84 char *url = NULL;
85 struct smbc_dirent *dirent;
86 int ret = 0;
88 DEBUG (fprintf (stderr, "readdir (%s)\n", path));
90 if (!create_smb_context (&ctx))
91 return -errno;
95 url = make_url (path);
96 if (NULL == url)
98 ret = -ENOMEM;
99 break;
102 file = smbc_getFunctionOpendir (ctx) (ctx, url);
103 if (NULL == file)
105 ret = -errno;
106 break;
109 smbc_getFunctionLseekdir (ctx) (ctx, file, 0);
111 while (NULL != (dirent = smbc_getFunctionReaddir (ctx) (ctx, file)))
113 struct stat stbuf;
115 switch (dirent->smbc_type)
117 case SMBC_DIR:
118 stbuf.st_mode = DT_DIR << 12;
119 break;
121 case SMBC_FILE:
122 stbuf.st_mode = DT_REG << 12;
123 break;
125 case SMBC_LINK:
126 stbuf.st_mode = DT_LNK << 12;
127 break;
129 default:
130 break;
133 DEBUG (fprintf (stderr, " %s\n", dirent->name));
134 if (1 == filler (h, dirent->name, &stbuf, 0)) /* if error */
136 ret = -1;
137 break;
140 } while (false /*CONSTCOND*/);
142 if (NULL != file)
143 (void)smbc_getFunctionClosedir (ctx) (ctx, file);
145 if (NULL != url)
146 free (url);
148 destroy_smb_context (ctx, 0);
149 return ret;
153 int usmb_releasedir (const char *path UNUSED, struct fuse_file_info *fi)
155 SMBCFILE *file = fd_to_smbcfile (fi->fh);
156 DEBUG (fprintf (stderr, "releasedir (%s, %p)\n", path, (void *)file));
157 return (0 > smbc_getFunctionClosedir (ctx) (ctx, file)) ? -errno : 0;
161 int usmb_setxattr (const char *path, const char *name, const char *value,
162 size_t size, int flags)
164 char *url = make_url (path);
165 if (NULL == url)
166 return -ENOMEM;
168 DEBUG (fprintf (stderr, "setxattr (%s, %s, %p, %u, %x)\n",
169 path, url, value, size, flags));
170 int ret = smbc_getFunctionSetxattr (ctx) (ctx, url, name,
171 value, size, flags) ? -errno : 0;
172 free (url);
173 return ret;
177 int usmb_getxattr (const char *path, const char *name, char *value, size_t size)
179 char *url = make_url (path);
180 if (NULL == url)
181 return -ENOMEM;
183 DEBUG (fprintf (stderr, "getxattr (%s, %s, %p, %u)\n",
184 path, url, value, size));
185 int ret = smbc_getFunctionGetxattr (ctx) (ctx, url, name,
186 value, size) ? -errno : 0;
187 free (url);
188 return ret;
192 int usmb_listxattr (const char *path, char *list, size_t size)
194 char *url = make_url (path);
195 if (NULL == url)
196 return -ENOMEM;
198 DEBUG (fprintf (stderr, "listxattr (%s, %p, %u)\n", url, list, size));
199 int ret = smbc_getFunctionListxattr (ctx) (ctx, url, list, size) ? -errno : 0;
200 free (url);
201 return ret;
205 int usmb_removexattr (const char *path, const char *name)
207 char *url = make_url (path);
208 if (NULL == url)
209 return -ENOMEM;
211 DEBUG (fprintf (stderr, "removexattr (%s, %s)\n", url, name));
212 int ret = smbc_getFunctionRemovexattr (ctx) (ctx, url, name) ? -errno : 0;
213 free (url);
214 return ret;