JustinBinber opened a new issue, #41561:
URL: https://github.com/apache/beam/issues/41561

   ### What happened?
   
   The Python local filesystem deletes an existing destination before checking 
whether it is the source. A self-copy therefore deletes the original file or 
directory and then fails. This is reachable through the public 
`FileSystems.copy` entry point:
   
   ```python
   from pathlib import Path
   from tempfile import TemporaryDirectory
   from apache_beam.io.filesystem import BeamIOError
   from apache_beam.io.filesystems import FileSystems
   
   with TemporaryDirectory() as directory:
       path = Path(directory) / "data"
       path.write_text("original data")
       try:
           FileSystems.copy([str(path)], [str(path)])
       except BeamIOError:
           pass
       print(path.exists())  # False: the source has been deleted
   ```
   
   Equivalent paths such as `directory/./data` and a nonempty directory copied 
to itself have the same problem. A missing source also causes an existing 
destination to be deleted before the copy fails.
   
   Expected: reject a self-copy without deleting the original data. Reject a 
missing source before removing an existing destination. Other pairs in a batch 
should still be attempted, and ordinary overwrite behavior should remain 
supported.
   
   Root cause: `LocalFileSystem.copy` calls `os.remove(destination)` or 
`shutil.rmtree(destination)` before `shutil.copy2` / `shutil.copytree` sees the 
source. I am preparing a focused guard using `os.path.samefile` before removal, 
plus real filesystem regressions for files, directories, equivalent paths, hard 
links, public dispatch, normal overwrite and batch error aggregation. This does 
not make every copy operation atomic or redesign ancestor/descendant directory 
copies.
   
   Reproduced with current master `66d61898bd79abae2bc4fee1f5d1650644e15610` 
Python sources on Windows/Python 3.11.7. The source checkout uses generated 
protobuf modules from the released Beam 2.78.0 wheel; the actual local 
filesystem and public dispatch code are from master, without mocks.
   
   ### Issue Priority
   
   Priority: 2 (default / most bugs should be filed as P2)
   
   ### Issue Components
   
   - [x] Component: Python SDK
   - [ ] Component: Java SDK
   - [ ] Component: Go SDK
   - [ ] Component: Typescript SDK
   - [x] Component: IO connector
   


-- 
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]

Reply via email to