Cygwin: (mostly) drop NT4 and Samba < 3.0 support
[newlib-cygwin.git] / winsup / cygwin / fhandler / netdrive.cc
blob7c3207139846b812c145e00f8aa56d283ab4d60d
1 /* fhandler_netdrive.cc: fhandler for // and //MACHINE handling
3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
7 details. */
9 #include "winsup.h"
10 #include <stdlib.h>
11 #include "cygerrno.h"
12 #include "security.h"
13 #include "path.h"
14 #include "fhandler.h"
15 #include "dtable.h"
16 #include "cygheap.h"
17 #include "cygthread.h"
18 #include "tls_pbuf.h"
20 #include <dirent.h>
22 enum
24 GET_RESOURCE_OPENENUM = 1,
25 GET_RESOURCE_OPENENUMTOP = 2,
26 GET_RESOURCE_ENUM = 3
29 struct netdriveinf
31 int what;
32 int ret;
33 PVOID in;
34 PVOID out;
35 DWORD outsize;
36 HANDLE sem;
39 struct net_hdls
41 HANDLE net;
42 HANDLE dom;
45 static void
46 wnet_dbg_out (const char *func, DWORD ndi_ret)
48 DWORD gle_ret;
49 DWORD error;
50 WCHAR errorbuf[MAX_PATH];
51 WCHAR namebuf[MAX_PATH];
53 if (ndi_ret != ERROR_EXTENDED_ERROR)
55 debug_printf ("%s failed: %u", func, ndi_ret);
56 return;
58 gle_ret = WNetGetLastErrorW (&error, errorbuf, MAX_PATH, namebuf, MAX_PATH);
59 if (gle_ret == NO_ERROR)
60 debug_printf ("%s failed: %u --> %u from '%W': '%W'",
61 func, ndi_ret, error, namebuf, errorbuf);
62 else
63 debug_printf ("WNetGetLastError failed: %u", gle_ret);
66 static DWORD
67 thread_netdrive (void *arg)
69 netdriveinf *ndi = (netdriveinf *) arg;
70 WCHAR provider[256], *dummy = NULL;
71 LPNETRESOURCEW nro;
72 DWORD cnt, size;
73 struct net_hdls *nh;
74 tmp_pathbuf tp;
76 ReleaseSemaphore (ndi->sem, 1, NULL);
77 switch (ndi->what)
79 case GET_RESOURCE_OPENENUMTOP:
80 nro = (LPNETRESOURCEW) tp.c_get ();
81 nh = (struct net_hdls *) ndi->out;
82 ndi->ret = WNetGetProviderNameW (WNNC_NET_LANMAN, provider,
83 (size = 256, &size));
84 if (ndi->ret != NO_ERROR)
86 wnet_dbg_out ("WNetGetProviderNameW", ndi->ret);
87 break;
89 memset (nro, 0, sizeof *nro);
90 nro->dwScope = RESOURCE_GLOBALNET;
91 nro->dwType = RESOURCETYPE_ANY;
92 nro->dwDisplayType = RESOURCEDISPLAYTYPE_GROUP;
93 nro->dwUsage = RESOURCEUSAGE_RESERVED | RESOURCEUSAGE_CONTAINER;
94 nro->lpRemoteName = provider;
95 nro->lpProvider = provider;
96 ndi->ret = WNetOpenEnumW (RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
97 RESOURCEUSAGE_ALL, nro, &nh->net);
98 if (ndi->ret != NO_ERROR)
100 wnet_dbg_out ("WNetOpenEnumW", ndi->ret);
101 break;
103 while ((ndi->ret = WNetEnumResourceW (nh->net, (cnt = 1, &cnt), nro,
104 (size = NT_MAX_PATH, &size)))
105 == NO_ERROR)
107 ndi->ret = WNetOpenEnumW (RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
108 RESOURCEUSAGE_ALL, nro, &nh->dom);
109 if (ndi->ret == NO_ERROR)
110 break;
112 break;
113 case GET_RESOURCE_OPENENUM:
114 nro = (LPNETRESOURCEW) tp.c_get ();
115 nh = (struct net_hdls *) ndi->out;
116 ndi->ret = WNetGetProviderNameW (WNNC_NET_LANMAN, provider,
117 (size = 256, &size));
118 if (ndi->ret != NO_ERROR)
120 wnet_dbg_out ("WNetGetProviderNameW", ndi->ret);
121 break;
123 ((LPNETRESOURCEW) ndi->in)->lpProvider = provider;
124 ndi->ret = WNetGetResourceInformationW ((LPNETRESOURCEW) ndi->in, nro,
125 (size = NT_MAX_PATH, &size),
126 &dummy);
127 if (ndi->ret != NO_ERROR)
129 wnet_dbg_out ("WNetGetResourceInformationW", ndi->ret);
130 break;
132 ndi->ret = WNetOpenEnumW (RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
133 RESOURCEUSAGE_ALL, nro, &nh->dom);
134 break;
135 case GET_RESOURCE_ENUM:
136 nh = (struct net_hdls *) ndi->in;
137 if (!nh->dom)
139 ndi->ret = ERROR_NO_MORE_ITEMS;
140 break;
142 nro = (LPNETRESOURCEW) tp.c_get ();
143 while ((ndi->ret = WNetEnumResourceW (nh->dom, (cnt = 1, &cnt),
144 (LPNETRESOURCEW) ndi->out,
145 &ndi->outsize)) != NO_ERROR
146 && nh->net)
148 WNetCloseEnum (nh->dom);
149 nh->dom = NULL;
150 while ((ndi->ret = WNetEnumResourceW (nh->net, (cnt = 1, &cnt), nro,
151 (size = NT_MAX_PATH, &size)))
152 == NO_ERROR)
154 ndi->ret = WNetOpenEnumW (RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
155 RESOURCEUSAGE_ALL, nro, &nh->dom);
156 if (ndi->ret == NO_ERROR)
157 break;
159 if (ndi->ret != NO_ERROR)
160 break;
162 break;
164 ReleaseSemaphore (ndi->sem, 1, NULL);
165 return 0;
168 static DWORD
169 create_thread_and_wait (int what, PVOID in, PVOID out, DWORD outsize,
170 const char *name)
172 netdriveinf ndi = { what, 0, in, out, outsize,
173 CreateSemaphore (&sec_none_nih, 0, 2, NULL) };
174 cygthread *thr = new cygthread (thread_netdrive, &ndi, name);
175 if (thr->detach (ndi.sem))
176 ndi.ret = ERROR_OPERATION_ABORTED;
177 CloseHandle (ndi.sem);
178 return ndi.ret;
181 virtual_ftype_t
182 fhandler_netdrive::exists ()
184 char *to;
185 const char *from;
186 size_t len = strlen (get_name ());
187 if (len == 2)
188 return virt_rootdir;
190 char namebuf[len + 1];
191 tmp_pathbuf tp;
192 PWCHAR name = tp.w_get ();
194 for (to = namebuf, from = get_name (); *from; to++, from++)
195 *to = (*from == '/') ? '\\' : *from;
196 *to = '\0';
198 struct net_hdls nh = { NULL, NULL };
199 NETRESOURCEW nr = {0};
200 nr.dwType = RESOURCETYPE_DISK;
201 sys_mbstowcs (name, NT_MAX_PATH, namebuf);
202 nr.lpRemoteName = name;
203 DWORD ret = create_thread_and_wait (GET_RESOURCE_OPENENUM,
204 &nr, &nh, 0, "WNetOpenEnum");
205 if (nh.dom)
206 WNetCloseEnum (nh.dom);
207 return ret != NO_ERROR ? virt_none : virt_directory;
210 fhandler_netdrive::fhandler_netdrive ():
211 fhandler_virtual ()
216 fhandler_netdrive::fstat (struct stat *buf)
218 const char *path = get_name ();
219 debug_printf ("fstat (%s)", path);
221 fhandler_base::fstat (buf);
223 buf->st_mode = S_IFDIR | STD_RBITS | STD_XBITS;
224 buf->st_ino = get_ino ();
226 return 0;
230 fhandler_netdrive::readdir (DIR *dir, dirent *de)
232 NETRESOURCEW *nro;
233 DWORD ret;
234 int res;
235 tmp_pathbuf tp;
237 if (!dir->__d_position)
239 size_t len = strlen (get_name ());
240 PWCHAR name = NULL;
241 NETRESOURCEW nr = { 0 };
242 struct net_hdls *nh;
244 if (len != 2) /* // */
246 const char *from;
247 char *to;
248 char *namebuf = (char *) alloca (len + 1);
249 for (to = namebuf, from = get_name (); *from; to++, from++)
250 *to = (*from == '/') ? '\\' : *from;
251 *to = '\0';
252 name = tp.w_get ();
253 sys_mbstowcs (name, NT_MAX_PATH, namebuf);
255 nr.lpRemoteName = name;
256 nr.dwType = RESOURCETYPE_DISK;
257 nh = (struct net_hdls *) ccalloc (HEAP_FHANDLER, 1, sizeof *nh);
258 ret = create_thread_and_wait (len == 2 ? GET_RESOURCE_OPENENUMTOP
259 : GET_RESOURCE_OPENENUM,
260 &nr, nh, 0, "WNetOpenEnum");
261 if (ret != NO_ERROR)
263 dir->__handle = INVALID_HANDLE_VALUE;
264 res = geterrno_from_win_error (ret);
265 goto out;
267 dir->__handle = (HANDLE) nh;
269 nro = (LPNETRESOURCEW) tp.c_get ();
270 ret = create_thread_and_wait (GET_RESOURCE_ENUM, dir->__handle, nro,
271 NT_MAX_PATH, "WnetEnumResource");
272 if (ret != NO_ERROR)
273 res = geterrno_from_win_error (ret);
274 else
276 dir->__d_position++;
277 PWCHAR bs = wcsrchr (nro->lpRemoteName, L'\\');
278 bs = bs ? bs + 1 : nro->lpRemoteName;
279 if (strlen (get_name ()) == 2)
281 UNICODE_STRING ss, ds;
283 tp.u_get (&ds);
284 RtlInitUnicodeString (&ss, bs);
285 RtlDowncaseUnicodeString (&ds, &ss, FALSE);
286 sys_wcstombs (de->d_name, sizeof de->d_name,
287 ds.Buffer, ds.Length / sizeof (WCHAR));
288 de->d_ino = hash_path_name (get_ino (), de->d_name);
290 else
292 sys_wcstombs (de->d_name, sizeof de->d_name, bs);
293 char *rpath = tp.c_get ();
294 sys_wcstombs (rpath, NT_MAX_PATH, nro->lpRemoteName);
295 de->d_ino = readdir_get_ino (rpath, false);
297 de->d_type = DT_DIR;
299 res = 0;
301 out:
302 syscall_printf ("%d = readdir(%p, %p)", res, dir, de);
303 return res;
306 void
307 fhandler_netdrive::seekdir (DIR *dir, long pos)
309 rewinddir (dir);
310 if (pos < 0)
311 return;
312 while (dir->__d_position < pos)
313 if (readdir (dir, dir->__d_dirent))
314 break;
317 void
318 fhandler_netdrive::rewinddir (DIR *dir)
320 if (dir->__handle != INVALID_HANDLE_VALUE)
322 struct net_hdls *nh = (struct net_hdls *) dir->__handle;
323 if (nh->dom)
324 WNetCloseEnum (nh->dom);
325 if (nh->net)
326 WNetCloseEnum (nh->net);
327 cfree (nh);
329 dir->__handle = INVALID_HANDLE_VALUE;
330 return fhandler_virtual::rewinddir (dir);
334 fhandler_netdrive::closedir (DIR *dir)
336 rewinddir (dir);
337 return fhandler_virtual::closedir (dir);
341 fhandler_netdrive::open (int flags, mode_t mode)
343 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
345 set_errno (EEXIST);
346 return 0;
348 if (flags & O_WRONLY)
350 set_errno (EISDIR);
351 return 0;
353 /* Open a fake handle to \\Device\\Null */
354 return open_null (flags);
358 fhandler_netdrive::close ()
360 /* Skip fhandler_virtual::close, which is a no-op. */
361 return fhandler_base::close ();