42d869209d9b0d4a40fac7c9f88e1a70c75d261e
[kai/samba-autobuild/.git] / source4 / ntvfs / cifs_posix_cli / svfs_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    simpler Samba VFS filesystem backend for clients which support the
5    CIFS Unix Extensions or newer CIFS POSIX protocol extensions
6
7
8    Copyright (C) Andrew Tridgell 2003
9    Copyright (C) Steve French 2006
10
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.
15    
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.
20    
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/>.
23 */
24 /*
25   utility functions for cifs posix backend
26 */
27
28 #include "includes.h"
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
35 /*
36   convert a windows path to a unix path - don't do any manging or case sensitive handling
37 */
38 char *cifspsx_unix_path(struct ntvfs_module_context *ntvfs,
39                      struct ntvfs_request *req, const char *name)
40 {
41         struct cifspsx_private *private = ntvfs->private_data;
42         char *ret;
43
44         if (*name != '\\') {
45                 ret = talloc_asprintf(req, "%s/%s", private->connectpath, name);
46         } else {
47                 ret = talloc_asprintf(req, "%s%s", private->connectpath, name);
48         }
49         all_string_sub(ret, "\\", "/", 0);
50
51         strlower(ret + strlen(private->connectpath));
52
53         return ret;
54 }
55
56
57 /*
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
61 */
62 struct cifspsx_dir *cifspsx_list_unix(TALLOC_CTX *mem_ctx, struct ntvfs_request *req, const char *unix_path)
63 {
64         char *p, *mask;
65         struct cifspsx_dir *dir;
66         DIR *odir;
67         struct dirent *dent;
68         uint_t allocated = 0;
69         char *low_mask;
70
71         dir = talloc(mem_ctx, struct cifspsx_dir);
72         if (!dir) { return NULL; }
73
74         dir->count = 0;
75         dir->files = 0;
76
77         /* find the base directory */
78         p = strrchr(unix_path, '/');
79         if (!p) { return NULL; }
80
81         dir->unix_dir = talloc_strndup(mem_ctx, unix_path, PTR_DIFF(p, unix_path));
82         if (!dir->unix_dir) { return NULL; }
83
84         /* the wildcard pattern is the last part */
85         mask = p+1;
86
87         low_mask = talloc_strdup(mem_ctx, mask);
88         if (!low_mask) { return NULL; }
89         strlower(low_mask);
90
91         odir = opendir(dir->unix_dir);
92         if (!odir) { return NULL; }
93
94         while ((dent = readdir(odir))) {
95                 uint_t i = dir->count;
96                 char *full_name;
97                 char *low_name;
98
99                 if (strchr(dent->d_name, ':') && !strchr(unix_path, ':')) {
100                         /* don't show streams in dir listing */
101                         continue;
102                 }
103
104                 low_name = talloc_strdup(mem_ctx, dent->d_name);
105                 if (!low_name) { continue; }
106                 strlower(low_name);
107
108                 /* check it matches the wildcard pattern */
109                 if (ms_fnmatch(low_mask, low_name, PROTOCOL_NT1) != 0) {
110                         continue;
111                 }
112                 
113                 if (dir->count >= allocated) {
114                         allocated = (allocated + 100) * 1.2;
115                         dir->files = talloc_realloc(dir, dir->files, struct cifspsx_dirfile, allocated);
116                         if (!dir->files) { 
117                                 closedir(odir);
118                                 return NULL;
119                         }
120                 }
121
122                 dir->files[i].name = low_name;
123                 if (!dir->files[i].name) { continue; }
124
125                 asprintf(&full_name, "%s/%s", dir->unix_dir, dir->files[i].name);
126                 if (!full_name) { continue; }
127
128                 if (stat(full_name, &dir->files[i].st) == 0) { 
129                         dir->count++;
130                 }
131
132                 free(full_name); 
133         }
134
135         closedir(odir);
136
137         return dir;
138 }
139
140 /*
141   read a directory and find all matching file names and stat info
142   returned names are separate unix and DOS names. The returned names
143   are relative to the directory
144 */
145 struct cifspsx_dir *cifspsx_list(struct ntvfs_module_context *ntvfs, struct ntvfs_request *req, const char *pattern)
146 {
147         struct cifspsx_private *private = ntvfs->private_data;
148         char *unix_path;
149
150         unix_path = cifspsx_unix_path(ntvfs, req, pattern);
151         if (!unix_path) { return NULL; }
152
153         return cifspsx_list_unix(private, req, unix_path);
154 }
155
156
157 /*******************************************************************
158 set the time on a file via file descriptor
159 *******************************************************************/
160 int cifspsx_file_utime(int fd, struct utimbuf *times)
161 {
162         char *fd_path = NULL;
163         int ret;
164
165         asprintf(&fd_path, "/proc/self/%d", fd);
166         if (!fd_path) {
167                 errno = ENOMEM;
168                 return -1;
169         }
170         
171         ret = utime(fd_path, times);
172         free(fd_path);
173         return ret;
174 }
175
176
177 /*
178   map a unix file attrib to a DOS attribute
179 */
180 uint16_t cifspsx_unix_to_dos_attrib(mode_t mode)
181 {
182         uint16_t ret = 0;
183         if (S_ISDIR(mode)) ret |= FILE_ATTRIBUTE_DIRECTORY;
184         if (!(mode & S_IWUSR)) ret |= FILE_ATTRIBUTE_READONLY;
185         return ret;
186 }
187