kingsword09 commented on PR #6326:
URL: https://github.com/apache/opendal/pull/6326#issuecomment-2993943118
In the code, the `if_not_exists` flag is used to ensure that the write
operation only happens if the target file does not already exist. If
`if_not_exists = true`, we want to throw a `ConditionNotMatch` exception when
the target file exists. In the previous implementation, a `tmp_path` temporary
file was always created, so each write operation was actually performed on a
new temporary file. As a result, the existence of the target file was never
checked, and the exception was not triggered as expected.
The core of this change is to check whether `if_not_exists` is true before
creating the temporary file (`tmp_path`). If it is, we try to write directly
to the target path using `OpenOptions::create_new(true)`, which guarantees
that the write will only succeed if the file does not already exist. If the
target file exists, a `ConditionNotMatch` error is thrown. This avoids the
problem of always writing to a temporary file and missing the existence check.
In short:
• The previous logic always created a temporary file, so it couldn’t detect
if the target file existed.
• Now, we check `if_not_exists` first. If it’s true, we try to write
directly to the target path, so the error can be thrown correctly and the
expected behavior is achieved.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]