This commit is contained in:
bel
2020-01-13 03:37:51 +00:00
commit c8eb52f9ba
2023 changed files with 702080 additions and 0 deletions

72
.rclone_repo/cmd/touch/touch.go Executable file
View File

@@ -0,0 +1,72 @@
package touch
import (
"bytes"
"time"
"github.com/ncw/rclone/cmd"
"github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fs/object"
"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(&notCreateNewFile, "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 := object.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
}

View File

@@ -0,0 +1,119 @@
package touch
import (
"testing"
"time"
"github.com/ncw/rclone/fs"
"github.com/ncw/rclone/fstest"
"github.com/stretchr/testify/require"
_ "github.com/ncw/rclone/backend/local"
)
var (
t1 = fstest.Time("2017-02-03T04:05:06.499999999Z")
)
func checkFile(t *testing.T, r fs.Fs, path string, content string) {
layout := defaultLayout
if len(timeAsArgument) == len(layoutDateWithTime) {
layout = layoutDateWithTime
}
timeAtrFromFlags, err := time.Parse(layout, timeAsArgument)
require.NoError(t, err)
file1 := fstest.NewItem(path, content, timeAtrFromFlags)
fstest.CheckItems(t, r, file1)
}
// TestMain drives the tests
func TestMain(m *testing.M) {
fstest.TestMain(m)
}
func TestTouchOneFile(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
err := Touch(r.Fremote, "newFile")
require.NoError(t, err)
_, err = r.Fremote.NewObject("newFile")
require.NoError(t, err)
}
func TestTouchWithNoCreateFlag(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
notCreateNewFile = true
err := Touch(r.Fremote, "newFile")
require.NoError(t, err)
_, err = r.Fremote.NewObject("newFile")
require.Error(t, err)
notCreateNewFile = false
}
func TestTouchWithTimestamp(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
timeAsArgument = "060102"
srcFileName := "oldFile"
err := Touch(r.Fremote, srcFileName)
require.NoError(t, err)
checkFile(t, r.Fremote, srcFileName, "")
}
func TestTouchWithLognerTimestamp(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
timeAsArgument = "2006-01-02T15:04:05"
srcFileName := "oldFile"
err := Touch(r.Fremote, srcFileName)
require.NoError(t, err)
checkFile(t, r.Fremote, srcFileName, "")
}
func TestTouchUpdateTimestamp(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
srcFileName := "a"
content := "aaa"
file1 := r.WriteObject(srcFileName, content, t1)
fstest.CheckItems(t, r.Fremote, file1)
timeAsArgument = "121212"
err := Touch(r.Fremote, "a")
require.NoError(t, err)
checkFile(t, r.Fremote, srcFileName, content)
}
func TestTouchUpdateTimestampWithCFlag(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
srcFileName := "a"
content := "aaa"
file1 := r.WriteObject(srcFileName, content, t1)
fstest.CheckItems(t, r.Fremote, file1)
notCreateNewFile = true
timeAsArgument = "121212"
err := Touch(r.Fremote, "a")
require.NoError(t, err)
checkFile(t, r.Fremote, srcFileName, content)
notCreateNewFile = false
}
func TestTouchCreateMultipleDirAndFile(t *testing.T) {
r := fstest.NewRun(t)
defer r.Finalise()
longPath := "a/b/c.txt"
err := Touch(r.Fremote, longPath)
require.NoError(t, err)
file1 := fstest.NewItem("a/b/c.txt", "", t1)
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1}, []string{"a", "a/b"}, fs.ModTimeNotSupported)
}