Remove "store create time" code, cause create time to be stored
[Samba/fernandojvsilva.git] / source3 / modules / vfs_netatalk.c
blobfa9e774c1a718a7517633a4b6d9532c2a71cb922
1 /*
2 * AppleTalk VFS module for Samba-3.x
4 * Copyright (C) Alexei Kotovich, 2002
5 * Copyright (C) Stefan (metze) Metzmacher, 2003
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_VFS
26 #define APPLEDOUBLE ".AppleDouble"
27 #define ADOUBLEMODE 0777
29 /* atalk functions */
31 static int atalk_build_paths(TALLOC_CTX *ctx, const char *path,
32 const char *fname, char **adbl_path, char **orig_path,
33 SMB_STRUCT_STAT *adbl_info, SMB_STRUCT_STAT *orig_info);
35 static int atalk_unlink_file(const char *path);
37 static int atalk_get_path_ptr(char *path)
39 int i = 0;
40 int ptr = 0;
42 for (i = 0; path[i]; i ++) {
43 if (path[i] == '/')
44 ptr = i;
45 /* get out some 'spam';) from win32's file name */
46 else if (path[i] == ':') {
47 path[i] = '\0';
48 break;
52 return ptr;
55 static int atalk_build_paths(TALLOC_CTX *ctx, const char *path, const char *fname,
56 char **adbl_path, char **orig_path,
57 SMB_STRUCT_STAT *adbl_info, SMB_STRUCT_STAT *orig_info)
59 int ptr0 = 0;
60 int ptr1 = 0;
61 char *dname = 0;
62 char *name = 0;
64 if (!ctx || !path || !fname || !adbl_path || !orig_path ||
65 !adbl_info || !orig_info)
66 return -1;
67 #if 0
68 DEBUG(3, ("ATALK: PATH: %s[%s]\n", path, fname));
69 #endif
70 if (strstr(path, APPLEDOUBLE) || strstr(fname, APPLEDOUBLE)) {
71 DEBUG(3, ("ATALK: path %s[%s] already contains %s\n", path, fname, APPLEDOUBLE));
72 return -1;
75 if (fname[0] == '.') ptr0 ++;
76 if (fname[1] == '/') ptr0 ++;
78 *orig_path = talloc_asprintf(ctx, "%s/%s", path, &fname[ptr0]);
80 /* get pointer to last '/' */
81 ptr1 = atalk_get_path_ptr(*orig_path);
83 sys_lstat(*orig_path, orig_info);
85 if (S_ISDIR(orig_info->st_ex_mode)) {
86 *adbl_path = talloc_asprintf(ctx, "%s/%s/%s/",
87 path, &fname[ptr0], APPLEDOUBLE);
88 } else {
89 dname = talloc_strdup(ctx, *orig_path);
90 dname[ptr1] = '\0';
91 name = *orig_path;
92 *adbl_path = talloc_asprintf(ctx, "%s/%s/%s",
93 dname, APPLEDOUBLE, &name[ptr1 + 1]);
95 #if 0
96 DEBUG(3, ("ATALK: DEBUG:\n%s\n%s\n", *orig_path, *adbl_path));
97 #endif
98 sys_lstat(*adbl_path, adbl_info);
99 return 0;
102 static int atalk_unlink_file(const char *path)
104 int ret = 0;
106 become_root();
107 ret = unlink(path);
108 unbecome_root();
110 return ret;
113 static void atalk_add_to_list(name_compare_entry **list)
115 int i, count = 0;
116 name_compare_entry *new_list = 0;
117 name_compare_entry *cur_list = 0;
119 cur_list = *list;
121 if (cur_list) {
122 for (i = 0, count = 0; cur_list[i].name; i ++, count ++) {
123 if (strstr(cur_list[i].name, APPLEDOUBLE))
124 return;
128 if (!(new_list = SMB_CALLOC_ARRAY(name_compare_entry, (count == 0 ? 1 : count + 1))))
129 return;
131 for (i = 0; i < count; i ++) {
132 new_list[i].name = SMB_STRDUP(cur_list[i].name);
133 new_list[i].is_wild = cur_list[i].is_wild;
136 new_list[i].name = SMB_STRDUP(APPLEDOUBLE);
137 new_list[i].is_wild = False;
139 free_namearray(*list);
141 *list = new_list;
142 new_list = 0;
143 cur_list = 0;
146 static void atalk_rrmdir(TALLOC_CTX *ctx, char *path)
148 char *dpath;
149 SMB_STRUCT_DIRENT *dent = 0;
150 SMB_STRUCT_DIR *dir;
152 if (!path) return;
154 dir = sys_opendir(path);
155 if (!dir) return;
157 while (NULL != (dent = sys_readdir(dir))) {
158 if (strcmp(dent->d_name, ".") == 0 ||
159 strcmp(dent->d_name, "..") == 0)
160 continue;
161 if (!(dpath = talloc_asprintf(ctx, "%s/%s",
162 path, dent->d_name)))
163 continue;
164 atalk_unlink_file(dpath);
167 sys_closedir(dir);
170 /* Disk operations */
172 /* Directory operations */
174 static SMB_STRUCT_DIR *atalk_opendir(struct vfs_handle_struct *handle, const char *fname, const char *mask, uint32 attr)
176 SMB_STRUCT_DIR *ret = 0;
178 ret = SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
181 * when we try to perform delete operation upon file which has fork
182 * in ./.AppleDouble and this directory wasn't hidden by Samba,
183 * MS Windows explorer causes the error: "Cannot find the specified file"
184 * There is some workaround to avoid this situation, i.e. if
185 * connection has not .AppleDouble entry in either veto or hide
186 * list then it would be nice to add one.
189 atalk_add_to_list(&handle->conn->hide_list);
190 atalk_add_to_list(&handle->conn->veto_list);
192 return ret;
195 static int atalk_rmdir(struct vfs_handle_struct *handle, const char *path)
197 bool add = False;
198 TALLOC_CTX *ctx = 0;
199 char *dpath;
201 if (!handle->conn->origpath || !path) goto exit_rmdir;
203 /* due to there is no way to change bDeleteVetoFiles variable
204 * from this module, gotta use talloc stuff..
207 strstr(path, APPLEDOUBLE) ? (add = False) : (add = True);
209 if (!(ctx = talloc_init("remove_directory")))
210 goto exit_rmdir;
212 if (!(dpath = talloc_asprintf(ctx, "%s/%s%s",
213 handle->conn->origpath, path, add ? "/"APPLEDOUBLE : "")))
214 goto exit_rmdir;
216 atalk_rrmdir(ctx, dpath);
218 exit_rmdir:
219 talloc_destroy(ctx);
220 return SMB_VFS_NEXT_RMDIR(handle, path);
223 /* File operations */
225 static int atalk_rename(struct vfs_handle_struct *handle,
226 const struct smb_filename *smb_fname_src,
227 const struct smb_filename *smb_fname_dst)
229 int ret = 0;
230 char *oldname = NULL;
231 char *adbl_path = NULL;
232 char *orig_path = NULL;
233 SMB_STRUCT_STAT adbl_info;
234 SMB_STRUCT_STAT orig_info;
235 NTSTATUS status;
237 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
239 status = get_full_smb_filename(talloc_tos(), smb_fname_src, &oldname);
240 if (!NT_STATUS_IS_OK(status)) {
241 return ret;
244 if (atalk_build_paths(talloc_tos(), handle->conn->origpath, oldname,
245 &adbl_path, &orig_path, &adbl_info,
246 &orig_info) != 0)
247 goto exit_rename;
249 if (S_ISDIR(orig_info.st_ex_mode) || S_ISREG(orig_info.st_ex_mode)) {
250 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));
251 goto exit_rename;
254 atalk_unlink_file(adbl_path);
256 exit_rename:
257 TALLOC_FREE(oldname);
258 TALLOC_FREE(adbl_path);
259 TALLOC_FREE(orig_path);
260 return ret;
263 static int atalk_unlink(struct vfs_handle_struct *handle,
264 const struct smb_filename *smb_fname)
266 int ret = 0, i;
267 char *path = NULL;
268 char *adbl_path = NULL;
269 char *orig_path = NULL;
270 SMB_STRUCT_STAT adbl_info;
271 SMB_STRUCT_STAT orig_info;
272 NTSTATUS status;
274 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
276 status = get_full_smb_filename(talloc_tos(), smb_fname, &path);
277 if (!NT_STATUS_IS_OK(status)) {
278 return ret;
281 /* no .AppleDouble sync if veto or hide list is empty,
282 * otherwise "Cannot find the specified file" error will be caused
285 if (!handle->conn->veto_list) return ret;
286 if (!handle->conn->hide_list) return ret;
288 for (i = 0; handle->conn->veto_list[i].name; i ++) {
289 if (strstr(handle->conn->veto_list[i].name, APPLEDOUBLE))
290 break;
293 if (!handle->conn->veto_list[i].name) {
294 for (i = 0; handle->conn->hide_list[i].name; i ++) {
295 if (strstr(handle->conn->hide_list[i].name, APPLEDOUBLE))
296 break;
297 else {
298 DEBUG(3, ("ATALK: %s is not hidden, skipped..\n",
299 APPLEDOUBLE));
300 goto exit_unlink;
305 if (atalk_build_paths(talloc_tos(), handle->conn->origpath, path,
306 &adbl_path, &orig_path,
307 &adbl_info, &orig_info) != 0)
308 goto exit_unlink;
310 if (S_ISDIR(orig_info.st_ex_mode) || S_ISREG(orig_info.st_ex_mode)) {
311 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path));
312 goto exit_unlink;
315 atalk_unlink_file(adbl_path);
317 exit_unlink:
318 TALLOC_FREE(path);
319 TALLOC_FREE(adbl_path);
320 TALLOC_FREE(orig_path);
321 return ret;
324 static int atalk_chmod(struct vfs_handle_struct *handle, const char *path, mode_t mode)
326 int ret = 0;
327 char *adbl_path = 0;
328 char *orig_path = 0;
329 SMB_STRUCT_STAT adbl_info;
330 SMB_STRUCT_STAT orig_info;
331 TALLOC_CTX *ctx;
333 ret = SMB_VFS_NEXT_CHMOD(handle, path, mode);
335 if (!path) return ret;
337 if (!(ctx = talloc_init("chmod_file")))
338 return ret;
340 if (atalk_build_paths(ctx, handle->conn->origpath, path, &adbl_path, &orig_path,
341 &adbl_info, &orig_info) != 0)
342 goto exit_chmod;
344 if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
345 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));
346 goto exit_chmod;
349 chmod(adbl_path, ADOUBLEMODE);
351 exit_chmod:
352 talloc_destroy(ctx);
353 return ret;
356 static int atalk_chown(struct vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
358 int ret = 0;
359 char *adbl_path = 0;
360 char *orig_path = 0;
361 SMB_STRUCT_STAT adbl_info;
362 SMB_STRUCT_STAT orig_info;
363 TALLOC_CTX *ctx;
365 ret = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
367 if (!path) return ret;
369 if (!(ctx = talloc_init("chown_file")))
370 return ret;
372 if (atalk_build_paths(ctx, handle->conn->origpath, path, &adbl_path, &orig_path,
373 &adbl_info, &orig_info) != 0)
374 goto exit_chown;
376 if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
377 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));
378 goto exit_chown;
381 if (chown(adbl_path, uid, gid) == -1) {
382 DEBUG(3, ("ATALK: chown error %s\n", strerror(errno)));
385 exit_chown:
386 talloc_destroy(ctx);
387 return ret;
390 static int atalk_lchown(struct vfs_handle_struct *handle, const char *path, uid_t uid, gid_t gid)
392 int ret = 0;
393 char *adbl_path = 0;
394 char *orig_path = 0;
395 SMB_STRUCT_STAT adbl_info;
396 SMB_STRUCT_STAT orig_info;
397 TALLOC_CTX *ctx;
399 ret = SMB_VFS_NEXT_CHOWN(handle, path, uid, gid);
401 if (!path) return ret;
403 if (!(ctx = talloc_init("lchown_file")))
404 return ret;
406 if (atalk_build_paths(ctx, handle->conn->origpath, path, &adbl_path, &orig_path,
407 &adbl_info, &orig_info) != 0)
408 goto exit_lchown;
410 if (!S_ISDIR(orig_info.st_ex_mode) && !S_ISREG(orig_info.st_ex_mode)) {
411 DEBUG(3, ("ATALK: %s has passed..\n", orig_path));
412 goto exit_lchown;
415 if (lchown(adbl_path, uid, gid) == -1) {
416 DEBUG(3, ("ATALK: lchown error %s\n", strerror(errno)));
419 exit_lchown:
420 talloc_destroy(ctx);
421 return ret;
424 static struct vfs_fn_pointers vfs_netatalk_fns = {
425 .opendir = atalk_opendir,
426 .rmdir = atalk_rmdir,
427 .rename = atalk_rename,
428 .unlink = atalk_unlink,
429 .chmod = atalk_chmod,
430 .chown = atalk_chown,
431 .lchown = atalk_lchown,
434 NTSTATUS vfs_netatalk_init(void);
435 NTSTATUS vfs_netatalk_init(void)
437 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "netatalk",
438 &vfs_netatalk_fns);