OSDN Git Service

98bec6fa847019803fd970c346afbbc9ba756695
[pf3gnuchains/pf3gnuchains3x.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 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
15 #include <sys/ioctl.h>
16 #include <fcntl.h>
17
18 #define isproc_dev(devn) \
19   (devn == FH_PROC || devn == FH_REGISTRY || devn == FH_PROCESS || \
20    devn == FH_PROCNET)
21
22 #define isvirtual_dev(devn) \
23   (isproc_dev (devn) || devn == FH_CYGDRIVE || devn == FH_NETDRIVE)
24
25 inline bool
26 has_attribute (DWORD attributes, DWORD attribs_to_test)
27 {
28   return attributes != INVALID_FILE_ATTRIBUTES
29          && (attributes & attribs_to_test);
30 }
31
32 enum executable_states
33 {
34   is_executable,
35   dont_care_if_executable,
36   not_executable = dont_care_if_executable,
37   dont_know_if_executable
38 };
39
40 struct suffix_info
41 {
42   const char *name;
43   int addon;
44   suffix_info (const char *s, int addit = 0): name (s), addon (addit) {}
45 };
46
47 extern suffix_info stat_suffixes[];
48
49 enum pathconv_arg
50 {
51   PC_SYM_FOLLOW         = 0x0001,
52   PC_SYM_NOFOLLOW       = 0x0002,
53   PC_SYM_CONTENTS       = 0x0008,
54   PC_NOFULL             = 0x0010,
55   PC_NULLEMPTY          = 0x0020,
56   PC_CHECK_EA           = 0x0040,
57   PC_POSIX              = 0x0080,
58   PC_NOWARN             = 0x0100,
59   PC_NO_ACCESS_CHECK    = 0x00800000
60 };
61
62 #define PC_NONULLEMPTY -1
63
64 #include "sys/mount.h"
65
66 enum path_types
67 {
68   PATH_NOTHING          = 0,
69   PATH_SYMLINK          = MOUNT_SYMLINK,
70   PATH_BINARY           = MOUNT_BINARY,
71   PATH_EXEC             = MOUNT_EXEC,
72   PATH_NOTEXEC          = MOUNT_NOTEXEC,
73   PATH_CYGWIN_EXEC      = MOUNT_CYGWIN_EXEC,
74   PATH_RO               = MOUNT_RO,
75   PATH_NOACL            = MOUNT_NOACL,
76   PATH_NOPOSIX          = MOUNT_NOPOSIX,
77   PATH_ALL_EXEC         = (PATH_CYGWIN_EXEC | PATH_EXEC),
78   PATH_NO_ACCESS_CHECK  = PC_NO_ACCESS_CHECK,
79   PATH_LNK              = 0x01000000,
80   PATH_TEXT             = 0x02000000,
81   PATH_REP              = 0x04000000,
82   PATH_HAS_SYMLINKS     = 0x10000000,
83   PATH_SOCKET           = 0x40000000
84 };
85
86 extern "C" char *__stdcall cstrdup (const char *) __attribute__ ((regparm(1)));
87
88 class symlink_info;
89
90 class path_conv
91 {
92   DWORD fileattr;
93   ULONG caseinsensitive;
94   fs_info fs;
95   PWCHAR wide_path;
96   UNICODE_STRING uni_path;
97   void add_ext_from_sym (symlink_info&);
98  public:
99
100   unsigned path_flags;
101   char *known_suffix;
102   int error;
103   device dev;
104
105   bool isremote () const {return fs.is_remote_drive ();}
106   ULONG objcaseinsensitive () const {return caseinsensitive;}
107   bool has_acls () const {return !(path_flags & PATH_NOACL) && fs.has_acls (); }
108   bool hasgood_inode () const {return fs.hasgood_inode (); }
109   bool isgood_inode (__ino64_t ino) const;
110   int has_symlinks () const {return path_flags & PATH_HAS_SYMLINKS;}
111   int has_buggy_open () const {return fs.has_buggy_open ();}
112   int has_buggy_fileid_dirinfo () const {return fs.has_buggy_fileid_dirinfo ();}
113   int binmode () const
114   {
115     if (path_flags & PATH_BINARY)
116       return O_BINARY;
117     if (path_flags & PATH_TEXT)
118       return O_TEXT;
119     return 0;
120   }
121   int issymlink () const {return path_flags & PATH_SYMLINK;}
122   int is_lnk_symlink () const {return path_flags & PATH_LNK;}
123   int is_rep_symlink () const {return path_flags & PATH_REP;}
124   int isdevice () const {return dev.devn && dev.devn != FH_FS && dev.devn != FH_FIFO;}
125   int isfifo () const {return dev == FH_FIFO;}
126   int isspecial () const {return dev.devn && dev.devn != FH_FS;}
127   int iscygdrive () const {return dev.devn == FH_CYGDRIVE;}
128   int is_auto_device () const {return isdevice () && !is_fs_special ();}
129   int is_fs_device () const {return isdevice () && is_fs_special ();}
130   int is_fs_special () const {return dev.is_fs_special ();}
131   int is_lnk_special () const {return is_fs_device () || isfifo () || is_lnk_symlink ();}
132   int issocket () const {return dev.devn == FH_UNIX;}
133   int iscygexec () const {return path_flags & PATH_CYGWIN_EXEC;}
134   void set_cygexec (bool isset)
135   {
136     if (isset)
137       path_flags |= PATH_CYGWIN_EXEC;
138     else
139       path_flags &= ~PATH_CYGWIN_EXEC;
140   }
141   bool isro () const {return !!(path_flags & PATH_RO);}
142   bool exists () const {return fileattr != INVALID_FILE_ATTRIBUTES;}
143   bool has_attribute (DWORD x) const {return exists () && (fileattr & x);}
144   int isdir () const {return has_attribute (FILE_ATTRIBUTE_DIRECTORY);}
145   executable_states exec_state ()
146   {
147     extern int _check_for_executable;
148     if (path_flags & PATH_ALL_EXEC)
149       return is_executable;
150     if (path_flags & PATH_NOTEXEC)
151       return not_executable;
152     if (!_check_for_executable)
153       return dont_care_if_executable;
154     return dont_know_if_executable;
155   }
156
157   void set_symlink (DWORD n) {path_flags |= PATH_SYMLINK; symlink_length = n;}
158   void set_has_symlinks () {path_flags |= PATH_HAS_SYMLINKS;}
159   void set_exec (int x = 1) {path_flags |= x ? PATH_EXEC : PATH_NOTEXEC;}
160
161   void check (const UNICODE_STRING *upath, unsigned opt = PC_SYM_FOLLOW,
162               const suffix_info *suffixes = NULL) __attribute__ ((regparm(3)));
163   void check (const char *src, unsigned opt = PC_SYM_FOLLOW,
164               const suffix_info *suffixes = NULL) __attribute__ ((regparm(3)));
165
166   path_conv (const device& in_dev)
167   : fileattr (INVALID_FILE_ATTRIBUTES), wide_path (NULL), path_flags (0),
168     known_suffix (NULL), error (0), dev (in_dev), normalized_path (NULL)
169   {
170     path = cstrdup (in_dev.native);
171   }
172
173   path_conv (int, const char *src, unsigned opt = PC_SYM_FOLLOW,
174              const suffix_info *suffixes = NULL)
175   : wide_path (NULL), normalized_path (NULL), path (NULL)
176   {
177     check (src, opt, suffixes);
178   }
179
180   path_conv (const UNICODE_STRING *src, unsigned opt = PC_SYM_FOLLOW,
181              const suffix_info *suffixes = NULL)
182   : wide_path (NULL), normalized_path (NULL), path (NULL)
183   {
184     check (src, opt | PC_NULLEMPTY, suffixes);
185   }
186
187   path_conv (const char *src, unsigned opt = PC_SYM_FOLLOW,
188              const suffix_info *suffixes = NULL)
189   : wide_path (NULL), normalized_path (NULL), path (NULL)
190   {
191     check (src, opt | PC_NULLEMPTY, suffixes);
192   }
193
194   path_conv ()
195   : fileattr (INVALID_FILE_ATTRIBUTES), wide_path (NULL), path_flags (0),
196     known_suffix (NULL), error (0), normalized_path (NULL), path (NULL)
197   {}
198
199   ~path_conv ();
200   inline char *get_win32 () { return path; }
201   PUNICODE_STRING get_nt_native_path ();
202   POBJECT_ATTRIBUTES get_object_attr (OBJECT_ATTRIBUTES &attr,
203                                       SECURITY_ATTRIBUTES &sa);
204   inline size_t get_wide_win32_path_len ()
205   {
206     get_nt_native_path ();
207     return uni_path.Length / sizeof (WCHAR);
208   }
209
210   PWCHAR get_wide_win32_path (PWCHAR wc);
211   operator DWORD &() {return fileattr;}
212   operator int () {return fileattr; }
213   path_conv &operator =(path_conv &pc)
214   {
215     memcpy (this, &pc, sizeof pc);
216     path = cstrdup (pc.path);
217     set_normalized_path (pc.normalized_path);
218     wide_path = NULL;
219     return *this;
220   }
221   DWORD get_devn () const {return dev.devn;}
222   short get_unitn () const {return dev.minor;}
223   DWORD file_attributes () const {return fileattr;}
224   void file_attributes (DWORD new_attr) {fileattr = new_attr;}
225   DWORD fs_flags () {return fs.flags ();}
226   DWORD fs_name_len () {return fs.name_len ();}
227   bool fs_is_fat () const {return fs.is_fat ();}
228   bool fs_is_ntfs () const {return fs.is_ntfs ();}
229   bool fs_is_samba () const {return fs.is_samba ();}
230   bool fs_is_nfs () const {return fs.is_nfs ();}
231   bool fs_is_netapp () const {return fs.is_netapp ();}
232   bool fs_is_cdrom () const {return fs.is_cdrom ();}
233   bool fs_is_mvfs () const {return fs.is_mvfs ();}
234   ULONG fs_serial_number () const {return fs.serial_number ();}
235   inline char *set_path (const char *p);
236   void fillin (HANDLE h);
237   bool is_binary ();
238
239   unsigned __stdcall ndisk_links (DWORD);
240   char *normalized_path;
241   void set_normalized_path (const char *) __attribute__ ((regparm (2)));
242   DWORD get_symlink_length () { return symlink_length; };
243  private:
244   DWORD symlink_length;
245   char *path;
246 };
247
248 /* Symlink marker */
249 #define SYMLINK_COOKIE "!<symlink>"
250
251 /* Socket marker */
252 #define SOCKET_COOKIE  "!<socket >"
253
254 int __stdcall slash_unc_prefix_p (const char *path) __attribute__ ((regparm(1)));
255
256 enum fe_types
257 {
258   FE_NADA = 0,          /* Nothing special */
259   FE_NNF = 1,           /* Return NULL if not found */
260   FE_NATIVE = 2,        /* Return native path in path_conv struct */
261   FE_CWD = 4,           /* Search CWD for program */
262   FE_DLL = 8            /* Search for DLLs, not executables. */
263 };
264 const char *__stdcall find_exec (const char *name, path_conv& buf,
265                                  const char *winenv = "PATH=",
266                                  unsigned opt = FE_NADA,
267                                  const char **known_suffix = NULL)
268   __attribute__ ((regparm(3)));
269
270 /* Common macros for checking for invalid path names */
271 #define isdrive(s) (isalpha (*(s)) && (s)[1] == ':')
272 #define iswdrive(s) (iswalpha (*(s)) && (s)[1] == L':')
273
274 static inline bool
275 has_exec_chars (const char *buf, int len)
276 {
277   return len >= 2 &&
278          ((buf[0] == '#' && buf[1] == '!') ||
279           (buf[0] == ':' && buf[1] == '\n') ||
280           (buf[0] == 'M' && buf[1] == 'Z'));
281 }
282
283 int pathmatch (const char *path1, const char *path2, bool caseinsensitive) __attribute__ ((regparm (3)));
284 int pathnmatch (const char *path1, const char *path2, int len, bool caseinsensitive) __attribute__ ((regparm (3)));
285 bool has_dot_last_component (const char *dir, bool test_dot_dot) __attribute__ ((regparm (2)));
286
287 int path_prefix_p (const char *path1, const char *path2, int len1,
288                    bool caseinsensitive) __attribute__ ((regparm (3)));
289
290 bool is_floppy (const char *);
291 int normalize_win32_path (const char *, char *, char *&);
292 int normalize_posix_path (const char *, char *, char *&);
293 PUNICODE_STRING get_nt_native_path (const char *, UNICODE_STRING&);
294
295 /* FIXME: Move to own include file eventually */
296
297 #define MAX_ETC_FILES 2
298 class etc
299 {
300   friend class dtable;
301   static int curr_ix;
302   static HANDLE changed_h;
303   static bool change_possible[MAX_ETC_FILES + 1];
304   static OBJECT_ATTRIBUTES fn[MAX_ETC_FILES + 1];
305   static LARGE_INTEGER last_modified[MAX_ETC_FILES + 1];
306   static bool dir_changed (int);
307   static int init (int, POBJECT_ATTRIBUTES);
308   static bool file_changed (int);
309   static bool test_file_change (int);
310   friend class pwdgrp;
311 };