r65: added support for file streams in the simple NTVFS backend
[samba.git] / source / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 /*
23   utility functions for simple backend
24 */
25
26 #include "includes.h"
27 #include "svfs.h"
28
29 /*
30   convert a windows path to a unix path - don't do any manging or case sensitive handling
31 */
32 char *svfs_unix_path(struct request_context *req, const char *name)
33 {
34         struct svfs_private *private = req->conn->ntvfs_private;
35         char *ret;
36
37         if (*name != '\\') {
38                 ret = talloc_asprintf(req->mem_ctx, "%s/%s", private->connectpath, name);
39         } else {
40                 ret = talloc_asprintf(req->mem_ctx, "%s%s", private->connectpath, name);
41         }
42         all_string_sub(ret, "\\", "/", 0);
43
44         strlower(ret);
45
46         return ret;
47 }
48
49
50 /*
51   read a directory and find all matching file names and stat info
52   returned names are separate unix and DOS names. The returned names
53   are relative to the directory
54 */
55 struct svfs_dir *svfs_list_unix(TALLOC_CTX *mem_ctx, struct request_context *req, const char *unix_path)
56 {
57         char *p, *mask;
58         struct svfs_dir *dir;
59         DIR *odir;
60         struct dirent *dent;
61         uint_t allocated = 0;
62         char *low_mask;
63
64         dir = talloc(mem_ctx, sizeof(struct svfs_dir));
65         if (!dir) { return NULL; }
66
67         dir->count = 0;
68         dir->files = 0;
69
70         /* find the base directory */
71         p = strrchr(unix_path, '/');
72         if (!p) { return NULL; }
73
74         dir->unix_dir = talloc_strndup(mem_ctx, unix_path, PTR_DIFF(p, unix_path));
75         if (!dir->unix_dir) { return NULL; }
76
77         /* the wildcard pattern is the last part */
78         mask = p+1;
79
80         low_mask = talloc_strdup(mem_ctx, mask);
81         if (!low_mask) { return NULL; }
82         strlower(low_mask);
83
84         odir = opendir(dir->unix_dir);
85         if (!odir) { return NULL; }
86
87         while ((dent = readdir(odir))) {
88                 uint_t i = dir->count;
89                 char *full_name;
90                 char *low_name;
91
92                 if (strchr(dent->d_name, ':') && !strchr(unix_path, ':')) {
93                         /* don't show streams in dir listing */
94                         continue;
95                 }
96
97                 low_name = talloc_strdup(mem_ctx, dent->d_name);
98                 if (!low_name) { continue; }
99                 strlower(low_name);
100
101                 /* check it matches the wildcard pattern */
102                 if (ms_fnmatch(low_mask, low_name, PROTOCOL_NT1) != 0) {
103                         continue;
104                 }
105                 
106                 if (dir->count >= allocated) {
107                         allocated = (allocated + 100) * 1.2;
108                         dir->files = talloc_realloc(mem_ctx, dir->files, allocated * sizeof(dir->files[0]));
109                         if (!dir->files) { 
110                                 closedir(odir);
111                                 return NULL;
112                         }
113                 }
114
115                 dir->files[i].name = low_name;
116                 if (!dir->files[i].name) { continue; }
117
118                 asprintf(&full_name, "%s/%s", dir->unix_dir, dir->files[i].name);
119                 if (!full_name) { continue; }
120
121                 if (stat(full_name, &dir->files[i].st) == 0) { 
122                         dir->count++;
123                 }
124
125                 free(full_name); 
126         }
127
128         closedir(odir);
129
130         return dir;
131 }
132
133 /*
134   read a directory and find all matching file names and stat info
135   returned names are separate unix and DOS names. The returned names
136   are relative to the directory
137 */
138 struct svfs_dir *svfs_list(TALLOC_CTX *mem_ctx, struct request_context *req, const char *pattern)
139 {
140         char *unix_path;
141
142         unix_path = svfs_unix_path(req, pattern);
143         if (!unix_path) { return NULL; }
144
145         return svfs_list_unix(mem_ctx, req, unix_path);
146 }
147
148
149 /*******************************************************************
150 set the time on a file via file descriptor
151 *******************************************************************/
152 int svfs_file_utime(int fd, struct utimbuf *times)
153 {
154         char *fd_path = NULL;
155         int ret;
156
157         asprintf(&fd_path, "/proc/self/%d", fd);
158         if (!fd_path) {
159                 errno = ENOMEM;
160                 return -1;
161         }
162         
163         ret = utime(fd_path, times);
164         free(fd_path);
165         return ret;
166 }
167
168
169 /*
170   map a unix file attrib to a DOS attribute
171 */
172 uint16 svfs_unix_to_dos_attrib(mode_t mode)
173 {
174         uint16 ret = 0;
175         if (S_ISDIR(mode)) ret |= FILE_ATTRIBUTE_DIRECTORY;
176         if (!(mode & S_IWUSR)) ret |= FILE_ATTRIBUTE_READONLY;
177         return ret;
178 }
179
180 /*
181   build a file_id from a stat struct
182 */
183 large_t svfs_file_id(struct stat *st)
184 {
185         large_t ret = st->st_ino;
186         ret <<= 32;
187         ret |= st->st_dev;
188         return ret;
189 }