r23792: convert Samba4 to GPLv3
[mdw/samba.git] / source4 / ntvfs / simple / svfs_util.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    simple NTVFS filesystem backend
5
6    Copyright (C) Andrew Tridgell 2003
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 /*
22   utility functions for simple backend
23 */
24
25 #include "includes.h"
26 #include "system/filesys.h"
27 #include "svfs.h"
28 #include "system/time.h"
29 #include "system/dir.h"
30 #include "ntvfs/ntvfs.h"
31
32 /*
33   convert a windows path to a unix path - don't do any manging or case sensitive handling
34 */
35 char *svfs_unix_path(struct ntvfs_module_context *ntvfs,
36                      struct ntvfs_request *req, const char *name)
37 {
38         struct svfs_private *private = ntvfs->private_data;
39         char *ret;
40
41         if (*name != '\\') {
42                 ret = talloc_asprintf(req, "%s/%s", private->connectpath, name);
43         } else {
44                 ret = talloc_asprintf(req, "%s%s", private->connectpath, name);
45         }
46         all_string_sub(ret, "\\", "/", 0);
47
48         strlower(ret + strlen(private->connectpath));
49
50         return ret;
51 }
52
53
54 /*
55   read a directory and find all matching file names and stat info
56   returned names are separate unix and DOS names. The returned names
57   are relative to the directory
58 */
59 struct svfs_dir *svfs_list_unix(TALLOC_CTX *mem_ctx, struct ntvfs_request *req, const char *unix_path)
60 {
61         char *p, *mask;
62         struct svfs_dir *dir;
63         DIR *odir;
64         struct dirent *dent;
65         uint_t allocated = 0;
66         char *low_mask;
67
68         dir = talloc(mem_ctx, struct svfs_dir);
69         if (!dir) { return NULL; }
70
71         dir->count = 0;
72         dir->files = 0;
73
74         /* find the base directory */
75         p = strrchr(unix_path, '/');
76         if (!p) { return NULL; }
77
78         dir->unix_dir = talloc_strndup(mem_ctx, unix_path, PTR_DIFF(p, unix_path));
79         if (!dir->unix_dir) { return NULL; }
80
81         /* the wildcard pattern is the last part */
82         mask = p+1;
83
84         low_mask = talloc_strdup(mem_ctx, mask);
85         if (!low_mask) { return NULL; }
86         strlower(low_mask);
87
88         odir = opendir(dir->unix_dir);
89         if (!odir) { return NULL; }
90
91         while ((dent = readdir(odir))) {
92                 uint_t i = dir->count;
93                 char *full_name;
94                 char *low_name;
95
96                 if (strchr(dent->d_name, ':') && !strchr(unix_path, ':')) {
97                         /* don't show streams in dir listing */
98                         continue;
99                 }
100
101                 low_name = talloc_strdup(mem_ctx, dent->d_name);
102                 if (!low_name) { continue; }
103                 strlower(low_name);
104
105                 /* check it matches the wildcard pattern */
106                 if (ms_fnmatch(low_mask, low_name, PROTOCOL_NT1) != 0) {
107                         continue;
108                 }
109                 
110                 if (dir->count >= allocated) {
111                         allocated = (allocated + 100) * 1.2;
112                         dir->files = talloc_realloc(dir, dir->files, struct svfs_dirfile, allocated);
113                         if (!dir->files) { 
114                                 closedir(odir);
115                                 return NULL;
116                         }
117                 }
118
119                 dir->files[i].name = low_name;
120                 if (!dir->files[i].name) { continue; }
121
122                 asprintf(&full_name, "%s/%s", dir->unix_dir, dir->files[i].name);
123                 if (!full_name) { continue; }
124
125                 if (stat(full_name, &dir->files[i].st) == 0) { 
126                         dir->count++;
127                 }
128
129                 free(full_name); 
130         }
131
132         closedir(odir);
133
134         return dir;
135 }
136
137 /*
138   read a directory and find all matching file names and stat info
139   returned names are separate unix and DOS names. The returned names
140   are relative to the directory
141 */
142 struct svfs_dir *svfs_list(struct ntvfs_module_context *ntvfs, struct ntvfs_request *req, const char *pattern)
143 {
144         struct svfs_private *private = ntvfs->private_data;
145         char *unix_path;
146
147         unix_path = svfs_unix_path(ntvfs, req, pattern);
148         if (!unix_path) { return NULL; }
149
150         return svfs_list_unix(private, req, unix_path);
151 }
152
153
154 /*******************************************************************
155 set the time on a file via file descriptor
156 *******************************************************************/
157 int svfs_file_utime(int fd, struct utimbuf *times)
158 {
159         char *fd_path = NULL;
160         int ret;
161
162         asprintf(&fd_path, "/proc/self/%d", fd);
163         if (!fd_path) {
164                 errno = ENOMEM;
165                 return -1;
166         }
167         
168         ret = utime(fd_path, times);
169         free(fd_path);
170         return ret;
171 }
172
173
174 /*
175   map a unix file attrib to a DOS attribute
176 */
177 uint16_t svfs_unix_to_dos_attrib(mode_t mode)
178 {
179         uint16_t ret = 0;
180         if (S_ISDIR(mode)) ret |= FILE_ATTRIBUTE_DIRECTORY;
181         if (!(mode & S_IWUSR)) ret |= FILE_ATTRIBUTE_READONLY;
182         return ret;
183 }
184