2016-10-05 21:33:37 +08:00
|
|
|
/*
|
|
|
|
FUSE: Filesystem in Userspace
|
|
|
|
Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
|
|
|
|
|
|
|
|
This program can be distributed under the terms of the GNU LGPLv2.
|
|
|
|
See the file COPYING.LIB
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fuse_i.h"
|
|
|
|
#include "fuse_misc.h"
|
2022-11-06 12:25:25 +08:00
|
|
|
#include "fuse_kernel.h"
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2016-10-05 21:33:37 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-11-06 12:25:25 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_chan
|
|
|
|
{
|
|
|
|
struct fuse_session *se;
|
|
|
|
int fd;
|
|
|
|
size_t bufsize;
|
2016-10-05 21:33:37 +08:00
|
|
|
};
|
|
|
|
|
2022-11-30 07:24:09 +08:00
|
|
|
struct fuse_session *
|
|
|
|
fuse_session_new(void *data,
|
|
|
|
void *receive_buf,
|
|
|
|
void *process_buf,
|
|
|
|
void *destroy)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_session *se = (struct fuse_session *) malloc(sizeof(*se));
|
|
|
|
if (se == NULL) {
|
|
|
|
fprintf(stderr, "fuse: failed to allocate session\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
memset(se, 0, sizeof(*se));
|
2022-11-30 07:24:09 +08:00
|
|
|
se->f = data;
|
2022-11-06 12:25:25 +08:00
|
|
|
se->receive_buf = receive_buf;
|
|
|
|
se->process_buf = process_buf;
|
|
|
|
se->destroy = destroy;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return se;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch)
|
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
assert(se->ch == NULL);
|
|
|
|
assert(ch->se == NULL);
|
|
|
|
se->ch = ch;
|
|
|
|
ch->se = se;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void fuse_session_remove_chan(struct fuse_chan *ch)
|
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_session *se = ch->se;
|
|
|
|
if (se) {
|
|
|
|
assert(se->ch == ch);
|
|
|
|
se->ch = NULL;
|
|
|
|
ch->se = NULL;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
void
|
|
|
|
fuse_session_destroy(struct fuse_session *se)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-30 07:24:09 +08:00
|
|
|
se->destroy(se->f);
|
2022-11-06 12:25:25 +08:00
|
|
|
if(se->ch != NULL)
|
|
|
|
fuse_chan_destroy(se->ch);
|
|
|
|
free(se);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
void fuse_session_reset(struct fuse_session *se)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
se->exited = 0;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
int
|
|
|
|
fuse_session_exited(struct fuse_session *se)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
return se->exited;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
void
|
|
|
|
fuse_session_exit(struct fuse_session *se_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
se_->exited = 1;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-30 07:24:09 +08:00
|
|
|
void*
|
|
|
|
fuse_session_data(struct fuse_session *se)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-30 07:24:09 +08:00
|
|
|
return se->f;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
struct fuse_chan *
|
|
|
|
fuse_chan_new(int fd,
|
|
|
|
size_t bufsize)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
struct fuse_chan *ch;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
ch = (struct fuse_chan*)malloc(sizeof(*ch));
|
|
|
|
if(ch == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "fuse: failed to allocate channel\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
memset(ch, 0, sizeof(*ch));
|
2022-11-06 12:25:25 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
ch->fd = fd;
|
|
|
|
ch->bufsize = bufsize;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return ch;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fuse_chan_fd(struct fuse_chan *ch)
|
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
return ch->fd;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fuse_chan_clearfd(struct fuse_chan *ch)
|
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int fd = ch->fd;
|
|
|
|
ch->fd = -1;
|
|
|
|
return fd;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t fuse_chan_bufsize(struct fuse_chan *ch)
|
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
return ch->bufsize;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct fuse_session *fuse_chan_session(struct fuse_chan *ch)
|
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
return ch->se;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
void
|
|
|
|
fuse_chan_destroy(struct fuse_chan *ch)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
int fd;
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
fuse_session_remove_chan(ch);
|
2022-11-06 12:25:25 +08:00
|
|
|
|
|
|
|
fd = fuse_chan_fd(ch);
|
|
|
|
if(fd != -1)
|
|
|
|
close(fd);
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
free(ch);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|