16881f68fSopenharmony_ci/*
26881f68fSopenharmony_ci  FUSE: Filesystem in Userspace
36881f68fSopenharmony_ci  Copyright (C) 2017 Nikolaus Rath <Nikolaus@rath.org>
46881f68fSopenharmony_ci
56881f68fSopenharmony_ci  This program can be distributed under the terms of the GNU GPLv2.
66881f68fSopenharmony_ci  See the file COPYING.
76881f68fSopenharmony_ci*/
86881f68fSopenharmony_ci
96881f68fSopenharmony_ci/** @file
106881f68fSopenharmony_ci *
116881f68fSopenharmony_ci * minimal example filesystem that prints out all capabilities
126881f68fSopenharmony_ci * supported by the kernel and then exits.
136881f68fSopenharmony_ci *
146881f68fSopenharmony_ci * Compile with:
156881f68fSopenharmony_ci *
166881f68fSopenharmony_ci *     gcc -Wall printcap.c `pkg-config fuse3 --cflags --libs` -o printcap
176881f68fSopenharmony_ci *
186881f68fSopenharmony_ci * ## Source code ##
196881f68fSopenharmony_ci * \include printcap.c
206881f68fSopenharmony_ci */
216881f68fSopenharmony_ci
226881f68fSopenharmony_ci#define FUSE_USE_VERSION 31
236881f68fSopenharmony_ci
246881f68fSopenharmony_ci#include <fuse_lowlevel.h>
256881f68fSopenharmony_ci#include <stdio.h>
266881f68fSopenharmony_ci#include <unistd.h>
276881f68fSopenharmony_ci#include <string.h>
286881f68fSopenharmony_ci#include <stdlib.h>
296881f68fSopenharmony_ci
306881f68fSopenharmony_cistruct fuse_session *se;
316881f68fSopenharmony_ci
326881f68fSopenharmony_cistatic void pc_init(void *userdata,
336881f68fSopenharmony_ci		    struct fuse_conn_info *conn)
346881f68fSopenharmony_ci{
356881f68fSopenharmony_ci	(void) userdata;
366881f68fSopenharmony_ci
376881f68fSopenharmony_ci	printf("Protocol version: %d.%d\n", conn->proto_major,
386881f68fSopenharmony_ci	       conn->proto_minor);
396881f68fSopenharmony_ci	printf("Capabilities:\n");
406881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_ASYNC_READ)
416881f68fSopenharmony_ci			printf("\tFUSE_CAP_ASYNC_READ\n");
426881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_POSIX_LOCKS)
436881f68fSopenharmony_ci			printf("\tFUSE_CAP_POSIX_LOCKS\n");
446881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_ATOMIC_O_TRUNC)
456881f68fSopenharmony_ci			printf("\tFUSE_CAP_ATOMIC_O_TRUNC\n");
466881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_EXPORT_SUPPORT)
476881f68fSopenharmony_ci			printf("\tFUSE_CAP_EXPORT_SUPPORT\n");
486881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_DONT_MASK)
496881f68fSopenharmony_ci			printf("\tFUSE_CAP_DONT_MASK\n");
506881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_SPLICE_MOVE)
516881f68fSopenharmony_ci			printf("\tFUSE_CAP_SPLICE_MOVE\n");
526881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_SPLICE_READ)
536881f68fSopenharmony_ci			printf("\tFUSE_CAP_SPLICE_READ\n");
546881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_SPLICE_WRITE)
556881f68fSopenharmony_ci			printf("\tFUSE_CAP_SPLICE_WRITE\n");
566881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_FLOCK_LOCKS)
576881f68fSopenharmony_ci			printf("\tFUSE_CAP_FLOCK_LOCKS\n");
586881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_IOCTL_DIR)
596881f68fSopenharmony_ci			printf("\tFUSE_CAP_IOCTL_DIR\n");
606881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_AUTO_INVAL_DATA)
616881f68fSopenharmony_ci			printf("\tFUSE_CAP_AUTO_INVAL_DATA\n");
626881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_READDIRPLUS)
636881f68fSopenharmony_ci			printf("\tFUSE_CAP_READDIRPLUS\n");
646881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_READDIRPLUS_AUTO)
656881f68fSopenharmony_ci			printf("\tFUSE_CAP_READDIRPLUS_AUTO\n");
666881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_ASYNC_DIO)
676881f68fSopenharmony_ci			printf("\tFUSE_CAP_ASYNC_DIO\n");
686881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_WRITEBACK_CACHE)
696881f68fSopenharmony_ci			printf("\tFUSE_CAP_WRITEBACK_CACHE\n");
706881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_NO_OPEN_SUPPORT)
716881f68fSopenharmony_ci			printf("\tFUSE_CAP_NO_OPEN_SUPPORT\n");
726881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_PARALLEL_DIROPS)
736881f68fSopenharmony_ci			printf("\tFUSE_CAP_PARALLEL_DIROPS\n");
746881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_POSIX_ACL)
756881f68fSopenharmony_ci			printf("\tFUSE_CAP_POSIX_ACL\n");
766881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_CACHE_SYMLINKS)
776881f68fSopenharmony_ci			printf("\tFUSE_CAP_CACHE_SYMLINKS\n");
786881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_NO_OPENDIR_SUPPORT)
796881f68fSopenharmony_ci			printf("\tFUSE_CAP_NO_OPENDIR_SUPPORT\n");
806881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_EXPLICIT_INVAL_DATA)
816881f68fSopenharmony_ci			printf("\tFUSE_CAP_EXPLICIT_INVAL_DATA\n");
826881f68fSopenharmony_ci	if(conn->capable & FUSE_CAP_EXPIRE_ONLY)
836881f68fSopenharmony_ci			printf("\tFUSE_CAP_EXPIRE_ONLY\n");
846881f68fSopenharmony_ci	fuse_session_exit(se);
856881f68fSopenharmony_ci}
866881f68fSopenharmony_ci
876881f68fSopenharmony_ci
886881f68fSopenharmony_cistatic const struct fuse_lowlevel_ops pc_oper = {
896881f68fSopenharmony_ci	.init		= pc_init,
906881f68fSopenharmony_ci};
916881f68fSopenharmony_ci
926881f68fSopenharmony_ciint main(int argc, char **argv)
936881f68fSopenharmony_ci{
946881f68fSopenharmony_ci	struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
956881f68fSopenharmony_ci	char *mountpoint;
966881f68fSopenharmony_ci	int ret = -1;
976881f68fSopenharmony_ci
986881f68fSopenharmony_ci	mountpoint = strdup("/tmp/fuse_printcap_XXXXXX");
996881f68fSopenharmony_ci	if(mkdtemp(mountpoint) == NULL) {
1006881f68fSopenharmony_ci		perror("mkdtemp");
1016881f68fSopenharmony_ci		return 1;
1026881f68fSopenharmony_ci	}
1036881f68fSopenharmony_ci
1046881f68fSopenharmony_ci	printf("FUSE library version %s\n", fuse_pkgversion());
1056881f68fSopenharmony_ci	fuse_lowlevel_version();
1066881f68fSopenharmony_ci
1076881f68fSopenharmony_ci	se = fuse_session_new(&args, &pc_oper,
1086881f68fSopenharmony_ci			      sizeof(pc_oper), NULL);
1096881f68fSopenharmony_ci	if (se == NULL)
1106881f68fSopenharmony_ci	    goto err_out1;
1116881f68fSopenharmony_ci
1126881f68fSopenharmony_ci	if (fuse_set_signal_handlers(se) != 0)
1136881f68fSopenharmony_ci	    goto err_out2;
1146881f68fSopenharmony_ci
1156881f68fSopenharmony_ci	if (fuse_session_mount(se, mountpoint) != 0)
1166881f68fSopenharmony_ci	    goto err_out3;
1176881f68fSopenharmony_ci
1186881f68fSopenharmony_ci	ret = fuse_session_loop(se);
1196881f68fSopenharmony_ci
1206881f68fSopenharmony_ci	fuse_session_unmount(se);
1216881f68fSopenharmony_cierr_out3:
1226881f68fSopenharmony_ci	fuse_remove_signal_handlers(se);
1236881f68fSopenharmony_cierr_out2:
1246881f68fSopenharmony_ci	fuse_session_destroy(se);
1256881f68fSopenharmony_cierr_out1:
1266881f68fSopenharmony_ci	rmdir(mountpoint);
1276881f68fSopenharmony_ci	free(mountpoint);
1286881f68fSopenharmony_ci	fuse_opt_free_args(&args);
1296881f68fSopenharmony_ci
1306881f68fSopenharmony_ci	return ret ? 1 : 0;
1316881f68fSopenharmony_ci}
132