Skip to content

Commit 34d26bc

Browse files
author
atollk
committed
Removed preservation of access time in copy/move/mirror functions as changing the access time itself was an access.
1 parent f016533 commit 34d26bc

File tree

8 files changed

+41
-23
lines changed

8 files changed

+41
-23
lines changed

fs/_bulk.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313

1414
from .copy import copy_file_internal
1515
from .errors import BulkCopyFailed
16-
from .tools import copy_file_data
1716

1817
if typing.TYPE_CHECKING:
1918
from .base import FS
2019
from types import TracebackType
21-
from typing import IO, List, Optional, Text, Type
20+
from typing import List, Optional, Text, Type
2221

2322

2423
class _Worker(threading.Thread):

fs/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import six
2323

2424
from . import copy, errors, fsencode, iotools, move, tools, walk, wildcard
25-
from .copy import copy_metadata
25+
from .copy import copy_cmtime
2626
from .glob import BoundGlobber
2727
from .mode import validate_open_mode
2828
from .path import abspath, join, normpath
@@ -419,7 +419,7 @@ def copy(
419419
with closing(self.open(src_path, "rb")) as read_file:
420420
# FIXME(@althonos): typing complains because open return IO
421421
self.upload(dst_path, read_file) # type: ignore
422-
copy_metadata(self, src_path, self, dst_path)
422+
copy_cmtime(self, src_path, self, dst_path)
423423

424424
def copydir(
425425
self,
@@ -1130,8 +1130,8 @@ def move(self, src_path, dst_path, overwrite=False, preserve_time=False):
11301130
with self.open(src_path, "rb") as read_file:
11311131
# FIXME(@althonos): typing complains because open return IO
11321132
self.upload(dst_path, read_file) # type: ignore
1133+
copy_cmtime(self, src_path, self, dst_path)
11331134
self.remove(src_path)
1134-
copy_metadata(self, src_path, self, dst_path)
11351135

11361136
def open(
11371137
self,

fs/copy.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def copy_file(
159159
else:
160160
with _src_fs.openbin(src_path) as read_file:
161161
_dst_fs.upload(dst_path, read_file)
162-
copy_metadata(_src_fs, src_path, _dst_fs, dst_path)
162+
copy_cmtime(_src_fs, src_path, _dst_fs, dst_path)
163163

164164

165165
def copy_file_internal(
@@ -199,7 +199,7 @@ def copy_file_internal(
199199
with src_fs.openbin(src_path) as read_file:
200200
dst_fs.upload(dst_path, read_file)
201201

202-
copy_metadata(src_fs, src_path, dst_fs, dst_path)
202+
copy_cmtime(src_fs, src_path, dst_fs, dst_path)
203203

204204

205205
def copy_file_if_newer(
@@ -445,14 +445,14 @@ def dst():
445445
on_copy(_src_fs, dir_path, _dst_fs, copy_path)
446446

447447

448-
def copy_metadata(
448+
def copy_cmtime(
449449
src_fs, # type: Union[FS, Text]
450450
src_path, # type: Text
451451
dst_fs, # type: Union[FS, Text]
452452
dst_path, # type: Text
453453
):
454454
# type: (...) -> None
455-
"""Copies metadata from one file to another.
455+
"""Copy modified time metadata from one file to another.
456456
457457
Arguments:
458458
src_fs (FS or str): Source filesystem (instance or URL).
@@ -461,7 +461,13 @@ def copy_metadata(
461461
dst_path (str): Path to a directory on the destination filesystem.
462462
463463
"""
464-
# TODO(preserve_time)
464+
namespaces = ("details", "metadata_changed", "modified")
465465
with manage_fs(src_fs, writeable=False) as _src_fs:
466466
with manage_fs(dst_fs, create=True) as _dst_fs:
467-
pass
467+
src_meta = _src_fs.getinfo(src_path, namespaces)
468+
src_details = src_meta.raw.get("details", {})
469+
dst_details = {}
470+
for value in ("metadata_changed", "modified"):
471+
if value in src_details:
472+
dst_details[value] = src_details[value]
473+
_dst_fs.setinfo(dst_path, {"details": dst_details})

fs/wrapfs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ def move(self, src_path, dst_path, overwrite=False, preserve_time=False):
177177
raise errors.DestinationExists(_dst_path)
178178
move_file(src_fs, _src_path, dst_fs, _dst_path, preserve_time=preserve_time)
179179

180-
def movedir(self, src_path, dst_path, create=False):
181-
# type: (Text, Text, bool) -> None
180+
def movedir(self, src_path, dst_path, create=False, preserve_time=False):
181+
# type: (Text, Text, bool, bool) -> None
182182
src_fs, _src_path = self.delegate_path(src_path)
183183
dst_fs, _dst_path = self.delegate_path(dst_path)
184184
with unwrap_errors({_src_path: src_path, _dst_path: dst_path}):

tests/test_copy.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ def test_copy_fs(self, workers):
3737
dst_file1_info = dst_fs.getinfo("test.txt", namespaces)
3838
dst_file2_info = dst_fs.getinfo("foo/bar/baz.txt", namespaces)
3939
self.assertEqual(dst_file1_info.modified, src_file1_info.modified)
40-
self.assertEqual(dst_file1_info.accessed, src_file1_info.accessed)
4140
self.assertEqual(
4241
dst_file1_info.metadata_changed, src_file1_info.metadata_changed
4342
)
4443
self.assertEqual(dst_file2_info.modified, src_file2_info.modified)
45-
self.assertEqual(dst_file2_info.accessed, src_file2_info.accessed)
4644
self.assertEqual(
4745
dst_file2_info.metadata_changed, src_file2_info.metadata_changed
4846
)
@@ -53,6 +51,28 @@ def test_copy_value_error(self):
5351
with self.assertRaises(ValueError):
5452
fs.copy.copy_fs(src_fs, dst_fs, workers=-1)
5553

54+
def test_copy_dir0(self):
55+
namespaces = ("details", "accessed", "metadata_changed", "modified")
56+
57+
src_fs = open_fs("mem://")
58+
src_fs.makedirs("foo/bar")
59+
src_fs.makedirs("foo/empty")
60+
src_fs.touch("test.txt")
61+
src_fs.touch("foo/bar/baz.txt")
62+
src_file2_info = src_fs.getinfo("foo/bar/baz.txt", namespaces)
63+
64+
with open_fs("mem://") as dst_fs:
65+
fs.copy.copy_dir(src_fs, "/foo", dst_fs, "/", workers=0, preserve_time=True)
66+
self.assertTrue(dst_fs.isdir("bar"))
67+
self.assertTrue(dst_fs.isdir("empty"))
68+
self.assertTrue(dst_fs.isfile("bar/baz.txt"))
69+
70+
dst_file2_info = dst_fs.getinfo("bar/baz.txt", namespaces)
71+
self.assertEqual(dst_file2_info.modified, src_file2_info.modified)
72+
self.assertEqual(
73+
dst_file2_info.metadata_changed, src_file2_info.metadata_changed
74+
)
75+
5676
@parameterized.expand([(0,), (1,), (2,), (4,)])
5777
def test_copy_dir(self, workers):
5878
namespaces = ("details", "accessed", "metadata_changed", "modified")
@@ -62,7 +82,6 @@ def test_copy_dir(self, workers):
6282
src_fs.makedirs("foo/empty")
6383
src_fs.touch("test.txt")
6484
src_fs.touch("foo/bar/baz.txt")
65-
src_file1_info = src_fs.getinfo("test.txt", namespaces)
6685
src_file2_info = src_fs.getinfo("foo/bar/baz.txt", namespaces)
6786

6887
with open_fs("mem://") as dst_fs:
@@ -75,7 +94,6 @@ def test_copy_dir(self, workers):
7594

7695
dst_file2_info = dst_fs.getinfo("bar/baz.txt", namespaces)
7796
self.assertEqual(dst_file2_info.modified, src_file2_info.modified)
78-
self.assertEqual(dst_file2_info.accessed, src_file2_info.accessed)
7997
self.assertEqual(
8098
dst_file2_info.metadata_changed, src_file2_info.metadata_changed
8199
)

tests/test_memoryfs.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,4 @@ def test_copy_preserve_time(self):
8080

8181
dst_info = self.fs.getinfo("bar/file.txt", namespaces)
8282
self.assertEqual(dst_info.modified, src_info.modified)
83-
self.assertEqual(dst_info.accessed, src_info.accessed)
8483
self.assertEqual(dst_info.metadata_changed, src_info.metadata_changed)

tests/test_mirror.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class TestMirror(unittest.TestCase):
1313
def _contents(self, fs):
1414
"""Extract an FS in to a simple data structure."""
15-
namespaces = ("details", "accessed", "metadata_changed", "modified")
15+
namespaces = ("details", "metadata_changed", "modified")
1616
contents = []
1717
for path, dirs, files in fs.walk():
1818
for info in dirs:
@@ -28,7 +28,6 @@ def _contents(self, fs):
2828
"file",
2929
_bytes,
3030
_info.modified,
31-
_info.accessed,
3231
_info.metadata_changed,
3332
)
3433
)

tests/test_move.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ def test_move_fs(self):
3232
dst_file1_info = dst_fs.getinfo("test.txt", namespaces)
3333
dst_file2_info = dst_fs.getinfo("foo/bar/baz.txt", namespaces)
3434
self.assertEqual(dst_file1_info.modified, src_file1_info.modified)
35-
self.assertEqual(dst_file1_info.accessed, src_file1_info.accessed)
3635
self.assertEqual(
3736
dst_file1_info.metadata_changed, src_file1_info.metadata_changed
3837
)
3938
self.assertEqual(dst_file2_info.modified, src_file2_info.modified)
40-
self.assertEqual(dst_file2_info.accessed, src_file2_info.accessed)
4139
self.assertEqual(
4240
dst_file2_info.metadata_changed, src_file2_info.metadata_changed
4341
)
@@ -62,7 +60,6 @@ def test_move_dir(self):
6260
if self.preserve_time:
6361
dst_file2_info = dst_fs.getinfo("bar/baz.txt", namespaces)
6462
self.assertEqual(dst_file2_info.modified, src_file2_info.modified)
65-
self.assertEqual(dst_file2_info.accessed, src_file2_info.accessed)
6663
self.assertEqual(
6764
dst_file2_info.metadata_changed, src_file2_info.metadata_changed
6865
)

0 commit comments

Comments
 (0)