Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Fix: Allow opening the root directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Svetlin Ralchev committed Apr 19, 2018
1 parent 727a09e commit bee8722
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 75 deletions.
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func main() {
file, err := parcello.Open("message.txt")
file, err := parcello.Open("document/message.txt")
if err != nil {
panic(err)
}
Expand Down
File renamed without changes.
52 changes: 26 additions & 26 deletions example/public/resource.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
// Package public contains embedded resources
// Auto-generated at Wed Apr 18 15:57:27 CEST 2018
// Auto-generated at Thu Apr 19 16:57:58 CEST 2018
package public

import "github.com/phogolabs/parcello"

func init() {
parcello.AddResource([]byte{
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 236, 148, 207, 106, 227,
48, 16, 198, 125, 214, 83, 204, 190, 64, 52, 138, 255, 133,
96, 114, 217, 221, 243, 6, 54, 16, 114, 148, 229, 73, 100,
144, 45, 35, 41, 255, 222, 190, 216, 78, 75, 161, 37, 189,
52, 13, 165, 249, 93, 62, 27, 15, 243, 121, 36, 190, 105,
200, 123, 185, 163, 73, 56, 133, 232, 86, 32, 34, 102, 73,
50, 104, 158, 165, 131, 226, 116, 124, 239, 17, 185, 136,
68, 60, 205, 210, 44, 201, 99, 76, 34, 20, 113, 26, 99, 4,
120, 179, 63, 122, 197, 222, 7, 233, 34, 68, 39, 141, 210,
87, 234, 124, 144, 219, 237, 149, 239, 151, 89, 94, 244, 155,
192, 254, 7, 233, 66, 221, 238, 96, 77, 165, 39, 119, 32,
7, 182, 5, 29, 66, 55, 231, 220, 88, 37, 141, 182, 62, 204,
103, 56, 67, 198, 54, 118, 15, 74, 182, 32, 149, 34, 239,
33, 104, 2, 106, 74, 170, 42, 170, 192, 145, 183, 123, 167,
8, 202, 51, 28, 106, 95, 247, 77, 231, 140, 189, 219, 137,
51, 118, 239, 193, 31, 12, 28, 169, 244, 117, 32, 94, 183,
21, 157, 38, 58, 52, 230, 243, 61, 62, 202, 255, 84, 36, 207,
249, 207, 80, 228, 17, 138, 180, 79, 208, 35, 255, 95, 64,
241, 235, 207, 191, 223, 171, 205, 242, 47, 244, 119, 191,
96, 197, 40, 0, 69, 105, 171, 115, 255, 0, 80, 104, 177, 88,
147, 81, 182, 33, 8, 22, 150, 210, 41, 50, 198, 22, 92, 139,
75, 65, 183, 88, 233, 218, 195, 145, 202, 78, 238, 8, 106,
15, 195, 42, 169, 96, 235, 108, 243, 118, 69, 20, 188, 27,
44, 248, 232, 81, 240, 209, 244, 222, 103, 241, 19, 121, 2,
0, 0, 255, 255,
31, 139, 8, 0, 0, 0, 0, 0, 0, 255, 236, 148, 77, 110, 194,
48, 16, 133, 179, 246, 41, 166, 23, 192, 54, 132, 4, 161,
136, 77, 219, 117, 145, 138, 132, 88, 58, 206, 128, 35, 57,
113, 100, 79, 248, 185, 125, 149, 132, 118, 83, 68, 55, 168,
168, 42, 223, 230, 37, 202, 104, 94, 198, 214, 155, 194, 233,
182, 194, 154, 120, 133, 33, 168, 29, 142, 232, 72, 209, 141,
17, 66, 136, 36, 142, 123, 77, 147, 105, 175, 98, 60, 188,
119, 200, 84, 70, 114, 50, 78, 166, 73, 156, 78, 68, 28, 9,
57, 157, 196, 73, 4, 226, 214, 63, 114, 137, 54, 144, 242,
145, 16, 94, 89, 109, 174, 212, 5, 82, 219, 237, 149, 239,
231, 89, 190, 244, 143, 192, 222, 73, 121, 42, 235, 29, 172,
49, 15, 232, 247, 232, 193, 213, 96, 136, 154, 57, 231, 214,
105, 101, 141, 11, 52, 159, 137, 153, 96, 108, 227, 90, 208,
170, 6, 165, 53, 134, 0, 100, 16, 176, 202, 177, 40, 176,
0, 143, 193, 181, 94, 35, 228, 39, 216, 151, 161, 236, 154,
206, 25, 187, 216, 137, 51, 118, 239, 193, 31, 244, 28, 48,
15, 37, 33, 47, 235, 2, 143, 35, 67, 149, 189, 189, 199, 79,
249, 31, 203, 248, 51, 255, 137, 144, 105, 151, 255, 46, 65,
143, 252, 255, 2, 217, 211, 203, 219, 243, 106, 179, 124,
133, 238, 238, 23, 44, 27, 4, 32, 203, 93, 113, 234, 30, 0,
50, 35, 23, 107, 180, 218, 85, 8, 228, 96, 169, 188, 70, 107,
93, 198, 141, 60, 23, 52, 139, 149, 41, 3, 28, 48, 111, 212,
14, 161, 12, 208, 175, 146, 2, 182, 222, 85, 223, 87, 68,
198, 155, 222, 130, 15, 30, 25, 31, 76, 239, 125, 22, 255,
145, 15, 0, 0, 0, 255, 255,
})
}
105 changes: 59 additions & 46 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,66 +92,37 @@ func (m *Manager) Open(name string) (ReadOnlyFile, error) {

// OpenFile is the generalized open call; most users will use Open
func (m *Manager) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
path := split(name)
parent, node := find(path, nil, m.root)
if parent == nil {
return nil, fmt.Errorf("Directory does not exist")
parent, node, err := m.open(name, flag)
if err != nil {
return nil, err
}

if hasFlag(os.O_CREATE, flag) {
if node != nil {
if !hasFlag(os.O_TRUNC, flag) {
return nil, &os.PathError{Op: "open", Path: name, Err: os.ErrExist}
}
}
if isWritable(flag) && node != nil && node.IsDir {
return nil, &os.PathError{Op: "open", Path: name, Err: ErrIsDirectory}
}

base := path[len(path)-1]
node = &Node{
Name: base,
IsDir: false,
ModTime: time.Now(),
if hasFlag(os.O_CREATE, flag) {
if node != nil && !hasFlag(os.O_TRUNC, flag) {
return nil, &os.PathError{Op: "open", Path: name, Err: os.ErrExist}
}

parent.Children = append(parent.Children, node)
} else {
if node == nil {
return nil, &os.PathError{Op: "open", Path: name, Err: os.ErrNotExist}
}
if node.IsDir {
return nil, &os.PathError{Op: "open", Path: name, Err: ErrIsDirectory}
}
node = createNode(filepath.Base(name), parent, node)
}

if hasFlag(os.O_WRONLY, flag) ||
hasFlag(os.O_RDWR, flag) ||
hasFlag(os.O_APPEND, flag) {
node.ModTime = time.Now()
if node == nil {
return nil, &os.PathError{Op: "open", Path: name, Err: os.ErrNotExist}
}

return createResourceFile(node, flag)
}

func createResourceFile(node *Node, flag int) (File, error) {
if node.Content == nil || hasFlag(os.O_TRUNC, flag) {
buf := make([]byte, 0)
node.Content = &buf
node.Mutex = &sync.RWMutex{}
}

f := NewResourceFile(node)

if hasFlag(os.O_APPEND, flag) {
_, _ = f.Seek(0, os.SEEK_END)
}

if hasFlag(os.O_RDWR, flag) {
return f, nil
}
if hasFlag(os.O_WRONLY, flag) {
return &woFile{f}, nil
func (m *Manager) open(name string, flag int) (*Node, *Node, error) {
parent, node := find(split(name), nil, m.root)
if node != m.root && parent == nil {
return nil, nil, fmt.Errorf("Directory does not exist")
}

return &roFile{f}, nil
return parent, node, nil
}

// Walk walks the file tree rooted at root, calling walkFn for each file or
Expand Down Expand Up @@ -235,10 +206,52 @@ func walk(path string, node *Node, fn filepath.WalkFunc) error {
return nil
}

func createNode(name string, parent, node *Node) *Node {
node = &Node{
Name: name,
IsDir: false,
ModTime: time.Now(),
}

parent.Children = append(parent.Children, node)
return node
}

func createResourceFile(node *Node, flag int) (File, error) {
if isWritable(flag) {
node.ModTime = time.Now()
}

if node.Content == nil || hasFlag(os.O_TRUNC, flag) {
buf := make([]byte, 0)
node.Content = &buf
node.Mutex = &sync.RWMutex{}
}

f := NewResourceFile(node)

if hasFlag(os.O_APPEND, flag) {
_, _ = f.Seek(0, os.SEEK_END)
}

if hasFlag(os.O_RDWR, flag) {
return f, nil
}
if hasFlag(os.O_WRONLY, flag) {
return &woFile{f}, nil
}

return &roFile{f}, nil
}

func hasFlag(flag int, flags int) bool {
return flags&flag == flag
}

func isWritable(flag int) bool {
return hasFlag(os.O_WRONLY, flag) || hasFlag(os.O_RDWR, flag) || hasFlag(os.O_APPEND, flag)
}

type roFile struct {
*ResourceFile
}
Expand Down
18 changes: 16 additions & 2 deletions manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ var _ = Describe("Manager", func() {
})

Describe("Open", func() {
It("opens the root successfully", func() {
file, err := manager.Open("/")
Expect(file).NotTo(BeNil())
Expect(err).To(BeNil())
})

Context("when the resource is empty", func() {
It("returns an error", func() {
file, err := manager.Open("/migration.sql")
Expand All @@ -114,8 +120,8 @@ var _ = Describe("Manager", func() {
Context("when the file is directory", func() {
It("returns an error", func() {
file, err := manager.Open("/resource/reports")
Expect(file).To(BeNil())
Expect(err).To(MatchError("open /resource/reports: Is directory"))
Expect(file).NotTo(BeNil())
Expect(err).To(BeNil())
})
})

Expand Down Expand Up @@ -184,6 +190,14 @@ var _ = Describe("Manager", func() {
})
})

Context("when the file is directory", func() {
It("returns an error", func() {
file, err := manager.OpenFile("/resource/reports", os.O_RDWR, 0600)
Expect(file).To(BeNil())
Expect(err).To(MatchError("open /resource/reports: Is directory"))
})
})

Context("when the file exists", func() {
It("truncs the file content", func() {
file, err := manager.OpenFile("/resource/reports/2018.txt", os.O_CREATE|os.O_TRUNC, 0600)
Expand Down

0 comments on commit bee8722

Please sign in to comment.