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

Touching files using pyxrootd #1737

Closed
ScottDemarest opened this issue Jul 14, 2022 · 2 comments
Closed

Touching files using pyxrootd #1737

ScottDemarest opened this issue Jul 14, 2022 · 2 comments

Comments

@ScottDemarest
Copy link

I want to be able to touch files, updating their modtime timestamps. My particular application requires that there be an option to truncate the file or not upon touching it. To truncate touch it, I'm just opening it using the DELETE flag and to not truncate, I'm opening with the UPDATE flag. The former method results in the timestamp updating while the latter doesnt and attached is a simple script that demonstrates this. Is there a way to update the modtime without modifying the file, to "touch" it as its called?

from XRootD import client
from XRootD.client.flags import DirListFlags, OpenFlags, MkDirFlags, QueryCode
import time

local_server = 'YOUR LOCAL SERVER'
path = '/tmp/eggs'

with client.File() as f:
status, _ = f.open(local_server+"/"+path, OpenFlags.DELETE)
if not status.ok:
raise OSError(f"Something went wrong: {status.message}")
status, _ = f.write('green\neggs\nand\nham\n')
if not status.ok:
raise OSError(f"Something went wrong: {status.message}")

myclient = client.FileSystem(local_server)

status, deets = myclient.stat(path)
if not status.ok:
raise OSError(f"Something went wrong: {status.message}")
t1 = deets.modtime
time.sleep(1)
print(t1)

with client.File() as f:
f.open(local_server+"/"+path, OpenFlags.UPDATE)

status, deets = myclient.stat(path)
t2 = deets.modtime
time.sleep(1)
print(t2)

with client.File() as f:
f.open(local_server+"/"+path, OpenFlags.DELETE)

status, deets = myclient.stat(path)
t3 = deets.modtime
time.sleep(1)
print(t3)

print(t1<t2)
print(t2<t3)
assert t1 < t2 and t2 < t3

@abh3
Copy link
Member

abh3 commented Jul 14, 2022

The only way to update the modification time without actually writing to the file is to issue a truncate() request but specifying the actual size of the file. The file will not change but the mod time will be update. If you specify a size of zero then it's equivalent to opening it with the DELETE option. Of course, this is unsafe operation if other people are currently writing to the file but, based on what you say, this won't be happening.

@abh3 abh3 closed this as completed Jul 14, 2022
@ScottDemarest
Copy link
Author

It works, thank you for the advice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants