Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
ishank011 committed Oct 14, 2021
1 parent 1ddf1a0 commit 3724c6a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 49 deletions.
37 changes: 11 additions & 26 deletions pkg/eosclient/eosbinary/eosbinary.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,16 +462,7 @@ func (c *Client) GetFileInfoByInode(ctx context.Context, auth eosclient.Authoriz
info.Inode = inode
}

// We need to inherit the ACLs for the parent directory as these are not available for files
if !info.IsDir {
parentInfo, err := c.GetFileInfoByPath(ctx, auth, path.Dir(info.File))
// Even if this call fails, at least return the current file object
if err == nil {
info.SysACL.Entries = append(info.SysACL.Entries, parentInfo.SysACL.Entries...)
}
}

return info, nil
return c.mergeParentACLsForFiles(ctx, auth, info), nil
}

// GetFileInfoByFXID returns the FileInfo by the given file id in hexadecimal
Expand All @@ -487,21 +478,12 @@ func (c *Client) GetFileInfoByFXID(ctx context.Context, auth eosclient.Authoriza
return nil, err
}

// We need to inherit the ACLs for the parent directory as these are not available for files
if !info.IsDir {
parentInfo, err := c.GetFileInfoByPath(ctx, auth, path.Dir(info.File))
// Even if this call fails, at least return the current file object
if err == nil {
info.SysACL.Entries = append(info.SysACL.Entries, parentInfo.SysACL.Entries...)
}
}

return info, nil
return c.mergeParentACLsForFiles(ctx, auth, info), nil
}

