1/* $NetBSD: v26.c,v 1.1 2022/01/22 08:09:40 pho Exp $ */
2
3/*
4 * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote
16 *    products derived from this software without specific prior written
17 *    permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#if !defined(lint)
34__RCSID("$NetBSD: v26.c,v 1.1 2022/01/22 08:09:40 pho Exp $");
35#endif /* !lint */
36
37#include <err.h>
38#include <fuse_internal.h>
39#include <stdlib.h>
40#include <string.h>
41
42struct fuse_chan *
43fuse_mount_v26(const char *mountpoint, struct fuse_args *args) {
44    int nominal_fd;
45
46    /* fuse_mount() in FUSE 2.6 returns a fuse_chan instead of fd. We
47     * still need to store the channel in the global list, because
48     * users may call fuse_destroy() before fuse_unmount().
49     */
50    nominal_fd = fuse_mount_v25(mountpoint, args);
51    if (nominal_fd == -1)
52        return NULL;
53
54    return fuse_chan_peek(nominal_fd);
55}
56
57static bool
58is_same_channel(struct fuse_chan* chan, void* priv) {
59    return chan == (struct fuse_chan*)priv;
60}
61
62void
63fuse_unmount_v26(const char *mountpoint, struct fuse_chan *ch) {
64    /* Although the API documentation doesn't say so, fuse_unmount()
65     * from FUSE >= 2.6 < 3.0 in fact allows "ch" to be NULL. */
66    if (ch)
67        if (strcmp(mountpoint, fuse_chan_mountpoint(ch)) != 0)
68            warnx("%s: mountpoint `%s' differs from that was passed to fuse_mount(): %s",
69                  __func__, mountpoint, fuse_chan_mountpoint(ch));
70
71    /* Ask fuse_unmount_v11() to find the channel object that is
72     * already in our hand. We are going to need to know its index in
73     * the global list anyway. */
74    fuse_unmount_v11(mountpoint);
75}
76
77struct fuse *
78fuse_new_v26(struct fuse_chan *ch, struct fuse_args *args,
79             const void *op, int op_version,
80             void *user_data) {
81    int idx;
82
83    /* Although the fuse_chan object is already in our hand, we need
84     * to know its index in the global list because that's what
85     * fuse_new_v25() wants. */
86    if (fuse_chan_find(is_same_channel, &idx, ch) == NULL)
87        errx(EXIT_FAILURE, "%s: cannot find the channel index", __func__);
88
89    return fuse_new_v25(idx, args, op, op_version, user_data);
90}
91
92struct fuse *
93fuse_setup_v26(int argc, char *argv[],
94               const void *op, int op_version,
95               char **mountpoint, int *multithreaded,
96               void *user_data) {
97    struct fuse* fuse;
98    struct fuse_cmdline_opts opts;
99
100    fuse = __fuse_setup(argc, argv, op, op_version, user_data, &opts);
101    if (fuse == NULL)
102        return NULL;
103
104    *mountpoint = opts.mountpoint; /* Transfer the ownership of the string. */
105    *multithreaded = !opts.singlethread;
106    return fuse;
107}
108
109void
110fuse_teardown_v26(struct fuse *fuse,
111                  char *mountpoint __attribute__((__unused__))) {
112    __fuse_teardown(fuse);
113}
114