2 * AppleTalk VFS module for Samba-3.x
4 * Copyright (C) Alexei Kotovich, 2002
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
38 #define APPLEDOUBLE ".AppleDouble"
39 #define ADOUBLEMODE 0777
43 static int atalk_build_paths(TALLOC_CTX
*ctx
, const char *path
,
44 const char *fname
, char **adbl_path
, char **orig_path
,
45 SMB_STRUCT_STAT
*adbl_info
, SMB_STRUCT_STAT
*orig_info
);
47 static int atalk_unlink_file(const char *path
);
49 static struct vfs_ops default_vfs_ops
; /* For passthrough operation */
50 static struct smb_vfs_handle_struct
*atalk_handle
;
52 static int atalk_get_path_ptr(char *path
)
57 for (i
= 0; path
[i
]; i
++) {
60 /* get out some 'spam';) from win32's file name */
61 else if (path
[i
] == ':') {
70 static int atalk_build_paths(TALLOC_CTX
*ctx
, const char *path
, const char *fname
,
71 char **adbl_path
, char **orig_path
,
72 SMB_STRUCT_STAT
*adbl_info
, SMB_STRUCT_STAT
*orig_info
)
79 if (!ctx
|| !path
|| !fname
|| !adbl_path
|| !orig_path
||
80 !adbl_info
|| !orig_info
)
83 DEBUG(3, ("ATALK: PATH: %s[%s]\n", path
, fname
));
85 if (strstr(path
, APPLEDOUBLE
) || strstr(fname
, APPLEDOUBLE
)) {
86 DEBUG(3, ("ATALK: path %s[%s] already contains %s\n", path
, fname
, APPLEDOUBLE
));
90 if (fname
[0] == '.') ptr0
++;
91 if (fname
[1] == '/') ptr0
++;
93 *orig_path
= talloc_asprintf(ctx
, "%s/%s", path
, &fname
[ptr0
]);
95 /* get pointer to last '/' */
96 ptr1
= atalk_get_path_ptr(*orig_path
);
98 sys_lstat(*orig_path
, orig_info
);
100 if (S_ISDIR(orig_info
->st_mode
)) {
101 *adbl_path
= talloc_asprintf(ctx
, "%s/%s/%s/",
102 path
, &fname
[ptr0
], APPLEDOUBLE
);
104 dname
= talloc_strdup(ctx
, *orig_path
);
107 *adbl_path
= talloc_asprintf(ctx
, "%s/%s/%s",
108 dname
, APPLEDOUBLE
, &name
[ptr1
+ 1]);
111 DEBUG(3, ("ATALK: DEBUG:\n%s\n%s\n", *orig_path
, *adbl_path
));
113 sys_lstat(*adbl_path
, adbl_info
);
117 static int atalk_unlink_file(const char *path
)
128 static void atalk_add_to_list(name_compare_entry
**list
)
131 name_compare_entry
*new_list
= 0;
132 name_compare_entry
*cur_list
= 0;
137 for (i
= 0, count
= 0; cur_list
[i
].name
; i
++, count
++) {
138 if (strstr(cur_list
[i
].name
, APPLEDOUBLE
))
143 if (!(new_list
= calloc(1,
144 (count
== 0 ? 1 : count
+ 1) * sizeof(name_compare_entry
))))
147 for (i
= 0; i
< count
; i
++) {
148 new_list
[i
].name
= strdup(cur_list
[i
].name
);
149 new_list
[i
].is_wild
= cur_list
[i
].is_wild
;
152 new_list
[i
].name
= strdup(APPLEDOUBLE
);
153 new_list
[i
].is_wild
= False
;
155 free_namearray(*list
);
162 static void atalk_rrmdir(TALLOC_CTX
*ctx
, char *path
)
165 struct dirent
*dent
= 0;
173 while (NULL
!= (dent
= readdir(dir
))) {
174 if (strcmp(dent
->d_name
, ".") == 0 ||
175 strcmp(dent
->d_name
, "..") == 0)
177 if (!(dpath
= talloc_asprintf(ctx
, "%s/%s",
178 path
, dent
->d_name
)))
180 atalk_unlink_file(dpath
);
186 /* Disk operations */
188 /* Directory operations */
190 DIR *atalk_opendir(struct connection_struct
*conn
, const char *fname
)
194 ret
= default_vfs_ops
.opendir(conn
, fname
);
197 * when we try to perform delete operation upon file which has fork
198 * in ./.AppleDouble and this directory wasn't hidden by Samba,
199 * MS Windows explorer causes the error: "Cannot find the specified file"
200 * There is some workaround to avoid this situation, i.e. if
201 * connection has not .AppleDouble entry in either veto or hide
202 * list then it would be nice to add one.
205 atalk_add_to_list(&conn
->hide_list
);
206 atalk_add_to_list(&conn
->veto_list
);
211 static int atalk_rmdir(struct connection_struct
*conn
, const char *path
)
217 if (!conn
|| !conn
->origpath
|| !path
) goto exit_rmdir
;
219 /* due to there is no way to change bDeleteVetoFiles variable
220 * from this module, gotta use talloc stuff..
223 strstr(path
, APPLEDOUBLE
) ? (add
= False
) : (add
= True
);
225 if (!(ctx
= talloc_init("remove_directory")))
228 if (!(dpath
= talloc_asprintf(ctx
, "%s/%s%s",
229 conn
->origpath
, path
, add
? "/"APPLEDOUBLE
: "")))
232 atalk_rrmdir(ctx
, dpath
);
236 return default_vfs_ops
.rmdir(conn
, path
);
239 /* File operations */
241 static int atalk_rename(struct connection_struct
*conn
, const char *old
, const char *new)
246 SMB_STRUCT_STAT adbl_info
;
247 SMB_STRUCT_STAT orig_info
;
250 ret
= default_vfs_ops
.rename(conn
, old
, new);
252 if (!conn
|| !old
) return ret
;
254 if (!(ctx
= talloc_init("rename_file")))
257 if (atalk_build_paths(ctx
, conn
->origpath
, old
, &adbl_path
, &orig_path
,
258 &adbl_info
, &orig_info
) != 0)
261 if (S_ISDIR(orig_info
.st_mode
) || S_ISREG(orig_info
.st_mode
)) {
262 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path
));
266 atalk_unlink_file(adbl_path
);
273 static int atalk_unlink(struct connection_struct
*conn
, const char *path
)
278 SMB_STRUCT_STAT adbl_info
;
279 SMB_STRUCT_STAT orig_info
;
282 ret
= default_vfs_ops
.unlink(conn
, path
);
284 if (!conn
|| !path
) return ret
;
286 /* no .AppleDouble sync if veto or hide list is empty,
287 * otherwise "Cannot find the specified file" error will be caused
290 if (!conn
->veto_list
) return ret
;
291 if (!conn
->hide_list
) return ret
;
293 for (i
= 0; conn
->veto_list
[i
].name
; i
++) {
294 if (strstr(conn
->veto_list
[i
].name
, APPLEDOUBLE
))
298 if (!conn
->veto_list
[i
].name
) {
299 for (i
= 0; conn
->hide_list
[i
].name
; i
++) {
300 if (strstr(conn
->hide_list
[i
].name
, APPLEDOUBLE
))
303 DEBUG(3, ("ATALK: %s is not hidden, skipped..\n",
310 if (!(ctx
= talloc_init("unlink_file")))
313 if (atalk_build_paths(ctx
, conn
->origpath
, path
, &adbl_path
, &orig_path
,
314 &adbl_info
, &orig_info
) != 0)
317 if (S_ISDIR(orig_info
.st_mode
) || S_ISREG(orig_info
.st_mode
)) {
318 DEBUG(3, ("ATALK: %s has passed..\n", adbl_path
));
322 atalk_unlink_file(adbl_path
);
329 static int atalk_chmod(struct connection_struct
*conn
, const char *path
, mode_t mode
)
334 SMB_STRUCT_STAT adbl_info
;
335 SMB_STRUCT_STAT orig_info
;
338 ret
= default_vfs_ops
.chmod(conn
, path
, mode
);
340 if (!conn
|| !path
) return ret
;
342 if (!(ctx
= talloc_init("chmod_file")))
345 if (atalk_build_paths(ctx
, conn
->origpath
, path
, &adbl_path
, &orig_path
,
346 &adbl_info
, &orig_info
) != 0)
349 if (!S_ISDIR(orig_info
.st_mode
) && !S_ISREG(orig_info
.st_mode
)) {
350 DEBUG(3, ("ATALK: %s has passed..\n", orig_path
));
354 chmod(adbl_path
, ADOUBLEMODE
);
361 static int atalk_chown(struct connection_struct
*conn
, const char *path
, uid_t uid
, gid_t gid
)
366 SMB_STRUCT_STAT adbl_info
;
367 SMB_STRUCT_STAT orig_info
;
370 ret
= default_vfs_ops
.chown(conn
, path
, uid
, gid
);
372 if (!conn
|| !path
) return ret
;
374 if (!(ctx
= talloc_init("chown_file")))
377 if (atalk_build_paths(ctx
, conn
->origpath
, path
, &adbl_path
, &orig_path
,
378 &adbl_info
, &orig_info
) != 0)
381 if (!S_ISDIR(orig_info
.st_mode
) && !S_ISREG(orig_info
.st_mode
)) {
382 DEBUG(3, ("ATALK: %s has passed..\n", orig_path
));
386 chown(adbl_path
, uid
, gid
);
393 static vfs_op_tuple atalk_ops
[] = {
395 /* Directory operations */
397 {atalk_opendir
, SMB_VFS_OP_OPENDIR
, SMB_VFS_LAYER_TRANSPARENT
},
398 {atalk_rmdir
, SMB_VFS_OP_RMDIR
, SMB_VFS_LAYER_TRANSPARENT
},
400 /* File operations */
402 {atalk_rename
, SMB_VFS_OP_RENAME
, SMB_VFS_LAYER_TRANSPARENT
},
403 {atalk_unlink
, SMB_VFS_OP_UNLINK
, SMB_VFS_LAYER_TRANSPARENT
},
404 {atalk_chmod
, SMB_VFS_OP_CHMOD
, SMB_VFS_LAYER_TRANSPARENT
},
405 {atalk_chown
, SMB_VFS_OP_CHOWN
, SMB_VFS_LAYER_TRANSPARENT
},
407 /* Finish VFS operations definition */
409 {NULL
, SMB_VFS_OP_NOOP
, SMB_VFS_LAYER_NOOP
}
412 /* VFS initialisation function. Return vfs_op_tuple array back to SAMBA. */
413 static vfs_op_tuple
*netatalk_init(const struct vfs_ops
*def_vfs_ops
,
414 struct smb_vfs_handle_struct
*vfs_handle
)
416 memcpy(&default_vfs_ops
, def_vfs_ops
, sizeof(struct vfs_ops
));
418 atalk_handle
= vfs_handle
;
420 DEBUG(3, ("ATALK: vfs module loaded\n"));
424 int vfs_netatalk_init(void)
426 return smb_register_vfs("netatalk", netatalk_init
, SMB_VFS_INTERFACE_VERSION
);