Skip to content
Extraits de code Groupes Projets

Database fuse

Fermé Kubat a demandé de fusionner database-fuse vers master
2 files
+ 207
3
Comparer les modifications
  • Côte à côte
  • En ligne

Fichiers

#include "sqlite3.h"
#define FUSE_USE_VERSION 31
#define _GNU_SOURCE
#include <fuse3/fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <assert.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <strings.h>
static volatile const char * database_path = NULL; /**<< @brief The path to the database. */
static volatile const char * database = NULL; /**<< @brief The database that contains all the karas. */
static void * lektor_init(struct fuse_conn_info * conn, struct fuse_config * cfg)
{
(void) conn;
(void) cfg;
if (sqlite3_open((const char *) database_path, (sqlite3 **) &database) != 0)
{ fprintf(stderr, "[LEKTOR_FUSE] error while opening database <%s>\n", database_path); }
return NULL;
}
static int lektor_getattr(const char * path, struct stat * stbuf, struct fuse_file_info * info)
{
(void) info;
int res = -ENOENT;
memset(stbuf, 0, sizeof(struct fuse_file_info));
size_t length = strlen(path);
if (length == 1 && strncmp(path, "/", 1) == 0)
{
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
return 0;
}
else if (strncmp(path, "/playlists", 10) == 0)
{
if (length == 10)
{
// The directory otself
stbuf->st_mode = S_IFDIR | 0777;
stbuf->st_nlink = 2;
return 0;
}
else if (strncmp(path + 10, "/README", 7) == 0)
{
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
return 0;
}
}
else if (strncmp(path, "/authors", 8) == 0)
{
if (length == 8)
{
// The directory itself
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
return 0;
}
else if (strncmp(path + 8, "/README", 7) == 0)
{
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
return 0;
}
}
else
{ res = -ENOENT; }
return res;
}
// static int lektor_mkdir(const char * path, mode_t mode)
// {
// }
//
//
// static int lektor_rmdir(const char * path)
// {
// }
static int lektor_read(const char * path, char * buf, size_t size, off_t offset, struct fuse_file_info * info)
{
(void) info;
(void) offset;
(void) size;
size_t length = strlen(path);
if (length > strlen("README") && strncmp(path + (length - 6), "README", 6) == 0)
{
memcpy(buf, "sample readme", strlen("sample readme"));
return 0;
}
else
{
return -ENOENT;
}
}
// static int lektor_open(const char * path, struct fuse_file_info * info)
// {
// }
static int lektor_readdir(const char * path,
void * buf,
fuse_fill_dir_t filler,
off_t offset,
struct fuse_file_info * fi,
enum fuse_readdir_flags flags)
{
(void) offset;
(void) fi;
(void) flags;
size_t length = strlen(path);
if (length == 0 || path[0] != '/')
{ return -ENOENT; }
filler(buf, ".", NULL, 0, 0);
filler(buf, "..", NULL, 0, 0);
path++; // Skip the first '/'
length--; // Skip the first '/'
struct stat simple_readonly =
{
.st_mode = S_IFREG | 0444,
.st_uid = 0,
.st_gid = 0,
};
if (length == 0)
{
/* We are in the root directory */
filler(buf, "playlists", NULL, 0, 0);
filler(buf, "authors", NULL, 0, 0);
}
else if (length == 7 && strncmp(path, "authors", 7) == 0)
{
filler(buf, "README", &simple_readonly, 0, 0);
}
else if (length == 9 && strncmp(path, "playlists", 9) == 0)
{
filler(buf, "README", &simple_readonly, 0, 0);
}
else
{ return -ENOENT; }
return 0;
}
static void lektor_deinit(void * ptr)
{
(void) ptr;
if (database == NULL)
{ return; }
sqlite3_close((sqlite3 *) database);
}
static struct fuse_operations lektor_oper =
{
.init = lektor_init,
.destroy = lektor_deinit,
.getattr = lektor_getattr,
// .open = lektor_open,
.read = lektor_read,
// .mkdir = lektor_mkdir,
// .rmdir = lektor_rmdir,
.readdir = lektor_readdir,
};
int main(int argc, char * argv[])
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
return fuse_main(args.argc, args.argv, &lektor_oper, NULL);
}
Chargement en cours