forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mkdir.go
92 lines (84 loc) · 1.78 KB
/
mkdir.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2012-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Mkdir makes a new directory.
//
// Synopsis:
// mkdir [-m mode] [-v] [-p] DIRECTORY...
//
// Options:
// -m: make all needed directories in the path
// -v: directory mode (ex: 666)
// -p: print each directory as it is made
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
)
const (
cmd = "mkdir [-m mode] [-v] [-p] <directory> [more directories]"
DefaultCreationMode = 0777
StickyBit = 01000
SgidBit = 02000
SuidBit = 04000
)
var (
mode = flag.String("m", "", "Directory mode")
mkall = flag.Bool("p", false, "Make all needed directories in the path")
verbose = flag.Bool("v", false, "Print each directory as it is made")
f = os.Mkdir
)
func init() {
// Usage Definition
defUsage := flag.Usage
flag.Usage = func() {
os.Args[0] = cmd
defUsage()
}
}
func main() {
flag.Parse()
if len(flag.Args()) < 1 {
flag.Usage()
os.Exit(1)
}
if *mkall {
f = os.MkdirAll
}
// Get Correct Creation Mode
var m uint64
var err error
if *mode == "" {
m = DefaultCreationMode
} else {
m, err = strconv.ParseUint(*mode, 8, 32)
if err != nil || m > 07777 {
log.Fatalf("invalid mode '%s'", *mode)
}
}
createMode := os.FileMode(m)
if m&StickyBit != 0 {
createMode |= os.ModeSticky
}
if m&SgidBit != 0 {
createMode |= os.ModeSetgid
}
if m&SuidBit != 0 {
createMode |= os.ModeSetuid
}
for _, name := range flag.Args() {
if err := f(name, createMode); err != nil {
log.Printf("%v: %v\n", name, err)
continue
}
if *verbose {
fmt.Printf("%v\n", name)
}
if *mode != "" {
os.Chmod(name, createMode)
}
}
}