rpcclient3: Factor out cli_rpc_pipe_open_bind_schannel()
[Samba.git] / source4 / ntvfs / simple / svfs_util.c
blobd1901ae01994c3ac180d67465c12b9f486530fbe
1 /*
2 Unix SMB/CIFS implementation.
4 simple NTVFS filesystem backend
6 Copyright (C) Andrew Tridgell 2003
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.
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.
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/>.
22 utility functions for simple backend
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"
34 convert a windows path to a unix path - don't do any manging or case sensitive handling
36 char *svfs_unix_path(struct ntvfs_module_context *ntvfs,
37 struct ntvfs_request *req, const char *name)
39 struct svfs_private *p = ntvfs->private_data;
40 char *ret;
41 char *name_lower = strlower_talloc(p, name);
43 if (*name != '\\') {
44 ret = talloc_asprintf(req, "%s/%s", p->connectpath, name_lower);
45 } else {
46 ret = talloc_asprintf(req, "%s%s", p->connectpath, name_lower);
48 all_string_sub(ret, "\\", "/", 0);
49 talloc_free(name_lower);
50 return ret;
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
59 struct svfs_dir *svfs_list_unix(TALLOC_CTX *mem_ctx, struct ntvfs_request *req, const char *unix_path)
61 char *p, *mask;
62 struct svfs_dir *dir;
63 DIR *odir;
64 struct dirent *dent;
65 unsigned int allocated = 0;
66 char *low_mask;
68 dir = talloc(mem_ctx, struct svfs_dir);
69 if (!dir) { return NULL; }
71 dir->count = 0;
72 dir->files = 0;
74 /* find the base directory */
75 p = strrchr(unix_path, '/');
76 if (!p) { return NULL; }
78 dir->unix_dir = talloc_strndup(mem_ctx, unix_path, PTR_DIFF(p, unix_path));
79 if (!dir->unix_dir) { return NULL; }
81 /* the wildcard pattern is the last part */
82 mask = p+1;
84 low_mask = strlower_talloc(mem_ctx, mask);
85 if (!low_mask) { return NULL; }
87 odir = opendir(dir->unix_dir);
88 if (!odir) { return NULL; }
90 while ((dent = readdir(odir))) {
91 unsigned int i = dir->count;
92 char *full_name;
93 char *low_name;
95 if (strchr(dent->d_name, ':') && !strchr(unix_path, ':')) {
96 /* don't show streams in dir listing */
97 continue;
100 low_name = strlower_talloc(mem_ctx, dent->d_name);
101 if (!low_name) { continue; }
103 /* check it matches the wildcard pattern */
104 if (ms_fnmatch_protocol(low_mask, low_name, PROTOCOL_NT1,
105 false) != 0) {
106 continue;
109 if (dir->count >= allocated) {
110 allocated = (allocated + 100) * 1.2;
111 dir->files = talloc_realloc(dir, dir->files, struct svfs_dirfile, allocated);
112 if (!dir->files) {
113 closedir(odir);
114 return NULL;
118 dir->files[i].name = low_name;
119 if (!dir->files[i].name) { continue; }
121 full_name = talloc_asprintf(mem_ctx, "%s/%s", dir->unix_dir,
122 dir->files[i].name);
123 if (!full_name) { continue; }
125 if (stat(full_name, &dir->files[i].st) == 0) {
126 dir->count++;
129 talloc_free(full_name);
132 closedir(odir);
134 return dir;
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
142 struct svfs_dir *svfs_list(struct ntvfs_module_context *ntvfs, struct ntvfs_request *req, const char *pattern)
144 struct svfs_private *p = ntvfs->private_data;
145 char *unix_path;
147 unix_path = svfs_unix_path(ntvfs, req, pattern);
148 if (!unix_path) { return NULL; }
150 return svfs_list_unix(p, req, unix_path);
154 /*******************************************************************
155 set the time on a file via file descriptor
156 *******************************************************************/
157 int svfs_file_utime(int fd, struct utimbuf *times)
159 char *fd_path = NULL;
160 int ret;
162 ret = asprintf(&fd_path, "/proc/self/%d", fd);
163 if (ret == -1) {
164 errno = ENOMEM;
165 return -1;
168 if (!fd_path) {
169 errno = ENOMEM;
170 return -1;
173 ret = utime(fd_path, times);
174 free(fd_path);
175 return ret;
180 map a unix file attrib to a DOS attribute
182 uint16_t svfs_unix_to_dos_attrib(mode_t mode)
184 uint16_t ret = 0;
185 if (S_ISDIR(mode)) ret |= FILE_ATTRIBUTE_DIRECTORY;
186 if (!(mode & S_IWUSR)) ret |= FILE_ATTRIBUTE_READONLY;
187 return ret;