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