jborean93 / smbprotocol

Python SMBv2 and v3 Client
MIT License
320 stars 74 forks source link

Question. How to open file for write and fail if the file exists? #298

Closed Gelembjuk closed 1 day ago

Gelembjuk commented 5 days ago

I have the code to upload files to SMB.

It looks like:

file_handle = smbclient.open_file(self._path, "wb")

i would like to make it to fail when a file exists. I can check if the file exists with the another call. But i would like to make this like atomic operation (to solve concurency problem)

I have the code from old project using the other lib for SMB

ctx = smbc.Context()
...
flags = os.O_CREAT | os.O_WRONLY | os.O_TRUNC
if not overwrite:
    flags |= os.O_EXCL

try:
    smb_file = ctx.open(smb_path, flags)

Is it possible to do same with the smbprotocol?

Another question. Not related to this but i do not want to create new ticket.

How to change a file modification time on SMB? Is it possible with this library? maybe some equivalent of touch command?

jborean93 commented 5 days ago

In this case you want to use xb as x represents create file but fail if it already exists. We use the same mode flags as the builtin open call where x is

open for exclusive creation, failing if the file already exists

You can see how the various modes map in the code with x being CreateDisposition.FILE_CREATE.

https://github.com/jborean93/smbprotocol/blob/a0b854414c828542a24f13e4ef5f28d18cf7c752/src/smbclient/_io.py#L69

jborean93 commented 1 day ago

Closing per the above.