"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
Add touch command - fixes #1594
Jakub Tasiemski3cf7c61aa019
diff --git a/cmd/all/all.go b/cmd/all/all.goindex 4def69c75..7e52e4c1d 100644--- a/cmd/all/all.go+++ b/cmd/all/all.go@@ -43,6 +43,7 @@ import (_ "github.com/ncw/rclone/cmd/sha1sum"_ "github.com/ncw/rclone/cmd/size"_ "github.com/ncw/rclone/cmd/sync"+ _ "github.com/ncw/rclone/cmd/touch"_ "github.com/ncw/rclone/cmd/tree"_ "github.com/ncw/rclone/cmd/version")diff --git a/cmd/touch/touch.go b/cmd/touch/touch.gonew file mode 100644index 000000000..d3b455c97--- /dev/null+++ b/cmd/touch/touch.go@@ -0,0 +1,71 @@+package touch++import (+ "bytes"+ "time"++ "github.com/ncw/rclone/cmd"+ "github.com/ncw/rclone/fs"+ "github.com/pkg/errors"+ "github.com/spf13/cobra"+)++var (+ notCreateNewFile bool+ timeAsArgument string+)++const defaultLayout string = "060102"+const layoutDateWithTime = "2006-01-02T15:04:05"++func init() {+ cmd.Root.AddCommand(commandDefintion)+ flags := commandDefintion.Flags()+ flags.BoolVarP(¬CreateNewFile, "no-create", "C", false, "Do not create the file if it does not exist.")+ flags.StringVarP(&timeAsArgument, "timestamp", "t", "", "Change the modification times to the specified time instead of the current time of day. The argument is of the form 'YYMMDD' (ex. 17.10.30) or 'YYYY-MM-DDTHH:MM:SS' (ex. 2006-01-02T15:04:05)")+}++var commandDefintion = &cobra.Command{+ Use: "touch remote:path",+ Short: `Create new file or change file modification time.`,+ 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 {+ return Touch(fsrc, srcFileName)+ })+ },+}++//Touch create new file or change file modification time.+func Touch(fsrc fs.Fs, srcFileName string) error {+ timeAtr := time.Now()+ if timeAsArgument != "" {+ layout := defaultLayout+ if len(timeAsArgument) == len(layoutDateWithTime) {+ layout = layoutDateWithTime+ }+ timeAtrFromFlags, err := time.Parse(layout, timeAsArgument)+ if err != nil {+ return errors.Wrap(err, "failed to parse date/time argument")+ }+ timeAtr = timeAtrFromFlags+ }+ file, err := fsrc.NewObject(srcFileName)+ if err != nil {+ if !notCreateNewFile {+ var buffer []byte+ src := fs.NewStaticObjectInfo(srcFileName, timeAtr, int64(len(buffer)), true, nil, fsrc)+ _, err = fsrc.Put(bytes.NewBuffer(buffer), src)+ if err != nil {+ return err+ }+ }+ return nil+ }+ err = file.SetModTime(timeAtr)+ if err != nil {+ return errors.Wrap(err, "touch: couldn't set mod time")+ }+ return nil+}diff --git a/cmd/touch/touch_test.go b/cmd/touch/touch_test.gonew file mode 100644index 000000000..a87c87959--- /dev/null+++ b/cmd/touch/touch_test.go@@ -0,0 +1,61 @@+package touch++import (+ "testing"+ "time"++ "github.com/ncw/rclone/fs"+ "github.com/ncw/rclone/fstest"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"++ _ "github.com/ncw/rclone/local"+)++func TestTouch(t *testing.T) {+ fstest.Initialise()+ f, err := fs.NewFs("testfiles")+ err = Touch(f, "newFile")+ require.NoError(t, err)+ file, errFile := f.NewObject("newFile")+ require.NoError(t, errFile)+ err = file.Remove()+ require.NoError(t, err)++ notCreateNewFile = true+ err = Touch(f, "fileWithCflag")+ require.NoError(t, err)+ file, errFile = f.NewObject("fileWithCflag")+ require.Error(t, errFile)+ notCreateNewFile = false++ timeAsArgument = "060102"+ err = Touch(f, "oldFile")+ require.NoError(t, err)+ file, err = f.NewObject("oldFile")+ require.NoError(t, err)+ curretTime := time.Now()+ require.NoError(t, err)+ print(file.ModTime().Year() < curretTime.Year())+ assert.Equal(t, true, file.ModTime().Year() < curretTime.Year())+ err = file.Remove()+ require.NoError(t, err)++ timeAsArgument = "2006-01-02T15:04:05"+ err = Touch(f, "oldFile")+ require.NoError(t, err)+ file, err = f.NewObject("oldFile")+ require.NoError(t, err)+ assert.Equal(t, true, file.ModTime().Year() < curretTime.Year())++ timeAsArgument = ""+ err = Touch(f, "oldFile")+ require.NoError(t, err)+ file, err = f.NewObject("oldFile")+ require.NoError(t, err)+ timeBetween2007YearAndCurrent, errTime := time.Parse("060102", "121212")+ require.NoError(t, errTime)+ assert.Equal(t, true, file.ModTime().Year() > timeBetween2007YearAndCurrent.Year())+ err = file.Remove()+ require.NoError(t, err)+}