Skip to content

Commit 7266dc4

Browse files
committed
feat: [tools-v2] bs list dir
1 parent bf406a0 commit 7266dc4

File tree

6 files changed

+257
-0
lines changed

6 files changed

+257
-0
lines changed

tools-v2/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ A tool for CurveFS & CurveBs.
4646
- [list logical-pool](#list-logical-pool)
4747
- [list server](#list-server)
4848
- [list client](#list-client)
49+
- [list dir](#list-dir)
4950
- [query](#query-1)
5051
- [query file](#query-file)
5152
- [status](#status-1)
@@ -861,6 +862,24 @@ Output:
861862
+------------+------+
862863
```
863864

865+
##### list dir
866+
867+
list dir information in curvebs
868+
869+
```bash
870+
curve bs list dir --dir /
871+
```
872+
873+
Output:
874+
875+
```bash
876+
+------+-------------+----------+-----------------+------------+---------------------+---------------+-------------+
877+
| ID | FILENAME | PARENTID | FILETYPE | OWNER | CTIME | ALLOCATEDSIZE | FILESIZE |
878+
+------+-------------+----------+-----------------+------------+---------------------+---------------+-------------+
879+
| 1 | /RecycleBin | 0 | INODE_DIRECTORY | root | 2022-11-12 16:38:25 | 0 B | 0 B |
880+
+------+-------------+----------+-----------------+------------+---------------------+---------------+-------------+
881+
```
882+
864883
### query
865884

866885
##### query file

tools-v2/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module github.com/opencurve/curve/tools-v2
22

33
go 1.19
44

5+
replace github.com/optiopay/kafka => github.com/cilium/kafka v0.0.0-20180809090225-01ce283b732b
6+
57
require (
68
github.com/cilium/cilium v1.12.1
79
github.com/deckarep/golang-set/v2 v2.1.0

tools-v2/internal/utils/row.go

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package cobrautil
2525
const (
2626
ROW_ADDR = "addr"
2727
ROW_ALLOC = "alloc"
28+
ROW_ALLOC_SIZE = "allocatedSize"
2829
ROW_BLOCKSIZE = "blocksize"
2930
ROW_CAPACITY = "capacity"
3031
ROW_CHILD_LIST = "childList"
@@ -40,6 +41,8 @@ const (
4041
ROW_EXPLAIN = "explain"
4142
ROW_EXTERNAL_ADDR = "externalAddr"
4243
ROW_FILE_SIZE = "fileSize"
44+
ROW_FILE_TYPE = "fileType"
45+
ROW_FILE_NAME = "fileName"
4346
ROW_FS_ID = "fsId"
4447
ROW_FS_NAME = "fsName"
4548
ROW_FS_TYPE = "fsType"
@@ -64,6 +67,7 @@ const (
6467
ROW_ORIGINAL_PATH = "originalPath"
6568
ROW_OWNER = "owner"
6669
ROW_PARENT = "parent"
70+
ROW_PARENT_ID = "parentId"
6771
ROW_PARTITION_ID = "partitionId"
6872
ROW_PEER_ADDR = "peerAddr"
6973
ROW_PEER_ID = "peerId"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
/*
2+
* Copyright (c) 2022 NetEase Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* Project: CurveCli
19+
* Created Date: 2022-11-17
20+
* Author: Sindweller
21+
*/
22+
23+
package dir
24+
25+
import (
26+
"context"
27+
"fmt"
28+
"github.com/dustin/go-humanize"
29+
cmderror "github.com/opencurve/curve/tools-v2/internal/error"
30+
cobrautil "github.com/opencurve/curve/tools-v2/internal/utils"
31+
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
32+
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/query/file"
33+
"github.com/opencurve/curve/tools-v2/pkg/config"
34+
"github.com/opencurve/curve/tools-v2/pkg/output"
35+
"github.com/opencurve/curve/tools-v2/proto/proto/nameserver2"
36+
"github.com/spf13/cobra"
37+
"github.com/spf13/viper"
38+
"google.golang.org/grpc"
39+
"log"
40+
"time"
41+
)
42+
43+
const (
44+
dirExample = `$ curve bs list dir --dir /`
45+
)
46+
47+
type ListDirRpc struct {
48+
Info *basecmd.Rpc
49+
Request *nameserver2.ListDirRequest
50+
curveFSClient nameserver2.CurveFSServiceClient
51+
}
52+
53+
var _ basecmd.RpcFunc = (*ListDirRpc)(nil) // check interface
54+
55+
type DirCommand struct {
56+
basecmd.FinalCurveCmd
57+
Rpc []*ListDirRpc
58+
}
59+
60+
var _ basecmd.FinalCurveCmdFunc = (*DirCommand)(nil) // check interface
61+
62+
func (lRpc *ListDirRpc) NewRpcClient(cc grpc.ClientConnInterface) {
63+
lRpc.curveFSClient = nameserver2.NewCurveFSServiceClient(cc)
64+
}
65+
66+
func (lRpc *ListDirRpc) Stub_Func(ctx context.Context) (interface{}, error) {
67+
return lRpc.curveFSClient.ListDir(ctx, lRpc.Request)
68+
}
69+
70+
func NewDirCommand() *cobra.Command {
71+
return NewListDirCommand().Cmd
72+
}
73+
74+
func NewListDirCommand() *DirCommand {
75+
lsCmd := &DirCommand{
76+
FinalCurveCmd: basecmd.FinalCurveCmd{
77+
Use: "dir",
78+
Short: "list directory information in curvebs",
79+
Example: dirExample,
80+
},
81+
}
82+
83+
basecmd.NewFinalCurveCli(&lsCmd.FinalCurveCmd, lsCmd)
84+
return lsCmd
85+
}
86+
87+
// AddFlags implements basecmd.FinalCurveCmdFunc
88+
func (pCmd *DirCommand) AddFlags() {
89+
config.AddBsMdsFlagOption(pCmd.Cmd)
90+
config.AddRpcRetryTimesFlag(pCmd.Cmd)
91+
config.AddRpcTimeoutFlag(pCmd.Cmd)
92+
config.AddBsDirOptionFlag(pCmd.Cmd)
93+
config.AddBsUserOptionFlag(pCmd.Cmd)
94+
config.AddBsPasswordOptionFlag(pCmd.Cmd)
95+
}
96+
97+
// Init implements basecmd.FinalCurveCmdFunc
98+
func (pCmd *DirCommand) Init(cmd *cobra.Command, args []string) error {
99+
mdsAddrs, err := config.GetBsMdsAddrSlice(pCmd.Cmd)
100+
if err.TypeCode() != cmderror.CODE_SUCCESS {
101+
return err.ToError()
102+
}
103+
104+
timeout := config.GetFlagDuration(pCmd.Cmd, config.RPCTIMEOUT)
105+
retrytimes := config.GetFlagInt32(pCmd.Cmd, config.RPCRETRYTIMES)
106+
fileName := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_DIR)
107+
owner := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_USER)
108+
date, errDat := cobrautil.GetTimeofDayUs()
109+
if errDat.TypeCode() != cmderror.CODE_SUCCESS {
110+
return errDat.ToError()
111+
}
112+
113+
rpc := &ListDirRpc{
114+
Request: &nameserver2.ListDirRequest{
115+
FileName: &fileName,
116+
Owner: &owner,
117+
Date: &date,
118+
},
119+
Info: basecmd.NewRpc(mdsAddrs, timeout, retrytimes, "ListDir"),
120+
}
121+
// auth
122+
password := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_PASSWORD)
123+
if owner == viper.GetString(config.VIPER_CURVEBS_USER) && len(password) != 0 {
124+
strSig := cobrautil.GetString2Signature(date, owner)
125+
sig := cobrautil.CalcString2Signature(strSig, password)
126+
rpc.Request.Signature = &sig
127+
}
128+
pCmd.Rpc = append(pCmd.Rpc, rpc)
129+
header := []string{
130+
cobrautil.ROW_ID,
131+
cobrautil.ROW_FILE_NAME,
132+
cobrautil.ROW_PARENT_ID,
133+
cobrautil.ROW_FILE_TYPE,
134+
cobrautil.ROW_OWNER,
135+
cobrautil.ROW_CTIME,
136+
cobrautil.ROW_ALLOC_SIZE,
137+
cobrautil.ROW_FILE_SIZE,
138+
}
139+
pCmd.SetHeader(header)
140+
pCmd.TableNew.SetAutoMergeCellsByColumnIndex(cobrautil.GetIndexSlice(
141+
pCmd.Header, header,
142+
))
143+
return nil
144+
}
145+
146+
// Print implements basecmd.FinalCurveCmdFunc
147+
func (pCmd *DirCommand) Print(cmd *cobra.Command, args []string) error {
148+
return output.FinalCmdOutput(&pCmd.FinalCurveCmd, pCmd)
149+
}
150+
151+
// RunCommand implements basecmd.FinalCurveCmdFunc
152+
func (pCmd *DirCommand) RunCommand(cmd *cobra.Command, args []string) error {
153+
var infos []*basecmd.Rpc
154+
var funcs []basecmd.RpcFunc
155+
for _, rpc := range pCmd.Rpc {
156+
infos = append(infos, rpc.Info)
157+
funcs = append(funcs, rpc)
158+
}
159+
results, errs := basecmd.GetRpcListResponse(infos, funcs)
160+
if len(errs) == len(infos) {
161+
mergeErr := cmderror.MergeCmdErrorExceptSuccess(errs)
162+
return mergeErr.ToError()
163+
}
164+
var errors []*cmderror.CmdError
165+
rows := make([]map[string]string, 0)
166+
for _, res := range results {
167+
infos := res.(*nameserver2.ListDirResponse).GetFileInfo()
168+
for _, info := range infos {
169+
row := make(map[string]string)
170+
dirName := config.GetBsFlagString(pCmd.Cmd, config.CURVEBS_DIR)
171+
var fileName string
172+
if dirName == "/" {
173+
fileName = dirName + info.GetFileName()
174+
} else {
175+
fileName = dirName + "/" + info.GetFileName()
176+
}
177+
row[cobrautil.ROW_ID] = fmt.Sprintf("%d", info.GetId())
178+
row[cobrautil.ROW_FILE_NAME] = fileName
179+
row[cobrautil.ROW_PARENT_ID] = fmt.Sprintf("%d", info.GetParentId())
180+
row[cobrautil.ROW_FILE_TYPE] = fmt.Sprintf("%v", info.GetFileType())
181+
row[cobrautil.ROW_OWNER] = info.GetOwner()
182+
row[cobrautil.ROW_CTIME] = time.Unix(int64(info.GetCtime()/1000000), 0).Format("2006-01-02 15:04:05")
183+
184+
// generate a query file command
185+
fInfoCmd := file.NewQueryFileCommand()
186+
config.AlignFlagsValue(pCmd.Cmd, fInfoCmd.Cmd, []string{
187+
config.RPCRETRYTIMES, config.RPCTIMEOUT, config.CURVEBS_MDSADDR,
188+
config.CURVEBS_PATH,
189+
})
190+
fInfoCmd.Cmd.Flags().Set("path", fileName)
191+
192+
// Get file size
193+
sizeRes, err := file.GetFileSize(fInfoCmd.Cmd)
194+
if err.TypeCode() != cmderror.CODE_SUCCESS {
195+
// TODO handle err
196+
log.Printf("%s failed to get file size: %v", info.GetFileName(), err)
197+
//return err.ToError()
198+
}
199+
row[cobrautil.ROW_FILE_SIZE] = fmt.Sprintf("%s", humanize.IBytes(sizeRes.GetFileSize()))
200+
// Get allocated size
201+
allocRes, err := file.GetAllocatedSize(fInfoCmd.Cmd)
202+
if err.TypeCode() != cmderror.CODE_SUCCESS {
203+
// TODO handle err
204+
log.Printf("%s failed to get allocated size: %v", info.GetFileName(), err)
205+
//return err.ToError()
206+
}
207+
row[cobrautil.ROW_ALLOC_SIZE] = fmt.Sprintf("%s", humanize.IBytes(allocRes.GetAllocatedSize()))
208+
rows = append(rows, row)
209+
}
210+
}
211+
list := cobrautil.ListMap2ListSortByKeys(rows, pCmd.Header, []string{
212+
cobrautil.ROW_FILE_NAME,
213+
})
214+
pCmd.TableNew.AppendBulk(list)
215+
errRet := cmderror.MergeCmdError(errors)
216+
pCmd.Error = &errRet
217+
pCmd.Result = results
218+
return nil
219+
}
220+
221+
// ResultPlainOutput implements basecmd.FinalCurveCmdFunc
222+
func (pCmd *DirCommand) ResultPlainOutput() error {
223+
return output.FinalCmdOutputPlain(&pCmd.FinalCurveCmd)
224+
}

tools-v2/pkg/cli/command/curvebs/list/list.go

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ package list
2525
import (
2626
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command"
2727
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/client"
28+
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/dir"
2829
logicalpool "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/logicalPool"
2930
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/server"
3031
"github.com/spf13/cobra"
@@ -41,6 +42,7 @@ func (listCmd *ListCommand) AddSubCommands() {
4142
logicalpool.NewLogicalPoolCommand(),
4243
server.NewServerCommand(),
4344
client.NewClientCommand(),
45+
dir.NewDirCommand(),
4446
)
4547
}
4648

tools-v2/pkg/config/bs.go

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const (
4747
CURVEBS_PASSWORD = "password"
4848
VIPER_CURVEBS_PASSWORD = "curvebs.root.password"
4949
CURVEBS_DEFAULT_PASSWORD = "root_password"
50+
CURVEBS_DIR = "dir"
5051
)
5152

5253
var (
@@ -126,6 +127,11 @@ func AddBsPasswordOptionFlag(cmd *cobra.Command) {
126127
AddBsStringOptionFlag(cmd, CURVEBS_PASSWORD, "user password")
127128
}
128129

130+
// dir
131+
func AddBsDirOptionFlag(cmd *cobra.Command) {
132+
AddBsStringOptionFlag(cmd, CURVEBS_DIR, "directory path")
133+
}
134+
129135
// etcd
130136
func AddBsEtcdAddrFlag(cmd *cobra.Command) {
131137
AddBsStringOptionFlag(cmd, CURVEBS_ETCDADDR, "etcd address, should be like 127.0.0.1:8700,127.0.0.1:8701,127.0.0.1:8702")

0 commit comments

Comments
 (0)