static char *get_sync_dir(alpm_handle_t *handle) { size_t len = strlen(handle->dbpath) + 6; char *syncpath; struct stat buf; MALLOC(syncpath, len, RET_ERR(handle, ALPM_ERR_MEMORY, NULL)); sprintf(syncpath, "%s%s", handle->dbpath, "sync/"); if(stat(syncpath, &buf) != 0) { _alpm_log(handle, ALPM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n", syncpath); if(_alpm_makepath(syncpath) != 0) { free(syncpath); RET_ERR(handle, ALPM_ERR_SYSTEM, NULL); } } else if(!S_ISDIR(buf.st_mode)) { _alpm_log(handle, ALPM_LOG_WARNING, _("removing invalid file: %s\n"), syncpath); if(unlink(syncpath) != 0 || _alpm_makepath(syncpath) != 0) { free(syncpath); RET_ERR(handle, ALPM_ERR_SYSTEM, NULL); } } return syncpath; }
/** Lock the database */ int _alpm_handle_lock(alpm_handle_t *handle) { char *dir, *ptr; ASSERT(handle->lockfile != NULL, return -1); ASSERT(handle->lockfd < 0, return 0); /* create the dir of the lockfile first */ dir = strdup(handle->lockfile); ptr = strrchr(dir, '/'); if(ptr) { *ptr = '\0'; } if(_alpm_makepath(dir)) { FREE(dir); return -1; } FREE(dir); do { handle->lockfd = open(handle->lockfile, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0000); } while(handle->lockfd == -1 && errno == EINTR); return (handle->lockfd >= 0 ? 0 : -1); }
/** Lock the database */ int _alpm_handle_lock(alpm_handle_t *handle) { int fd; char *dir, *ptr; ASSERT(handle->lockfile != NULL, return -1); ASSERT(handle->lckstream == NULL, return 0); /* create the dir of the lockfile first */ dir = strdup(handle->lockfile); ptr = strrchr(dir, '/'); if(ptr) { *ptr = '\0'; } if(_alpm_makepath(dir)) { FREE(dir); return -1; } FREE(dir); do { fd = open(handle->lockfile, O_WRONLY | O_CREAT | O_EXCL, 0000); } while(fd == -1 && errno == EINTR); if(fd >= 0) { FILE *f = fdopen(fd, "w"); fprintf(f, "%ld\n", (long)getpid()); fflush(f); fsync(fd); handle->lckstream = f; return 0; } return -1; }
static int checkdbdir(alpm_db_t *db) { struct stat buf; const char *path = _alpm_db_path(db); if(stat(path, &buf) != 0) { _alpm_log(db->handle, ALPM_LOG_DEBUG, "database dir '%s' does not exist, creating it\n", path); if(_alpm_makepath(path) != 0) { RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1); } } else if(!S_ISDIR(buf.st_mode)) { _alpm_log(db->handle, ALPM_LOG_WARNING, _("removing invalid database: %s\n"), path); if(unlink(path) != 0 || _alpm_makepath(path) != 0) { RET_ERR(db->handle, ALPM_ERR_SYSTEM, -1); } } return 0; }
/** Check the alpm cachedirs for existance and find a writable one. * If no valid cache directory can be found, use /tmp. * @param handle the context handle * @return pointer to a writable cache directory. */ const char *_alpm_filecache_setup(alpm_handle_t *handle) { struct stat buf; alpm_list_t *i; char *cachedir; const char *tmpdir; /* Loop through the cache dirs until we find a usable directory */ for(i = handle->cachedirs; i; i = i->next) { cachedir = i->data; if(stat(cachedir, &buf) != 0) { /* cache directory does not exist.... try creating it */ _alpm_log(handle, ALPM_LOG_WARNING, _("no %s cache exists, creating...\n"), cachedir); if(_alpm_makepath(cachedir) == 0) { _alpm_log(handle, ALPM_LOG_DEBUG, "using cachedir: %s\n", cachedir); return cachedir; } } else if(!S_ISDIR(buf.st_mode)) { _alpm_log(handle, ALPM_LOG_DEBUG, "skipping cachedir, not a directory: %s\n", cachedir); } else if(_alpm_access(handle, NULL, cachedir, W_OK) != 0) { _alpm_log(handle, ALPM_LOG_DEBUG, "skipping cachedir, not writable: %s\n", cachedir); } else if(!(buf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))) { _alpm_log(handle, ALPM_LOG_DEBUG, "skipping cachedir, no write bits set: %s\n", cachedir); } else { _alpm_log(handle, ALPM_LOG_DEBUG, "using cachedir: %s\n", cachedir); return cachedir; } } /* we didn't find a valid cache directory. use TMPDIR or /tmp. */ if((tmpdir = getenv("TMPDIR")) && stat(tmpdir, &buf) && S_ISDIR(buf.st_mode)) { /* TMPDIR was good, we can use it */ } else { tmpdir = "/tmp"; } alpm_option_add_cachedir(handle, tmpdir); cachedir = handle->cachedirs->prev->data; _alpm_log(handle, ALPM_LOG_DEBUG, "using cachedir: %s\n", cachedir); _alpm_log(handle, ALPM_LOG_WARNING, _("couldn't find or create package cache, using %s instead\n"), cachedir); return cachedir; }