Пример #1
0
/*
 * Calculate the pfs_id given a path to a directory or a @@PFS or @@%llx:%d
 * softlink.
 */
int
getpfs(struct hammer_ioc_pseudofs_rw *pfs, char *path)
{
	int fd;
	char *p;

	bzero(pfs, sizeof(*pfs));
	pfs->ondisk = malloc(sizeof(*pfs->ondisk));
	bzero(pfs->ondisk, sizeof(*pfs->ondisk));
	pfs->bytes = sizeof(*pfs->ondisk);

	/*
	 * Trailing '/' must be removed so that upon pfs-destroy
	 * the symlink can be deleted without problems.
	 * Root directory (/) must be excluded from this.
	 */
	p = path + (int)strlen(path) - 1;
	assert(p >= path);
	while (p != path && *p == '/')
		*p-- = 0;

	fd = scanpfsid(pfs, path);
	if (fd < 0) {
		/*
		 * Once it comes here the hammer command may fail even if
		 * this function returns valid file descriptor.
		 */
		fd = open(path, O_RDONLY);
		if (fd >= 0) {
			pfs->pfs_id = -1;
			ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs);
			if (pfs->pfs_id == -1) {
				close(fd);
				fd = -1;
			}
		}
	}

	if (fd < 0) {
		fprintf(stderr, "Cannot access PFS %s: %s\n",
			path, strerror(errno));
		exit(1);
	}

	/*
	 * pfs.pfs_id should have been set to non -1.  In this case fd
	 * could be any fd of HAMMER inodes since HAMMERIOC_GET_PSEUDOFS
	 * doesn't depend on inode attributes if it's set to a valid id.
	 */
	if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
		fprintf(stderr, "Cannot access PFS %s: %s\n",
			path, strerror(errno));
		exit(1);
	}
	return(fd);
}
Пример #2
0
/*
 * Calculate the PFS id given a path to a file/directory or
 * a @@%llx:%d softlink.
 */
int
getpfs(struct hammer_ioc_pseudofs_rw *pfs, const char *path)
{
	int fd;

	clrpfs(pfs, NULL, -1);

	/*
	 * Extract the PFS id.
	 * dirname(path) is supposed to be a directory in PFS#0.
	 */
	if (scanpfsid(pfs, path) == 0) {
		path = dirname(path); /* strips trailing / first if any */
	}

	/*
	 * Open the path regardless of scanpfsid() result, since some
	 * commands can take a regular file/directory (e.g. pfs-status).
	 */
	fd = open(path, O_RDONLY);
	if (fd < 0) {
		fprintf(stderr, "Failed to open %s\n", path);
		exit(1);
	}

	/*
	 * If pfs.pfs_id has been set to non -1, the file descriptor fd
	 * could be any fd of HAMMER inodes since HAMMERIOC_GET_PSEUDOFS
	 * doesn't depend on inode attributes if it's set to a valid id.
	 */
	if (ioctl(fd, HAMMERIOC_GET_PSEUDOFS, pfs) < 0) {
		fprintf(stderr, "Cannot access %s: %s\n",
			path, strerror(errno));
		exit(1);
	}
	return(fd);
}