Example #1
0
int
open_stobj(char *StObjFileName)
{
    int			fd;
    struct stat		sb;
    off_t		size;
    char		*mapaddr;
    invt_sescounter_t	cnt;
    char		*name;

    errno=0;
    fd = open_and_lock( StObjFileName, FILE_WRITE, wait_for_locks );
    if (fd < 0) {
	return fd;
    }

    name = strdup(StObjFileName);
    if(name == NULL) {
	fprintf(stderr, "%s: internal memory error: strdup stobj_name\n", g_programName);
	exit(1);
    }

    read_n_bytes(fd, &cnt, sizeof(invt_sescounter_t), StObjFileName);
    lseek( fd, 0, SEEK_SET );
    errno = 0;
    if (fstat(fd, &sb) < 0) {
	fprintf(stderr, "Could not get stat info on %s\n", StObjFileName);
	perror("fstat");
	return -1;
    }
    size = sb.st_size;
    mapaddr = mmap_n_bytes(fd, size, BOOL_FALSE, StObjFileName);

    return add_stobj(name, fd, size, mapaddr, (invt_sescounter_t *)mapaddr);
}
static int trav_priv_init(trav_priv_t *ptrav, const char *fname)
{
	FILE *fp = open_and_lock(fname);
	if(!fp) {
		nt_error("open and lock file: %s failed.\n", fname);
		exit(-1);
	}
	ptrav->fp = fp;
	return 0;
}
Example #3
0
/*
 * To prevent after board packed, and others did not update their list.
 * The ent could be wrong, and the user might update the wrong .DIR entry.
 */
int safely_read_dir(const char *direct, int opened_fd, int ent,
		const FILEHEADER *ofhr, FILEHEADER *nfhr)
{
	int fd, rtval = -1;

	if (opened_fd)
		fd = opened_fd;
	else
		fd = open_and_lock(direct);
	if (fd == -1)
		goto err_out;

	if (get_record_byfd(fd, nfhr, FH_SIZE, ent) == -1)
		goto out;

	if (nfhr->postno == ofhr->postno && !strcmp(nfhr->title, ofhr->title)) {
		/*
		 * The ent position should be correct,
		 * if postno and the title is the same.
		 */
		if (!(ofhr->accessed & FILE_DELE) &&
		    nfhr->accessed & FILE_DELE)
			goto out;
		rtval = 0;
		goto out;
	}

	/*
	 * Search for correct record if still there.
	 * This could spend some CPU/DISK time.
	 */
	/*
	 * FIXME: Just returning error for now.
	 */

out:
	if (!opened_fd)
		unlock_and_close(fd);
err_out:
	return rtval;
}
Example #4
0
/*
 * To prevent after board packed, and others did not update their list.
 * The ent could be wrong, and the user might update the wrong .DIR entry.
 */
int safely_substitute_dir(const char *direct, int opened_fd, int ent,
		const FILEHEADER *ofhr, FILEHEADER *nfhr,
		unsigned char mark_unread)
{
	int fd, rtval = -1;
	FILEHEADER tfhr;

	if (opened_fd)
		fd = opened_fd;
	else
		fd = open_and_lock(direct);
	if (fd == -1)
		goto err_out;

	if (safely_read_dir(NULL, fd, ent, ofhr, &tfhr))
		goto out;

	if (mark_unread) {
		nfhr->mtime = time(NULL);
		get_only_postno(direct, fd, nfhr);
	}
	if (substitute_record_byfd(fd, nfhr, FH_SIZE, ent))
		goto out;
	rtval = 0;

	/*
	 * Search for correct record if still there.
	 * This could spend some CPU/DISK time.
	 */
	/*
	 * FIXME: Just returning error for now.
	 */

out:
	if (!opened_fd)
		unlock_and_close(fd);
err_out:
	return rtval;
}
Example #5
0
int main(int argc, char *argv[])
{
	int n, fd;
	bool gotone = false;
	char *me, *buf = alloca(400);
	char *nicname = alloca(40);

	if ((me = get_username(&buf)) == NULL) {
		fprintf(stderr, "Failed to get username\n");
		exit(1);
	}

	if (argc != 4)
		usage(argv[0], true);

	if (!create_db_dir(DB_FILE)) {
		fprintf(stderr, "Failed to create directory for db file\n");
		exit(1);
	}

	if ((fd = open_and_lock(DB_FILE)) < 0) {
		fprintf(stderr, "Failed to lock %s\n", DB_FILE);
		exit(1);
	}

	n = get_alloted(me, argv[2], argv[3]);
	if (n > 0)
		gotone = get_nic_if_avail(fd, me, argv[1], argv[2], argv[3], n, &nicname);
	close(fd);
	if (!gotone) {
		fprintf(stderr, "Quota reached\n");
		exit(1);
	}

	// Now create the link

	exit(0);
}