taffcns.c
来自「db.* (pronounced dee-be star) is an adva」· C语言 代码 · 共 544 行 · 第 1/2 页
C
544 行
{ /* TAF does not exist - try to create it */ task->lfn = open_b(task->dbtaf, fopt | O_CREAT, PSP_FLAG_SYNC, task); if (task->lfn == NULL) { task->dboptions |= rd_opt; return (dberr(S_TAFCREATE)); } STAT_taf_open(task); memset(&tafbuf, '\0', sizeof(TAFFILE)); tafbuf.unicode = DBSTAR_UNICODE_FLAG; vtstrcpy(tafbuf.lmc_type, task->lmc_type); if (task->lockmgrn) vtstrcpy(tafbuf.lockmgrn, task->lockmgrn); else tafbuf.lockmgrn[0] = DB_TEXT('\0'); if (psp_fileSeekWrite(task->lfn, 0, &tafbuf, sizeof(tafbuf)) < (int) sizeof(tafbuf)) { psp_fileClose(task->lfn); task->lfn = NULL; task->dboptions |= rd_opt; return (dberr(S_DBLERR)); } STAT_taf_write(sizeof(tafbuf), task); commit_file(task->lfn, task); if (task->dboptions & PORTABLE) { PSP_FH lfg; /* also create lock file guard file - guard file must be opened with O_SYNC */ lfg = open_b(dblfg_name(task), O_CREAT|O_SYNC, 0, task); if (lfg != NULL) psp_fileClose(lfg); } } else { task->dboptions |= rd_opt; return (dberr(S_TAFLOCK)); } } else STAT_taf_open(task); task->dboptions |= rd_opt; return (task->db_status);}/* ====================================================================== Close Transaction Activity File*/int INTERNAL_FCN taf_close(DB_TASK *task){ if (task->lfn != NULL) { psp_fileClose(task->lfn); task->lfn = NULL; } return (task->db_status);}/* ====================================================================== Gain access and read transaction activity file*/int INTERNAL_FCN taf_access(int fcn, DB_TASK *task){ register int not_locked; int bytes; int taf_count = 0; int first_open = task->lfn == 0; int status; /* if PORTABLE open after getting lock below */ if (!(task->dboptions & PORTABLE) && !task->lfn) { if (taf_open(task) != S_OKAY) return task->db_status; }retry: errno = taf_count = 0; /* use as retry count until opened */ do {#ifdef MULTI_TAFFILE if (task->dboptions & MULTITAF && fcn & NO_TAFFILE_LOCK) not_locked = 0; else#endif not_locked = taf_lock(task); if (not_locked) { if (++taf_count >= MAXTRIES || psp_errno() == EINVAL) { /* this should never happen, but ... */ status = S_DBLACCESS; goto clean; } naptime(task); adjust_naptime(BY_NAP_FAILURE, task); } } while (not_locked); adjust_naptime(BY_NAP_SUCCESS, task); if (task->dboptions & PORTABLE && !task->lfn) { /* open the file only after obtaining exclusive access */ if (taf_open(task) != S_OKAY) return task->db_status; } bytes = psp_fileSeekRead(task->lfn, 0, &tafbuf, sizeof(tafbuf)); if (bytes < 0) {#ifdef MULTI_TAFFILE if (!(task->dboptions & MULTITAF) || !(fcn & NO_TAFFILE_LOCK))#endif taf_unlock(task); status = S_DBLERR; goto clean; } STAT_taf_read(sizeof(tafbuf), task); /* If a logfile is to be deleted, this check is not necessary */ if ((fcn & ADD_LOGFILE) && tafbuf.cnt >= TAFLIMIT) {#ifdef MULTI_TAFFILE if (!(task->dboptions & MULTITAF) || !(fcn & NO_TAFFILE_LOCK))#endif taf_unlock(task); /* Have to wait until a slot opens up */ naptime(task); adjust_naptime(BY_NAP_FAILURE, task); /* possible infinite loop if over TAFLIMIT commits and all processes are attempting to do a task->taf_add() */ goto retry; } adjust_naptime(BY_NAP_SUCCESS, task); return (task->db_status);clean: if (first_open && task->lfn) taf_close(task); return dberr(status);}/* ====================================================================== Write and release transaction activity file*/int INTERNAL_FCN taf_release(int fcn, DB_TASK *task){ int stat = S_OKAY; if (!(task->dboptions & READONLY)) { if (psp_fileSeekWrite(task->lfn, 0, &tafbuf, sizeof(tafbuf)) < (int) sizeof(tafbuf)) { /* need to do taf_unlock before returning */ stat = dberr(S_DBLERR); } else STAT_taf_write(sizeof(tafbuf), task); }#ifdef MULTI_TAFFILE if (!(task->dboptions & MULTITAF) || !(fcn & NO_TAFFILE_LOCK))#endif taf_unlock(task);#if 0 taf_close(task); /* so lots of tasks don't chew up all available file handles */#endif return (task->db_status = stat);}/* ====================================================================== Add log file name to taf*/int INTERNAL_FCN taf_add(const DB_TCHAR *tlogfile, DB_TASK *task){ int mode = ADD_LOGFILE;#ifdef MULTI_TAFFILE if (task->dboptions & MULTITAF) mode |= NO_TAFFILE_LOCK;#endif if (taf_access(mode, task) == S_OKAY) { vtstrncpy(tafbuf.files[tafbuf.cnt++], psp_pathGetFile(tlogfile), LOGFILELEN); taf_release(mode, task); } return task->db_status;}/* ====================================================================== Delete log file name from taf*/int INTERNAL_FCN taf_del(const DB_TCHAR *tlogfile, DB_TASK *task){ register int i; DB_TCHAR *file_ptr; DB_TCHAR *base_logfile = psp_pathGetFile(tlogfile); for (i = 0; i < tafbuf.cnt; ++i) { if (vtstrcmp(file_ptr = tafbuf.files[i], base_logfile) == 0) { if (i < --tafbuf.cnt) { vtstrncpy(file_ptr, tafbuf.files[tafbuf.cnt], LOGFILELEN); break; } } } return task->db_status;}/***************************************************************************/static DB_TCHAR * INTERNAL_FCN dblfg_name(DB_TASK *task){ DB_TCHAR *far_ptr; static DB_TCHAR dblfg[FILENMLEN]; /* lfg file is same name as task->dbtaf, but with .lfg extension */ vtstrcpy(dblfg, task->dbtaf); if ((far_ptr = vtstrrchr(dblfg, DB_TEXT('.'))) != NULL) *far_ptr = DB_TEXT('\0'); /* strip off . suffix */ vtstrcat(dblfg, DB_TEXT(".lfg")); /* append .lfg suffix */ return dblfg;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?