Exemple #1
0
/**
 * write() equivalent
 *
 * @param path the path of the file to be written
 * @param buf buffer holding write() data
 * @param size how many bytes should be written (size of *buf)
 * @param offset starting of the write
 * @param fi struct fuse_file_info used for open() flags
 * @return(0 on success, -errno otherwise)
 */
int tagsistant_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
    int res = 0, tagsistant_errno = 0, fh = 0;

	TAGSISTANT_START("WRITE on %s [size: %lu offset: %lu]", path, (unsigned long) size, (long unsigned int) offset);

	tagsistant_querytree *qtree = tagsistant_querytree_new(path, 0, 0, 1, 1);

	// -- malformed --
	if (QTREE_IS_MALFORMED(qtree))
		TAGSISTANT_ABORT_OPERATION(ENOENT);

	// -- alias --
	if (QTREE_IS_ALIAS(qtree) && qtree->alias) {
		res = size;

		gchar *_buf = g_strndup(buf, size);

		// end the string at the first carriage return or line feed character
		gchar *path_ptr = rindex(_buf, '\n');
		if (path_ptr) *path_ptr = '/';

		path_ptr = rindex(_buf, '\r');
		if (path_ptr) *path_ptr = '/';

		// remove double slashes
		GRegex *rx = g_regex_new("//", 0, 0, NULL);
		gchar *_buf2 = g_regex_replace(rx, _buf, -1, 0, "/", 0, NULL);
		g_regex_unref(rx);
		g_free(_buf);
		_buf = _buf2;

		// get the size of the buffer
		size_t max_size = MIN(size, TAGSISTANT_ALIAS_MAX_LENGTH - 1);
		size_t real_size = MIN(max_size, strlen(_buf));

		// copy the buffer to a temporary variable
		gchar *value = g_strndup(_buf, real_size);

		// save the buffer on disk
		tagsistant_sql_alias_set(qtree->dbi, qtree->alias, value);

		g_free(value);
		g_free(_buf);

	} else

	// -- object on disk --
	if (QTREE_POINTS_TO_OBJECT(qtree)) {
		if (!qtree->full_archive_path) {
			dbg('F', LOG_ERR, "Null qtree->full_archive_path");
			TAGSISTANT_ABORT_OPERATION(EFAULT);
		}

#if TAGSISTANT_ENABLE_FILE_HANDLE_CACHING
		if (fi->fh) {
			tagsistant_get_file_handle(fi, fh);
			res = pwrite(fh, buf, size, offset);
			tagsistant_errno = errno;
		}

		if ((-1 == res) || (0 == fh)) {
			if (fh) close(fh);
			fh = open(qtree->full_archive_path, fi->flags|O_WRONLY);
			if (fh)	res = pwrite(fh, buf, size, offset);
			else res = -1;
			tagsistant_errno = errno;
		}

		tagsistant_set_file_handle(fi, fh);
#else
		fh = open(qtree->full_archive_path, fi->flags|O_WRONLY);
		if (fh) {
			res = pwrite(fh, buf, size, offset);
			tagsistant_errno = errno;
			close(fh);
		} else {
			TAGSISTANT_ABORT_OPERATION(errno);
		}
#endif
	}

	// -- tags --
	// -- stats --
	// -- relations --
	else TAGSISTANT_ABORT_OPERATION(EROFS);

	// dbg('F', LOG_ERR, "Yeah!");

TAGSISTANT_EXIT_OPERATION:
	if ( res == -1 ) {
		TAGSISTANT_STOP_ERROR("WRITE %s (%s) (%s): %d %d: %s", path, qtree->full_archive_path, tagsistant_querytree_type(qtree), res, tagsistant_errno, strerror(tagsistant_errno));
		tagsistant_querytree_destroy(qtree, TAGSISTANT_ROLLBACK_TRANSACTION);
		return (-tagsistant_errno);
	} else {
		TAGSISTANT_STOP_OK("WRITE %s (%s): OK", path, tagsistant_querytree_type(qtree));
		tagsistant_querytree_destroy(qtree, TAGSISTANT_COMMIT_TRANSACTION);
		return (res);
	}
}
Exemple #2
0
/**
 * open() equivalent
 *
 * @param path the path to be open()ed
 * @param fi struct fuse_file_info holding open() flags
 * @return(0 on success, -errno otherwise)
 */
