📜 ⬆️ ⬇️

How to patch cp under FreeBSD?

We teach FreeBSD to copy their extended attributes along with the files.


I am a big fan of cp - in terms of elegance and indestructibility, this utility, in my opinion, is second only to dd . But under FreeBSD, it has one modest flaw - when copying a file, cp loses its extended attributes (extattr). In a previous publication, I proposed a patch for the find command , which adds the ability to search for files by the contents of their extended attributes. But what's the point if your keywords and comments get lost when copying a file? It's time to take the last bastion on the path of full support for extended attributes in the fryashechke.

As always, Google helped. First, there was an interesting article " Extended attributes: the good, the bad, the bad, " which offers a workaround to solve the problem of finding files by the contents of their extended attributes. That is, what I tried to solve in the previous publication at the patch level of the find utility, here it is decided by regular OS tools. From the same article it follows that the conquest of cp - it seems, indeed, the last frontier that must be overcome to activate full support for extattr in fryashechku.

Next, Google slips a patch for NetBSD , forcing cp to copy extended attributes. To read him - so there is nothing to do at all. Just add the line in the right place of the source code:

if (pflag && (fcpxattr(from_fd, to_fd) != 0)) warn("%s: error copying extended attributes", to.p_path); 

And that's all. You can copy. Well, let's go to the FreeBSD sources, open the utils.c file, add the cherished lines to it and ... We arrived. FreeBSD does not know what fcpxattr () is . In general, the sys / extattr.h header file is responsible for declaring this function, but he does not know about it either. All these fcpxattr is a NetBSD counter. They have sys / extattr.h upgraded to FreeBSD.
')
But my task was to teach cp to copy extended attributes, and not to shovel half the core of the system. Therefore, without any doubt, I decided to include the required extattr functionality in the cp sources. Of course, from the point of view of system integrity, this is wrong, but as a patch for a specific task, it will come down.

In order to make fewer errors in the code, I looked for NetBSD sources of extattr.c and took them as a sample.

In the end, I got such a patch (for FreeBSD 11.2.0-RELEASE) .

You can apply it like this:

 cd /usr/src/bin/cp patch < /patch-cp.diff make make install clean 

If you noticed, in my version, extended attributes are always copied, and not just with the -p flag, as in the NetBSD version. It seemed to me that this is more convenient. In any case, as in the previous publication, I posted on Github the full code for the cp utility , copied from the FreeBSD repository, with my changes. So you can have fun too.

Source: https://habr.com/ru/post/425795/


All Articles