/
check-atime.py
executable file
·69 lines (64 loc) · 2.35 KB
/
check-atime.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
"""
This utility script checks which atime mode (strictatime, relatime or noatime)
is in use for the current filesystem
"""
import os
import time
from datetime import datetime
def datetime_to_timestamp(dt):
return time.mktime(dt.timetuple()) + dt.microsecond/1e6
def set_file_access_time(filename, atime):
"""
Set the access time of a given filename to the given atime.
atime must be a datetime object.
"""
stat = os.stat(filename)
mtime = stat.st_mtime
os.utime(filename, (datetime_to_timestamp(atime), mtime))
def last_file_access_time(filename):
"""
Get a datetime() representing the last access time of the given file.
The returned datetime object is in local time
"""
return datetime.fromtimestamp(os.stat(filename).st_atime)
try:
# Create test file
with open("test.txt", "w") as outfile:
outfile.write("test!")
time.sleep(0.1)
# Read & get first atime
with open("test.txt") as infile:
infile.read()
atime1 = last_file_access_time("test.txt")
# Now read file
time.sleep(0.1)
with open("test.txt") as infile:
infile.read()
# Different atime after read?
atime2 = last_file_access_time("test.txt")
# Set OLD atime for relatime check!
set_file_access_time("test.txt", datetime(2000, 1, 1, 0, 0, 0))
# Access again
with open("test.txt") as infile:
infile.read()
# Different atime now
atime3 = last_file_access_time("test.txt")
# Check atime
changed_after_simple_access = atime2 > atime1
changed_after_old_atime = atime3 > atime1
# Convert mode to text and print
if (not changed_after_simple_access) and (not changed_after_old_atime):
print("Your filesystem is mounted in NOATIME mode - access times will NEVER be updated automatically")
elif (not changed_after_simple_access) and changed_after_old_atime:
print("Your filesystem is mounted in RELATIME mode - access times will only be updated if they are too old")
elif changed_after_simple_access and (not changed_after_old_atime):
print("Unable to determine your access time mode")
else: # Both updated
print("Your filesystem is mounted in STRICTATIME mode - access times will be updated on EVERY file access")
finally:
# Delete our test file
try:
os.remove("test.txt")
except:
pass