2022-08-28 11:21:57 +00:00
|
|
|
// Package touch provides the touch command.
|
2017-11-20 11:16:05 +00:00
|
|
|
package touch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-06-17 08:34:30 +00:00
|
|
|
"context"
|
2021-11-04 10:12:57 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-01-27 18:46:53 +00:00
|
|
|
"log"
|
2017-11-20 11:16:05 +00:00
|
|
|
"time"
|
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2019-10-11 15:55:04 +00:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2022-01-27 18:46:53 +00:00
|
|
|
"github.com/rclone/rclone/fs/fspath"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs/object"
|
2021-05-22 19:06:24 +00:00
|
|
|
"github.com/rclone/rclone/fs/operations"
|
2017-11-20 11:16:05 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
notCreateNewFile bool
|
|
|
|
timeAsArgument string
|
2020-03-17 12:28:23 +00:00
|
|
|
localTime bool
|
2021-05-22 19:06:24 +00:00
|
|
|
recursive bool
|
2017-11-20 11:16:05 +00:00
|
|
|
)
|
|
|
|
|
2020-06-10 10:00:25 +00:00
|
|
|
const (
|
|
|
|
defaultLayout string = "060102"
|
2021-05-22 19:06:24 +00:00
|
|
|
layoutDateWithTime string = "2006-01-02T15:04:05"
|
|
|
|
layoutDateWithTimeNano string = "2006-01-02T15:04:05.999999999"
|
2020-06-10 10:00:25 +00:00
|
|
|
)
|
2017-11-20 11:16:05 +00:00
|
|
|
|
|
|
|
func init() {
|
2019-10-11 15:58:11 +00:00
|
|
|
cmd.Root.AddCommand(commandDefinition)
|
2019-10-11 15:55:04 +00:00
|
|
|
cmdFlags := commandDefinition.Flags()
|
2021-08-16 09:30:01 +00:00
|
|
|
flags.BoolVarP(cmdFlags, ¬CreateNewFile, "no-create", "C", false, "Do not create the file if it does not exist (implied with --recursive)")
|
|
|
|
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")
|
|
|
|
flags.BoolVarP(cmdFlags, &recursive, "recursive", "R", false, "Recursively touch all files")
|
2017-11-20 11:16:05 +00:00
|
|
|
}
|
|
|
|
|
2019-10-11 15:58:11 +00:00
|
|
|
var commandDefinition = &cobra.Command{
|
2017-11-20 11:16:05 +00:00
|
|
|
Use: "touch remote:path",
|
|
|
|
Short: `Create new file or change file modification time.`,
|
2020-03-17 12:28:23 +00:00
|
|
|
Long: `
|
2021-05-22 19:06:24 +00:00
|
|
|
Set the modification time on file(s) as specified by remote:path to
|
2020-03-17 12:28:23 +00:00
|
|
|
have the current time.
|
|
|
|
|
2021-05-22 19:06:24 +00:00
|
|
|
If remote:path does not exist then a zero sized file will be created,
|
|
|
|
unless ` + "`--no-create`" + ` or ` + "`--recursive`" + ` is provided.
|
2020-03-17 12:28:23 +00:00
|
|
|
|
2021-05-22 19:06:24 +00:00
|
|
|
If ` + "`--recursive`" + ` is used then recursively sets the modification
|
|
|
|
time on all existing files that is found under the path. Filters are supported,
|
|
|
|
and you can test with the ` + "`--dry-run`" + ` or the ` + "`--interactive`" + ` flag.
|
|
|
|
|
|
|
|
If ` + "`--timestamp`" + ` is used then sets the modification time to that
|
2020-03-17 12:28:23 +00:00
|
|
|
time instead of the current time. Times may be specified as one of:
|
|
|
|
|
2020-10-13 21:49:58 +00:00
|
|
|
- 'YYMMDD' - e.g. 17.10.30
|
|
|
|
- 'YYYY-MM-DDTHH:MM:SS' - e.g. 2006-01-02T15:04:05
|
|
|
|
- 'YYYY-MM-DDTHH:MM:SS.SSS' - e.g. 2006-01-02T15:04:05.123456789
|
2020-03-17 12:28:23 +00:00
|
|
|
|
2021-05-22 19:06:24 +00:00
|
|
|
Note that value of ` + "`--timestamp`" + ` is in UTC. If you want local time
|
|
|
|
then add the ` + "`--localtime`" + ` flag.
|
2020-03-17 12:28:23 +00:00
|
|
|
`,
|
2022-11-26 22:40:49 +00:00
|
|
|
Annotations: map[string]string{
|
|
|
|
"versionIntroduced": "v1.39",
|
|
|
|
},
|
2017-11-20 11:16:05 +00:00
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
2022-01-27 18:46:53 +00:00
|
|
|
f, remote := newFsDst(args)
|
2017-11-20 11:16:05 +00:00
|
|
|
cmd.Run(true, false, command, func() error {
|
2022-01-27 18:46:53 +00:00
|
|
|
return Touch(context.Background(), f, remote)
|
2017-11-20 11:16:05 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-01-27 18:46:53 +00:00
|
|
|
// newFsDst creates a new dst fs from the arguments.
|
|
|
|
//
|
|
|
|
// The returned fs will never point to a file. It will point to the
|
|
|
|
// parent directory of specified path, and is returned together with
|
|
|
|
// the basename of file or directory, except if argument is only a
|
|
|
|
// remote name. Similar to cmd.NewFsDstFile, but without raising fatal
|
|
|
|
// when name of file or directory is empty (e.g. "remote:" or "remote:path/").
|
|
|
|
func newFsDst(args []string) (f fs.Fs, remote string) {
|
|
|
|
root, remote, err := fspath.Split(args[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Parsing %q failed: %v", args[0], err)
|
|
|
|
}
|
|
|
|
if root == "" {
|
|
|
|
root = "."
|
|
|
|
}
|
|
|
|
f = cmd.NewFsDir([]string{root})
|
|
|
|
return f, remote
|
|
|
|
}
|
|
|
|
|
2021-05-22 19:06:24 +00:00
|
|
|
// parseTimeArgument parses a timestamp string according to specific layouts
|
|
|
|
func parseTimeArgument(timeString string) (time.Time, error) {
|
|
|
|
layout := defaultLayout
|
|
|
|
if len(timeString) == len(layoutDateWithTime) {
|
|
|
|
layout = layoutDateWithTime
|
|
|
|
} else if len(timeString) > len(layoutDateWithTime) {
|
|
|
|
layout = layoutDateWithTimeNano
|
|
|
|
}
|
|
|
|
if localTime {
|
|
|
|
return time.ParseInLocation(layout, timeString, time.Local)
|
|
|
|
}
|
|
|
|
return time.Parse(layout, timeString)
|
|
|
|
}
|
|
|
|
|
|
|
|
// timeOfTouch returns the time value set on files
|
|
|
|
func timeOfTouch() (time.Time, error) {
|
|
|
|
var t time.Time
|
2017-11-20 11:16:05 +00:00
|
|
|
if timeAsArgument != "" {
|
2021-05-22 19:06:24 +00:00
|
|
|
var err error
|
|
|
|
if t, err = parseTimeArgument(timeAsArgument); err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
return t, fmt.Errorf("failed to parse timestamp argument: %w", err)
|
2020-03-17 12:28:23 +00:00
|
|
|
}
|
2021-05-22 19:06:24 +00:00
|
|
|
} else {
|
|
|
|
t = time.Now()
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createEmptyObject creates an empty object (file) with specified timestamp
|
|
|
|
func createEmptyObject(ctx context.Context, remote string, modTime time.Time, f fs.Fs) error {
|
|
|
|
var buffer []byte
|
|
|
|
src := object.NewStaticObjectInfo(remote, modTime, int64(len(buffer)), true, nil, f)
|
|
|
|
_, err := f.Put(ctx, bytes.NewBuffer(buffer), src)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Touch create new file or change file modification time.
|
2022-01-27 18:46:53 +00:00
|
|
|
func Touch(ctx context.Context, f fs.Fs, remote string) error {
|
2021-05-22 19:06:24 +00:00
|
|
|
t, err := timeOfTouch()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-11-20 11:16:05 +00:00
|
|
|
}
|
2021-05-22 19:06:24 +00:00
|
|
|
fs.Debugf(nil, "Touch time %v", t)
|
2022-01-27 18:46:53 +00:00
|
|
|
file, err := f.NewObject(ctx, remote)
|
2017-11-20 11:16:05 +00:00
|
|
|
if err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
if errors.Is(err, fs.ErrorObjectNotFound) {
|
2022-01-27 18:46:53 +00:00
|
|
|
// Touching non-existant path, possibly creating it as new file
|
|
|
|
if remote == "" {
|
|
|
|
fs.Logf(f, "Not touching empty directory")
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-22 19:06:24 +00:00
|
|
|
if notCreateNewFile {
|
2022-08-14 02:56:32 +00:00
|
|
|
fs.Logf(f, "Not touching nonexistent file due to --no-create")
|
2021-05-22 19:06:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if recursive {
|
2022-01-27 18:46:53 +00:00
|
|
|
// For consistency, --recursive never creates new files.
|
2022-08-14 02:56:32 +00:00
|
|
|
fs.Logf(f, "Not touching nonexistent file due to --recursive")
|
2021-05-22 19:06:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if operations.SkipDestructive(ctx, f, "touch (create)") {
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-27 18:46:53 +00:00
|
|
|
fs.Debugf(f, "Touching (creating) %q", remote)
|
|
|
|
if err = createEmptyObject(ctx, remote, t, f); err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("failed to touch (create): %w", err)
|
2017-11-20 11:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-04 10:12:57 +00:00
|
|
|
if errors.Is(err, fs.ErrorIsDir) {
|
2022-01-27 18:46:53 +00:00
|
|
|
// Touching existing directory
|
2021-05-22 19:06:24 +00:00
|
|
|
if recursive {
|
2022-01-27 18:46:53 +00:00
|
|
|
fs.Debugf(f, "Touching recursively files in directory %q", remote)
|
|
|
|
return operations.TouchDir(ctx, f, remote, t, true)
|
2021-05-22 19:06:24 +00:00
|
|
|
}
|
2022-01-27 18:46:53 +00:00
|
|
|
fs.Debugf(f, "Touching non-recursively files in directory %q", remote)
|
|
|
|
return operations.TouchDir(ctx, f, remote, t, false)
|
2021-05-22 19:06:24 +00:00
|
|
|
}
|
|
|
|
return err
|
2017-11-20 11:16:05 +00:00
|
|
|
}
|
2021-05-22 19:06:24 +00:00
|
|
|
// Touch single existing file
|
2022-01-27 18:46:53 +00:00
|
|
|
if !operations.SkipDestructive(ctx, remote, "touch") {
|
|
|
|
fs.Debugf(f, "Touching %q", remote)
|
2021-05-22 19:06:24 +00:00
|
|
|
err = file.SetModTime(ctx, t)
|
|
|
|
if err != nil {
|
2021-11-04 10:12:57 +00:00
|
|
|
return fmt.Errorf("failed to touch: %w", err)
|
2021-05-22 19:06:24 +00:00
|
|
|
}
|
2017-11-20 11:16:05 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|