Skip to content

Commit

Permalink
Merge pull request #23 from MisterDA/updates
Browse files Browse the repository at this point in the history
Use namespaced C OCaml FFI functions (compat w/ OCaml 5), updates
  • Loading branch information
astrada committed Apr 27, 2023
2 parents 4cff750 + 383e9d2 commit 0e3b24c
Show file tree
Hide file tree
Showing 21 changed files with 462 additions and 357 deletions.
2 changes: 1 addition & 1 deletion .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version=0.14.2
version=0.25.1
profile=conventional
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ prerequisites:
- dune >= 1.6

available at

https://dune.build/

2) Installing OCamlFuse
Expand Down
18 changes: 11 additions & 7 deletions conf-libfuse.opam
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
maintainer: "https://github.com/ocaml/opam-repository/issues"
synopsis: "Virtual package relying on FUSE"
description:
"This package can only install if the fuse library is installed on the system."
maintainer: ["https://github.com/ocaml/opam-repository/issues"]
authors: ["Multiple"]
license: "LGPL-2.1-only"
homepage: "https://github.com/libfuse/libfuse"
bug-reports: "https://github.com/ocaml/opam-repository/issues"
authors: "Multiple"
license: "LGPL-2.1-only"
build: ["pkg-config" "--exists" "fuse"]
depends: [
"dune" {>= "3.7"}
"conf-pkg-config" {build}
"odoc" {with-doc}
]
dev-repo: "git+https://github.com/astrada/ocamlfuse.git"
build: ["pkg-config" "--exists" "fuse"]
depexts: [
["libfuse-dev"] {os-family = "debian"}
["fuse-dev"] {os-distribution = "alpine"}
Expand All @@ -18,7 +25,4 @@ depexts: [
["fuse2"] {os-family = "arch"}
["macfuse"] {os = "macos" & os-distribution = "homebrew"}
]
synopsis: "Virtual package relying on FUSE"
description:
"This package can only install if the fuse library is installed on the system."
flags: conf
12 changes: 12 additions & 0 deletions conf-libfuse.opam.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
build: ["pkg-config" "--exists" "fuse"]
depexts: [
["libfuse-dev"] {os-family = "debian"}
["fuse-dev"] {os-distribution = "alpine"}
["fuse-devel"] {os-distribution = "centos"}
["fuse-devel"] {os-distribution = "fedora"}
["fuse-devel"] {os-distribution = "ol"}
["fuse-devel"] {os-family = "suse"}
["fuse2"] {os-family = "arch"}
["macfuse"] {os = "macos" & os-distribution = "homebrew"}
]
flags: conf
44 changes: 43 additions & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1 +1,43 @@
(lang dune 1.6)
(lang dune 3.7)

(generate_opam_files true)

(source (github astrada/ocamlfuse))
(authors
"Vincenzo Ciancia"
"Olaf Hering <olaf@aepfle.de>"
"Alessandro Strada <alessandro.strada@gmail.com>")
(maintainers "Alessandro Strada <alessandro.strada@gmail.com>")
(license "GPL-1.0-or-later")

(package
(name conf-libfuse)
(synopsis "Virtual package relying on FUSE")
(description "This package can only install if the fuse library is installed on the system.")
(maintainers "https://github.com/ocaml/opam-repository/issues")
(authors "Multiple")
(homepage "https://github.com/libfuse/libfuse")
(bug_reports "https://github.com/ocaml/opam-repository/issues")
(license "LGPL-2.1-only")
(allow_empty)
(depends
("conf-pkg-config" :build)))

(package
(name ocamlfuse)
(synopsis "OCaml bindings for FUSE (Filesystem in UserSpacE)")
(description "\
This is a binding to FUSE for the OCaml programming language, enabling
you to write multithreaded filesystems in the OCaml language. It has
been designed with simplicity as a goal, as you can see by looking at
example/fusexmp.ml. Efficiency has also been a separate goal. The
Bigarray library is used for read and writes, allowing the library to
do zero-copy in OCaml land.")
(depends
(ocaml (>= 4.02.3))
base-bigarray
base-threads
base-unix
camlidl
dune-configurator
conf-libfuse))
110 changes: 59 additions & 51 deletions example/fusexmp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,29 @@ let retrieve_descr i = Unix_util.file_descr_of_int i

let xmp_read _path buf offset d =
let hnd = retrieve_descr d in
ignore (LargeFile.lseek hnd offset SEEK_SET) ;
ignore (LargeFile.lseek hnd offset SEEK_SET);
Unix_util.read hnd buf

let xmp_write _path buf offset d =
let hnd = retrieve_descr d in
ignore (LargeFile.lseek hnd offset SEEK_SET) ;
ignore (LargeFile.lseek hnd offset SEEK_SET);
Unix_util.write hnd buf

(* Extended attributes helpers *)

let xattr = Hashtbl.create 256

let xattr_lock = Mutex.create ()

let with_xattr_lock f x =
Mutex.lock xattr_lock ;
let v = try f x with e -> Mutex.unlock xattr_lock ; raise e in
Mutex.unlock xattr_lock ; v
Mutex.lock xattr_lock;
let v =
try f x
with e ->
Mutex.unlock xattr_lock;
raise e
in
Mutex.unlock xattr_lock;
v

let lskeys t = with_xattr_lock (Hashtbl.fold (fun k _v l -> k :: l) t) []

Expand All @@ -65,64 +70,67 @@ let init_attr xattr path =

let _ =
main Sys.argv
{ init= (fun () -> Printf.printf "filesystem started\n%!")
; statfs= Unix_util.statvfs
; getattr= Unix.LargeFile.lstat
; readdir=
(fun path _hnd -> "." :: ".." :: Array.to_list (Sys.readdir path))
; opendir=
{
init = (fun () -> Printf.printf "filesystem started\n%!");
statfs = Unix_util.statvfs;
getattr = Unix.LargeFile.lstat;
readdir =
(fun path _hnd -> "." :: ".." :: Array.to_list (Sys.readdir path));
opendir =
(fun path flags ->
Unix.close (Unix.openfile path flags 0) ;
None )
; releasedir= (fun _path _mode _hnd -> ())
; fsyncdir= (fun _path _ds _hnd -> Printf.printf "sync dir\n%!")
; readlink= Unix.readlink
; utime= Unix.utimes
; fopen= (fun path flags -> Some (store_descr (Unix.openfile path flags 0)))
; read= xmp_read
; write= xmp_write
; mknod= (fun path mode -> close (openfile path [O_CREAT; O_EXCL] mode))
; mkdir= Unix.mkdir
; unlink= Unix.unlink
; rmdir= Unix.rmdir
; symlink= Unix.symlink
; rename= Unix.rename
; link= Unix.link
; chmod= Unix.chmod
; chown= Unix.chown
; truncate= Unix.LargeFile.truncate
; release= (fun _path _mode hnd -> Unix.close (retrieve_descr hnd))
; flush= (fun _path _hnd -> ())
; fsync= (fun _path _ds _hnd -> Printf.printf "sync\n%!")
; listxattr=
Unix.close (Unix.openfile path flags 0);
None);
releasedir = (fun _path _mode _hnd -> ());
fsyncdir = (fun _path _ds _hnd -> Printf.printf "sync dir\n%!");
readlink = Unix.readlink;
utime = Unix.utimes;
fopen =
(fun path flags -> Some (store_descr (Unix.openfile path flags 0)));
read = xmp_read;
write = xmp_write;
mknod = (fun path mode -> close (openfile path [ O_CREAT; O_EXCL ] mode));
mkdir = Unix.mkdir;
unlink = Unix.unlink;
rmdir = Unix.rmdir;
symlink = Unix.symlink;
rename = Unix.rename;
link = Unix.link;
chmod = Unix.chmod;
chown = Unix.chown;
truncate = Unix.LargeFile.truncate;
release = (fun _path _mode hnd -> Unix.close (retrieve_descr hnd));
flush = (fun _path _hnd -> ());
fsync = (fun _path _ds _hnd -> Printf.printf "sync\n%!");
listxattr =
(fun path ->
init_attr xattr path ;
lskeys (Hashtbl.find xattr path) )
; getxattr=
init_attr xattr path;
lskeys (Hashtbl.find xattr path));
getxattr =
(fun path attr ->
with_xattr_lock
(fun () ->
init_attr xattr path ;
init_attr xattr path;
try Hashtbl.find (Hashtbl.find xattr path) attr
with Not_found ->
raise
(Unix.Unix_error
( EUNKNOWNERR 61 (* TODO: this is system-dependent *)
, "getxattr"
, path )) )
() )
; setxattr=
( EUNKNOWNERR 61 (* TODO: this is system-dependent *),
"getxattr",
path )))
());
setxattr =
(fun path attr value _flag ->
(* TODO: This currently ignores flags *)
with_xattr_lock
(fun () ->
init_attr xattr path ;
Hashtbl.replace (Hashtbl.find xattr path) attr value )
() )
; removexattr=
init_attr xattr path;
Hashtbl.replace (Hashtbl.find xattr path) attr value)
());
removexattr =
(fun path attr ->
with_xattr_lock
(fun () ->
init_attr xattr path ;
Hashtbl.remove (Hashtbl.find xattr path) attr )
() ) }
init_attr xattr path;
Hashtbl.remove (Hashtbl.find xattr path) attr)
());
}
4 changes: 1 addition & 3 deletions example/hello.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ open Bigarray
open Fuse

