Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Touch files after moving #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pkg/filesystem/directory_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package filesystem

import (
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -519,6 +520,28 @@ func Rename(
)
}

// Touch updates the access and modification times of the given path to the
// current time.
//
// This function does not create non-existent files.
func Touch(directory *Directory, nameOrPath string) error {
var filePath string

// If a target directory has been provided, then verify that the target name
// is a valid name and not a path.
if directory != nil {
if err := ensureValidName(nameOrPath); err != nil {
return errors.Wrap(err, "target name invalid")
}
filePath = filepath.Join(directory.file.Name(), nameOrPath)
} else {
filePath = nameOrPath
}

now := time.Now()
return os.Chtimes(filePath, now, now)
}

// IsCrossDeviceError checks whether or not an error returned from rename
// represents a cross-device error.
func IsCrossDeviceError(err error) bool {
Expand Down
23 changes: 23 additions & 0 deletions pkg/filesystem/directory_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"
"syscall"
"time"

"github.com/pkg/errors"

Expand Down Expand Up @@ -521,6 +522,28 @@ func Rename(
return os.Rename(sourceNameOrPath, targetNameOrPath)
}

// Touch updates the access and modification times of the given path to the
// current time.
//
// This function does not create non-existent files.
func Touch(directory *Directory, nameOrPath string) error {
var filePath string

// If a target directory has been provided, then verify that the target name
// is a valid name and not a path.
if directory != nil {
if err := ensureValidName(nameOrPath); err != nil {
return errors.Wrap(err, "target name invalid")
}
filePath = filepath.Join(directory.file.Name(), nameOrPath)
} else {
filePath = nameOrPath
}

now := time.Now()
return os.Chtimes(filePath, now, now)
}

const (
// _ERROR_NOT_SAME_DEVICE is the error code returned by MoveFileEx on
// Windows when attempting to move a file across devices. This can actually
Expand Down
8 changes: 8 additions & 0 deletions pkg/synchronization/core/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,10 @@ func (t *transitioner) findAndMoveStagedFileIntoPlace(
// Attempt to atomically rename the file. If we succeed, we're done.
renameErr := filesystem.Rename(nil, stagedPath, parent, name)
if renameErr == nil {
if true { // TODO: Only touch when configured to
// Ignore errors, since updating access and modification times is not mandatory
filesystem.Touch(parent, name)
}
return nil
}

Expand Down Expand Up @@ -612,6 +616,10 @@ func (t *transitioner) findAndMoveStagedFileIntoPlace(
// there's not much we can or need to do about them at this point.
os.Remove(stagedPath)

if true { // TODO: Only touch when configured to
filesystem.Touch(parent, name)
}

// Success.
return nil
}
Expand Down