1 /* path.h: path data structures
3 Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005,
4 2006, 2007, 2008 Red Hat, Inc.
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
15 #include <sys/ioctl.h>
18 #define isproc_dev(devn) \
19 (devn == FH_PROC || devn == FH_REGISTRY || devn == FH_PROCESS || \
22 #define isvirtual_dev(devn) \
23 (isproc_dev (devn) || devn == FH_CYGDRIVE || devn == FH_NETDRIVE)
26 has_attribute (DWORD attributes, DWORD attribs_to_test)
28 return attributes != INVALID_FILE_ATTRIBUTES
29 && (attributes & attribs_to_test);
32 enum executable_states
35 dont_care_if_executable,
36 not_executable = dont_care_if_executable,
37 dont_know_if_executable
44 suffix_info (const char *s, int addit = 0): name (s), addon (addit) {}
47 extern suffix_info stat_suffixes[];
51 PC_SYM_FOLLOW = 0x0001,
52 PC_SYM_NOFOLLOW = 0x0002,
53 PC_SYM_CONTENTS = 0x0008,
55 PC_NULLEMPTY = 0x0020,
59 PC_NO_ACCESS_CHECK = 0x00800000
62 #define PC_NONULLEMPTY -1
64 #include "sys/mount.h"
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,
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
86 extern "C" char *__stdcall cstrdup (const char *) __attribute__ ((regparm(1)));
93 ULONG caseinsensitive;
96 UNICODE_STRING uni_path;
97 void add_ext_from_sym (symlink_info&);
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 ();}
115 if (path_flags & PATH_BINARY)
117 if (path_flags & PATH_TEXT)
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)
137 path_flags |= PATH_CYGWIN_EXEC;
139 path_flags &= ~PATH_CYGWIN_EXEC;
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 ()
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;
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;}
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)));
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)
170 path = cstrdup (in_dev.native);
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)
177 check (src, opt, suffixes);
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)
184 check (src, opt | PC_NULLEMPTY, suffixes);
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)
191 check (src, opt | PC_NULLEMPTY, suffixes);
195 : fileattr (INVALID_FILE_ATTRIBUTES), wide_path (NULL), path_flags (0),
196 known_suffix (NULL), error (0), normalized_path (NULL), path (NULL)
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 ()
206 get_nt_native_path ();
207 return uni_path.Length / sizeof (WCHAR);
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)
215 memcpy (this, &pc, sizeof pc);
216 path = cstrdup (pc.path);
217 set_normalized_path (pc.normalized_path);
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);
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; };
244 DWORD symlink_length;
249 #define SYMLINK_COOKIE "!<symlink>"
252 #define SOCKET_COOKIE "!<socket >"
254 int __stdcall slash_unc_prefix_p (const char *path) __attribute__ ((regparm(1)));
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. */
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)));
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':')
275 has_exec_chars (const char *buf, int len)
278 ((buf[0] == '#' && buf[1] == '!') ||
279 (buf[0] == ':' && buf[1] == '\n') ||
280 (buf[0] == 'M' && buf[1] == 'Z'));
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)));
287 int path_prefix_p (const char *path1, const char *path2, int len1,
288 bool caseinsensitive) __attribute__ ((regparm (3)));
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&);
295 /* FIXME: Move to own include file eventually */
297 #define MAX_ETC_FILES 2
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);