From 3724c6acc39c620ffc508dbbf4c8bf6605139485 Mon Sep 17 00:00:00 2001 From: Ishank Arora Date: Thu, 14 Oct 2021 16:08:21 +0200 Subject: [PATCH] Refactor code --- pkg/eosclient/eosbinary/eosbinary.go | 37 ++++++++----------------- pkg/eosclient/eosgrpc/eosgrpc.go | 40 ++++++++++++---------------- 2 files changed, 28 insertions(+), 49 deletions(-) diff --git a/pkg/eosclient/eosbinary/eosbinary.go b/pkg/eosclient/eosbinary/eosbinary.go index 94b02bdbe8..ac5e4dff4b 100644 --- a/pkg/eosclient/eosbinary/eosbinary.go +++ b/pkg/eosclient/eosbinary/eosbinary.go @@ -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 @@ -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 @@ -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)) @@ -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. diff --git a/pkg/eosclient/eosgrpc/eosgrpc.go b/pkg/eosclient/eosgrpc/eosgrpc.go index 0c5e81a544..a85ec28f41 100644 --- a/pkg/eosclient/eosgrpc/eosgrpc.go +++ b/pkg/eosclient/eosgrpc/eosgrpc.go @@ -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)) @@ -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. @@ -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) @@ -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" @@ -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