If lock is a FileHandle we must convert it into a FileLock because
[AROS.git] / compiler / clib / __stat.c
blob6fb9dc8918832b55a6cf988adb681f0f9ac8c240
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <dos/dos.h>
7 #include <proto/dos.h>
8 #include <proto/exec.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <time.h>
16 #include "__stat.h"
17 #include "__arosc_privdata.h"
19 #include <sys/stat.h>
20 #include <aros/debug.h>
22 /* 2922 is the number of days between 1.1.1970 and 1.1.1978 (2 leap
23 years and 6 normal). The former number is the start value
24 for time(), the latter the start time for the AmigaOS
25 time functions.
27 #define OFFSET_FROM_1970 2922*24*60*60
29 static mode_t __prot_a2u(ULONG protect);
30 static uid_t __id_a2u(UWORD id);
31 static void hashlittle2(const void *key, size_t length,
32 uint32_t *pc, uint32_t *pb);
33 static void __fill_statbuffer(
34 struct stat *sb,
35 char *buffer,
36 struct FileInfoBlock *fib,
37 int fallback_to_defaults,
38 BPTR lock);
40 int __stat(BPTR lock, struct stat *sb, BOOL filehandle)
42 struct FileInfoBlock *fib;
43 UBYTE *buffer;
44 int buffersize = 256;
45 int fallback_to_defaults = 0;
46 BOOL Examined;
48 fib = AllocDosObject(DOS_FIB, NULL);
50 if (!fib)
52 errno = __arosc_ioerr2errno(IoErr());
54 return -1;
57 Examined = filehandle
58 ? ExamineFH(lock, fib)
59 : Examine(lock, fib);
60 if (!Examined)
62 if(IoErr() == ERROR_NOT_IMPLEMENTED)
64 fallback_to_defaults = 1;
66 else
68 errno = __arosc_ioerr2errno(IoErr());
69 FreeDosObject(DOS_FIB, fib);
70 return -1;
74 /* Get the full path of the stated filesystem object and use it to
75 compute hash value */
78 BOOL GotName;
80 if(!(buffer = AllocVec(buffersize, MEMF_ANY)))
82 errno = ENOMEM;
83 FreeDosObject(DOS_FIB, fib);
84 return -1;
87 GotName = filehandle
88 ? NameFromFH(lock, buffer, buffersize)
89 : NameFromLock(lock, buffer, buffersize);
90 if(GotName)
91 break;
92 else if( IoErr() == ERROR_OBJECT_IN_USE
93 || IoErr() == ERROR_NOT_IMPLEMENTED
94 || (IoErr() == ERROR_OBJECT_NOT_FOUND && fib->fib_EntryType == ST_PIPEFILE))
96 /* We can't retrieve name because lock is an exclusive lock
97 or Examine is not implemented in this handler
98 or the lock refers to an XPIPE: file having always empty name */
99 buffer[0] = '\0';
100 break;
102 else if(IoErr() != ERROR_LINE_TOO_LONG)
104 errno = __arosc_ioerr2errno(IoErr());
105 FreeDosObject(DOS_FIB, fib);
106 FreeVec(buffer);
107 return -1;
109 FreeVec(buffer);
110 buffersize *= 2;
112 while(TRUE);
114 // We need a FileLock. Otherwise a call of Info() within __fill_statbuffer() will crash
115 // FIXME: how can we get a lock on an exclusive file?
116 if (filehandle)
118 BPTR filelock = DupLockFromFH(lock);
119 __fill_statbuffer(sb, (char*) buffer, fib, fallback_to_defaults, filelock);
120 UnLock(filelock);
122 else
124 __fill_statbuffer(sb, (char*) buffer, fib, fallback_to_defaults, lock);
127 FreeVec(buffer);
128 FreeDosObject(DOS_FIB, fib);
130 return 0;
134 int __stat_from_path(const char *path, struct stat *sb)
136 int len;
137 char *mypath;
138 int cwdsize = FILENAME_MAX;
139 char *cwd = NULL;
140 char *abspath = NULL;
141 char *filepart = NULL;
142 char *split;
143 struct FileInfoBlock *fib = NULL;
144 BPTR lock = BNULL;
145 BPTR cwdlock;
146 int fallback_to_defaults = 0;
147 BOOL loop;
148 int res = -1;
150 /* copy path and strip trailing slash */
151 len = strlen(path);
152 if (!(mypath = AllocVec(len + 1, MEMF_ANY)))
154 errno = ENOMEM;
155 goto out;
157 strcpy(mypath, path);
158 if (len && mypath[len-1] == '/')
159 mypath[len-1] = '\0';
161 /* do we have an absolute path */
162 if (!strchr(mypath, ':'))
164 /* no, then create one */
165 cwdlock = CurrentDir(BNULL);
166 CurrentDir(cwdlock);
169 if (!(cwd = AllocVec(cwdsize, MEMF_ANY)))
171 errno = ENOMEM;
172 goto out;
175 if (NameFromLock(cwdlock, cwd, cwdsize))
176 break;
177 else if (IoErr() != ERROR_LINE_TOO_LONG)
179 errno = __arosc_ioerr2errno(IoErr());
180 goto out;
183 FreeVec(cwd);
184 cwdsize *= 2;
186 while (TRUE);
188 /* get memory for current dir + '/' + input path + zero byte */
189 len = strlen(cwd) + 1 + len + 1;
190 abspath = AllocVec(len, MEMF_ANY);
191 if (!abspath)
193 errno = ENOMEM;
194 goto out;
197 strcpy(abspath, cwd);
198 AddPart(abspath, mypath, len);
199 FreeVec(mypath);
201 else
202 abspath = mypath;
204 /* split into path part and file part */
205 split = FilePart(abspath);
206 filepart = AllocVec(strlen(split) + 1, MEMF_ANY);
207 if (!filepart)
209 errno = ENOMEM;
210 goto out;
212 strcpy(filepart, split);
213 *split = '\0';
215 if ( !(fib = AllocDosObject(DOS_FIB, NULL))
216 || !(lock = Lock(abspath, SHARED_LOCK)))
218 errno = __arosc_ioerr2errno(IoErr());
219 goto out;
222 /* examine parent directory of object to stat */
223 if (!Examine(lock, fib))
225 if (IoErr() == ERROR_NOT_IMPLEMENTED)
226 fallback_to_defaults = 1;
227 else
229 errno = __arosc_ioerr2errno(IoErr());
230 goto out;
234 if (*filepart == '\0' || fallback_to_defaults)
235 __fill_statbuffer(sb, abspath, fib, fallback_to_defaults, lock);
236 else
237 /* examine entries of parent directory until we find the object to stat */
240 loop = ExNext(lock, fib);
242 if (loop)
244 if (stricmp(fib->fib_FileName, filepart) == 0)
246 __fill_statbuffer(sb, abspath, fib, 0, lock);
247 res = 0;
248 break;
251 continue;
254 if (IoErr() != ERROR_NO_MORE_ENTRIES)
255 errno = __arosc_ioerr2errno(IoErr());
256 else
257 /* nothing found to stat */
258 errno = ENOENT;
260 while (loop);
262 out:
263 if (lock)
264 UnLock(lock);
266 if (cwd)
267 FreeVec(cwd);
269 /* if we had absolute path as input, mypath is free'd here */
270 if (abspath)
271 FreeVec(abspath);
273 if (filepart)
274 FreeVec(filepart);
276 if (fib)
277 FreeDosObject(DOS_FIB, fib);
279 return res;
283 static mode_t __prot_a2u(ULONG protect)
285 mode_t uprot = 0000;
287 if ((protect & FIBF_SCRIPT))
288 uprot |= 0111;
289 /* The following three flags are low-active! */
290 if (!(protect & FIBF_EXECUTE))
291 uprot |= 0100;
292 if (!(protect & FIBF_WRITE))
293 uprot |= 0200;
294 if (!(protect & FIBF_READ))
295 uprot |= 0400;
296 if ((protect & FIBF_GRP_EXECUTE))
297 uprot |= 0010;
298 if ((protect & FIBF_GRP_WRITE))
299 uprot |= 0020;
300 if ((protect & FIBF_GRP_READ))
301 uprot |= 0040;
302 if ((protect & FIBF_OTR_EXECUTE))
303 uprot |= 0001;
304 if ((protect & FIBF_OTR_WRITE))
305 uprot |= 0002;
306 if ((protect & FIBF_OTR_READ))
307 uprot |= 0004;
309 return uprot;
313 static uid_t __id_a2u(UWORD id)
315 switch(id)
317 case (UWORD)-1:
318 return 0;
320 case (UWORD)-2:
321 return (UWORD)-1;
323 case 0:
324 return (UWORD)-2;
326 default:
327 return id;
331 /* The hash function code below is adapted from Bob Jenkins public domain hash
332 function, see http://burtleburtle.net/bob/hash/doobs.html for details */
334 #if AROS_BIG_ENDIAN
335 # define HASH_LITTLE_ENDIAN 0
336 #else
337 # define HASH_LITTLE_ENDIAN 1
338 #endif
340 #define hashsize(n) ((uint32_t)1<<(n))
341 #define hashmask(n) (hashsize(n)-1)
342 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
344 #define mix(a,b,c) \
346 a -= c; a ^= rot(c, 4); c += b; \
347 b -= a; b ^= rot(a, 6); a += c; \
348 c -= b; c ^= rot(b, 8); b += a; \
349 a -= c; a ^= rot(c,16); c += b; \
350 b -= a; b ^= rot(a,19); a += c; \
351 c -= b; c ^= rot(b, 4); b += a; \
354 #define final(a,b,c) \
356 c ^= b; c -= rot(b,14); \
357 a ^= c; a -= rot(c,11); \
358 b ^= a; b -= rot(a,25); \
359 c ^= b; c -= rot(b,16); \
360 a ^= c; a -= rot(c,4); \
361 b ^= a; b -= rot(a,14); \
362 c ^= b; c -= rot(b,24); \
366 * hashlittle2() -- hash a variable-length key into two 32-bit values
367 * k : the key (the unaligned variable-length array of bytes)
368 * length : the length of the key, counting by bytes
369 * pc : IN: primary initval, OUT: primary hash
370 * pb : IN: secondary initval, OUT: secondary hash
372 * Returns two 32-bit hash values. This is good enough for hash table
373 * lookup with 2^^64 buckets, or if you want a second hash if you're not
374 * happy with the first, or if you want a probably-unique 64-bit ID for
375 * the key. *pc is better mixed than *pb, so use *pc first. If you want
376 * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)".
378 static void hashlittle2(
379 const void *key, /* the key to hash */
380 size_t length, /* length of the key */
381 uint32_t *pc, /* IN: primary initval, OUT: primary hash */
382 uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */
384 uint32_t a,b,c; /* internal state */
385 union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */
387 /* Set up the internal state */
388 a = b = c = 0xdeadbeef + ((uint32_t)length) + *pc;
389 c += *pb;
391 u.ptr = key;
392 if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
393 const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
394 #ifdef VALGRIND
395 const uint8_t *k8;
396 #endif
398 /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
399 while (length > 12)
401 a += k[0];
402 b += k[1];
403 c += k[2];
404 mix(a,b,c);
405 length -= 12;
406 k += 3;
409 /*----------------------------- handle the last (probably partial) block */
411 * "k[2]&0xffffff" actually reads beyond the end of the string, but
412 * then masks off the part it's not allowed to read. Because the
413 * string is aligned, the masked-off tail is in the same word as the
414 * rest of the string. Every machine with memory protection I've seen
415 * does it on word boundaries, so is OK with this. But VALGRIND will
416 * still catch it and complain. The masking trick does make the hash
417 * noticably faster for short strings (like English words).
419 #ifndef VALGRIND
421 switch(length)
423 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
424 case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
425 case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
426 case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
427 case 8 : b+=k[1]; a+=k[0]; break;
428 case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
429 case 6 : b+=k[1]&0xffff; a+=k[0]; break;
430 case 5 : b+=k[1]&0xff; a+=k[0]; break;
431 case 4 : a+=k[0]; break;
432 case 3 : a+=k[0]&0xffffff; break;
433 case 2 : a+=k[0]&0xffff; break;
434 case 1 : a+=k[0]&0xff; break;
435 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
438 #else /* make valgrind happy */
440 k8 = (const uint8_t *)k;
441 switch(length)
443 case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
444 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
445 case 10: c+=((uint32_t)k8[9])<<8; /* fall through */
446 case 9 : c+=k8[8]; /* fall through */
447 case 8 : b+=k[1]; a+=k[0]; break;
448 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
449 case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */
450 case 5 : b+=k8[4]; /* fall through */
451 case 4 : a+=k[0]; break;
452 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
453 case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */
454 case 1 : a+=k8[0]; break;
455 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
458 #endif /* !valgrind */
460 } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) {
461 const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
462 const uint8_t *k8;
464 /*--------------- all but last block: aligned reads and different mixing */
465 while (length > 12)
467 a += k[0] + (((uint32_t)k[1])<<16);
468 b += k[2] + (((uint32_t)k[3])<<16);
469 c += k[4] + (((uint32_t)k[5])<<16);
470 mix(a,b,c);
471 length -= 12;
472 k += 6;
475 /*----------------------------- handle the last (probably partial) block */
476 k8 = (const uint8_t *)k;
477 switch(length)
479 case 12: c+=k[4]+(((uint32_t)k[5])<<16);
480 b+=k[2]+(((uint32_t)k[3])<<16);
481 a+=k[0]+(((uint32_t)k[1])<<16);
482 break;
483 case 11: c+=((uint32_t)k8[10])<<16; /* fall through */
484 case 10: c+=k[4];
485 b+=k[2]+(((uint32_t)k[3])<<16);
486 a+=k[0]+(((uint32_t)k[1])<<16);
487 break;
488 case 9 : c+=k8[8]; /* fall through */
489 case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
490 a+=k[0]+(((uint32_t)k[1])<<16);
491 break;
492 case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */
493 case 6 : b+=k[2];
494 a+=k[0]+(((uint32_t)k[1])<<16);
495 break;
496 case 5 : b+=k8[4]; /* fall through */
497 case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
498 break;
499 case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */
500 case 2 : a+=k[0];
501 break;
502 case 1 : a+=k8[0];
503 break;
504 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
507 } else { /* need to read the key one byte at a time */
508 const uint8_t *k = (const uint8_t *)key;
510 /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
511 while (length > 12)
513 a += k[0];
514 a += ((uint32_t)k[1])<<8;
515 a += ((uint32_t)k[2])<<16;
516 a += ((uint32_t)k[3])<<24;
517 b += k[4];
518 b += ((uint32_t)k[5])<<8;
519 b += ((uint32_t)k[6])<<16;
520 b += ((uint32_t)k[7])<<24;
521 c += k[8];
522 c += ((uint32_t)k[9])<<8;
523 c += ((uint32_t)k[10])<<16;
524 c += ((uint32_t)k[11])<<24;
525 mix(a,b,c);
526 length -= 12;
527 k += 12;
530 /*-------------------------------- last block: affect all 32 bits of (c) */
531 switch(length) /* all the case statements fall through */
533 case 12: c+=((uint32_t)k[11])<<24;
534 case 11: c+=((uint32_t)k[10])<<16;
535 case 10: c+=((uint32_t)k[9])<<8;
536 case 9 : c+=k[8];
537 case 8 : b+=((uint32_t)k[7])<<24;
538 case 7 : b+=((uint32_t)k[6])<<16;
539 case 6 : b+=((uint32_t)k[5])<<8;
540 case 5 : b+=k[4];
541 case 4 : a+=((uint32_t)k[3])<<24;
542 case 3 : a+=((uint32_t)k[2])<<16;
543 case 2 : a+=((uint32_t)k[1])<<8;
544 case 1 : a+=k[0];
545 break;
546 case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */
550 final(a,b,c);
551 *pc=c; *pb=b;
554 static void __fill_statbuffer(
555 struct stat *sb,
556 char *buffer,
557 struct FileInfoBlock *fib,
558 int fallback_to_defaults,
559 BPTR lock)
561 struct aroscbase *aroscbase = __GM_GetBase();
562 uint64_t hash;
563 uint32_t pc = 1, pb = 1; /* initial hash values */
565 hashlittle2(buffer, strlen((char*) buffer), &pc, &pb);
566 hash = pc + (((uint64_t)pb)<<32);
568 if(fallback_to_defaults)
570 /* Empty file, not protected, as old as it can be... */
571 fib->fib_Size = 0;
572 fib->fib_NumBlocks = 0;
573 fib->fib_Date.ds_Days = 0;
574 fib->fib_Date.ds_Minute = 0;
575 fib->fib_Date.ds_Tick = 0;
576 fib->fib_OwnerUID = 0;
577 fib->fib_OwnerGID = 0;
578 fib->fib_Protection = 0;
579 /* Most probable value */
580 fib->fib_DirEntryType = ST_PIPEFILE;
583 sb->st_dev = (dev_t)((struct FileLock *)BADDR(lock))->fl_Volume;
584 sb->st_ino = hash; /* hash value will be truncated if st_ino size is
585 smaller than uint64_t, but it's ok */
586 sb->st_size = (off_t)fib->fib_Size;
587 sb->st_atime =
588 sb->st_ctime =
589 sb->st_mtime = (fib->fib_Date.ds_Days * 24*60 + fib->fib_Date.ds_Minute + aroscbase->acb_gmtoffset) * 60 +
590 fib->fib_Date.ds_Tick / TICKS_PER_SECOND + OFFSET_FROM_1970;
591 sb->st_uid = __id_a2u(fib->fib_OwnerUID);
592 sb->st_gid = __id_a2u(fib->fib_OwnerGID);
593 sb->st_mode = __prot_a2u(fib->fib_Protection);
596 struct InfoData info;
598 if (Info(lock, &info))
600 sb->st_blksize = info.id_BytesPerBlock;
602 else
604 /* The st_blksize is just a guideline anyway, so we set it
605 to 1024 in case Info() didn't succeed */
606 sb->st_blksize = 1024;
609 if(fib->fib_Size > 0 && sb->st_blksize > 0)
610 sb->st_blocks =
611 (1 + ((long) fib->fib_Size - 1) / sb->st_blksize) *
612 (sb->st_blksize / 512);
613 else
614 sb->st_blocks = 0;
616 switch (fib->fib_DirEntryType)
618 case ST_PIPEFILE:
619 /* don't use S_IFIFO, we don't have a mkfifo() call ! */
620 sb->st_mode |= S_IFCHR;
621 break;
623 case ST_ROOT:
624 case ST_USERDIR:
625 case ST_LINKDIR:
626 sb->st_nlink = 1;
627 sb->st_mode |= S_IFDIR;
628 break;
630 case ST_SOFTLINK:
631 sb->st_nlink = 1;
632 sb->st_mode |= S_IFLNK;
633 break;
635 case ST_FILE:
636 case ST_LINKFILE:
637 default:
638 sb->st_nlink = 1;
639 sb->st_mode |= S_IFREG;