]> git.lyx.org Git - lyx.git/blob - src/support/FileInfo.C
small cleanup, doxygen, formatting changes
[lyx.git] / src / support / FileInfo.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  * 
5  *           LyX, The Document Processor
6  *        
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2000 The LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 //#include <sys/types.h>
19 //#include <sys/stat.h>
20
21 #include <cerrno>
22 #include "FileInfo.h"
23
24 #if !S_IRUSR
25 # if S_IREAD
26 #  define S_IRUSR S_IREAD
27 # else
28 #  define S_IRUSR 00400
29 # endif
30 #endif
31
32 #if !S_IWUSR
33 # if S_IWRITE
34 #  define S_IWUSR S_IWRITE
35 # else
36 #  define S_IWUSR 00200
37 # endif
38 #endif
39
40 #if !S_IXUSR
41 # if S_IEXEC
42 #  define S_IXUSR S_IEXEC
43 # else
44 #  define S_IXUSR 00100
45 # endif
46 #endif
47
48 #ifdef STAT_MACROS_BROKEN
49 #undef S_ISBLK
50 #undef S_ISCHR
51 #undef S_ISDIR
52 #undef S_ISFIFO
53 #undef S_ISLNK
54 #undef S_ISMPB
55 #undef S_ISMPC
56 #undef S_ISNWK
57 #undef S_ISREG
58 #undef S_ISSOCK
59 #endif 
60
61 #if !defined(S_ISBLK) && defined(S_IFBLK)
62 #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
63 #endif
64 #if !defined(S_ISCHR) && defined(S_IFCHR)
65 #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
66 #endif
67 #if !defined(S_ISDIR) && defined(S_IFDIR)
68 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
69 #endif
70 #if !defined(S_ISREG) && defined(S_IFREG)
71 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
72 #endif
73 #if !defined(S_ISFIFO) && defined(S_IFIFO)
74 #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
75 #endif
76 #if !defined(S_ISLNK) && defined(S_IFLNK)
77 #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
78 #endif
79 #if !defined(S_ISSOCK) && defined(S_IFSOCK)
80 #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
81 #endif
82 #if !defined(S_ISMPB) && defined(S_IFMPB) /* V7 */
83 #define S_ISMPB(m) (((m) & S_IFMT) == S_IFMPB)
84 #define S_ISMPC(m) (((m) & S_IFMT) == S_IFMPC)
85 #endif
86 #if !defined(S_ISNWK) && defined(S_IFNWK) /* HP/UX */
87 #define S_ISNWK(m) (((m) & S_IFMT) == S_IFNWK)
88 #endif
89
90 // Since major is a function on SVR4, we can't use `ifndef major'. 
91 // might want to put MAJOR_IN_MKDEV for SYSV
92 #ifdef MAJOR_IN_MKDEV
93 #include <sys/mkdev.h>
94 #define HAVE_MAJOR
95 #endif
96 #ifdef MAJOR_IN_SYSMACROS
97 #include <sys/sysmacros.h>
98 #define HAVE_MAJOR
99 #endif
100 #ifdef major            
101 #define HAVE_MAJOR
102 #endif
103
104 #ifndef HAVE_MAJOR
105 #define major(dev)  (((dev) >> 8) & 0xff)
106 #define minor(dev)  ((dev) & 0xff)
107 #define makedev(maj, min)  (((maj) << 8) | (min))
108 #endif
109 #undef HAVE_MAJOR
110
111
112 FileInfo::FileInfo()
113 {
114         init();
115 }
116
117
118 FileInfo::FileInfo(string const & path, bool link)
119         : fname(path)
120 {
121         init();
122         dostat(link);
123 }
124
125
126 FileInfo::FileInfo(int fildes)
127 {
128         init();
129         status = fstat(fildes, &buf);
130         if (status) err = errno;
131 }
132
133
134 void FileInfo::init()
135 {
136         status = 0;
137         err = NoErr;
138 }
139
140
141 void FileInfo::dostat(bool link)
142 {
143         if (link) {
144                 status = ::lstat(fname.c_str(), &buf);
145         } else {
146                 status = ::stat(fname.c_str(), &buf);
147         }
148         if (status) err = errno;
149 }
150
151
152 FileInfo & FileInfo::newFile(string const & path, bool link)
153 {
154         fname = path;
155         
156         status = 0;
157         err = NoErr;
158
159         dostat(link);
160
161         return *this;
162 }
163
164
165 FileInfo & FileInfo::newFile(int fildes)
166 {
167         status = 0;
168         err = NoErr;
169         status = fstat(fildes, &buf);
170         if (status) err = errno;
171         return *this;
172 }
173
174
175 // should not be in FileInfo
176 char const * FileInfo::typeIndicator() const
177 {
178         if (S_ISDIR(buf.st_mode)) return ("/");
179 #ifdef S_ISLNK
180         if (S_ISLNK(buf.st_mode)) return ("@");
181 #endif
182 #ifdef S_ISFIFO
183         if (S_ISFIFO(buf.st_mode)) return ("|");
184 #endif
185 #ifdef S_ISSOCK
186         if (S_ISSOCK(buf.st_mode)) return ("=");
187 #endif
188         if (S_ISREG(buf.st_mode) && (buf.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH)))
189                 return ("*");
190         return "";
191 }
192
193
194 mode_t FileInfo::getMode() const
195 {
196         return buf.st_mode;
197 }
198
199
200 // should not be in FileInfo
201 void FileInfo::modeString(char * szString) const
202 {
203         szString[0] = typeLetter();
204         flagRWX((buf.st_mode & 0700) << 0, &szString[1]);
205         flagRWX((buf.st_mode & 0070) << 3, &szString[4]);
206         flagRWX((buf.st_mode & 0007) << 6, &szString[7]);
207         setSticky(szString);
208         szString[10] = 0;
209 }
210
211
212 // should not be in FileInfo
213 char FileInfo::typeLetter() const
214 {
215 #ifdef S_ISBLK
216         if (S_ISBLK(buf.st_mode)) return 'b';
217 #endif
218         if (S_ISCHR(buf.st_mode)) return 'c';
219         if (S_ISDIR(buf.st_mode)) return 'd';
220         if (S_ISREG(buf.st_mode)) return '-';
221 #ifdef S_ISFIFO
222         if (S_ISFIFO(buf.st_mode)) return 'p';
223 #endif
224 #ifdef S_ISLNK
225         if (S_ISLNK(buf.st_mode)) return 'l';
226 #endif
227 #ifdef S_ISSOCK
228         if (S_ISSOCK(buf.st_mode)) return 's';
229 #endif
230 #ifdef S_ISMPC
231         if (S_ISMPC(buf.st_mode)) return 'm';
232 #endif
233 #ifdef S_ISNWK
234         if (S_ISNWK(buf.st_mode)) return 'n';
235 #endif
236         return '?';
237 }
238
239
240 // should not be in FileInfo
241 void FileInfo::flagRWX(mode_t i, char * szString) const
242 {
243         szString[0] = (i & S_IRUSR) ? 'r' : '-';
244         szString[1] = (i & S_IWUSR) ? 'w' : '-';
245         szString[2] = (i & S_IXUSR) ? 'x' : '-';
246 }
247
248
249 // should not be in FileInfo
250 void FileInfo::setSticky(char * szString) const
251 {
252 #ifdef S_ISUID
253         if (buf.st_mode & S_ISUID) {
254                 if (szString[3] != 'x') szString[3] = 'S';
255                 else szString[3] = 's';
256         }
257 #endif
258 #ifdef S_ISGID
259         if (buf.st_mode & S_ISGID) {
260                 if (szString[6] != 'x') szString[6] = 'S';
261                 else szString[6] = 's';
262         }
263 #endif
264 #ifdef S_ISVTX
265         if (buf.st_mode & S_ISVTX) {
266                 if (szString[9] != 'x') szString[9] = 'T';
267                 else szString[9] = 't';
268         }
269 #endif
270 }
271
272
273 time_t FileInfo::getModificationTime() const
274 {
275         return buf.st_mtime;
276 }
277
278
279 time_t FileInfo::getAccessTime() const
280 {
281         return buf.st_atime;
282 }
283
284
285 time_t FileInfo::getStatusChangeTime() const
286 {
287         return buf.st_ctime;
288 }
289
290
291 nlink_t FileInfo::getNumberOfLinks() const
292 {
293         return buf.st_nlink;
294 }
295
296
297 uid_t FileInfo::getUid() const
298 {
299         return buf.st_uid;
300 }
301
302
303 gid_t FileInfo::getGid() const
304 {
305         return buf.st_gid;
306 }
307
308
309 off_t FileInfo::getSize() const
310 {
311         return buf.st_size;
312 }
313
314
315 int FileInfo::getError() const
316 {
317         return err;
318 }
319
320
321 bool FileInfo::isOK() const
322 {
323         // DEC cxx 6.0 chokes on this bizarre construct (compiler bug)
324         // return (status) ? false : true;
325         // So I replaced it with a simpler one (JMarc)
326         return status == 0;
327 }
328
329
330 bool FileInfo::isLink() const
331 {
332         return S_ISLNK(buf.st_mode);
333 }
334
335
336 bool FileInfo::isRegular() const
337 {
338         return S_ISREG(buf.st_mode);
339 }
340
341
342 bool FileInfo::isDir() const
343 {
344         return S_ISDIR(buf.st_mode);
345 }
346
347
348 bool FileInfo::isChar() const
349 {
350         return S_ISCHR(buf.st_mode);
351 }
352
353
354 bool FileInfo::isBlock() const
355 {
356         return S_ISBLK(buf.st_mode);
357 }
358
359
360 bool FileInfo::isFifo() const
361 {
362         return S_ISFIFO(buf.st_mode);
363 }
364
365
366 bool FileInfo::isSocket() const
367 {
368 #ifdef S_ISSOCK
369         return S_ISSOCK(buf.st_mode);
370 #else
371         return false;
372 #endif
373 }
374
375
376 // should not be in FileInfo
377 bool FileInfo::access(int p) const
378 {
379         // if we don't have a filename we fail
380         if (fname.empty()) return false;
381         
382         if (::access(fname.c_str(), p) == 0)
383                 return true;
384         else {
385                 // If we were really kind, we would also tell why
386                 // the file access failed.
387                 return false;
388         }
389 }
390
391
392