forked from lance-format/lance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
33 lines (24 loc) · 975 Bytes
/
utils.py
File metadata and controls
33 lines (24 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The Lance Authors
import os
from pathlib import Path
def wipe_os_cache(dataset_uri: str):
if dataset_uri.startswith("/"):
path = dataset_uri
elif dataset_uri.startswith("file://"):
path = Path(dataset_uri.removeprefix("file://"))
else:
return
if not hasattr(os, "posix_fadvise"):
raise NotImplementedError("posix_fadvise not available on this platform")
POSIX_FADV_DONTNEED = 4 # Tell kernel we don't need this data in cache
directory = Path(path)
file_iterator = directory.rglob("*")
for filepath in file_iterator:
# Skip directories, symlinks, and non-regular files
if not filepath.is_file():
continue
with open(filepath, "rb") as f:
fd = f.fileno()
# offset=0, length=0 means drop entire file from cache
os.posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED)