diff options
Diffstat (limited to 'os_brew')
-rw-r--r-- | os_brew/ctime.c | 81 | ||||
-rw-r--r-- | os_brew/fclose.c | 32 | ||||
-rw-r--r-- | os_brew/fgetc.c | 31 | ||||
-rw-r--r-- | os_brew/fgets.c | 42 | ||||
-rw-r--r-- | os_brew/fopen.c | 89 | ||||
-rw-r--r-- | os_brew/fwrite.c | 31 | ||||
-rw-r--r-- | os_brew/getcwd.c | 61 | ||||
-rw-r--r-- | os_brew/globals.c | 53 | ||||
-rw-r--r-- | os_brew/localtime.c | 78 | ||||
-rw-r--r-- | os_brew/os_abort.c | 28 | ||||
-rw-r--r-- | os_brew/os_abs.c | 22 | ||||
-rw-r--r-- | os_brew/os_clock.c | 34 | ||||
-rw-r--r-- | os_brew/os_config.c | 59 | ||||
-rw-r--r-- | os_brew/os_dir.c | 84 | ||||
-rw-r--r-- | os_brew/os_errno.c | 196 | ||||
-rw-r--r-- | os_brew/os_handle.c | 102 | ||||
-rw-r--r-- | os_brew/os_mkdir.c | 40 | ||||
-rw-r--r-- | os_brew/os_open.c | 51 | ||||
-rw-r--r-- | os_brew/os_pid.c | 33 | ||||
-rw-r--r-- | os_brew/os_rename.c | 43 | ||||
-rw-r--r-- | os_brew/os_rw.c | 154 | ||||
-rw-r--r-- | os_brew/os_seek.c | 53 | ||||
-rw-r--r-- | os_brew/os_stat.c | 89 | ||||
-rw-r--r-- | os_brew/os_truncate.c | 48 | ||||
-rw-r--r-- | os_brew/os_unlink.c | 40 | ||||
-rw-r--r-- | os_brew/os_yield.c | 40 |
26 files changed, 1614 insertions, 0 deletions
diff --git a/os_brew/ctime.c b/os_brew/ctime.c new file mode 100644 index 00000000..7013116b --- /dev/null +++ b/os_brew/ctime.c @@ -0,0 +1,81 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2001-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +static void __os_brew_ct_numb __P((char *, int)); + +/* + * __os_ctime -- + * Format a time-stamp. + */ +char * +__os_ctime(tod, time_buf) + const time_t *tod; + char *time_buf; +{ + JulianType jt; + time_t tt; + char *ncp; + + strcpy(time_buf, "Thu Jan 01 00:00:00 1970"); + time_buf[CTIME_BUFLEN - 1] = '\0'; + + /* + * Berkeley DB uses POSIX time values internally, convert to a BREW + * time value. + */ + tt = *tod - BREW_EPOCH_OFFSET + LOCALTIMEOFFSET(NULL); + GETJULIANDATE(tt, &jt); + + /* + * wWeekDay : Day of the week 0-6 (0=Monday, 6=Sunday) + */ + ncp = &"MonTueWedThuFriSatSun"[jt.wWeekDay*3]; + time_buf[0] = *ncp++; + time_buf[1] = *ncp++; + time_buf[2] = *ncp; + ncp = &"JanFebMarAprMayJunJulAugSepOctNovDec"[(jt.wMonth - 1) * 3]; + time_buf[4] = *ncp++; + time_buf[5] = *ncp++; + time_buf[6] = *ncp; + + __os_brew_ct_numb(time_buf + 8, jt.wDay); + /* Add 100 to keep the leading zero. */ + __os_brew_ct_numb(time_buf + 11, jt.wHour + 100); + __os_brew_ct_numb(time_buf + 14, jt.wMinute + 100); + __os_brew_ct_numb(time_buf + 17, jt.wSecond + 100); + + if (jt.wYear < 100) { /* 9 99 */ + time_buf[20] = ' '; + time_buf[21] = ' '; + __os_brew_ct_numb(time_buf + 22, jt.wYear); + } else { /* 99 1999 */ + __os_brew_ct_numb(time_buf + 20, jt.wYear / 100); + __os_brew_ct_numb(time_buf + 22, jt.wYear % 100 + 100); + } + + return (time_buf); +} + +/* + * __os_brew_ct_numb -- + * Append ASCII representations for two digits to a string. + */ +static void +__os_brew_ct_numb(cp, n) + char *cp; + int n; +{ + cp[0] = ' '; + if (n >= 10) + cp[0] = (n / 10) % 10 + '0'; + cp[1] = n % 10 + '0'; +} diff --git a/os_brew/fclose.c b/os_brew/fclose.c new file mode 100644 index 00000000..59745ee1 --- /dev/null +++ b/os_brew/fclose.c @@ -0,0 +1,32 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2006-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * fclose -- + * + * PUBLIC: #ifndef HAVE_FCLOSE + * PUBLIC: int fclose __P((FILE *)); + * PUBLIC: #endif + */ +int +fclose(fp) + FILE *fp; +{ + /* + * Release (close) the file. + * + * Returns the updated reference count, which is of no use to us. + */ + (void)IFILE_Release(fp); + + return (0); +} diff --git a/os_brew/fgetc.c b/os_brew/fgetc.c new file mode 100644 index 00000000..19aa5b17 --- /dev/null +++ b/os_brew/fgetc.c @@ -0,0 +1,31 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2006-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * fgetc -- + * + * PUBLIC: #ifndef HAVE_FGETC + * PUBLIC: int fgetc __P((FILE *)); + * PUBLIC: #endif + */ +int +fgetc(fp) + FILE *fp; +{ + char b[1]; + + if (IFILE_Read(fp, b, 1)) + return ((int)b[0]); + + __os_set_errno(EIO); + return (EOF); +} diff --git a/os_brew/fgets.c b/os_brew/fgets.c new file mode 100644 index 00000000..8b2565d0 --- /dev/null +++ b/os_brew/fgets.c @@ -0,0 +1,42 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2006-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * fgets -- + * + * PUBLIC: #ifndef HAVE_FGETS + * PUBLIC: char *fgets __P((char *, int, FILE *)); + * PUBLIC: #endif + */ +char * +fgets(s, n, fp) + char *s; + int n; + FILE *fp; +{ + int c; + char *cs; + + c = 0; + cs = s; + + while (--n > 0 && (c = fgetc(fp)) != EOF) { + *cs++ = c; + if (c == '\n') + break; + } + if (c == EOF && cs == s) + return (NULL); + + *cs++ = '\0'; + return (s); +} diff --git a/os_brew/fopen.c b/os_brew/fopen.c new file mode 100644 index 00000000..1d4066b6 --- /dev/null +++ b/os_brew/fopen.c @@ -0,0 +1,89 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2006-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * fopen -- + * + * PUBLIC: #ifndef HAVE_FOPEN + * PUBLIC: FILE *fopen __P((const char *, const char *)); + * PUBLIC: #endif + */ +FILE * +fopen(filename, mode) + const char *filename, *mode; +{ + IFile *pIFile; + IFileMgr *pIFileMgr; + OpenFileMode flags; + int f_exists, ret, update_flag; + + /* + * Note: files are created with read/write privilege. + * + * Upon successful completion, fopen() returns a pointer to the + * object controlling the stream. Otherwise, NULL is returned, + * and errno is set to indicate the error. + */ + DB_ASSERT(NULL, filename != NULL && mode != NULL); + + FILE_MANAGER_CREATE(NULL, pIFileMgr, ret); + if (ret != 0) { + __os_set_errno(ret); + return (NULL); + } + + /* + * The argument mode points to a string beginning with one of the + * following sequences: + * r or rb + * Open file for reading. + * w or wb + * Truncate to zero length or create file for writing. + * a or ab + * Append; open or create file for writing at end-of-file. + * r+ or rb+ or r+b + * Open file for update (reading and writing). + * w+ or wb+ or w+b + * Truncate to zero length or create file for update. + * a+ or ab+ or a+b + * Append; open or create file for update, writing at end-of-file. + */ + flags = 0; + update_flag = strchr(mode, '+') ? 1 : 0; + switch (*mode) { + case 'a': /* append mode */ + flags = _OFM_APPEND | _OFM_CREATE; + break; + case 'r': /* read mode */ + flags = update_flag ? _OFM_READWRITE : _OFM_READ; + break; + case 'w': /* write mode */ + flags = _OFM_READWRITE | _OFM_CREATE; + break; + } + + f_exists = IFILEMGR_Test(pIFileMgr, filename) == SUCCESS ? 1 : 0; + if (f_exists) + LF_CLR(_OFM_CREATE); /* Clear _OFM_CREATE. */ + else + LF_CLR(~_OFM_CREATE); /* Leave only _OFM_CREATE. */ + + if ((pIFile = IFILEMGR_OpenFile( + pIFileMgr, filename, (OpenFileMode)flags)) == NULL) { + FILE_MANAGER_ERR(NULL, + pIFileMgr, filename, "IFILEMGR_OpenFile", ret); + __os_set_errno(ret); + } + + IFILEMGR_Release(pIFileMgr); + return (pIFile); +} diff --git a/os_brew/fwrite.c b/os_brew/fwrite.c new file mode 100644 index 00000000..28f11884 --- /dev/null +++ b/os_brew/fwrite.c @@ -0,0 +1,31 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2006-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * fwrite -- + * + * PUBLIC: #ifndef HAVE_FWRITE + * PUBLIC: size_t fwrite __P((const void *, size_t, size_t, FILE *)); + * PUBLIC: #endif + */ +size_t +fwrite(buf, size, count, fp) + const void *buf; + size_t size, count; + FILE *fp; +{ + if (fp == stderr) { + DBGPRINTF("%.*s", (int)count, buf); + return (size * count); + } else + return ((size_t)IFILE_Write(fp, buf, size * count) / size); +} diff --git a/os_brew/getcwd.c b/os_brew/getcwd.c new file mode 100644 index 00000000..8e5fb182 --- /dev/null +++ b/os_brew/getcwd.c @@ -0,0 +1,61 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2006-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * getcwd -- + */ +char * +getcwd(buf, size) + char *buf; + size_t size; +{ + IFileMgr *pIFileMgr; + int ret; +#ifndef HAVE_BREW_SDK2 + int outlen; +#endif + + FILE_MANAGER_CREATE(NULL, pIFileMgr, ret); + if (ret != 0) { + __os_set_errno(ret); + return (NULL); + } + + buf[0] = '\0'; + +#ifdef AEE_SIMULATOR + /* If AEE_SIMULATOR, we should mimic the resolvepath. */ + if (IFILEMGR_Test(pIFileMgr, "fs:/") == SUCCESS) + /* Current directory. */ + (void)strncpy(buf, "fs:/", size - 1); + else + FILE_MANAGER_ERR( + NULL, pIFileMgr, NULL, "IFILEMGR_ResolvePath", ret); +#else +#ifndef HAVE_BREW_SDK2 + outlen = size; + if (IFILEMGR_ResolvePath(pIFileMgr, ".", buf, &outlen) != SUCCESS) + FILE_MANAGER_ERR( + NULL, pIFileMgr, NULL, "IFILEMGR_ResolvePath", ret); +#endif +#endif + + IFILEMGR_Release(pIFileMgr); + + if (ret == 0) + return (buf); + + __os_set_errno(ret); + + COMPQUIET(size, 0); + return (NULL); +} diff --git a/os_brew/globals.c b/os_brew/globals.c new file mode 100644 index 00000000..d25efe6b --- /dev/null +++ b/os_brew/globals.c @@ -0,0 +1,53 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1999-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * brew_bdb_begin -- + * Initialize the BREW port of Berkeley DB. + */ +int +brew_bdb_begin() +{ + void *p; + + /* + * The BREW ARM compiler can't handle statics or globals, so we have + * store them off the AEEApplet and initialize them in in-line code. + */ + p = ((BDBApp *)GETAPPINSTANCE())->db_global_values; + if (p == NULL) { + if ((p = malloc(sizeof(DB_GLOBALS))) == NULL) + return (ENOMEM); + memset(p, 0, sizeof(DB_GLOBALS)); + + ((BDBApp *)GETAPPINSTANCE())->db_global_values = p; + + TAILQ_INIT(&DB_GLOBAL(envq)); + DB_GLOBAL(db_line) = + "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="; + } + return (0); +} + +/* + * brew_bdb_end -- + * Close down the BREW port of Berkeley DB. + */ +void +brew_bdb_end() +{ + void *p; + + p = ((BDBApp *)GETAPPINSTANCE())->db_global_values; + + free(p); +} diff --git a/os_brew/localtime.c b/os_brew/localtime.c new file mode 100644 index 00000000..4060da21 --- /dev/null +++ b/os_brew/localtime.c @@ -0,0 +1,78 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2006-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * localtime -- + * + * PUBLIC: #ifndef HAVE_LOCALTIME + * PUBLIC: struct tm *localtime __P((const time_t *)); + * PUBLIC: #endif + */ +struct tm * +localtime(tod) + const time_t *tod; +{ + JulianType jt; + boolean is_ds; /* Daylight savings. */ + time_t tt; + int increment; + + /* + * Berkeley DB uses POSIX time values internally, convert to a BREW + * time value. + */ + is_ds = 0; + tt = *tod - BREW_EPOCH_OFFSET + LOCALTIMEOFFSET(&is_ds); + + GETJULIANDATE(tt, &jt); + + DB_GLOBAL(ltm).tm_sec = jt.wSecond; /* seconds (0 - 60) */ + DB_GLOBAL(ltm).tm_min = jt.wMinute; /* minutes (0 - 59) */ + DB_GLOBAL(ltm).tm_hour = jt.wHour; /* hours (0 - 23) */ + DB_GLOBAL(ltm).tm_mday = jt.wDay; /* day of month (1 - 31) */ + DB_GLOBAL(ltm).tm_mon = jt.wMonth - 1; /* month of year (0 - 11) */ + /* year - 1900 */ + DB_GLOBAL(ltm).tm_year = jt.wYear - 1900; + /* day of week (Sunday = 0) */ + DB_GLOBAL(ltm).tm_wday = (jt.wWeekDay + 1) % 7; + /* day of year (0 - 365) */ + switch (DB_GLOBAL(ltm).tm_mon) { + default: + case 0: increment = 0; break; + case 1: increment = 31; break; + case 2: increment = 59; break; /* Feb = 28 */ + case 3: increment = 90; break; + case 4: increment = 120; break; + case 5: increment = 151; break; + case 6: increment = 181; break; + case 7: increment = 212; break; + case 8: increment = 243; break; + case 9: increment = 273; break; + case 10: increment = 304; break; + case 11: increment = 334; break; + } + DB_GLOBAL(ltm).tm_yday = increment + DB_GLOBAL(ltm).tm_mday - 1; + + if (DB_GLOBAL(ltm).tm_mon > 1 && /* +1 leap years after Feb. */ + jt.wYear % 4 == 0 && (jt.wYear % 100 != 0 || jt.wYear % 400 == 0)) + DB_GLOBAL(ltm).tm_yday += 1; + + DB_GLOBAL(ltm).tm_isdst = is_ds; /* daylight savings time */ + + /* + * !!! + * This routine is not thread-safe. Berkeley DB should convert + * to using localtime_r() where it's available, and this routine + * should be re-written in the form of localtime_r(). + */ + return (&DB_GLOBAL(ltm)); +} diff --git a/os_brew/os_abort.c b/os_brew/os_abort.c new file mode 100644 index 00000000..7869ff66 --- /dev/null +++ b/os_brew/os_abort.c @@ -0,0 +1,28 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2005-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_abort -- + */ +void +__os_abort(env) + ENV *env; +{ + AEEApplet *app; + + COMPQUIET(env, NULL); + + app = (AEEApplet *)GETAPPINSTANCE(); + ISHELL_CloseApplet(app->m_pIShell, FALSE); + + /* NOTREACHED */ +} diff --git a/os_brew/os_abs.c b/os_brew/os_abs.c new file mode 100644 index 00000000..0a718339 --- /dev/null +++ b/os_brew/os_abs.c @@ -0,0 +1,22 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2006-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_abspath -- + * Return if a path is an absolute path. + */ +int +__os_abspath(path) + const char *path; +{ + return (path[0] == 'f' && path[1] == 's' && path[2] == ':'); +} diff --git a/os_brew/os_clock.c b/os_brew/os_clock.c new file mode 100644 index 00000000..95b8c8b8 --- /dev/null +++ b/os_brew/os_clock.c @@ -0,0 +1,34 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2006-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_gettime -- + * Return the current time-of-day clock in seconds and nanoseconds. + */ +void +__os_gettime(env, tp, monotonic) + ENV *env; + db_timespec *tp; + int monotonic; +{ + /* + * Berkeley DB uses POSIX time values internally; convert a BREW time + * value into a POSIX time value. + */ + tp->tv_sec = +#ifdef HAVE_BREW_SDK2 + (time_t)GETTIMESECONDS() + BREW_EPOCH_OFFSET; +#else + (time_t)GETUTCSECONDS() + BREW_EPOCH_OFFSET; +#endif + tp->tv_nsec = 0; +} diff --git a/os_brew/os_config.c b/os_brew/os_config.c new file mode 100644 index 00000000..02f0340a --- /dev/null +++ b/os_brew/os_config.c @@ -0,0 +1,59 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1998-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_fs_notzero -- + * Return 1 if allocated filesystem blocks are not zeroed. + */ +int +__os_fs_notzero() +{ + /* + * XXX + * We don't know if the BREW filesystem zero-fills newly allocated + * filesystem blocks. For now, be conservative and zero out blocks + * in Berkeley DB. + * + * This should be tested. + */ + return (1); +} + +/* + * __os_support_direct_io -- + * Return 1 if we support direct I/O. + */ +int +__os_support_direct_io() +{ + return (0); +} + +/* + * __os_support_db_register -- + * Return 1 if the system supports DB_REGISTER. + */ +int +__os_support_db_register() +{ + return (0); +} + +/* + * __os_support_replication -- + * Return 1 if the system supports replication. + */ +int +__os_support_replication() +{ + return (0); +} diff --git a/os_brew/os_dir.c b/os_brew/os_dir.c new file mode 100644 index 00000000..6f05d0f3 --- /dev/null +++ b/os_brew/os_dir.c @@ -0,0 +1,84 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1997-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_dirlist -- + * Return a list of the files in a directory. + */ +int +__os_dirlist(env, dir, returndir, namesp, cntp) + ENV *env; + const char *dir; + int returndir, *cntp; + char ***namesp; +{ + FileInfo fi; + IFileMgr *pIFileMgr; + int arraysz, cnt, ret; + char *filename, *p, **names; + + FILE_MANAGER_CREATE(env, pIFileMgr, ret); + if (ret != 0) + return (ret); + + if ((ret = IFILEMGR_EnumInit(pIFileMgr, dir, FALSE)) != SUCCESS) { + IFILEMGR_Release(pIFileMgr); + __db_syserr(env, ret, "IFILEMGR_EnumInit"); + return (__os_posix_err(ret)); + } + + names = NULL; + arraysz = cnt = 0; + while (IFILEMGR_EnumNext(pIFileMgr, &fi) != FALSE) { + if (++cnt >= arraysz) { + arraysz += 100; + if ((ret = __os_realloc(env, + (u_int)arraysz * sizeof(char *), &names)) != 0) + goto nomem; + } + for (filename = fi.szName; + (p = strchr(filename, '\\')) != NULL; filename = p + 1) + ; + for (; (p = strchr(filename, '/')) != NULL; filename = p + 1) + ; + if ((ret = __os_strdup(env, filename, &names[cnt - 1])) != 0) + goto nomem; + } + IFILEMGR_Release(pIFileMgr); + + *namesp = names; + *cntp = cnt; + return (ret); + +nomem: if (names != NULL) + __os_dirfree(env, names, cnt); + IFILEMGR_Release(pIFileMgr); + + COMPQUIET(returndir, 0); + + return (ret); +} + +/* + * __os_dirfree -- + * Free the list of files. + */ +void +__os_dirfree(env, names, cnt) + ENV *env; + char **names; + int cnt; +{ + while (cnt > 0) + __os_free(env, names[--cnt]); + __os_free(env, names); +} diff --git a/os_brew/os_errno.c b/os_brew/os_errno.c new file mode 100644 index 00000000..bc5e0f25 --- /dev/null +++ b/os_brew/os_errno.c @@ -0,0 +1,196 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1999-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_get_errno_ret_zero -- + * Return the last system error, including an error of zero. + */ +int +__os_get_errno_ret_zero() +{ + /* This routine must be able to return the same value repeatedly. */ + return (errno); +} + +/* + * We've seen cases where system calls failed but errno was never set. For + * that reason, __os_get_errno() and __os_get_syserr set errno to EAGAIN if + * it's not already set, to work around the problem. For obvious reasons, + * we can only call this function if we know an error has occurred, that + * is, we can't test the return for a non-zero value after the get call. + * + * __os_get_errno -- + * Return the last ANSI C "errno" value or EAGAIN if the last error + * is zero. + */ +int +__os_get_errno() +{ + /* This routine must be able to return the same value repeatedly. */ + if (errno == 0) + __os_set_errno(EAGAIN); + return (errno); +} + +/* + * __os_get_syserr -- + * Return the last system error or EAGAIN if the last error is zero. + */ +int +__os_get_syserr() +{ + /* This routine must be able to return the same value repeatedly. */ + if (errno == 0) + __os_set_errno(EAGAIN); + return (errno); +} + +/* + * __os_set_errno -- + * Set the value of errno. + */ +void +__os_set_errno(evalue) + int evalue; +{ + /* + * This routine is called by the compatibility interfaces (DB 1.85, + * dbm and hsearch). Force values > 0, that is, not one of DB 2.X + * and later's public error returns. If something bad has happened, + * default to EFAULT -- a nasty return. Otherwise, default to EINVAL. + * As the compatibility APIs aren't included on Windows, the Windows + * version of this routine doesn't need this behavior. + */ + errno = + evalue >= 0 ? evalue : (evalue == DB_RUNRECOVERY ? EFAULT : EINVAL); +} + +/* + * __os_strerror -- + * Return a string associated with the system error. + */ +char * +__os_strerror(error, buf, len) + int error; + char *buf; + size_t len; +{ + char *p; + + switch (error) { + case EBADFILENAME: + p = "EBADFILENAME"; + break; + case EBADSEEKPOS: + p = "EBADSEEKPOS"; + break; +#ifndef HAVE_BREW_SDK2 + case EDIRNOEXISTS: + p = "EDIRNOEXISTS"; + break; +#endif + case EDIRNOTEMPTY: + p = "EDIRNOTEMPTY"; + break; + case EFILEEOF: + p = "EFILEEOF"; + break; + case EFILEEXISTS: + p = "EFILEEXISTS"; + break; + case EFILENOEXISTS: + p = "EFILENOEXISTS"; + break; + case EFILEOPEN: + p = "EFILEOPEN"; + break; + case EFSFULL: + p = "EFSFULL"; + break; +#ifndef HAVE_BREW_SDK2 + case EINVALIDOPERATION: + p = "EINVALIDOPERATION"; + break; + case ENOMEDIA: + p = "ENOMEDIA"; + break; +#endif + case ENOMEMORY: + p = "ENOMEMORY"; + break; + case EOUTOFNODES: + p = "EOUTOFNODES"; + break; + default: + p = __db_unknown_error(error); + break; + } + + (void)strncpy(buf, p, len - 1); + buf[len - 1] = '\0'; + + return (buf); +} + +/* + * __os_posix_err + * Convert a system error to a POSIX error. + */ +int +__os_posix_err(error) + int error; +{ + int ret; + + switch (error) { + case EBADFILENAME: +#ifndef HAVE_BREW_SDK2 + case EDIRNOEXISTS: +#endif + case EDIRNOTEMPTY: + case EFILENOEXISTS: + ret = ENOENT; + break; + + case EOUTOFNODES: + ret = EMFILE; + break; + + case ENOMEMORY: + ret = ENOMEM; + break; + + case EFSFULL: + ret = ENOSPC; + break; + +#ifndef HAVE_BREW_SDK2 + case EINVALIDOPERATION: + ret = DB_OPNOTSUP; + break; +#endif + + case EFILEEXISTS: + ret = EEXIST; + break; + + case EBADSEEKPOS: + case EFILEEOF: + ret = EIO; + break; + + default: + ret = EFAULT; + break; + } + return (ret); +} diff --git a/os_brew/os_handle.c b/os_brew/os_handle.c new file mode 100644 index 00000000..f71ff09b --- /dev/null +++ b/os_brew/os_handle.c @@ -0,0 +1,102 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1998-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_openhandle -- + * Open a file, using BREW open flags. + */ +int +__os_openhandle(env, name, flags, mode, fhpp) + ENV *env; + const char *name; + int flags, mode; + DB_FH **fhpp; +{ + DB_FH *fhp; + IFile *pIFile; + IFileMgr *pIFileMgr; + int f_exists, ret; + + COMPQUIET(mode, 0); + + FILE_MANAGER_CREATE(env, pIFileMgr, ret); + if (ret != 0) + return (ret); + + /* + * Allocate the file handle and copy the file name. We generally only + * use the name for verbose or error messages, but on systems where we + * can't unlink temporary files immediately, we use the name to unlink + * the temporary file when the file handle is closed. + */ + if ((ret = __os_calloc(env, 1, sizeof(DB_FH), &fhp)) != 0) + return (ret); + if ((ret = __os_strdup(env, name, &fhp->name)) != 0) + goto err; + + /* + * Test the file before opening. BREW doesn't want to see the + * _OFM_CREATE flag if the file already exists, and it doesn't + * want to see any other flag if the file doesn't exist. + */ + f_exists = IFILEMGR_Test(pIFileMgr, name) == SUCCESS ? 1 : 0; + if (f_exists) + LF_CLR(_OFM_CREATE); /* Clear _OFM_CREATE. */ + else + LF_CLR(~_OFM_CREATE); /* Leave only _OFM_CREATE. */ + + if ((pIFile = + IFILEMGR_OpenFile(pIFileMgr, name, (OpenFileMode)flags)) == NULL) { + FILE_MANAGER_ERR(env, + pIFileMgr, name, "IFILEMGR_OpenFile", ret); + goto err; + } + + fhp->ifp = pIFile; + IFILEMGR_Release(pIFileMgr); + + F_SET(fhp, DB_FH_OPENED); + *fhpp = fhp; + return (0); + +err: if (pIFile != NULL) + IFILE_Release(pIFile); + IFILEMGR_Release(pIFileMgr); + + if (fhp != NULL) + (void)__os_closehandle(env, fhp); + return (ret); +} + +/* + * __os_closehandle -- + * Close a file. + */ +int +__os_closehandle(env, fhp) + ENV *env; + DB_FH *fhp; +{ + /* Discard any underlying system file reference. */ + if (F_ISSET(fhp, DB_FH_OPENED)) + (void)IFILE_Release(fhp->ifp); + + /* Unlink the file if we haven't already done so. */ + if (F_ISSET(fhp, DB_FH_UNLINK)) + (void)__os_unlink(env, fhp->name, 0); + + if (fhp->name != NULL) + __os_free(env, fhp->name); + __os_free(env, fhp); + + return (0); +} diff --git a/os_brew/os_mkdir.c b/os_brew/os_mkdir.c new file mode 100644 index 00000000..ba46bb2a --- /dev/null +++ b/os_brew/os_mkdir.c @@ -0,0 +1,40 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1997-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_mkdir -- + * Create a directory. + */ +int +__os_mkdir(env, name, mode) + ENV *env; + const char *name; + int mode; +{ + IFileMgr *ifmp; + int ret; + + COMPQUIET(mode, 0); + + FILE_MANAGER_CREATE(env, ifmp, ret); + if (ret != 0) + return (ret); + + if (IFILEMGR_MkDir(ifmp, name) == SUCCESS) + ret = 0; + else + FILE_MANAGER_ERR(env, ifmp, name, "IFILEMGR_MkDir", ret); + + IFILEMGR_Release(ifmp); + + return (ret); +} diff --git a/os_brew/os_open.c b/os_brew/os_open.c new file mode 100644 index 00000000..6a370423 --- /dev/null +++ b/os_brew/os_open.c @@ -0,0 +1,51 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1997-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_open -- + * Open a file descriptor (including page size and log size information). + */ +int +__os_open(env, name, page_size, flags, mode, fhpp) + ENV *env; + const char *name; + u_int32_t page_size, flags; + int mode; + DB_FH **fhpp; +{ + OpenFileMode oflags; + int ret; + + COMPQUIET(page_size, 0); + +#define OKFLAGS \ + (DB_OSO_ABSMODE | DB_OSO_CREATE | DB_OSO_DIRECT | DB_OSO_DSYNC |\ + DB_OSO_EXCL | DB_OSO_RDONLY | DB_OSO_REGION | DB_OSO_SEQ | \ + DB_OSO_TEMP | DB_OSO_TRUNC) + if ((ret = __db_fchk(env, "__os_open", flags, OKFLAGS)) != 0) + return (ret); + + oflags = 0; + if (LF_ISSET(DB_OSO_CREATE)) + oflags |= _OFM_CREATE; + + if (LF_ISSET(DB_OSO_RDONLY)) + oflags |= _OFM_READ; + else + oflags |= _OFM_READWRITE; + + if ((ret = __os_openhandle(env, name, oflags, mode, fhpp)) != 0) + return (ret); + if (LF_ISSET(DB_OSO_REGION)) + F_SET(fhp, DB_FH_REGION); + return (ret); +} diff --git a/os_brew/os_pid.c b/os_brew/os_pid.c new file mode 100644 index 00000000..2a4b636c --- /dev/null +++ b/os_brew/os_pid.c @@ -0,0 +1,33 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2001-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_id -- + * Return the current process ID. + */ +void +__os_id(dbenv, pidp, tidp) + DB_ENV *dbenv; + pid_t *pidp; + db_threadid_t *tidp; +{ + AEEApplet *app; + + COMPQUIET(dbenv, NULL); + + if (pidp != NULL) { + app = (AEEApplet *)GETAPPINSTANCE(); + *pidp = (pid_t)ISHELL_ActiveApplet(app->m_pIShell); + } + if (tidp != NULL) + *tidp = 0; +} diff --git a/os_brew/os_rename.c b/os_brew/os_rename.c new file mode 100644 index 00000000..a6ca81c3 --- /dev/null +++ b/os_brew/os_rename.c @@ -0,0 +1,43 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1997-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_rename -- + * Rename a file. + */ +int +__os_rename(env, old, new, silent) + ENV *env; + const char *old, *new; + u_int32_t silent; +{ + IFileMgr *pIFileMgr; + int ret; + + FILE_MANAGER_CREATE(env, pIFileMgr, ret); + if (ret != 0) + return (ret); + + LAST_PANIC_CHECK_BEFORE_IO(env); + + if (IFILEMGR_Rename(pIFileMgr, old, new) == SUCCESS) + ret = 0; + else + if (!silent) + FILE_MANAGER_ERR(env, + pIFileMgr, old, "IFILEMGR_Rename", ret); + else + ret = __os_posix_err(IFILEMGR_GetLastError(pIFileMgr)); + + IFILEMGR_Release(pIFileMgr); + return (ret); +} diff --git a/os_brew/os_rw.c b/os_brew/os_rw.c new file mode 100644 index 00000000..cbbb413c --- /dev/null +++ b/os_brew/os_rw.c @@ -0,0 +1,154 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1997-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_io -- + * Do an I/O. + */ +int +__os_io(env, op, fhp, pgno, pgsize, relative, io_len, buf, niop) + ENV *env; + int op; + DB_FH *fhp; + db_pgno_t pgno; + u_int32_t pgsize, relative, io_len; + u_int8_t *buf; + size_t *niop; +{ + int ret; + + MUTEX_LOCK(env, fhp->mtx_fh); + + if ((ret = __os_seek(env, fhp, pgno, pgsize, relative)) != 0) + goto err; + switch (op) { + case DB_IO_READ: + ret = __os_read(env, fhp, buf, io_len, niop); + break; + case DB_IO_WRITE: + ret = __os_write(env, fhp, buf, io_len, niop); + break; + default: + ret = EINVAL; + break; + } + +err: MUTEX_UNLOCK(env, fhp->mtx_fh); + + return (ret); + +} + +/* + * __os_read -- + * Read from a file handle. + */ +int +__os_read(env, fhp, addr, len, nrp) + ENV *env; + DB_FH *fhp; + void *addr; + size_t len; + size_t *nrp; +{ + FileInfo pInfo; + int ret; + size_t offset, nr; + char *taddr; + + ret = 0; + +#if defined(HAVE_STATISTICS) + ++fhp->read_count; +#endif + + for (taddr = addr, offset = 0, nr = 0; + offset < len; taddr += nr, offset += (u_int32_t)nr) { + LAST_PANIC_CHECK_BEFORE_IO(env); + nr = (size_t)IFILE_Read(fhp->ifp, addr, len); + /* an error occured, or we reached the end of the file */ + if (nr == 0) + break; + } + if (nr == 0) { + IFILE_GetInfo(fhp->ifp, &pInfo); + if (pInfo.dwSize != 0) {/* not an empty file */ + /* + * If we have not reached the end of the file, + * we got an error in IFILE_Read + */ + if (IFILE_Seek( + fhp->ifp, _SEEK_CURRENT, 0) != pInfo.dwSize) { + ret = __os_get_syserr(); + __db_syserr(env, ret, "IFILE_Read: %#lx, %lu", + P_TO_ULONG(addr), (u_long)len); + ret = __os_posix_err(ret); + } + } + } + *nrp = (size_t)(taddr - (u_int8_t *)addr); + return (ret); +} + +/* + * __os_write -- + * Write to a file handle. + */ +int +__os_write(env, fhp, addr, len, nwp) + ENV *env; + DB_FH *fhp; + void *addr; + size_t len; + size_t *nwp; +{ + +#ifdef HAVE_FILESYSTEM_NOTZERO + /* Zero-fill as necessary. */ + if (__os_fs_notzero()) { + int ret; + if ((ret = __db_zero_fill(env, fhp)) != 0) + return (ret); + } +#endif + return (__os_physwrite(env, fhp, addr, len, nwp)); +} + +/* + * __os_physwrite -- + * Physical write to a file handle. + */ +int +__os_physwrite(env, fhp, addr, len, nwp) + ENV *env; + DB_FH *fhp; + void *addr; + size_t len; + size_t *nwp; +{ + int ret; + + ret = 0; + +#if defined(HAVE_STATISTICS) + ++fhp->write_count; +#endif + + LAST_PANIC_CHECK_BEFORE_IO(env); + if ((*nwp = (size_t)IFILE_Write(fhp->ifp, addr, len)) != len) { + ret = __os_get_syserr(); + __db_syserr(env, ret, "IFILE_Write: %#lx, %lu", + P_TO_ULONG(addr), (u_long)len); + ret = __os_posix_err(ret); + } + return (ret); +} diff --git a/os_brew/os_seek.c b/os_brew/os_seek.c new file mode 100644 index 00000000..7087b49a --- /dev/null +++ b/os_brew/os_seek.c @@ -0,0 +1,53 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1997-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_seek -- + * Seek to a page/byte offset in the file. + */ +int +__os_seek(env, fhp, pgno, pgsize, relative) + ENV *env; + DB_FH *fhp; + db_pgno_t pgno; + u_int32_t pgsize; + u_int32_t relative; +{ + off_t offset; + int ret; + +#if defined(HAVE_STATISTICS) + ++fhp->seek_count; +#endif + + offset = (off_t)pgsize * pgno + relative; + + /* + * Use BREW's lseek function IFILE_Seek. If the seek fails, the source + * returns EBADSEEKPOS. + */ + ret = IFILE_Seek(fhp->ifp, _SEEK_START, offset); + + if (ret == SUCCESS) { + fhp->pgsize = pgsize; + fhp->pgno = pgno; + fhp->offset = relative; + ret = 0; + } else { + __db_syserr(env, ret, + "seek: %lu: (%lu * %lu) + %lu", (u_long)offset, + (u_long)pgno, (u_long)pgsize, (u_long)relative); + ret = __os_posix_err(ret); + } + + return (ret); +} diff --git a/os_brew/os_stat.c b/os_brew/os_stat.c new file mode 100644 index 00000000..40b883f7 --- /dev/null +++ b/os_brew/os_stat.c @@ -0,0 +1,89 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1997-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_exists -- + * Return if the file exists. + */ +int +__os_exists(env, path, isdirp) + ENV *env; + const char *path; + int *isdirp; +{ + FileInfo fInfo; + IFileMgr *pIFileMgr; + int ret; + + FILE_MANAGER_CREATE(env, pIFileMgr, ret); + if (ret != 0) + return (ret); + + ret = 0; + if (IFILEMGR_Test(pIFileMgr, path) == EFAILED) { + FILE_MANAGER_ERR( + env, pIFileMgr, path, "IFILEMGR_Test", ret); + goto err; + } + + if (isdirp != NULL) { + if (IFILEMGR_GetInfo(pIFileMgr, path, &fInfo) == EFAILED) { + FILE_MANAGER_ERR( + env, pIFileMgr, path, "IFILEMGR_GetInfo", ret); + goto err; + } + *isdirp = fInfo.attrib == _FA_DIR ? 1 : 0; + } + +err: IFILEMGR_Release(pIFileMgr); + + return (ret); +} + +/* + * __os_ioinfo -- + * Return file size and I/O size; abstracted to make it easier + * to replace. + */ +int +__os_ioinfo(env, path, fhp, mbytesp, bytesp, iosizep) + ENV *env; + const char *path; + DB_FH *fhp; + u_int32_t *mbytesp, *bytesp, *iosizep; +{ + FileInfo fInfo; + IFileMgr *pIFileMgr; + int ret; + + FILE_MANAGER_CREATE(env, pIFileMgr, ret); + if (ret != 0) + return (ret); + + if (IFILE_GetInfo(fhp->ifp, &fInfo) != SUCCESS) { + FILE_MANAGER_ERR(env, pIFileMgr, path, "IFILE_GetInfo", ret); + goto err; + } + + /* Return the size of the file. */ + if (mbytesp != NULL) + *mbytesp = (u_int32_t)(fInfo.dwSize / MEGABYTE); + if (bytesp != NULL) + *bytesp = (u_int32_t)(fInfo.dwSize % MEGABYTE); + + /* Default the filesystem I/O size. */ + if (iosizep != NULL) + *iosizep = DB_DEF_IOSIZE; + +err: IFILEMGR_Release(pIFileMgr); + return (ret); +} diff --git a/os_brew/os_truncate.c b/os_brew/os_truncate.c new file mode 100644 index 00000000..b317c657 --- /dev/null +++ b/os_brew/os_truncate.c @@ -0,0 +1,48 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2004-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_truncate -- + * Truncate the file. + */ +int +__os_truncate(env, fhp, pgno, pgsize) + ENV *env; + DB_FH *fhp; + db_pgno_t pgno; + u_int32_t pgsize; +{ + IFileMgr *pIFileMgr; + off_t offset; + int ret; + + FILE_MANAGER_CREATE(env, pIFileMgr, ret); + if (ret != 0) + return (ret); + + /* + * Truncate a file so that "pgno" is discarded from the end of the + * file. + */ + offset = (off_t)pgsize * pgno; + + LAST_PANIC_CHECK_BEFORE_IO(env); + + if (IFILE_Truncate(fhp->ifp, offset) == SUCCESS) + ret = 0; + else + FILE_MANAGER_ERR(env, pIFileMgr, NULL, "IFILE_Truncate", ret); + + IFILEMGR_Release(pIFileMgr); + + return (ret); +} diff --git a/os_brew/os_unlink.c b/os_brew/os_unlink.c new file mode 100644 index 00000000..ff9e80f5 --- /dev/null +++ b/os_brew/os_unlink.c @@ -0,0 +1,40 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1997-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_unlink -- + * Remove a file. + */ +int +__os_unlink(env, path, overwrite_test) + ENV *env; + const char *path; + int overwrite_test; +{ + IFileMgr *ifmp; + int ret; + + FILE_MANAGER_CREATE(env, ifmp, ret); + if (ret != 0) + return (ret); + + LAST_PANIC_CHECK_BEFORE_IO(env); + + if (IFILEMGR_Remove(ifmp, path) == EFAILED) + FILE_MANAGER_ERR(env, ifmp, path, "IFILEMGR_Remove", ret); + + IFILEMGR_Release(ifmp); + + COMPQUIET(overwrite_test, 0); + + return (ret); +} diff --git a/os_brew/os_yield.c b/os_brew/os_yield.c new file mode 100644 index 00000000..ed7e7226 --- /dev/null +++ b/os_brew/os_yield.c @@ -0,0 +1,40 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 1997-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +#include "db_config.h" + +#include "db_int.h" + +/* + * __os_yield -- + * Yield the processor, optionally pausing until running again. + */ +void +__os_yield(env, secs, usecs) + ENV *env; + u_long secs, usecs; /* Seconds and microseconds. */ +{ + COMPQUIET(env, NULL); + +#ifdef HAVE_BREW_SDK2 + COMPQUIET(secs, 0); + COMPQUIET(usecs, 0); +#else + /* Don't require the values be normalized. */ + for (; usecs >= US_PER_SEC; usecs -= US_PER_SEC) + ++secs; + + /* + * Yield the processor so other processes or threads can run. + * + * Sheer raving paranoia -- don't sleep for 0 time, in case some + * implementation doesn't yield the processor in that case. + */ + MSLEEP(secs * MS_PER_SEC + (usecs / US_PER_MS) + 1); +#endif +} |