- /* Enuser dbpath is there, create if necessary */
- struct stat st;
- if (stat(vol->v_dbpath, &st) != 0) {
- if (errno != ENOENT) {
- dbd_log( LOGSTD, "Can't stat dbpath \"%s\": %s", vol->v_dbpath, strerror(errno));
- exit(EXIT_FAILURE);
- }
- if ((mkdir(vol->v_dbpath, 0755)) != 0) {
- dbd_log( LOGSTD, "Can't create dbpath \"%s\": %s", vol->v_dbpath, strerror(errno));
- exit(EXIT_FAILURE);
- }
- }
-
- /* Put "/.AppleDB" at end of volpath, get path from volinfo file */
- if ( (strlen(vol->v_dbpath) + strlen("/.AppleDB")) > MAXPATHLEN ) {
- dbd_log( LOGSTD, "Volume pathname too long");
- exit(EXIT_FAILURE);
- }
- strncpy(dbpath, vol->v_dbpath, MAXPATHLEN - strlen("/.AppleDB"));
- strcat(dbpath, "/.AppleDB");
-
- /* Check or create dbpath */
- int dbdirfd = open(dbpath, O_RDONLY);
- if (dbdirfd == -1 && errno == ENOENT) {
- if (errno == ENOENT) {
- if ((mkdir(dbpath, 0755)) != 0) {
- dbd_log( LOGSTD, "Can't create .AppleDB for \"%s\": %s", dbpath, strerror(errno));
- exit(EXIT_FAILURE);
- }
- } else {
- dbd_log( LOGSTD, "Somethings wrong with .AppleDB for \"%s\", giving up: %s", dbpath, strerror(errno));
- exit(EXIT_FAILURE);
- }
- } else {
- close(dbdirfd);
- }
-
- /* Get db lock */
- if ((db_locked = get_lock(LOCK_EXCL, dbpath)) == -1)
- goto exit_noenv;
- if (db_locked != LOCK_EXCL) {
- /* Couldn't get exclusive lock, try shared lock if -e wasn't requested */
- if (exclusive) {
- dbd_log(LOGSTD, "Database is in use and exlusive was requested");
- goto exit_noenv;
- }
- if ((db_locked = get_lock(LOCK_SHRD, NULL)) != LOCK_SHRD)
- goto exit_noenv;
- }
-
- /* Check if -f is requested and wipe db if yes */
- if ((flags & DBD_FLAGS_FORCE) && rebuild) {
- char cmd[8 + MAXPATHLEN];
- if ((db_locked = get_lock(LOCK_FREE, NULL)) != 0)
- goto exit_noenv;
-
- snprintf(cmd, 8 + MAXPATHLEN, "rm -rf \"%s\"", dbpath);
- dbd_log( LOGDEBUG, "Removing old database of volume: '%s'", volpath);
- system(cmd);
- if ((mkdir(dbpath, 0755)) != 0) {
- dbd_log( LOGSTD, "Can't create dbpath \"%s\": %s", dbpath, strerror(errno));
- exit(EXIT_FAILURE);