int DB_init() { apr_pool_t *p = NULL; apr_pool_initialize(); apr_pool_create(&p, NULL); if (access(DB_DIR, W_OK | X_OK) == -1) { apr_status_t rc = apr_dir_make_recursive(DB_DIR, APR_UREAD | APR_UWRITE | APR_UEXECUTE | APR_GREAD | APR_GWRITE | APR_GEXECUTE, p); check(rc == APR_SUCCESS, "Failed to make database dir: %s", DB_DIR); } if (access(DB_FILE, W_OK) == -1) { FILE *db = DB_open(DB_FILE, "w"); check(db, "Cannot open database: %s", DB_FILE); DB_close(db); } apr_pool_destroy(p); return 0; error: apr_pool_destroy(p); return -1; }
int DB_init() { apr_pool_t *p = NULL;// apr_pool_t被封装了,只能通过API对其进行操作 apr_pool_initialize(); apr_pool_create(&p, NULL); // 创建memory pool,返回apr_status_t类型的状态码 // 确定DB_DIR是否存在、是否有写和执行权限,成功返回0,否则-1 if(access(DB_DIR, W_OK | X_OK) == -1) { // 权限测试失败,可能是DB_DIR不存在,尝试创建 apr_status_t rc = apr_dir_make_recursive(DB_DIR, APR_UREAD | APR_UWRITE | APR_UEXECUTE | APR_GREAD | APR_GWRITE | APR_GEXECUTE, p); check(rc == APR_SUCCESS, "Failed to make database dir: %s", DB_DIR); } // 确定DB_FILE是否存在、是否有写权限 if(access(DB_FILE, W_OK) == -1) { // 权限测试失败,可能是DB_FILE不存在,尝试创建 FILE *db = DB_open(DB_FILE, "w"); check(db, "Cannot open database: %s", DB_FILE); DB_close(db); } apr_pool_destroy(p); return 0; error: apr_pool_destroy(p); return -1; }
/* 从数据库读取数据,并存入一个字符串中 */ static bstring DB_load() { FILE *db = NULL; bstring data = NULL; db = DB_open(DB_FILE, "r"); check(db, "Failed to open database: %s", DB_FILE); /*bstring bread (bNread readPtr, void * parm) * * Use a finite buffer fread-like function readPtr to create a bstring * filled with the entire contents of file-like source data in a roughly * efficient way. */ data = bread((bNread)fread, db); check(data, "Failed to read from db file: %s", DB_FILE); DB_close(db); return data; error: if(db) DB_close(db); if(data) bdestroy(data); return NULL; }
int DB_init() { apr_pool_t *p = NULL; apr_pool_initialize(); apr_pool_create(&p, NULL); if(access(DB_DIR, W_OK|X_OK) == -1){ apr_status_t rc = apr_dir_make_recursive(DB_DIR, APR_UREAD|APR_UWRITE|APR_UEXECUTE | APR_GREAD|APR_GWRITE|APR_GEXECUTE, p); check(rc == APR_SUCCESS, "Could not open %s.", DB_DIR); } else { debug("Access to %s is okay", DB_DIR); } if(access(DB_FILE, W_OK) == -1){ FILE *db = DB_open(DB_FILE, "w"); check(db, "Could not open %s.", DB_FILE); DB_close(db); } else { debug("No need to create db file %s", DB_FILE); } apr_pool_destroy(p); return 0; error: apr_pool_destroy(p); return -1; }
int DB_init() { apr_status_t rv; apr_pool_t *mp; rv = apr_initialize(); assert(rv == APR_SUCCESS); apr_pool_create(&mp, NULL); if (access(DB_DIR, W_OK | X_OK) == -1) { apr_status_t rc = apr_dir_make_recursive( DB_DIR, APR_UREAD | APR_UWRITE | APR_UEXECUTE | APR_GREAD | APR_GWRITE | APR_GEXECUTE, mp ); assert(rc == APR_SUCCESS); } if (access(DB_FILE, W_OK) == -1) { FILE *db = DB_open(DB_FILE, "w"); assert(db != NULL); DB_close(db); } apr_pool_destroy(mp); apr_terminate(); return 0; }
static bstring DB_load() { FILE *db = NULL; bstring data = NULL; db = DB_open(DB_FILE, "r"); check(db, "Failed to open database: %s", DB_FILE); data = bread((bNread)fread, db); check(data, "Failed to read from db file: %s", DB_FILE); DB_close(db); return data; error: if(db) DB_close(db); if(data) bdestroy(data); return NULL; }
static bstring DB_load() { FILE *db = NULL; bstring data = NULL; db = DB_open(DB_FILE, "r"); assert(db != NULL); data = bread((bNread)fread, db); assert(data != NULL); DB_close(db); return data; }
int DB_update(const char *url) { if(DB_find(url)) { log_info("Already recorded as installed: %s", url); } FILE *db = DB_open(DB_FILE, "a+"); check(db, "Failed to open DB file: %s", DB_FILE); bstring line = bfromcstr(url); bconchar(line, '\n'); int rc = fwrite(line->data, blength(line), 1, db); check(rc == 1, "Failed to append to the db."); return 0; error: if(db) DB_close(db); return -1; }
static bstring DB_load() { FILE *db = NULL; bstring data = NULL; db = DB_open(DB_FILE, "r"); check(db, "Failed to open %s.", DB_FILE); data = bread((bNread)fread, db); check(data, "Couldn't read %s.", DB_FILE); DB_close(db); return data; error: if(db) DB_close(db); if(data) bdestroy(data); return NULL; }
int DB_update(const char *url) { bool found = false; assert(DB_find(url, &found) == 0); if (found) { printf("INFO: Already recorded as installed: %s\n", url); } FILE *db = DB_open(DB_FILE, "a+"); assert(db != NULL); bstring line = bfromcstr(url); bconchar(line, '\n'); int rc = fwrite(line->data, blength(line), 1, db); assert(rc == 1); bdestroy(line); DB_close(db); return 0; }