Skip to content

Commit 50c0049

Browse files
authored
Merge pull request #24 from smkniazi/fixing-git
Fixing git
2 parents 0b75879 + 37f8f8a commit 50c0049

7 files changed

+144
-69
lines changed

Dir.go

+36-13
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import (
1818

1919
// Encapsulates state and operations for directory node on the HDFS file system
2020
type Dir struct {
21-
FileSystem *FileSystem // Pointer to the owning filesystem
22-
Attrs Attrs // Cached attributes of the directory, TODO: add TTL
23-
Parent *Dir // Pointer to the parent directory (allows computing fully-qualified paths on demand)
24-
Entries map[string]fs.Node // Cahed directory entries
25-
EntriesMutex sync.Mutex // Used to protect Entries
21+
FileSystem *FileSystem // Pointer to the owning filesystem
22+
Attrs Attrs // Cached attributes of the directory, TODO: add TTL
23+
Parent *Dir // Pointer to the parent directory (allows computing fully-qualified paths on demand)
24+
Entries map[string]*fs.Node // Cahed directory entries
25+
EntriesMutex sync.Mutex // Used to protect Entries
2626
}
2727

2828
// Verify that *Dir implements necesary FUSE interfaces
@@ -63,27 +63,44 @@ func (dir *Dir) Attr(ctx context.Context, a *fuse.Attr) error {
6363
return dir.Attrs.Attr(a)
6464
}
6565

66-
func (dir *Dir) EntriesGet(name string) fs.Node {
66+
func (dir *Dir) EntriesGet(name string) *fs.Node {
6767
dir.EntriesMutex.Lock()
6868
defer dir.EntriesMutex.Unlock()
6969
if dir.Entries == nil {
70-
dir.Entries = make(map[string]fs.Node)
70+
dir.Entries = make(map[string]*fs.Node)
7171
return nil
7272
}
7373
return dir.Entries[name]
7474
}
7575

76-
func (dir *Dir) EntriesSet(name string, node fs.Node) {
76+
func (dir *Dir) EntriesSet(name string, node *fs.Node) {
7777
dir.EntriesMutex.Lock()
7878
defer dir.EntriesMutex.Unlock()
7979

8080
if dir.Entries == nil {
81-
dir.Entries = make(map[string]fs.Node)
81+
dir.Entries = make(map[string]*fs.Node)
8282
}
8383

8484
dir.Entries[name] = node
8585
}
8686

87+
func (dir *Dir) EntriesUpdate(name string, attr Attrs) {
88+
dir.EntriesMutex.Lock()
89+
defer dir.EntriesMutex.Unlock()
90+
91+
if dir.Entries == nil {
92+
dir.Entries = make(map[string]*fs.Node)
93+
}
94+
95+
if node, ok := dir.Entries[name]; ok {
96+
if fnode, ok := (*node).(*File); ok {
97+
fnode.Attrs = attr
98+
} else if dnode, ok := (*node).(*Dir); ok {
99+
dnode.Attrs = attr
100+
}
101+
}
102+
}
103+
87104
func (dir *Dir) EntriesRemove(name string) {
88105
dir.EntriesMutex.Lock()
89106
defer dir.EntriesMutex.Unlock()
@@ -99,7 +116,7 @@ func (dir *Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
99116
}
100117

101118
if node := dir.EntriesGet(name); node != nil {
102-
return node, nil
119+
return *node, nil
103120
}
104121

105122
if dir.FileSystem.ExpandZips && strings.HasSuffix(name, ".zip@") {
@@ -174,7 +191,13 @@ func (dir *Dir) NodeFromAttrs(attrs Attrs) fs.Node {
174191
} else {
175192
node = &Dir{FileSystem: dir.FileSystem, Parent: dir, Attrs: attrs}
176193
}
177-
dir.EntriesSet(attrs.Name, node)
194+
195+
if n := dir.EntriesGet(attrs.Name); n != nil {
196+
dir.EntriesUpdate(attrs.Name, attrs)
197+
} else {
198+
dir.EntriesSet(attrs.Name, &node)
199+
}
200+
178201
return node
179202
}
180203

@@ -242,9 +265,9 @@ func (dir *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDir fs.N
242265
if err == nil {
243266
// Upon successful rename, updating in-memory representation of the file entry
244267
if node := dir.EntriesGet(req.OldName); node != nil {
245-
if fnode, ok := node.(*File); ok {
268+
if fnode, ok := (*node).(*File); ok {
246269
fnode.Attrs.Name = req.NewName
247-
} else if dnode, ok := node.(*Dir); ok {
270+
} else if dnode, ok := (*node).(*Dir); ok {
248271
dnode.Attrs.Name = req.NewName
249272
}
250273
dir.EntriesRemove(req.OldName)

FileSystemOperations_test.go

+47-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestSimple(t *testing.T) {
2020
//create a file, make sure that use and group information is correct
2121
testFile := filepath.Join(mountPoint, "somefile")
2222
loginfo(fmt.Sprintf("New file: %s", testFile), nil)
23-
createFile(t, testFile)
23+
createFile(t, testFile, "some data")
2424
fi, _ := os.Stat(testFile)
2525
fstat := fi.Sys().(*syscall.Stat_t)
2626
grupInfo, _ := user.LookupGroupId(fmt.Sprintf("%d", fstat.Gid))
@@ -30,6 +30,48 @@ func TestSimple(t *testing.T) {
3030
})
3131
}
3232

33+
// testing multiple read write clients perfile
34+
func TestMultipleRWCllients(t *testing.T) {
35+
36+
withMount(t, "/", func(mountPoint string, hdfsAccessor HdfsAccessor) {
37+
//create a file, make sure that use and group information is correct
38+
// mountPoint = "/tmp"
39+
testFile1 := filepath.Join(mountPoint, "somefile")
40+
testFile2 := filepath.Join(mountPoint, "somefile.bak")
41+
loginfo(fmt.Sprintf("New file: %s", testFile1), nil)
42+
createFile(t, testFile1, "initial data\nadsf\n")
43+
44+
c1, _ := os.OpenFile(testFile1, os.O_RDWR, 0600)
45+
c2, _ := os.OpenFile(testFile1, os.O_RDWR, 0600)
46+
c3, _ := os.OpenFile(testFile1, os.O_RDWR, 0600)
47+
48+
c1.WriteString("First client\n")
49+
c1.Close()
50+
51+
os.Rename(testFile1, testFile2)
52+
53+
c2.WriteString("Second client\nSecond client\n")
54+
c3.WriteString("Third client\nThird client\nThird Client\n")
55+
c2.Close()
56+
c3.Close()
57+
58+
c5, err := os.Open(testFile2)
59+
60+
if err != nil {
61+
t.Error("The file should have opened successfully")
62+
} else {
63+
loginfo("File opened successfully", nil)
64+
buffer := make([]byte, 1024)
65+
c5.Read(buffer)
66+
//fmt.Printf("%s", buffer)
67+
}
68+
c5.Close()
69+
70+
os.Remove(testFile1)
71+
os.Remove(testFile2)
72+
})
73+
}
74+
3375
func TestMountSubDir(t *testing.T) {
3476
//mount and create some files and dirs
3577
dirs := 5
@@ -41,7 +83,7 @@ func TestMountSubDir(t *testing.T) {
4183
mkdir(t, dir)
4284
for j := 0; j < filesPdir; j++ {
4385
f := filepath.Join(dir, "file"+strconv.Itoa(j))
44-
createFile(t, f)
86+
createFile(t, f, "initial data")
4587
}
4688
}
4789

@@ -57,8 +99,7 @@ func TestMountSubDir(t *testing.T) {
5799
if len(content) != filesPdir {
58100
t.Errorf("Failed. Expected == %d, Got %d ", filesPdir, len(content))
59101
for _, ent := range content {
60-
loginfo(fmt.Sprintf("%s", ent.Name()), nil)
61-
102+
loginfo(fmt.Sprintf("file/dir %s", ent.Name()), nil)
62103
}
63104
}
64105
})
@@ -78,8 +119,6 @@ func TestMountSubDir(t *testing.T) {
78119
}
79120

80121
func withMount(t testing.TB, srcDir string, fn func(mntPath string, hdfsAccessor HdfsAccessor)) {
81-
initLogger("error", false, "")
82-
83122
hdfsAccessor, err := NewHdfsAccessor("localhost:8020", WallClock{}, TLSConfig{TLS: false})
84123
if err != nil {
85124
logfatal(fmt.Sprintf("Error/NewHdfsAccessor: %v ", err), nil)
@@ -115,13 +154,13 @@ func mkdir(t testing.TB, dir string) {
115154

116155
}
117156

118-
func createFile(t testing.TB, filePath string) {
157+
func createFile(t testing.TB, filePath string, data string) {
119158
t.Helper()
120159
out, err := os.Create(filePath)
121160
if err != nil {
122161
t.Errorf("Faile to create test file %s. Error: %v", filePath, err)
123162
}
124-
out.WriteString("This is some test string")
163+
out.WriteString(data)
125164
out.Close()
126165
}
127166

0 commit comments

Comments
 (0)