{"action":"created","comment":{"url":"https://api.github.com/repos/shurcooL/go/pulls/comments/33225568","id":33225568,"diff_hunk":"@@ -0,0 +1,128 @@\n+// Package union offers a simple http.FileSystem that can unify multiple filesystems at various mount points.\n+package union\n+\n+import (\n+\t\"fmt\"\n+\t\"io\"\n+\t\"log\"\n+\t\"net/http\"\n+\t\"os\"\n+\t\"strings\"\n+\t\"time\"\n+\n+\t\"github.com/shurcooL/go/vfs/httpfs/vfsutil\"\n+)\n+\n+// New creates an union filesystem with the provided mapping of mount points to filesystems.\n+//\n+// Each mount point must be of form \"/mydir\". It must start with a '/', and contain a single directory name.\n+func New(mapping map[string]http.FileSystem) http.FileSystem {\n+\tu := &unionFS{\n+\t\tns: make(map[string]http.FileSystem),\n+\t\troot: &dirInfo{\n+\t\t\tname:    \"/\",\n+\t\t\tentries: []os.FileInfo{}, // Not nil.\n+\t\t\tmodTime: time.Time{},     // An empty union filesystem has never been modified.\n+\t\t},\n+\t}\n+\tfor mountPoint, fs := range mapping {\n+\t\tu.bind(mountPoint, fs)\n+\t}\n+\treturn u\n+}\n+\n+type unionFS struct {\n+\tns   map[string]http.FileSystem // Key is mount point, e.g., \"/mydir\".\n+\troot *dirInfo\n+}\n+\n+// bind mounts fs at mountPoint.\n+// mountPoint must be of form \"/mydir\". It must start with a '/', and contain a single directory name.\n+func (u *unionFS) bind(mountPoint string, fs http.FileSystem) {\n+\tfi, err := vfsutil.Stat(fs, \"/\")\n+\tif err != nil {\n+\t\tlog.Fatalln(\"can't stat root directory of provided filesystem:\", err)\n+\t}\n+\tmodTime := fi.ModTime()\n+\n+\tu.ns[mountPoint] = fs\n+\tu.root.entries = append(u.root.entries, &dirInfo{\n+\t\tname:    mountPoint[1:],\n+\t\tentries: nil, // entries is nil, not needed because this acts as a os.FileInfo only, not an openable dir.\n+\t\tmodTime: modTime,\n+\t})\n+\n+\tif modTime.After(u.root.modTime) {\n+\t\tu.root.modTime = modTime\n+\t}\n+}\n+\n+// Open opens the named file.\n+func (u *unionFS) Open(path string) (http.File, error) {\n+\t// TODO: Maybe clean path?\n+\tif path == \"/\" {\n+\t\treturn &dir{\n+\t\t\tdirInfo: u.root,\n+\t\t}, nil\n+\t}\n+\tfor prefix, fs := range u.ns {\n+\t\tif path == prefix || strings.HasPrefix(path, prefix+\"/\") {\n+\t\t\tinnerPath := path[len(prefix):]\n+\t\t\tif innerPath == \"\" {\n+\t\t\t\tinnerPath = \"/\"\n+\t\t\t}\n+\t\t\treturn fs.Open(innerPath)\n+\t\t}\n+\t}\n+\treturn nil, &os.PathError{Op: \"open\", Path: path, Err: os.ErrNotExist}\n+}\n+\n+// dirInfo is a static definition of a directory.\n+type dirInfo struct {\n+\tname    string\n+\tentries []os.FileInfo // Not nil.\n+\tmodTime time.Time\n+}\n+\n+func (d *dirInfo) Read([]byte) (int, error) {\n+\treturn 0, fmt.Errorf(\"cannot Read from directory %s\", d.name)\n+}\n+func (d *dirInfo) Close() error               { return nil }\n+func (d *dirInfo) Stat() (os.FileInfo, error) { return d, nil }\n+\n+func (d *dirInfo) Name() string       { return d.name }\n+func (d *dirInfo) Size() int64        { return 0 }\n+func (d *dirInfo) Mode() os.FileMode  { return 0755 | os.ModeDir }\n+func (d *dirInfo) ModTime() time.Time { return d.modTime }\n+func (d *dirInfo) IsDir() bool        { return true }\n+func (d *dirInfo) Sys() interface{}   { return nil }\n+\n+// dir is an opened dir instance.\n+type dir struct {\n+\t*dirInfo\n+\tpending []os.FileInfo\n+}\n+\n+func (d *dir) Seek(offset int64, whence int) (int64, error) {\n+\tif offset == 0 && whence == os.SEEK_SET {\n+\t\td.pending = nil","path":"vfs/httpfs/union/union.go","position":108,"original_position":108,"commit_id":"4c35f224bedcadc75ac41521d6022af6fda170b1","original_commit_id":"4c35f224bedcadc75ac41521d6022af6fda170b1","user":{"login":"ardan-bkennedy","id":2280005,"avatar_url":"https://avatars.githubusercontent.com/u/2280005?v=3","gravatar_id":"","url":"https://api.github.com/users/ardan-bkennedy","html_url":"https://github.com/ardan-bkennedy","followers_url":"https://api.github.com/users/ardan-bkennedy/followers","following_url":"https://api.github.com/users/ardan-bkennedy/following{/other_user}","gists_url":"https://api.github.com/users/ardan-bkennedy/gists{/gist_id}","starred_url":"https://api.github.com/users/ardan-bkennedy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ardan-bkennedy/subscriptions","organizations_url":"https://api.github.com/users/ardan-bkennedy/orgs","repos_url":"https://api.github.com/users/ardan-bkennedy/repos","events_url":"https://api.github.com/users/ardan-bkennedy/events{/privacy}","received_events_url":"https://api.github.com/users/ardan-bkennedy/received_events","type":"User","site_admin":false},"body":"Can this be called by multiple goroutines at the same time.","created_at":"2015-06-25T06:26:40Z","updated_at":"2015-06-25T06:26:40Z","html_url":"https://github.com/shurcooL/go/pull/21#discussion_r33225568","pull_request_url":"https://api.github.com/repos/shurcooL/go/pulls/21","_links":{"self":{"href":"https://api.github.com/repos/shurcooL/go/pulls/comments/33225568"},"html":{"href":"https://github.com/shurcooL/go/pull/21#discussion_r33225568"},"pull_request":{"href":"https://api.github.com/repos/shurcooL/go/pulls/21"}}},"pull_request":{"url":"https://api.github.com/repos/shurcooL/go/pulls/21","id":38512598,"html_url":"https://github.com/shurcooL/go/pull/21","diff_url":"https://github.com/shurcooL/go/pull/21.diff","patch_url":"https://github.com/shurcooL/go/pull/21.patch","issue_url":"https://api.github.com/repos/shurcooL/go/issues/21","number":21,"state":"open","locked":false,"title":"Add union filesystem that can mount multiple filesystems.","user":{"login":"shurcooL","id":1924134,"avatar_url":"https://avatars.githubusercontent.com/u/1924134?v=3","gravatar_id":"","url":"https://api.github.com/users/shurcooL","html_url":"https://github.com/shurcooL","followers_url":"https://api.github.com/users/shurcooL/followers","following_url":"https://api.github.com/users/shurcooL/following{/other_user}","gists_url":"https://api.github.com/users/shurcooL/gists{/gist_id}","starred_url":"https://api.github.com/users/shurcooL/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shurcooL/subscriptions","organizations_url":"https://api.github.com/users/shurcooL/orgs","repos_url":"https://api.github.com/users/shurcooL/repos","events_url":"https://api.github.com/users/shurcooL/events{/privacy}","received_events_url":"https://api.github.com/users/shurcooL/received_events","type":"User","site_admin":false},"body":"It's a very simple package that uses `http.FileSystem` interface. It can only mount filesystems at initialization, and each mounted filesystem is mounted onto a single unique directory in root.","created_at":"2015-06-25T05:12:47Z","updated_at":"2015-06-25T06:26:40Z","closed_at":null,"merged_at":null,"merge_commit_sha":"21f14c5b9b230163eb7b85aa9c1bec723c5fcfa8","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/shurcooL/go/pulls/21/commits","review_comments_url":"https://api.github.com/repos/shurcooL/go/pulls/21/comments","review_comment_url":"https://api.github.com/repos/shurcooL/go/pulls/comments{/number}","comments_url":"https://api.github.com/repos/shurcooL/go/issues/21/comments","statuses_url":"https://api.github.com/repos/shurcooL/go/statuses/4c35f224bedcadc75ac41521d6022af6fda170b1","head":{"label":"shurcooL:unionFS","ref":"unionFS","sha":"4c35f224bedcadc75ac41521d6022af6fda170b1","user":{"login":"shurcooL","id":1924134,"avatar_url":"https://avatars.githubusercontent.com/u/1924134?v=3","gravatar_id":"","url":"https://api.github.com/users/shurcooL","html_url":"https://github.com/shurcooL","followers_url":"https://api.github.com/users/shurcooL/followers","following_url":"https://api.github.com/users/shurcooL/following{/other_user}","gists_url":"https://api.github.com/users/shurcooL/gists{/gist_id}","starred_url":"https://api.github.com/users/shurcooL/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shurcooL/subscriptions","organizations_url":"https://api.github.com/users/shurcooL/orgs","repos_url":"https://api.github.com/users/shurcooL/repos","events_url":"https://api.github.com/users/shurcooL/events{/privacy}","received_events_url":"https://api.github.com/users/shurcooL/received_events","type":"User","site_admin":false},"repo":{"id":15602957,"name":"go","full_name":"shurcooL/go","owner":{"login":"shurcooL","id":1924134,"avatar_url":"https://avatars.githubusercontent.com/u/1924134?v=3","gravatar_id":"","url":"https://api.github.com/users/shurcooL","html_url":"https://github.com/shurcooL","followers_url":"https://api.github.com/users/shurcooL/followers","following_url":"https://api.github.com/users/shurcooL/following{/other_user}","gists_url":"https://api.github.com/users/shurcooL/gists{/gist_id}","starred_url":"https://api.github.com/users/shurcooL/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shurcooL/subscriptions","organizations_url":"https://api.github.com/users/shurcooL/orgs","repos_url":"https://api.github.com/users/shurcooL/repos","events_url":"https://api.github.com/users/shurcooL/events{/privacy}","received_events_url":"https://api.github.com/users/shurcooL/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/shurcooL/go","description":"Common Go code.","fork":false,"url":"https://api.github.com/repos/shurcooL/go","forks_url":"https://api.github.com/repos/shurcooL/go/forks","keys_url":"https://api.github.com/repos/shurcooL/go/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shurcooL/go/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shurcooL/go/teams","hooks_url":"https://api.github.com/repos/shurcooL/go/hooks","issue_events_url":"https://api.github.com/repos/shurcooL/go/issues/events{/number}","events_url":"https://api.github.com/repos/shurcooL/go/events","assignees_url":"https://api.github.com/repos/shurcooL/go/assignees{/user}","branches_url":"https://api.github.com/repos/shurcooL/go/branches{/branch}","tags_url":"https://api.github.com/repos/shurcooL/go/tags","blobs_url":"https://api.github.com/repos/shurcooL/go/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shurcooL/go/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shurcooL/go/git/refs{/sha}","trees_url":"https://api.github.com/repos/shurcooL/go/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shurcooL/go/statuses/{sha}","languages_url":"https://api.github.com/repos/shurcooL/go/languages","stargazers_url":"https://api.github.com/repos/shurcooL/go/stargazers","contributors_url":"https://api.github.com/repos/shurcooL/go/contributors","subscribers_url":"https://api.github.com/repos/shurcooL/go/subscribers","subscription_url":"https://api.github.com/repos/shurcooL/go/subscription","commits_url":"https://api.github.com/repos/shurcooL/go/commits{/sha}","git_commits_url":"https://api.github.com/repos/shurcooL/go/git/commits{/sha}","comments_url":"https://api.github.com/repos/shurcooL/go/comments{/number}","issue_comment_url":"https://api.github.com/repos/shurcooL/go/issues/comments{/number}","contents_url":"https://api.github.com/repos/shurcooL/go/contents/{+path}","compare_url":"https://api.github.com/repos/shurcooL/go/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shurcooL/go/merges","archive_url":"https://api.github.com/repos/shurcooL/go/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shurcooL/go/downloads","issues_url":"https://api.github.com/repos/shurcooL/go/issues{/number}","pulls_url":"https://api.github.com/repos/shurcooL/go/pulls{/number}","milestones_url":"https://api.github.com/repos/shurcooL/go/milestones{/number}","notifications_url":"https://api.github.com/repos/shurcooL/go/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shurcooL/go/labels{/name}","releases_url":"https://api.github.com/repos/shurcooL/go/releases{/id}","created_at":"2014-01-03T06:26:18Z","updated_at":"2015-06-16T12:23:26Z","pushed_at":"2015-06-25T05:59:03Z","git_url":"git://github.com/shurcooL/go.git","ssh_url":"git@github.com:shurcooL/go.git","clone_url":"https://github.com/shurcooL/go.git","svn_url":"https://github.com/shurcooL/go","homepage":null,"size":1157,"stargazers_count":42,"watchers_count":42,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":5,"mirror_url":null,"open_issues_count":4,"forks":5,"open_issues":4,"watchers":42,"default_branch":"master"}},"base":{"label":"shurcooL:master","ref":"master","sha":"472360bf505211b3576fad70c288cc1d5a072872","user":{"login":"shurcooL","id":1924134,"avatar_url":"https://avatars.githubusercontent.com/u/1924134?v=3","gravatar_id":"","url":"https://api.github.com/users/shurcooL","html_url":"https://github.com/shurcooL","followers_url":"https://api.github.com/users/shurcooL/followers","following_url":"https://api.github.com/users/shurcooL/following{/other_user}","gists_url":"https://api.github.com/users/shurcooL/gists{/gist_id}","starred_url":"https://api.github.com/users/shurcooL/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shurcooL/subscriptions","organizations_url":"https://api.github.com/users/shurcooL/orgs","repos_url":"https://api.github.com/users/shurcooL/repos","events_url":"https://api.github.com/users/shurcooL/events{/privacy}","received_events_url":"https://api.github.com/users/shurcooL/received_events","type":"User","site_admin":false},"repo":{"id":15602957,"name":"go","full_name":"shurcooL/go","owner":{"login":"shurcooL","id":1924134,"avatar_url":"https://avatars.githubusercontent.com/u/1924134?v=3","gravatar_id":"","url":"https://api.github.com/users/shurcooL","html_url":"https://github.com/shurcooL","followers_url":"https://api.github.com/users/shurcooL/followers","following_url":"https://api.github.com/users/shurcooL/following{/other_user}","gists_url":"https://api.github.com/users/shurcooL/gists{/gist_id}","starred_url":"https://api.github.com/users/shurcooL/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shurcooL/subscriptions","organizations_url":"https://api.github.com/users/shurcooL/orgs","repos_url":"https://api.github.com/users/shurcooL/repos","events_url":"https://api.github.com/users/shurcooL/events{/privacy}","received_events_url":"https://api.github.com/users/shurcooL/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/shurcooL/go","description":"Common Go code.","fork":false,"url":"https://api.github.com/repos/shurcooL/go","forks_url":"https://api.github.com/repos/shurcooL/go/forks","keys_url":"https://api.github.com/repos/shurcooL/go/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shurcooL/go/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shurcooL/go/teams","hooks_url":"https://api.github.com/repos/shurcooL/go/hooks","issue_events_url":"https://api.github.com/repos/shurcooL/go/issues/events{/number}","events_url":"https://api.github.com/repos/shurcooL/go/events","assignees_url":"https://api.github.com/repos/shurcooL/go/assignees{/user}","branches_url":"https://api.github.com/repos/shurcooL/go/branches{/branch}","tags_url":"https://api.github.com/repos/shurcooL/go/tags","blobs_url":"https://api.github.com/repos/shurcooL/go/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shurcooL/go/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shurcooL/go/git/refs{/sha}","trees_url":"https://api.github.com/repos/shurcooL/go/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shurcooL/go/statuses/{sha}","languages_url":"https://api.github.com/repos/shurcooL/go/languages","stargazers_url":"https://api.github.com/repos/shurcooL/go/stargazers","contributors_url":"https://api.github.com/repos/shurcooL/go/contributors","subscribers_url":"https://api.github.com/repos/shurcooL/go/subscribers","subscription_url":"https://api.github.com/repos/shurcooL/go/subscription","commits_url":"https://api.github.com/repos/shurcooL/go/commits{/sha}","git_commits_url":"https://api.github.com/repos/shurcooL/go/git/commits{/sha}","comments_url":"https://api.github.com/repos/shurcooL/go/comments{/number}","issue_comment_url":"https://api.github.com/repos/shurcooL/go/issues/comments{/number}","contents_url":"https://api.github.com/repos/shurcooL/go/contents/{+path}","compare_url":"https://api.github.com/repos/shurcooL/go/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shurcooL/go/merges","archive_url":"https://api.github.com/repos/shurcooL/go/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shurcooL/go/downloads","issues_url":"https://api.github.com/repos/shurcooL/go/issues{/number}","pulls_url":"https://api.github.com/repos/shurcooL/go/pulls{/number}","milestones_url":"https://api.github.com/repos/shurcooL/go/milestones{/number}","notifications_url":"https://api.github.com/repos/shurcooL/go/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shurcooL/go/labels{/name}","releases_url":"https://api.github.com/repos/shurcooL/go/releases{/id}","created_at":"2014-01-03T06:26:18Z","updated_at":"2015-06-16T12:23:26Z","pushed_at":"2015-06-25T05:59:03Z","git_url":"git://github.com/shurcooL/go.git","ssh_url":"git@github.com:shurcooL/go.git","clone_url":"https://github.com/shurcooL/go.git","svn_url":"https://github.com/shurcooL/go","homepage":null,"size":1157,"stargazers_count":42,"watchers_count":42,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":5,"mirror_url":null,"open_issues_count":4,"forks":5,"open_issues":4,"watchers":42,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/shurcooL/go/pulls/21"},"html":{"href":"https://github.com/shurcooL/go/pull/21"},"issue":{"href":"https://api.github.com/repos/shurcooL/go/issues/21"},"comments":{"href":"https://api.github.com/repos/shurcooL/go/issues/21/comments"},"review_comments":{"href":"https://api.github.com/repos/shurcooL/go/pulls/21/comments"},"review_comment":{"href":"https://api.github.com/repos/shurcooL/go/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/shurcooL/go/pulls/21/commits"},"statuses":{"href":"https://api.github.com/repos/shurcooL/go/statuses/4c35f224bedcadc75ac41521d6022af6fda170b1"}}}}