r1983: a completely new implementation of talloc
[bbaumbach/samba-autobuild/.git] / source4 / ntvfs / reference / ref_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 smbsrv_request *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(TALLOC_CTX *mem_ctx, struct smbsrv_request *req, const char *pattern)
56 {
57         char *unix_path;
58         char *p, *mask;
59         struct svfs_dir *dir;
60         DIR *odir;
61         struct dirent *dent;
62         uint_t allocated = 0;
63         char *low_mask;
64
65         unix_path = svfs_unix_path(req, pattern);
66         if (!unix_path) { return NULL; }
67
68         dir = talloc(mem_ctx, sizeof(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                 low_name = talloc_strdup(mem_ctx, dent->d_name);
97                 if (!low_name) { continue; }
98                 strlower(low_name);
99
100                 /* check it matches the wildcard pattern */
101                 if (ms_fnmatch(low_mask, low_name, PROTOCOL_NT1) != 0) {
102                         continue;
103                 }
104                 
105                 if (dir->count >= allocated) {
106                         allocated = (allocated + 100) * 1.2;
107                         dir->files = talloc_realloc(dir->files, allocated * sizeof(dir->files[0]));
108                         if (!dir->files) { 
109                                 closedir(odir);
110                                 return NULL;
111                         }
112                 }
113
114                 dir->files[i].name = low_name;
115                 if (!dir->files[i].name) { continue; }
116
117                 asprintf(&full_name, "%s/%s", dir->unix_dir, dir->files[i].name);
118                 if (!full_name) { continue; }
119
120                 if (stat(full_name, &dir->files[i].st) == 0) { 
121                         dir->count++;
122                 }
123
124                 free(full_name); 
125         }
126
127         closedir(odir);
128
129         return dir;
130 }
131
132
133 /*
134   convert a unix stat struct to a dos attrib
135 */
136 uint32_t svfs_file_attrib(struct stat *st)
137 {
138         if (S_ISDIR(st->st_mode)) {
139                 return FILE_ATTRIBUTE_DIRECTORY;
140         }
141         return 0;
142 }