Skip to content

Conversation

@illia-v
Copy link
Contributor

@illia-v illia-v commented May 24, 2022

Resolves #93180


This function should not be used for copying a file from special
filesystems like procfs and sysfs because of
`a known issue <https://lore.kernel.org/linux-fsdevel/20210126135012.1.If45b7cdc3ff707bc1efa17f5366057d60603c45f@changeid/>`_.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The link contains a fix. Was the fix merged into Linux?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it wasn't. I tested os.copy_file_range for copying /proc/cpuinfo on Linux 5.17, it returned 0 and the destination file was empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, some Python functions that operate files are prone to similar issues too :(. I'll create a few bug reports.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, some Python functions that operate files are prone to similar issues too :(. I'll create a few bug reports.

#93300 and #93296 are two examples.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to not add a link to an email.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about such a note?

:func:os.copy_file_range should not be used for copying a range of a pseudo file from a special filesystem like procfs and sysfs. It will always copy no bytes and return 0 as if the file was empty because of a known Linux kernel issue.

@vstinner
Copy link
Member

cc @pablogsal @giampaolo

@illia-v illia-v requested a review from vstinner May 27, 2022 13:30
reflinks (i.e., two or more inodes that share pointers to the same
copy-on-write disk blocks; supported file systems include btrfs and XFS)
and server-side copy (in the case of NFS). The copy is done as if
both files are opened as binary.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function arguments are file descriptors. What does "binary" mean for file descriptors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is not new. I guess it means that bytes written to a destination file will be equal to ones read from the source file.

In contrary, if a file range is read in the text mode in Python and it is written in the text mode to a different file, bytes may differ. For example, line endings may be changed. See https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind to rephase the sentence to explain that? Explain that the copy does not change the line endings, for example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 069651d.

BTW, shutil.copyfile looks to behave the same, but shutil.copyfileobj seems to respect the text mode.


This function should not be used for copying a file from special
filesystems like procfs and sysfs because of
`a known issue <https://lore.kernel.org/linux-fsdevel/20210126135012.1.If45b7cdc3ff707bc1efa17f5366057d60603c45f@changeid/>`_.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to not add a link to an email.

@illia-v illia-v requested a review from vstinner May 30, 2022 18:15
@illia-v illia-v force-pushed the fix-issue-93180 branch from 3c980b9 to 069651d Compare June 6, 2022 20:12
@illia-v illia-v requested a review from vstinner June 7, 2022 16:29
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@vstinner
Copy link
Member

vstinner commented Jun 8, 2022

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update os.copy_file_range documentation

3 participants