OSDN Git Service

* path.cc (conv_path_list): Take cygwin_conv_path_t as third parameter.
[pf3gnuchains/pf3gnuchains4x.git] / winsup / cygwin / path.h
1 /* path.h: path data structures
2
3    Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005,
4    2006, 2007, 2008, 2009, 2010, 2011 Red Hat, Inc.
5
6 This file is part of Cygwin.
7
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
10 details. */
11
12 #include "devices.h"
13 #include "mount.h"
14 #include "cygheap_malloc.h"
15 #include "nfs.h"
16
17 #include <sys/ioctl.h>
18 #include <fcntl.h>
19 #include <alloca.h>
20
21 inline bool
22 has_attribute (DWORD attributes, DWORD attribs_to_test)
23 {
24   return attributes != INVALID_FILE_ATTRIBUTES
25          && (attributes & attribs_to_test);
26 }
27
28 enum executable_states
29 {
30   is_executable,
31   dont_care_if_executable,
32   not_executable = dont_care_if_executable,
33   dont_know_if_executable
34 };
35
36 struct suffix_info
37 {
38   const char *name;
39   int addon;
40   suffix_info (const char *s, int addit = 0): name (s), addon (addit) {}
41 };
42
43 extern suffix_info stat_suffixes[];
44
45 enum pathconv_arg
46 {
47   PC_SYM_FOLLOW         = 0x0001,
48   PC_SYM_NOFOLLOW       = 0x0002,
49   PC_SYM_NOFOLLOW_REP   = 0x0004,
50   PC_SYM_CONTENTS       = 0x0008,
51   PC_NOFULL             = 0x0010,
52   PC_NULLEMPTY          = 0x0020,
53   PC_POSIX              = 0x0080,
54   PC_NOWARN             = 0x0100,
55   PC_OPEN               = 0x0200,       /* use open semantics */
56   PC_CTTY               = 0x0400,       /* could later be used as ctty */
57   PC_KEEP_HANDLE        = 0x00400000,
58   PC_NO_ACCESS_CHECK    = 0x00800000
59 };
60
61 #define PC_NONULLEMPTY -1
62
63 #include "sys/mount.h"
64
65 enum path_types
66 {
67   PATH_NOTHING          = 0,
68   PATH_SYMLINK          = MOUNT_SYMLINK,
69   PATH_BINARY           = MOUNT_BINARY,
70   PATH_EXEC             = MOUNT_EXEC,
71   PATH_NOTEXEC          = MOUNT_NOTEXEC,
72   PATH_CYGWIN_EXEC      = MOUNT_CYGWIN_EXEC,
73   PATH_RO               = MOUNT_RO,
74   PATH_NOACL            = MOUNT_NOACL,
75   PATH_NOPOSIX          = MOUNT_NOPOSIX,
76   PATH_DOS              = MOUNT_DOS,
77   PATH_IHASH            = MOUNT_IHASH,
78   PATH_ALL_EXEC         = (PATH_CYGWIN_EXEC | PATH_EXEC),
79   PATH_NO_ACCESS_CHECK  = PC_NO_ACCESS_CHECK,
80   PATH_CTTY             = 0x00400000,   /* could later be used as ctty */
81   PATH_OPEN             = 0x00800000,   /* use open semantics */
82   PATH_LNK              = 0x01000000,
83   PATH_TEXT             = 0x02000000,
84   PATH_REP              = 0x04000000,
85   PATH_HAS_SYMLINKS     = 0x10000000,
86   PATH_SOCKET           = 0x40000000
87 };
88
89 class symlink_info;
90 struct _FILE_NETWORK_OPEN_INFORMATION;
91
92 class path_conv_handle
93 {
94   HANDLE      hdl;
95   union {
96     /* Identical to FILE_NETWORK_OPEN_INFORMATION.  We don't want to pull in
97        ntdll.h here, though. */
98     struct {
99       LARGE_INTEGER CreationTime;
100       LARGE_INTEGER LastAccessTime;
101       LARGE_INTEGER LastWriteTime;
102       LARGE_INTEGER ChangeTime;
103       LARGE_INTEGER AllocationSize;
104       LARGE_INTEGER EndOfFile;
105       ULONG FileAttributes;
106     } _fnoi;
107     /* For NFS. */
108     fattr3 _fattr3;
109   } attribs;
110 public:
111   path_conv_handle () : hdl (NULL) {}
112   inline void set (HANDLE h) { hdl = h; }
113   inline void close ()
114   {
115     if (hdl)
116       CloseHandle (hdl);
117     set (NULL);
118   }
119   inline void dup (const path_conv_handle &pch)
120   {
121     if (!DuplicateHandle (GetCurrentProcess (), pch.handle (),
122                           GetCurrentProcess (), &hdl,
123                           0, TRUE, DUPLICATE_SAME_ACCESS))
124       hdl = NULL;
125   }
126   inline HANDLE handle () const { return hdl; }
127   inline struct _FILE_NETWORK_OPEN_INFORMATION *fnoi ()
128   { return (struct _FILE_NETWORK_OPEN_INFORMATION *) &attribs._fnoi; }
129   inline struct fattr3 *nfsattr ()
130   { return (struct fattr3 *) &attribs._fattr3; }
131 };
132
133 class path_conv
134 {
135   DWORD fileattr;
136   ULONG caseinsensitive;
137   fs_info fs;
138   PWCHAR wide_path;
139   UNICODE_STRING uni_path;
140   void add_ext_from_sym (symlink_info&);
141   DWORD symlink_length;
142   const char *path;
143   path_conv_handle conv_handle;
144  public:
145   unsigned path_flags;
146   const char *known_suffix;
147   const char *normalized_path;
148   int error;
149   device dev;
150
151   bool isremote () const {return fs.is_remote_drive ();}
152   ULONG objcaseinsensitive () const {return caseinsensitive;}
153   bool has_acls () const {return !(path_flags & PATH_NOACL) && fs.has_acls (); }
154   bool hasgood_inode () const {return !(path_flags & PATH_IHASH); }
155   bool isgood_inode (__ino64_t ino) const;
156   int has_symlinks () const {return path_flags & PATH_HAS_SYMLINKS;}
157   int has_dos_filenames_only () const {return path_flags & PATH_DOS;}
158   int has_buggy_open () const {return fs.has_buggy_open ();}
159   int has_buggy_reopen () const {return fs.has_buggy_reopen ();}
160   int has_buggy_fileid_dirinfo () const {return fs.has_buggy_fileid_dirinfo ();}
161   int has_buggy_basic_info () const {return fs.has_buggy_basic_info ();}
162   int binmode () const
163   {
164     if (path_flags & PATH_BINARY)
165       return O_BINARY;
166     if (path_flags & PATH_TEXT)
167       return O_TEXT;
168     return 0;
169   }
170   int issymlink () const {return path_flags & PATH_SYMLINK;}
171   int is_lnk_symlink () const {return path_flags & PATH_LNK;}
172   int is_rep_symlink () const {return path_flags & PATH_REP;}
173   int isdevice () const {return dev.not_device (FH_FS) && dev.not_device (FH_FIFO);}
174   int isfifo () const {return dev.is_device (FH_FIFO);}
175   int isspecial () const {return dev.not_device (FH_FS);}
176   int iscygdrive () const {return dev.is_device (FH_CYGDRIVE);}
177   int is_auto_device () const {return isdevice () && !is_fs_special ();}
178   int is_fs_device () const {return isdevice () && is_fs_special ();}
179   int is_fs_special () const {return dev.is_fs_special ();}
180   int is_lnk_special () const {return is_fs_device () || isfifo () || is_lnk_symlink ();}
181   int issocket () const {return dev.is_device (FH_UNIX);}
182   int iscygexec () const {return path_flags & PATH_CYGWIN_EXEC;}
183   int isopen () const {return path_flags & PATH_OPEN;}
184   int isctty_capable () const {return path_flags & PATH_CTTY;}
185   void set_cygexec (bool isset)
186   {
187     if (isset)
188       path_flags |= PATH_CYGWIN_EXEC;
189     else
190       path_flags &= ~PATH_CYGWIN_EXEC;
191   }
192   bool isro () const {return !!(path_flags & PATH_RO);}
193   bool exists () const {return fileattr != INVALID_FILE_ATTRIBUTES;}
194   bool has_attribute (DWORD x) const {return exists () && (fileattr & x);}
195   int isdir () const {return has_attribute (FILE_ATTRIBUTE_DIRECTORY);}
196   executable_states exec_state ()
197   {
198     extern int _check_for_executable;
199     if (path_flags & PATH_ALL_EXEC)
200       return is_executable;
201     if (path_flags & PATH_NOTEXEC)
202       return not_executable;
203     if (!_check_for_executable)
204       return dont_care_if_executable;
205     return dont_know_if_executable;
206   }
207
208   void set_symlink (DWORD n) {path_flags |= PATH_SYMLINK; symlink_length = n;}
209   void set_has_symlinks () {path_flags |= PATH_HAS_SYMLINKS;}
210   void set_exec (int x = 1) {path_flags |= x ? PATH_EXEC : PATH_NOTEXEC;}
211
212   void check (const UNICODE_STRING *upath, unsigned opt = PC_SYM_FOLLOW,
213               const suffix_info *suffixes = NULL) __attribute__ ((regparm(3)));
214   void check (const char *src, unsigned opt = PC_SYM_FOLLOW,
215               const suffix_info *suffixes = NULL) __attribute__ ((regparm(3)));
216
217   path_conv (const device& in_dev)
218   : fileattr (INVALID_FILE_ATTRIBUTES), wide_path (NULL), path (NULL),
219     path_flags (0), known_suffix (NULL), normalized_path (NULL), error (0),
220     dev (in_dev)
221   {
222     set_path (in_dev.native);
223   }
224
225   path_conv (int, const char *src, unsigned opt = PC_SYM_FOLLOW,
226              const suffix_info *suffixes = NULL)
227   : fileattr (INVALID_FILE_ATTRIBUTES), wide_path (NULL), path (NULL),
228     path_flags (0), known_suffix (NULL), normalized_path (NULL), error (0)
229   {
230     check (src, opt, suffixes);
231   }
232
233   path_conv (const UNICODE_STRING *src, unsigned opt = PC_SYM_FOLLOW,
234              const suffix_info *suffixes = NULL)
235   : fileattr (INVALID_FILE_ATTRIBUTES), wide_path (NULL), path (NULL),
236     path_flags (0), known_suffix (NULL), normalized_path (NULL), error (0)
237   {
238     check (src, opt | PC_NULLEMPTY, suffixes);
239   }
240
241   path_conv (const char *src, unsigned opt = PC_SYM_FOLLOW,
242              const suffix_info *suffixes = NULL)
243   : fileattr (INVALID_FILE_ATTRIBUTES), wide_path (NULL), path (NULL),
244     path_flags (0), known_suffix (NULL), normalized_path (NULL), error (0)
245   {
246     check (src, opt | PC_NULLEMPTY, suffixes);
247   }
248
249   path_conv ()
250   : fileattr (INVALID_FILE_ATTRIBUTES), wide_path (NULL), path (NULL),
251     path_flags (0), known_suffix (NULL), normalized_path (NULL), error (0)
252   {}
253
254   ~path_conv ();
255   inline const char *get_win32 () { return path; }
256   PUNICODE_STRING get_nt_native_path ();
257   inline POBJECT_ATTRIBUTES get_object_attr (OBJECT_ATTRIBUTES &attr,
258                                              SECURITY_ATTRIBUTES &sa)
259   {
260     if (!get_nt_native_path ())
261       return NULL;
262     InitializeObjectAttributes (&attr, &uni_path,
263                                 objcaseinsensitive ()
264                                 | (sa.bInheritHandle ? OBJ_INHERIT : 0),
265                                 NULL, sa.lpSecurityDescriptor);
266     return &attr;
267   }
268   inline void init_reopen_attr (POBJECT_ATTRIBUTES attr, HANDLE h)
269   {
270     if (has_buggy_reopen ())
271       InitializeObjectAttributes (attr, get_nt_native_path (),
272                                   objcaseinsensitive (), NULL, NULL)
273     else
274       InitializeObjectAttributes (attr, &ro_u_empty, objcaseinsensitive (),
275                                   h, NULL);
276   }
277   inline size_t get_wide_win32_path_len ()
278   {
279     get_nt_native_path ();
280     return uni_path.Length / sizeof (WCHAR);
281   }
282
283   PWCHAR get_wide_win32_path (PWCHAR wc);
284   operator DWORD &() {return fileattr;}
285   operator int () {return fileattr; }
286 # define cfree_and_null(x) \
287   if (x) \
288     { \
289       cfree ((void *) (x)); \
290       (x) = NULL; \
291     }
292   void free_strings ()
293   {
294     cfree_and_null (path);
295     cfree_and_null (normalized_path);
296     cfree_and_null (wide_path);
297   }
298   path_conv& eq_worker (const path_conv& pc, const char *in_path,
299                         const char *in_normalized_path)
300   {
301     free_strings ();
302     memcpy (this, &pc, sizeof pc);
303     path = cstrdup (in_path);
304     conv_handle.dup (pc.conv_handle);
305     normalized_path = cstrdup(pc.normalized_path);
306     if (pc.wide_path)
307       {
308         wide_path = cwcsdup (uni_path.Buffer);
309         if (!wide_path)
310           api_fatal ("cwcsdup would have returned NULL");
311         uni_path.Buffer = wide_path;
312       }
313     return *this;
314   }
315
316   path_conv &operator << (const path_conv& pc)
317   {
318     const char *save_path;
319     const char *save_normalized_path;
320     if (!path)
321       save_path = pc.path;
322     else
323       {
324         save_path = (char *) alloca (strlen (path) + 1);
325         strcpy ((char *) save_path, path);
326       }
327     if (!normalized_path)
328       save_normalized_path = pc.normalized_path;
329     else
330       {
331         save_normalized_path = (char *) alloca (strlen (normalized_path) + 1);
332         strcpy ((char *) save_normalized_path, path);
333       }
334     return eq_worker (pc, save_path, save_normalized_path);
335   }
336
337   path_conv &operator =(const path_conv& pc)
338   {
339     return eq_worker (pc, pc.path, pc.normalized_path);
340   }
341   DWORD get_devn () {return (DWORD) dev;}
342   short get_unitn () const {return dev.get_minor ();}
343   DWORD file_attributes () const {return fileattr;}
344   void file_attributes (DWORD new_attr) {fileattr = new_attr;}
345   DWORD fs_flags () {return fs.flags ();}
346   DWORD fs_name_len () {return fs.name_len ();}
347   bool fs_is_fat () const {return fs.is_fat ();}
348   bool fs_is_ntfs () const {return fs.is_ntfs ();}
349   bool fs_is_samba () const {return fs.is_samba ();}
350   bool fs_is_nfs () const {return fs.is_nfs ();}
351   bool fs_is_netapp () const {return fs.is_netapp ();}
352   bool fs_is_cdrom () const {return fs.is_cdrom ();}
353   bool fs_is_mvfs () const {return fs.is_mvfs ();}
354   bool fs_is_cifs () const {return fs.is_cifs ();}
355   bool fs_is_nwfs () const {return fs.is_nwfs ();}
356   bool fs_is_ncfsd () const {return fs.is_ncfsd ();}
357   ULONG fs_serial_number () const {return fs.serial_number ();}
358   inline const char *set_path (const char *p)
359   {
360     if (path)
361       cfree (modifiable_path ());
362     char *new_path = (char *) cmalloc_abort (HEAP_STR, strlen (p) + 7);
363     strcpy (new_path, p);
364     return path = new_path;
365   }
366   bool is_binary ();
367
368   HANDLE handle () const { return conv_handle.handle (); }
369   struct _FILE_NETWORK_OPEN_INFORMATION *fnoi () { return conv_handle.fnoi (); }
370   struct fattr3 *nfsattr () { return conv_handle.nfsattr (); }
371   void reset_conv_handle () { conv_handle.set (NULL); }
372   void close_conv_handle () { conv_handle.close (); }
373
374   __ino64_t get_ino_by_handle (HANDLE h);
375 #if 0 /* obsolete, method still exists in fhandler_disk_file.cc */
376   unsigned __stdcall ndisk_links (DWORD);
377 #endif
378   void set_normalized_path (const char *) __attribute__ ((regparm (2)));
379   DWORD get_symlink_length () { return symlink_length; };
380  private:
381   char *modifiable_path () {return (char *) path;}
382 };
383
384 /* Symlink marker */
385 #define SYMLINK_COOKIE "!<symlink>"
386
387 /* Socket marker */
388 #define SOCKET_COOKIE  "!<socket >"
389
390 /* Interix symlink marker */
391 #define INTERIX_SYMLINK_COOKIE  "IntxLNK\1"
392
393 enum fe_types
394 {
395   FE_NADA = 0,          /* Nothing special */
396   FE_NNF = 1,           /* Return NULL if not found */
397   FE_NATIVE = 2,        /* Return native path in path_conv struct */
398   FE_CWD = 4,           /* Search CWD for program */
399   FE_DLL = 8            /* Search for DLLs, not executables. */
400 };
401 const char *__stdcall find_exec (const char *name, path_conv& buf,
402                                  const char *winenv = "PATH=",
403                                  unsigned opt = FE_NADA,
404                                  const char **known_suffix = NULL)
405   __attribute__ ((regparm(3)));
406
407 /* Common macros for checking for invalid path names */
408 #define isdrive(s) (isalpha (*(s)) && (s)[1] == ':')
409 #define iswdrive(s) (iswalpha (*(s)) && (s)[1] == L':')
410
411 static inline bool
412 has_exec_chars (const char *buf, int len)
413 {
414   return len >= 2 &&
415          ((buf[0] == '#' && buf[1] == '!') ||
416           (buf[0] == ':' && buf[1] == '\n') ||
417           (buf[0] == 'M' && buf[1] == 'Z'));
418 }
419
420 int pathmatch (const char *path1, const char *path2, bool caseinsensitive) __attribute__ ((regparm (3)));
421 int pathnmatch (const char *path1, const char *path2, int len, bool caseinsensitive) __attribute__ ((regparm (3)));
422 bool has_dot_last_component (const char *dir, bool test_dot_dot) __attribute__ ((regparm (2)));
423
424 int path_prefix_p (const char *path1, const char *path2, int len1,
425                    bool caseinsensitive) __attribute__ ((regparm (3)));
426
427 bool is_floppy (const char *);
428 int normalize_win32_path (const char *, char *, char *&);
429 int normalize_posix_path (const char *, char *, char *&);
430 PUNICODE_STRING get_nt_native_path (const char *, UNICODE_STRING&, bool) __attribute__ ((regparm (3)));
431
432 /* FIXME: Move to own include file eventually */
433
434 #define MAX_ETC_FILES 2
435 class etc
436 {
437   friend class dtable;
438   static int curr_ix;
439   static HANDLE changed_h;
440   static bool change_possible[MAX_ETC_FILES + 1];
441   static OBJECT_ATTRIBUTES fn[MAX_ETC_FILES + 1];
442   static LARGE_INTEGER last_modified[MAX_ETC_FILES + 1];
443   static bool dir_changed (int);
444   static int init (int, POBJECT_ATTRIBUTES);
445   static bool file_changed (int);
446   static bool test_file_change (int);
447   friend class pwdgrp;
448 };