Skip to content

preserve_times in get function does not perserve mtime #136

@nagyszblcs

Description

@nagyszblcs

We were using the scp library to retrieve some files and it was important for us to preserve the timestamp.

We have seen that even though we use the get() function with preserve_times=True the atime will be preserved, however the mtime will not and it will be the timestamp of when the file was copied.

We don't know exactly why, but we found that the issue is that the os.utime() call, which sets the atime/mtime for the file is called before the file is closed. When the file is closed, the OS then updates the mtime of the file to the current timestamp.

in _recv_file function (Line 459):

        file_hdl.truncate()
        try:
            os.utime(path, self._utime)
            self._utime = None
            os.chmod(path, mode)
            # should we notify the other end?
        finally:
            file_hdl.close()
        # '\x00' confirmation sent in _recv_all

The solution we found, was the close the file and then set the time and the permissions. This fixed the issue for us.

        file_hdl.truncate()
        file_hdl.close()
        try:
            os.utime(path, self._utime)
            self._utime = None
            os.chmod(path, mode)
            # should we notify the other end?
        finally:
            pass
        # '\x00' confirmation sent in _recv_all

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions