2020-04-16 14:33:46 +02:00
|
|
|
package vfstest
|
2017-03-23 20:41:21 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2018-03-08 19:08:12 +01:00
|
|
|
"runtime"
|
2017-03-23 20:41:21 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-04-16 14:33:46 +02:00
|
|
|
"github.com/rclone/rclone/vfs"
|
2017-03-23 20:41:21 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-05-08 19:05:12 +02:00
|
|
|
// TestFileModTime tests mod times on files
|
2017-03-23 20:41:21 +01:00
|
|
|
func TestFileModTime(t *testing.T) {
|
|
|
|
run.skipIfNoFUSE(t)
|
|
|
|
|
|
|
|
run.createFile(t, "file", "123")
|
|
|
|
|
2018-08-04 12:16:43 +02:00
|
|
|
mtime := time.Date(2012, time.November, 18, 17, 32, 31, 0, time.UTC)
|
2020-04-16 14:33:46 +02:00
|
|
|
err := run.os.Chtimes(run.path("file"), mtime, mtime)
|
2017-03-23 20:41:21 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-16 14:33:46 +02:00
|
|
|
info, err := run.os.Stat(run.path("file"))
|
2017-03-23 20:41:21 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// avoid errors because of timezone differences
|
|
|
|
assert.Equal(t, info.ModTime().Unix(), mtime.Unix())
|
|
|
|
|
|
|
|
run.rm(t, "file")
|
|
|
|
}
|
2017-03-30 20:58:11 +02:00
|
|
|
|
2020-04-16 14:33:46 +02:00
|
|
|
// run.os.Create without opening for write too
|
|
|
|
func osCreate(name string) (vfs.OsFiler, error) {
|
|
|
|
return run.os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
2017-11-07 18:27:53 +01:00
|
|
|
}
|
|
|
|
|
2020-04-16 14:33:46 +02:00
|
|
|
// run.os.Create with append
|
|
|
|
func osAppend(name string) (vfs.OsFiler, error) {
|
|
|
|
return run.os.OpenFile(name, os.O_WRONLY|os.O_APPEND, 0666)
|
2019-10-15 02:57:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-08 19:05:12 +02:00
|
|
|
// TestFileModTimeWithOpenWriters tests mod time on open files
|
2017-03-30 20:58:11 +02:00
|
|
|
func TestFileModTimeWithOpenWriters(t *testing.T) {
|
|
|
|
run.skipIfNoFUSE(t)
|
2018-03-08 19:08:12 +01:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Skipping test on Windows")
|
|
|
|
}
|
2017-03-30 20:58:11 +02:00
|
|
|
|
2018-08-04 12:16:43 +02:00
|
|
|
mtime := time.Date(2012, time.November, 18, 17, 32, 31, 0, time.UTC)
|
2017-03-30 20:58:11 +02:00
|
|
|
filepath := run.path("cp-archive-test")
|
|
|
|
|
2017-11-07 18:27:53 +01:00
|
|
|
f, err := osCreate(filepath)
|
2017-03-30 20:58:11 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = f.Write([]byte{104, 105})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-16 14:33:46 +02:00
|
|
|
err = run.os.Chtimes(filepath, mtime, mtime)
|
2017-03-30 20:58:11 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-11-18 16:50:38 +01:00
|
|
|
run.waitForWriters()
|
|
|
|
|
2020-04-16 14:33:46 +02:00
|
|
|
info, err := run.os.Stat(filepath)
|
2017-03-30 20:58:11 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// avoid errors because of timezone differences
|
|
|
|
assert.Equal(t, info.ModTime().Unix(), mtime.Unix())
|
|
|
|
|
|
|
|
run.rm(t, "cp-archive-test")
|
|
|
|
}
|