1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-27 01:33:28 +02:00
ferret/pkg/stdlib/io/fs/write.go

160 lines
3.4 KiB
Go
Raw Normal View History

2019-10-21 17:26:49 +02:00
package fs
import (
"context"
"os"
"sort"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
// Write writes the given data into the file.
2019-10-21 17:30:04 +02:00
// @params path (String) - path to file to write into.
2019-10-21 17:26:49 +02:00
// @params data (Binary) - data to write.
// @params params (Object) optional - additional parameters:
// * mode (String):
// * x - Exclusive: returns an error if the file exist. It can be
// combined with other modes
// * a - Append: will create a file if the specified file does not exist
// * w - Write (Default): will create a file if the specified file does not exist
// @returns None
func Write(_ context.Context, args ...core.Value) (core.Value, error) {
err := validateRequiredWriteArgs(args)
if err != nil {
return values.None, err
}
fpath := args[0].String()
data := args[1].(values.Binary)
params := defaultParams
if len(args) == 3 {
params, err = parseParams(args[2])
if err != nil {
return values.None, core.Error(
err,
"parse `params` argument",
)
}
}
// 0222 - write only permissions
2019-10-21 18:02:44 +02:00
file, err := os.OpenFile(fpath, params.ModeFlag, 0222)
2019-10-21 17:26:49 +02:00
if err != nil {
return values.None, core.Error(err, "open file")
}
defer file.Close()
_, err = file.Write(data)
if err != nil {
return values.None, core.Error(err, "write file")
}
return values.None, nil
}
func validateRequiredWriteArgs(args []core.Value) error {
err := core.ValidateArgs(args, 2, 3)
if err != nil {
return core.Error(err, "validate arguments number")
}
pairs := []core.PairValueType{
2019-10-21 19:11:47 +02:00
core.NewPairValueType(args[0], types.String),
core.NewPairValueType(args[1], types.Binary),
2019-10-21 17:26:49 +02:00
}
err = core.ValidateValueTypePairs(pairs...)
if err != nil {
return core.Error(err, "validate arguments")
}
return nil
}
2019-10-21 18:02:44 +02:00
// parsedParams contains parsed additional parameters.
type parsedParams struct {
ModeFlag int
2019-10-21 17:26:49 +02:00
}
2019-10-21 18:02:44 +02:00
var defaultParams = parsedParams{
// the same as `w`
ModeFlag: os.O_WRONLY | os.O_CREATE | os.O_TRUNC,
2019-10-21 17:26:49 +02:00
}
2019-10-21 18:02:44 +02:00
func parseParams(value core.Value) (parsedParams, error) {
2019-10-21 17:26:49 +02:00
obj, ok := value.(*values.Object)
if !ok {
2019-10-21 18:02:44 +02:00
return parsedParams{}, core.Error(
2019-10-21 17:26:49 +02:00
core.ErrInvalidArgument,
"value should be an object",
)
}
params := defaultParams
modestr, exists := obj.Get(values.NewString("mode"))
if exists {
2019-10-21 18:02:44 +02:00
flag, err := parseWriteMode(modestr.String())
if err != nil {
return parsedParams{}, core.Error(
core.ErrInvalidArgument,
"parse write mode",
)
}
params.ModeFlag = flag
2019-10-21 17:26:49 +02:00
}
return params, nil
}
func parseWriteMode(s string) (int, error) {
letters := []rune(s)
count := len(letters)
if count == 0 || count > 2 {
return -1, core.Errorf(
core.ErrInvalidArgument,
"must be from 1 to 2 mode letters, got `%d`", count,
)
}
// sort letters for more convenient work with it
sort.Slice(letters, func(i, j int) bool { return letters[i] < letters[j] })
// minimum flag for writing to file
flag := os.O_WRONLY | os.O_CREATE
if count == 2 {
// since letter is sorted, `x` will always be the letters[1]
if letters[1] != 'x' {
return -1, core.Errorf(
core.ErrInvalidArgument,
"invalid mode `%s`", s,
)
}
flag |= os.O_EXCL
}
switch letters[0] {
case 'a':
flag |= os.O_APPEND
case 'w':
flag |= os.O_TRUNC
default:
2019-10-21 17:35:35 +02:00
return -1, core.Errorf(
2019-10-21 17:26:49 +02:00
core.ErrInvalidArgument,
"invalid mode `%s`", s,
)
}
return flag, nil
}