aboutsummaryrefslogtreecommitdiff
path: root/weed/shell/command_fs_mkdir.go
blob: 49dc8a3f8f405cc2bbc1eac979869cbb8a695b57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package shell

import (
	"context"
	"io"
	"os"
	"time"

	"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
	"github.com/seaweedfs/seaweedfs/weed/util"
)

func init() {
	Commands = append(Commands, &commandFsMkdir{})
}

type commandFsMkdir struct {
}

func (c *commandFsMkdir) Name() string {
	return "fs.mkdir"
}

func (c *commandFsMkdir) Help() string {
	return `create a directory

	fs.mkdir path/to/dir
`
}

func (c *commandFsMkdir) HasTag(CommandTag) bool {
	return false
}

func (c *commandFsMkdir) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {

	if handleHelpRequest(c, args, writer) {
		return nil
	}

	path, err := commandEnv.parseUrl(findInputDirectory(args))
	if err != nil {
		return err
	}

	dir, name := util.FullPath(path).DirAndName()

	err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {

		_, createErr := client.CreateEntry(context.Background(), &filer_pb.CreateEntryRequest{
			Directory: dir,
			Entry: &filer_pb.Entry{
				Name:        name,
				IsDirectory: true,
				Attributes: &filer_pb.FuseAttributes{
					Mtime:    time.Now().Unix(),
					Crtime:   time.Now().Unix(),
					FileMode: uint32(0777 | os.ModeDir),
				},
			},
		})
		return createErr
	})

	return
}