aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 071415ae13428e8809e27d14beb11dcddcbd8b24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main

import (
	"bazil.org/fuse"
	fusefs "bazil.org/fuse/fs"
	"flag"
	"fmt"
	"github.com/rfjakob/gocryptfs/frontend"
	"os"
	"path/filepath"
)

const (
	PROGRAM_NAME = "gocryptfs"
	USE_OPENSSL  = true

	ERREXIT_USAGE  = 1
	ERREXIT_NEWFS  = 2
	ERREXIT_MOUNT  = 3
	ERREXIT_SERVE  = 4
	ERREXIT_MOUNT2 = 5
)

func main() {
	// Parse command line arguments
	flag.Parse()
	if flag.NArg() < 2 {
		fmt.Printf("NArg=%d\n", flag.NArg())
		fmt.Printf("usage: %s CIPHERDIR MOUNTPOINT\n", PROGRAM_NAME)
		os.Exit(ERREXIT_USAGE)
	}

	cipherdir, _ := filepath.Abs(flag.Arg(0))
	mountpoint, err := filepath.Abs(flag.Arg(1))

	// Create the file system object
	var key [16]byte
	cfs, err := frontend.NewFS(key, cipherdir, USE_OPENSSL)
	if err != nil {
		fmt.Println(err)
		os.Exit(ERREXIT_NEWFS)
	}

	// Mount the file system
	mountOpts := []fuse.MountOption{
		fuse.FSName(PROGRAM_NAME),
		fuse.Subtype(PROGRAM_NAME),
		fuse.VolumeName(PROGRAM_NAME),
		fuse.LocalVolume(),
		fuse.MaxReadahead(1024 * 1024),
	}
	conn, err := fuse.Mount(mountpoint, mountOpts...)
	if err != nil {
		fmt.Println(err)
		os.Exit(ERREXIT_MOUNT)
	}
	defer conn.Close()

	// Start serving requests
	if err = fusefs.Serve(conn, cfs); err != nil {
		fmt.Println(err)
		os.Exit(ERREXIT_SERVE)
	}

	// Check for errors when mounting the file system
	<-conn.Ready
	if err = conn.MountError; err != nil {
		fmt.Println(err)
		os.Exit(ERREXIT_MOUNT2)
	}

	// We are done
	os.Exit(0)
}