2017-11-20 12:16:05 +01:00
|
|
|
package touch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2017-11-20 12:16:05 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2019-10-11 17:55:04 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/object"
|
2017-11-20 12:16:05 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
notCreateNewFile bool
|
|
|
|
timeAsArgument string
|
2020-03-17 13:28:23 +01:00
|
|
|
localTime bool
|
2017-11-20 12:16:05 +01:00
|
|
|
)
|
|
|
|
|
2020-06-10 12:00:25 +02:00
|
|
|
const (
|
|
|
|
defaultLayout string = "060102"
|
|
|
|
layoutDateWithTime = "2006-01-02T15:04:05"
|
|
|
|
layoutDateWithTimeNano = "2006-01-02T15:04:05.999999999"
|
|
|
|
)
|
2017-11-20 12:16:05 +01:00
|
|
|
|
|
|
|
func init() {
|
2019-10-11 17:58:11 +02:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2019-10-11 17:55:04 +02:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
|
|
|
flags.BoolVarP(cmdFlags, ¬CreateNewFile, "no-create", "C", false, "Do not create the file if it does not exist.")
|
2020-03-17 13:28:23 +01:00
|
|
|
flags.StringVarP(cmdFlags, &timeAsArgument, "timestamp", "t", "", "Use specified time instead of the current time of day.")
|
|
|
|
flags.BoolVarP(cmdFlags, &localTime, "localtime", "", false, "Use localtime for timestamp, not UTC.")
|
2017-11-20 12:16:05 +01:00
|
|
|
}
|
|
|
|
|
2019-10-11 17:58:11 +02:00
|
|
|
var commandDefinition = &cobra.Command{
|
2017-11-20 12:16:05 +01:00
|
|
|
Use: "touch remote:path",
|
|
|
|
Short: `Create new file or change file modification time.`,
|
2020-03-17 13:28:23 +01:00
|
|
|
Long: `
|
|
|
|
Set the modification time on object(s) as specified by remote:path to
|
|
|
|
have the current time.
|
|
|
|
|
|
|
|
If remote:path does not exist then a zero sized object will be created
|
|
|
|
unless the --no-create flag is provided.
|
|
|
|
|
|
|
|
If --timestamp is used then it will set the modification time to that
|
|
|
|
time instead of the current time. Times may be specified as one of:
|
|
|
|
|
|
|
|
- 'YYMMDD' - eg. 17.10.30
|
|
|
|
- 'YYYY-MM-DDTHH:MM:SS' - eg. 2006-01-02T15:04:05
|
2020-06-10 12:00:25 +02:00
|
|
|
- 'YYYY-MM-DDTHH:MM:SS.SSS' - eg. 2006-01-02T15:04:05.123456789
|
2020-03-17 13:28:23 +01:00
|
|
|
|
|
|
|
Note that --timestamp is in UTC if you want local time then add the
|
|
|
|
--localtime flag.
|
|
|
|
`,
|
2017-11-20 12:16:05 +01:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
fsrc, srcFileName := cmd.NewFsDstFile(args)
|
|
|
|
cmd.Run(true, false, command, func() error {
|
2019-06-17 10:34:30 +02:00
|
|
|
return Touch(context.Background(), fsrc, srcFileName)
|
2017-11-20 12:16:05 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
//Touch create new file or change file modification time.
|
2020-03-17 13:28:23 +01:00
|
|
|
func Touch(ctx context.Context, fsrc fs.Fs, srcFileName string) (err error) {
|
2017-11-20 12:16:05 +01:00
|
|
|
timeAtr := time.Now()
|
|
|
|
if timeAsArgument != "" {
|
|
|
|
layout := defaultLayout
|
|
|
|
if len(timeAsArgument) == len(layoutDateWithTime) {
|
|
|
|
layout = layoutDateWithTime
|
2020-06-10 12:00:25 +02:00
|
|
|
} else if len(timeAsArgument) > len(layoutDateWithTime) {
|
|
|
|
layout = layoutDateWithTimeNano
|
2017-11-20 12:16:05 +01:00
|
|
|
}
|
2020-03-17 13:28:23 +01:00
|
|
|
var timeAtrFromFlags time.Time
|
|
|
|
if localTime {
|
|
|
|
timeAtrFromFlags, err = time.ParseInLocation(layout, timeAsArgument, time.Local)
|
|
|
|
} else {
|
|
|
|
timeAtrFromFlags, err = time.Parse(layout, timeAsArgument)
|
|
|
|
}
|
2017-11-20 12:16:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to parse date/time argument")
|
|
|
|
}
|
|
|
|
timeAtr = timeAtrFromFlags
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
file, err := fsrc.NewObject(ctx, srcFileName)
|
2017-11-20 12:16:05 +01:00
|
|
|
if err != nil {
|
|
|
|
if !notCreateNewFile {
|
|
|
|
var buffer []byte
|
2018-01-12 17:30:54 +01:00
|
|
|
src := object.NewStaticObjectInfo(srcFileName, timeAtr, int64(len(buffer)), true, nil, fsrc)
|
2019-06-17 10:34:30 +02:00
|
|
|
_, err = fsrc.Put(ctx, bytes.NewBuffer(buffer), src)
|
2017-11-20 12:16:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
err = file.SetModTime(ctx, timeAtr)
|
2017-11-20 12:16:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "touch: couldn't set mod time")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|