1 /*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 1997, 1998 5 * Sleepycat Software. All rights reserved. 6 */ 7 8 #include "config.h" 9 10 #ifndef lint 11 static const char sccsid[] = "@(#)os_stat.c 10.18 (Sleepycat) 10/12/98"; 12 #endif /* not lint */ 13 14 #ifndef NO_SYSTEM_INCLUDES 15 #include <sys/types.h> 16 #include <sys/stat.h> 17 18 #include <errno.h> 19 #endif 20 21 #include "db_int.h" 22 #include "os_jump.h" 23 24 /* 25 * __os_exists -- 26 * Return if the file exists. 27 * 28 * PUBLIC: int __os_exists __P((const char *, int *)); 29 */ 30 int 31 __os_exists(path, isdirp) 32 const char *path; 33 int *isdirp; 34 { 35 struct stat sb; 36 37 if (__db_jump.j_exists != NULL) 38 return (__db_jump.j_exists(path, isdirp)); 39 40 if (stat(path, &sb) != 0) 41 return (errno); 42 43 #if !defined(S_ISDIR) || defined(STAT_MACROS_BROKEN) 44 #if defined(_WIN32) || defined(WIN16) 45 #define S_ISDIR(m) (_S_IFDIR & (m)) 46 #else 47 #define S_ISDIR(m) (((m) & 0170000) == 0040000) 48 #endif 49 #endif 50 if (isdirp != NULL) 51 *isdirp = S_ISDIR(sb.st_mode); 52 53 return (0); 54 } 55 56 /* 57 * __os_ioinfo -- 58 * Return file size and I/O size; abstracted to make it easier 59 * to replace. 60 * 61 * PUBLIC: int __os_ioinfo 62 * PUBLIC: __P((const char *, int, u_int32_t *, u_int32_t *, u_int32_t *)); 63 */ 64 int 65 __os_ioinfo(path, fd, mbytesp, bytesp, iosizep) 66 const char *path; 67 int fd; 68 u_int32_t *mbytesp, *bytesp, *iosizep; 69 { 70 struct stat sb; 71 72 if (__db_jump.j_ioinfo != NULL) 73 return (__db_jump.j_ioinfo(path, fd, mbytesp, bytesp, iosizep)); 74 75 if (fstat(fd, &sb) == -1) 76 return (errno); 77 78 /* Return the size of the file. */ 79 if (mbytesp != NULL) 80 *mbytesp = sb.st_size / MEGABYTE; 81 if (bytesp != NULL) 82 *bytesp = sb.st_size % MEGABYTE; 83 84 /* 85 * Return the underlying filesystem blocksize, if available. 86 * 87 * XXX 88 * Check for a 0 size -- the HP MPE/iX architecture has st_blksize, 89 * but it's always 0. 90 */ 91 #ifdef HAVE_ST_BLKSIZE 92 if (iosizep != NULL && (*iosizep = sb.st_blksize) == 0) 93 *iosizep = DB_DEF_IOSIZE; 94 #else 95 if (iosizep != NULL) 96 *iosizep = DB_DEF_IOSIZE; 97 #endif 98 return (0); 99 } 100