let default_stats = LargeFile.stat "."

let fname = "hello"

let name = "/" ^ fname

let contents : Fuse.buffer =
Expand Down Expand Up @@ -39,7 +37,7 @@ let do_read path buf ofs _ =
let ofs = Int64.to_int ofs in
let len = min (Array1.dim contents - ofs) (Array1.dim buf) in
Array1.blit (Array1.sub contents ofs len) (Array1.sub buf 0 len);
len )
len)
else raise (Unix_error (ENOENT, "read", path))

let _ =
Expand Down
6 changes: 3 additions & 3 deletions lib/Fuse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
OCamlFuse is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation (version 2 of the License).
OCamlFuse is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OCamlFuse. See the file LICENSE. If you haven't received
a copy of the GNU General Public License, write to:
Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
Expand Down
8 changes: 3 additions & 5 deletions lib/Fuse.mli
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
OCamlFuse is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation (version 2 of the License).
OCamlFuse is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OCamlFuse. See the file LICENSE. If you haven't received
a copy of the GNU General Public License, write to:
Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
Expand Down Expand Up @@ -75,7 +75,5 @@ type operations = {
}

val op_names_of_operations : operations -> Fuse_bindings.fuse_operation_names

val default_operations : operations

val main : Fuse_bindings.str array -> operations -> unit
13 changes: 6 additions & 7 deletions lib/Fuse_bindings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
OCamlFuse is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation (version 2 of the License).