// GetFileInfoByPath returns the FilInfo at the given path
func (c *Client) GetFileInfoByPath(ctx context.Context, auth eosclient.Authorization, resourcePath string) (*eosclient.FileInfo, error) {
args := []string{"file", "info", resourcePath, "-m"}
func (c *Client) GetFileInfoByPath(ctx context.Context, auth eosclient.Authorization, path string) (*eosclient.FileInfo, error) {
args := []string{"file", "info", path, "-m"}
stdout, _, err := c.executeEOS(ctx, args, auth)
if err != nil {
return nil, err
Expand All @@ -511,12 +493,16 @@ func (c *Client) GetFileInfoByPath(ctx context.Context, auth eosclient.Authoriza
return nil, err
}

if c.opt.VersionInvariant && !isVersionFolder(resourcePath) && !info.IsDir {
if inode, err := c.getVersionFolderInode(ctx, auth, resourcePath); err == nil {
if c.opt.VersionInvariant && !isVersionFolder(path) && !info.IsDir {
if inode, err := c.getVersionFolderInode(ctx, auth, path); err == nil {
info.Inode = inode
}
}

return c.mergeParentACLsForFiles(ctx, auth, info), nil
}

func (c *Client) mergeParentACLsForFiles(ctx context.Context, auth eosclient.Authorization, info *eosclient.FileInfo) *eosclient.FileInfo {
// We need to inherit the ACLs for the parent directory as these are not available for files
if !info.IsDir {
parentInfo, err := c.GetFileInfoByPath(ctx, auth, path.Dir(info.File))
Expand All @@ -525,8 +511,7 @@ func (c *Client) GetFileInfoByPath(ctx context.Context, auth eosclient.Authoriza
info.SysACL.Entries = append(info.SysACL.Entries, parentInfo.SysACL.Entries...)
}
}

return info, nil
return info
}

// SetAttr sets an extended attributes on a path.
Expand Down
40 changes: 17 additions & 23 deletions pkg/eosclient/eosgrpc/eosgrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ func (c *Client) GetFileInfoByInode(ctx context.Context, auth eosclient.Authoriz
info.Inode = inode
}

log.Debug().Str("func", "GetFileInfoByInode").Uint64("inode", inode).Msg("")
return c.mergeParentACLsForFiles(ctx, auth, info), nil
}

func (c *Client) mergeParentACLsForFiles(ctx context.Context, auth eosclient.Authorization, info *eosclient.FileInfo) *eosclient.FileInfo {
// We need to inherit the ACLs for the parent directory as these are not available for files
if !info.IsDir {
parentInfo, err := c.GetFileInfoByPath(ctx, auth, path.Dir(info.File))
Expand All @@ -492,9 +497,7 @@ func (c *Client) GetFileInfoByInode(ctx context.Context, auth eosclient.Authoriz
info.SysACL.Entries = append(info.SysACL.Entries, parentInfo.SysACL.Entries...)
}
}

log.Debug().Str("func", "GetFileInfoByInode").Uint64("inode", inode).Msg("")
return info, nil
return info
}

// SetAttr sets an extended attributes on a path.
Expand Down Expand Up @@ -585,9 +588,9 @@ func (c *Client) GetAttr(ctx context.Context, auth eosclient.Authorization, name
}

// GetFileInfoByPath returns the FilInfo at the given path
func (c *Client) GetFileInfoByPath(ctx context.Context, auth eosclient.Authorization, filePath string) (*eosclient.FileInfo, error) {
func (c *Client) GetFileInfoByPath(ctx context.Context, auth eosclient.Authorization, path string) (*eosclient.FileInfo, error) {
log := appctx.GetLogger(ctx)
log.Info().Str("func", "GetFileInfoByPath").Str("uid,gid", auth.Role.UID+","+auth.Role.GID).Str("path", filePath).Msg("")
log.Info().Str("func", "GetFileInfoByPath").Str("uid,gid", auth.Role.UID+","+auth.Role.GID).Str("path", path).Msg("")

// Initialize the common fields of the MDReq
mdrq, err := c.initMDRequest(ctx, auth)
Expand All @@ -597,18 +600,18 @@ func (c *Client) GetFileInfoByPath(ctx context.Context, auth eosclient.Authoriza

mdrq.Type = erpc.TYPE_STAT
mdrq.Id = new(erpc.MDId)
mdrq.Id.Path = []byte(filePath)
mdrq.Id.Path = []byte(path)

// Now send the req and see what happens
resp, err := c.cl.MD(ctx, mdrq)
if err != nil {
log.Error().Str("func", "GetFileInfoByPath").Err(err).Str("path", filePath).Str("err", err.Error())
log.Error().Str("func", "GetFileInfoByPath").Err(err).Str("path", path).Str("err", err.Error())

return nil, err
}
rsp, err := resp.Recv()
if err != nil {
log.Error().Str("func", "GetFileInfoByPath").Err(err).Str("path", filePath).Str("err", err.Error())
log.Error().Str("func", "GetFileInfoByPath").Err(err).Str("path", path).Str("err", err.Error())

// FIXME: this is very very bad and poisonous for the project!!!!!!!
// Apparently here we have to assume that an error in Recv() means "file not found"
Expand All @@ -619,34 +622,25 @@ func (c *Client) GetFileInfoByPath(ctx context.Context, auth eosclient.Authoriza
}

if rsp == nil {
return nil, errtypes.NotFound(fmt.Sprintf("%s:%s", "acltype", filePath))
return nil, errtypes.NotFound(fmt.Sprintf("%s:%s", "acltype", path))
}

log.Debug().Str("func", "GetFileInfoByPath").Str("path", filePath).Str("rsp:", fmt.Sprintf("%#v", rsp)).Msg("grpc response")
log.Debug().Str("func", "GetFileInfoByPath").Str("path", path).Str("rsp:", fmt.Sprintf("%#v", rsp)).Msg("grpc response")

info, err := c.grpcMDResponseToFileInfo(rsp, filepath.Dir(filePath))
info, err := c.grpcMDResponseToFileInfo(rsp, filepath.Dir(path))
if err != nil {
return nil, err
}

if c.opt.VersionInvariant && !isVersionFolder(filePath) && !info.IsDir {
inode, err := c.getVersionFolderInode(ctx, auth, filePath)
if c.opt.VersionInvariant && !isVersionFolder(path) && !info.IsDir {
inode, err := c.getVersionFolderInode(ctx, auth, path)
if err != nil {
return nil, err
}
info.Inode = inode
}

// We need to inherit the ACLs for the parent directory as these are not available for files
if !info.IsDir {
parentInfo, err := c.GetFileInfoByPath(ctx, auth, path.Dir(info.File))
// Even if this call fails, at least return the current file object
if err == nil {
info.SysACL.Entries = append(info.SysACL.Entries, parentInfo.SysACL.Entries...)
}
}

return info, nil
return c.mergeParentACLsForFiles(ctx, auth, info), nil
}

// GetFileInfoByFXID returns the FileInfo by the given file id in hexadecimal
Expand Down

0 comments on commit 3724c6a

Please sign in to comment.