2 Unix SMB/CIFS implementation.
4 simpler Samba VFS filesystem backend for clients which support the
5 CIFS Unix Extensions or newer CIFS POSIX protocol extensions
8 Copyright (C) Andrew Tridgell 2003
9 Copyright (C) Steve French 2006
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 utility functions for cifs posix backend
29 #include "system/filesys.h"
30 #include "cifsposix.h"
31 #include "system/time.h"
32 #include "system/dir.h"
33 #include "ntvfs/ntvfs.h"
34 #include "ntvfs/cifs_posix_cli/proto.h"
37 convert a windows path to a unix path - don't do any manging or case sensitive handling
39 char *cifspsx_unix_path(struct ntvfs_module_context *ntvfs,
40 struct ntvfs_request *req, const char *name)
42 struct cifspsx_private *p = ntvfs->private_data;
44 char *name_lower = strlower_talloc(p, name);
47 ret = talloc_asprintf(req, "%s/%s", p->connectpath, name_lower);
49 ret = talloc_asprintf(req, "%s%s", p->connectpath, name_lower);
51 all_string_sub(ret, "\\", "/", 0);
52 talloc_free(name_lower);
58 read a directory and find all matching file names and stat info
59 returned names are separate unix and DOS names. The returned names
60 are relative to the directory
62 struct cifspsx_dir *cifspsx_list_unix(TALLOC_CTX *mem_ctx, struct ntvfs_request *req, const char *unix_path)
65 struct cifspsx_dir *dir;
68 unsigned int allocated = 0;
71 dir = talloc(mem_ctx, struct cifspsx_dir);
72 if (!dir) { return NULL; }
77 /* find the base directory */
78 p = strrchr(unix_path, '/');
79 if (!p) { return NULL; }
81 dir->unix_dir = talloc_strndup(mem_ctx, unix_path, PTR_DIFF(p, unix_path));
82 if (!dir->unix_dir) { return NULL; }
84 /* the wildcard pattern is the last part */
87 low_mask = strlower_talloc(mem_ctx, mask);
88 if (!low_mask) { return NULL; }
90 odir = opendir(dir->unix_dir);
91 if (!odir) { return NULL; }
93 while ((dent = readdir(odir))) {
94 unsigned int i = dir->count;
99 if (strchr(dent->d_name, ':') && !strchr(unix_path, ':')) {
100 /* don't show streams in dir listing */
104 low_name = strlower_talloc(mem_ctx, dent->d_name);
105 if (!low_name) { continue; }
107 /* check it matches the wildcard pattern */
108 if (ms_fnmatch_protocol(low_mask, low_name, PROTOCOL_NT1) != 0) {
112 if (dir->count >= allocated) {
113 allocated = (allocated + 100) * 1.2;
114 dir->files = talloc_realloc(dir, dir->files, struct cifspsx_dirfile, allocated);
121 dir->files[i].name = low_name;
122 if (!dir->files[i].name) { continue; }
124 ret = asprintf(&full_name, "%s/%s", dir->unix_dir, dir->files[i].name);
129 if (!full_name) { continue; }
131 if (stat(full_name, &dir->files[i].st) == 0) {
144 read a directory and find all matching file names and stat info
145 returned names are separate unix and DOS names. The returned names
146 are relative to the directory
148 struct cifspsx_dir *cifspsx_list(struct ntvfs_module_context *ntvfs, struct ntvfs_request *req, const char *pattern)
150 struct cifspsx_private *p = ntvfs->private_data;
153 unix_path = cifspsx_unix_path(ntvfs, req, pattern);
154 if (!unix_path) { return NULL; }
156 return cifspsx_list_unix(p, req, unix_path);
160 /*******************************************************************
161 set the time on a file via file descriptor
162 *******************************************************************/
163 int cifspsx_file_utime(int fd, struct utimbuf *times)
165 char *fd_path = NULL;
168 ret = asprintf(&fd_path, "/proc/self/%d", fd);
178 ret = utime(fd_path, times);
185 map a unix file attrib to a DOS attribute
187 uint16_t cifspsx_unix_to_dos_attrib(mode_t mode)
190 if (S_ISDIR(mode)) ret |= FILE_ATTRIBUTE_DIRECTORY;
191 if (!(mode & S_IWUSR)) ret |= FILE_ATTRIBUTE_READONLY;