OCamlFuse is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with OCamlFuse. See the file LICENSE. If you haven't received
a copy of the GNU General Public License, write to:

Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
Expand All @@ -24,7 +24,7 @@
vincenzo_ml@yahoo.it
*/
quote(h,"#define FUSE_USE_VERSION 26")
quote(h,"#include <fuse.h>")
quote(h,"#include <fuse.h>")


typedef [abstract] void * fuse;
Expand Down Expand Up @@ -62,13 +62,13 @@ struct fuse_operation_names {
[string,unique] char * setxattr;
[string,unique] char * getxattr;
[string,unique] char * listxattr;
[string,unique] char * removexattr;
[string,unique] char * removexattr;
};

struct __fuse_context { /* TODO: what's the meaning of the private_data field? */
[ptr] struct fuse *fuse;
unsigned int uid;
unsigned int gid;
unsigned int gid;
unsigned int pid;
};

Expand All @@ -87,4 +87,3 @@ void ml_fuse_init();
void ml_fuse_main(int argc,[size_is(argc)] str argv[],[ptr] const struct fuse_operations *op);

[blocking] boolean fuse_exited([ptr] struct fuse * f);

6 changes: 3 additions & 3 deletions lib/Fuse_lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
OCamlFuse is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation (version 2 of the License).
OCamlFuse is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OCamlFuse. See the file LICENSE. If you haven't received
a copy of the GNU General Public License, write to:
Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
Expand Down
Loading

0 comments on commit 0e3b24c

Please sign in to comment.