Skip to content

Commit

Permalink
add RO filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
vgough committed Feb 26, 2018
1 parent 9409d9f commit cc289b5
Show file tree
Hide file tree
Showing 277 changed files with 65,758 additions and 0 deletions.
100 changes: 100 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/hanwen/go-fuse"
version = "20170619.0.0"

[prune]
go-tests = true
unused-packages = true
67 changes: 67 additions & 0 deletions cmd/ghvfs/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"os"
"time"

"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
kingpin "gopkg.in/alecthomas/kingpin.v2"

"github.com/vgough/ghvfs"
)

const (
appVersion = "0.1"
)

var (
app = kingpin.New("ghvfs", "GitHub Virtual FileSystem").
Version(appVersion).
Author("Valient Gough <[email protected]>").
DefaultEnvars()

debug = app.Flag("debug", "Enable debug mode.").Bool()
mountPoint = app.Arg("mountpoint", "Where to mount filesystem.").Required().ExistingDir()
token = app.Flag("token", "GitHub Auth Token.").Envar("GITHUB_TOKEN").String()
github = app.Flag("github", "GitHub URL, for private GHE.").String()
cacheSize = app.Flag("cache", "Size of attribute cache.").Int()
)

func main() {
kingpin.MustParse(app.Parse(os.Args[1:]))

if *debug {
ghvfs.Debug.SetOutput(os.Stderr)
}

opts := []ghvfs.Opt{}
if *token != "" {
opts = append(opts, ghvfs.WithToken(*token))
}
if *github != "" {
opts = append(opts, ghvfs.WithGHEndpoint(*github))
}
if *cacheSize > 0 {
opts = append(opts, ghvfs.WithCacheSize(*cacheSize))
}

fs := ghvfs.NewFS(opts...)
pathFs := pathfs.NewPathNodeFs(fs, nil)
fsOpts := nodefs.NewOptions()
fsOpts.AttrTimeout = 60 * time.Second
fsOpts.EntryTimeout = 60 * time.Second
conn := nodefs.NewFileSystemConnector(pathFs.Root(), fsOpts)
server, err := fuse.NewServer(conn.RawFS(), *mountPoint, &fuse.MountOptions{
Name: "githubfs",
Debug: *debug,
RememberInodes: true,
})
if err != nil {
ghvfs.Error.Printf("Mount fail: %v\n", err)
os.Exit(1)
}
ghvfs.Debug.Println("Mounted!")
server.Serve()
}
56 changes: 56 additions & 0 deletions files.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ghvfs

import (
"github.com/google/go-github/github"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"golang.org/x/net/context"
)

type fnode struct {
nodefs.File
fs *gfs
org, repo, ref, path string
content []byte
}

func newContentNode(fs *gfs, flags uint32, org, repo, ref, path string) (nodefs.File, fuse.Status) {
return &fnode{
File: nodefs.NewReadOnlyFile(nodefs.NewDefaultFile()),
fs: fs,
org: org,
repo: repo,
ref: ref,
path: path,
}, fuse.OK
}

func (f *fnode) Read(buf []byte, off int64) (fuse.ReadResult, fuse.Status) {
if f.content == nil {
Debug.Printf("loading content for %q", f.path)
ctx := context.Background()
opt := github.RepositoryContentGetOptions{
Ref: f.ref,
}
fc, _, _, err := f.fs.client.Repositories.GetContents(ctx, f.org, f.repo, f.path, &opt)
if err != nil {
return nil, fuse.ToStatus(err)
}
content, err := fc.GetContent()
if err != nil {
return nil, fuse.ToStatus(err)
}
f.content = []byte(content)
}

start := off
end := off + int64(len(buf))
if start > int64(len(f.content)) {
start = 0
end = 0
}
if end > int64(len(f.content)) {
end = int64(len(f.content))
}
return fuse.ReadResultData(f.content[start:end]), fuse.OK
}
Loading

0 comments on commit cc289b5

Please sign in to comment.