Esempio n. 1
0
/* check that our directory exists and try to create it if it doesn't
   return -1 on failure, 0 otherwise.
 */
int check_directory(const char* path) /*@globals errno,stderr@*/
{
    int rc;
    struct stat sb;

    if (path == NULL || *path == '\0') {
	return -1;
    }

    rc = stat(path, &sb);
    if (rc < 0) {
	if (ENOENT==errno) {
	    if (bf_mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR)) {
		fprintf(stderr, "Error creating directory '%s': %s\n",
			path, strerror(errno));
		return -1;
	    } else if (verbose > 0) {
		fprintf(dbgout, "Created directory %s .\n", path);
	    }
	    return 0;
	} else {
	    fprintf(stderr, "Error accessing directory '%s': %s\n",
		    path, strerror(errno));
	    return -1;
	}
    } else {
	if (! S_ISDIR(sb.st_mode)) {
	    fprintf(stderr, "Error: %s is not a directory.\n", path);
	}
    }
    return 0;
}
static int db_try_glock(bfpath *bfp, short locktype, int lockcmd)
{
    int ret;
    char *t;

    /* lock */
    ret = bf_mkdir(bfp->dirname, DIR_MODE);
    if (ret && errno != EEXIST) {
	print_error(__FILE__, __LINE__, "mkdir(%s): %s",
		bfp->dirname, strerror(errno));
	exit(EX_ERROR);
    }

    t = mxcat(bfp->dirname, DIRSEP_S, "lockfile-d", NULL);

    /* All we are interested in is that this file exists, we'll close it
     * right away as plock down will open it again */
    ret = open(t, O_RDWR|O_CREAT|O_EXCL, DS_MODE);
    if (ret < 0 && errno != EEXIST) {
	print_error(__FILE__, __LINE__, "open(%s): %s",
		t, strerror(errno));
	exit(EX_ERROR);
    }

    if (ret >= 0)
	close(ret);

    lockfd = plock(t, locktype, lockcmd);
    if (lockfd < 0 && errno != EAGAIN && errno != EACCES) {
	print_error(__FILE__, __LINE__, "lock(%s): %s",
		t, strerror(errno));
	exit(EX_ERROR);
    }

    xfree(t);
    /* lock set up */

    return lockfd;
}