int tagsistant_open(const char *path, struct fuse_file_info *fi)
{
    int res = -1, tagsistant_errno = ENOENT;

	TAGSISTANT_START("OPEN on %s", path);

	// build querytree
	tagsistant_querytree *qtree = tagsistant_querytree_new(path, 0, 0, 1, 0);

	// -- malformed --
	if (QTREE_IS_MALFORMED(qtree))
		TAGSISTANT_ABORT_OPERATION(ENOENT);

	// -- error message --
	if (qtree->error_message && g_regex_match_simple("@/error$", path, G_REGEX_EXTENDED, 0)) {
		res = 1;
		tagsistant_errno = 0;
		goto TAGSISTANT_EXIT_OPERATION;
	}

	// -- object --
	else if (QTREE_POINTS_TO_OBJECT(qtree)) {
		if (tagsistant_is_tags_list_file(qtree)) {
			res = open(tagsistant.tags, fi->flags|O_RDONLY);
			tagsistant_errno = errno;
			goto TAGSISTANT_EXIT_OPERATION;
		}

		if (!qtree->full_archive_path) {
			dbg('F', LOG_ERR, "Null qtree->full_archive_path");
			TAGSISTANT_ABORT_OPERATION(EFAULT);
		}

		res = open(qtree->full_archive_path, fi->flags /*|O_RDONLY */);
		tagsistant_errno = errno;

		if (-1 != res) {
#if TAGSISTANT_ENABLE_FILE_HANDLE_CACHING
			tagsistant_set_file_handle(fi, res);
			dbg('F', LOG_INFO, "Caching %" PRIu64 " = open(%s)", fi->fh, path);
//			fprintf(stderr, "Opened FD %lu\n", fi->fh);

#else
			close(res);
#endif

			tagsistant_querytree_check_tagging_consistency(qtree);

			if (QTREE_IS_TAGGABLE(qtree)) {
				if ((fi->flags & O_WRONLY) || (fi->flags & O_RDWR)) {
					// invalidate the checksum
					dbg('2', LOG_INFO, "Invalidating checksum on %s", path);
					tagsistant_invalidate_object_checksum(qtree->inode, qtree->dbi);
				} else {
					fi->keep_cache = 1;
				}
			}
		} else {
			tagsistant_set_file_handle(fi, 0);
		}
	}

	// -- stats --
	else if (QTREE_IS_STATS(qtree)) {
		res = open(tagsistant.tags, fi->flags|O_RDONLY);
		tagsistant_set_file_handle(fi, res);
		tagsistant_errno = errno;
		fi->keep_cache = 0;
	}

	// -- alias --
	else if (QTREE_IS_ALIAS(qtree) && qtree->alias) {
		if (tagsistant_sql_alias_exists(qtree->dbi, qtree->alias)) {
			res = 0;
			tagsistant_errno = 0;
		} else {
			TAGSISTANT_ABORT_OPERATION(ENOENT);
		}
	}

	// -- tags --
	// -- relations --
	else TAGSISTANT_ABORT_OPERATION(EROFS);

TAGSISTANT_EXIT_OPERATION:
	if ( res == -1 ) {
		TAGSISTANT_STOP_ERROR("OPEN on %s (%s) (%s): %d %d: %s", path, qtree->full_archive_path, tagsistant_querytree_type(qtree), res, tagsistant_errno, strerror(tagsistant_errno));
		tagsistant_querytree_destroy(qtree, TAGSISTANT_ROLLBACK_TRANSACTION);
		return (-tagsistant_errno);
	} else {
		TAGSISTANT_STOP_OK("OPEN on %s (%s): OK", path, tagsistant_querytree_type(qtree));
		tagsistant_querytree_destroy(qtree, TAGSISTANT_COMMIT_TRANSACTION);
		return (0);
	}
}