@@ -5,16 +5,15 @@ package main
5
5
import (
6
6
"fmt"
7
7
"os"
8
- "os/user"
9
8
"path"
9
+ "path/filepath"
10
10
"strings"
11
11
"sync"
12
12
"time"
13
13
14
14
"bazil.org/fuse"
15
15
"bazil.org/fuse/fs"
16
16
"golang.org/x/net/context"
17
- "logicalclocks.com/hopsfs-mount/ugcache"
18
17
)
19
18
20
19
// Encapsulates state and operations for directory node on the HDFS file system
@@ -49,7 +48,7 @@ func (dir *DirINode) AbsolutePathForChild(name string) string {
49
48
if path != "/" {
50
49
path = path + "/"
51
50
}
52
- return path + name
51
+ return path + filepath . Base ( name )
53
52
}
54
53
55
54
// Responds on FUSE request to get directory attributes
@@ -63,7 +62,7 @@ func (dir *DirINode) Attr(ctx context.Context, a *fuse.Attr) error {
63
62
}
64
63
65
64
}
66
- return dir .Attrs .Attr (a )
65
+ return dir .Attrs .ConvertAttrToFuse (a )
67
66
}
68
67
69
68
func (dir * DirINode ) EntriesGet (name string ) * fs.Node {
@@ -229,15 +228,16 @@ func (dir *DirINode) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fs.Node
229
228
}
230
229
logdebug ("mkdir successful" , Fields {Operation : Mkdir , Path : path .Join (dir .AbsolutePath (), req .Name )})
231
230
232
- err = dir .changeOwnership ( dir .AbsolutePathForChild (req .Name ), req .Uid , req .Gid )
231
+ err = ChownOp ( & dir .Attrs , dir . FileSystem , dir .AbsolutePathForChild (req .Name ), req .Uid , req .Gid )
233
232
if err != nil {
234
- logwarn ("Unable to change ownership of new dir" , Fields {Operation : Create , Path : dir .AbsolutePathForChild (req .Name ), UID : req .Uid , GID : req .Gid })
233
+ logwarn ("Unable to change ownership of new dir" , Fields {Operation : Create , Path : dir .AbsolutePathForChild (req .Name ),
234
+ UID : req .Uid , GID : req .Gid , Error : err })
235
235
//unable to change the ownership of the directory. so delete it as the operation as a whole failed
236
236
dir .FileSystem .getDFSConnector ().Remove (dir .AbsolutePathForChild (req .Name ))
237
237
return nil , err
238
238
}
239
239
240
- return dir .NodeFromAttrs (Attrs {Name : req .Name , Mode : req .Mode | os .ModeDir }), nil
240
+ return dir .NodeFromAttrs (Attrs {Name : req .Name , Mode : req .Mode | os .ModeDir , Uid : req . Uid , Gid : req . Gid }), nil
241
241
}
242
242
243
243
// Responds on FUSE Create request
@@ -253,36 +253,24 @@ func (dir *DirINode) Create(ctx context.Context, req *fuse.CreateRequest, resp *
253
253
//TODO remove the entry from the cache
254
254
return nil , nil , err
255
255
}
256
- file .AddHandle (handle )
257
256
258
- err = dir .changeOwnership (dir .AbsolutePathForChild (req .Name ), req .Uid , req .Gid )
257
+ file .AddHandle (handle )
258
+ err = ChownOp (& dir .Attrs , dir .FileSystem , dir .AbsolutePathForChild (req .Name ), req .Uid , req .Gid )
259
259
if err != nil {
260
- logwarn ("Unable to change ownership of new file" , Fields {Operation : Create , Path : dir .AbsolutePathForChild (req .Name ), UID : req .Uid , GID : req .Gid })
260
+ logwarn ("Unable to change ownership of new file" , Fields {Operation : Create , Path : dir .AbsolutePathForChild (req .Name ),
261
+ UID : req .Uid , GID : req .Gid , Error : err })
261
262
//unable to change the ownership of the file. so delete it as the operation as a whole failed
262
263
dir .FileSystem .getDFSConnector ().Remove (dir .AbsolutePathForChild (req .Name ))
263
264
return nil , nil , err
264
265
}
265
266
266
- return file , handle , nil
267
- }
268
-
269
- func (dir * DirINode ) changeOwnership (path string , uid uint32 , gid uint32 ) error {
270
- if hadoopUserID != uid { // the file is created by an other user, so change the ownership information
271
- user := ugcache .LookupUserName (uid )
272
- if user == "" {
273
- logwarn ("Unable to find user information" , Fields {Operation : Chown , Path : path , UID : uid , GID : gid })
274
- }
275
- group := ugcache .LookupGroupName (gid )
276
- if group == "" {
277
- logwarn ("Unable to find group information" , Fields {Operation : Chown , Path : path , UID : uid , GID : gid })
278
- }
279
- err := dir .FileSystem .getDFSConnector ().Chown (path , user , group )
280
- if err != nil {
281
- return err
282
- }
283
- loginfo ("Updated ownership" , Fields {Operation : Create , Path : path , User : user , Group : group })
267
+ //update the attributes of the file now
268
+ err = dir .LookupAttrs (file .Attrs .Name , & file .Attrs )
269
+ if err != nil {
270
+ return nil , nil , err
284
271
}
285
- return nil
272
+
273
+ return file , handle , nil
286
274
}
287
275
288
276
// Responds on FUSE Remove request
@@ -332,59 +320,33 @@ func (dir *DirINode) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp
332
320
dir .lockMutex ()
333
321
defer dir .unlockMutex ()
334
322
335
- // Get the filepath, so chmod in hdfs can work
323
+ if req .Valid .Size () {
324
+ return fmt .Errorf ("unsupported operation. Can not set size of a directory" )
325
+ }
326
+
336
327
path := dir .AbsolutePath ()
337
- var err error
338
328
339
329
if req .Valid .Mode () {
340
- loginfo ("Setting attributes" , Fields {Operation : Chmod , Path : path , Mode : req .Mode })
341
- (func () {
342
- err = dir .FileSystem .getDFSConnector ().Chmod (path , req .Mode )
343
- if err != nil {
344
- return
345
- }
346
- })()
347
-
348
- if err != nil {
349
- logerror ("Failed to set attributes" , Fields {Operation : Chmod , Path : path , Mode : req .Mode , Error : err })
350
- } else {
351
- dir .Attrs .Mode = req .Mode
330
+ if err := ChmodOp (& dir .Attrs , dir .FileSystem , path , req , resp ); err != nil {
331
+ logwarn ("Setattr (chmod) failed. " , Fields {Operation : Chmod , Path : path , Mode : req .Mode })
332
+ return err
352
333
}
353
334
}
354
335
355
- if req .Valid .Uid () {
356
- u , err := user .LookupId (fmt .Sprint (req .Uid ))
357
- owner := fmt .Sprint (req .Uid )
358
- group := fmt .Sprint (req .Gid )
359
- if err != nil {
360
- logerror (fmt .Sprintf ("Chown: username for uid %d not found, use uid/gid instead" , req .Uid ),
361
- Fields {Operation : Chown , Path : path , User : u , UID : owner , GID : group , Error : err })
362
- } else {
363
- owner = u .Username
364
- group = owner // hardcoded the group same as owner until LookupGroupId available
336
+ if req .Valid .Uid () || req .Valid .Gid () {
337
+ if err := SetAttrChownOp (& dir .Attrs , dir .FileSystem , path , req , resp ); err != nil {
338
+ logwarn ("Setattr (chown/chgrp )failed" , Fields {Operation : Chmod , Path : path , UID : req .Uid , GID : req .Gid })
339
+ return err
365
340
}
341
+ }
366
342
367
- loginfo ("Setting attributes" , Fields {Operation : Chown , Path : path , User : u , UID : owner , GID : group })
368
- (func () {
369
- err = dir .FileSystem .getDFSConnector ().Chown (path , owner , group )
370
- if err != nil {
371
- return
372
- }
373
- })()
374
-
375
- if err != nil {
376
- logerror ("Failed to set attributes" , Fields {Operation : Chown , Path : path , User : u , UID : owner , GID : group , Error : err })
377
- } else {
378
- dir .Attrs .Uid = req .Uid
379
- dir .Attrs .Gid = req .Gid
380
- }
343
+ if err := UpdateTS (& dir .Attrs , dir .FileSystem , path , req , resp ); err != nil {
344
+ return err
381
345
}
382
346
383
- return err
347
+ return nil
384
348
}
385
349
386
- var dirLockTime time.Time = time.Time {}
387
-
388
350
func (dir * DirINode ) lockMutex () {
389
351
dir .mutex .Lock ()
390
352
}
0 commit comments