diff --git a/CHANGELOG.md b/CHANGELOG.md index b3d1b77..72e6dd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ # Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [0.5.1] - 2021-11-18 +## [0.6.0] - 2021-11-18 ### Added + - mkfifo command. - rm command. - rmdir command. ### Changed diff --git a/README.md b/README.md index ab44b5d..d368af3 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ $ sudo ./installer.sh | ischroot| Detect if running in a chroot| | mbsh | Mimix Box Shell (In development)| | mkdir | Make directories| +| mkfifo | Make FIFO (Named pipe)| | mv | Rename SOURCE to DESTINATION, or move SOURCE(s) to DIRECTORY| | path | Manipulate filename path| | rm | Remove file(s) or directory(s)| diff --git a/cmd/mimixbox/main.go b/cmd/mimixbox/main.go index a35f32d..a61186d 100644 --- a/cmd/mimixbox/main.go +++ b/cmd/mimixbox/main.go @@ -50,7 +50,7 @@ type options struct { var osExit = os.Exit -const version = "0.5.1" +const version = "0.6.0" const ( ExitSuccess int = iota // 0 diff --git a/docs/man/mimixbox/en/mimixbox.1.md b/docs/man/mimixbox/en/mimixbox.1.md index 48bd9a3..b77976d 100644 --- a/docs/man/mimixbox/en/mimixbox.1.md +++ b/docs/man/mimixbox/en/mimixbox.1.md @@ -20,7 +20,7 @@ commands. # Command(applet)list **Common unix commands(applets)** -cat, chroot, echo, false, ischroot, mkdir, mv, rm, rmdir, touch, true, which +cat, chroot, echo, false, ischroot, mkdir, mkfifo, mv, rm, rmdir, touch, true, which **MimixBox Original commands(applets)** fakemovie, ghrdc, mbsh(sh), path, serial diff --git a/docs/man/mimixbox/ja/mimixbox.1.md b/docs/man/mimixbox/ja/mimixbox.1.md index 2d68458..c8beeb9 100644 --- a/docs/man/mimixbox/ja/mimixbox.1.md +++ b/docs/man/mimixbox/ja/mimixbox.1.md @@ -18,7 +18,7 @@ Coreutils等が提供する基本的な内容から実験的なコマンドま # コマンド(applet)一覧 **一般的なUnixコマンド(applet)** -cat, chroot, echo, false, ischroot, mkdir, mv, rm, rmdir, touch, true, which +cat, chroot, echo, false, ischroot, mkdir, mkfifo, mv, rm, rmdir, touch, true, which **MimixBoxオリジナルコマンド(applet)** fakemovie, ghrdc, mbsh(sh), path, serial diff --git a/internal/applets/applet.go b/internal/applets/applet.go index 9948725..140ecbe 100644 --- a/internal/applets/applet.go +++ b/internal/applets/applet.go @@ -20,6 +20,7 @@ import ( "fmt" "go/doc" "mimixbox/internal/applets/fileutils/mkdir" + "mimixbox/internal/applets/fileutils/mkfifo" "mimixbox/internal/applets/fileutils/mv" "mimixbox/internal/applets/fileutils/rm" "mimixbox/internal/applets/fileutils/rmdir" @@ -61,6 +62,7 @@ func init() { "ischroot": {ischroot.Run, "Detect if running in a chroot"}, "mbsh": {mbsh.Run, "Mimix Box Shell"}, "mkdir": {mkdir.Run, "Make directories"}, + "mkfifo": {mkfifo.Run, "Make FIFO (named pipe)"}, "mv": {mv.Run, "Rename SOURCE to DESTINATION, or move SOURCE(s) to DIRECTORY"}, "path": {path.Run, "Manipulate filename path"}, "rm": {rm.Run, "Remove file(s) or directory(s)"}, diff --git a/internal/applets/fileutils/mkfifo/mkfifo.go b/internal/applets/fileutils/mkfifo/mkfifo.go new file mode 100644 index 0000000..4f12106 --- /dev/null +++ b/internal/applets/fileutils/mkfifo/mkfifo.go @@ -0,0 +1,107 @@ +// +// mimixbox/internal/applets/fileutils/mkfifo/mkfifo.go +// +// Copyright 2021 Naohiro CHIKAMATSU +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package mkfifo + +import ( + "errors" + "fmt" + mb "mimixbox/internal/lib" + "os" + "syscall" + + "github.com/jessevdk/go-flags" +) + +const cmdName string = "mkfifo" + +const version = "1.0.0" + +var osExit = os.Exit + +// Exit code +const ( + ExitSuccess int = iota // 0 + ExitFailuer +) + +type options struct { + Version bool `short:"v" long:"version" description:"Show mkfifo command version"` +} + +func Run() (int, error) { + var opts options + var args []string + var err error + var status int + + if args, err = parseArgs(&opts); err != nil { + return ExitFailuer, nil + } + + // If an error occurs even once, the process is interrupted. + // It behaves differently from Coreutils. + for _, path := range args { + if mb.Exists(path) { + return ExitFailuer, errors.New("Can't make " + path + ": already exist") + } + if err := syscall.Mkfifo(path, 0666); err != nil { + return ExitFailuer, err + } + } + return status, nil +} + +func parseArgs(opts *options) ([]string, error) { + p := initParser(opts) + + args, err := p.Parse() + if err != nil { + return nil, err + } + + if opts.Version { + showVersion() + osExit(ExitSuccess) + } + + if !isValidArgNr(args) { + showHelp(p) + osExit(ExitFailuer) + } + return args, nil +} + +func initParser(opts *options) *flags.Parser { + parser := flags.NewParser(opts, flags.Default) + parser.Name = cmdName + parser.Usage = "[OPTIONS] FILE_PATH" + + return parser +} + +func isValidArgNr(args []string) bool { + return len(args) >= 1 +} + +func showVersion() { + description := cmdName + " version " + version + " (under Apache License verison 2.0)\n" + fmt.Print(description) +} + +func showHelp(p *flags.Parser) { + p.WriteHelp(os.Stdout) +}