Search code examples
perltarsetuid

How do I preserve the setuid bit in tar archives with Perl's Archive::Tar?


I'm using Perl's Archive::Tar module. It preserves the file permissions but doesn't preserve the sticky bit. At the other end where I extract the archive, all the sticky bits are gone. I think UNIX/LINUX operating system stores these sticky bits somewhere else. How can I make my archive preserve sticky bits also?

Using the -p switch to tar preserves it but how do I do it using Archive::Tar? I'm using Perl's module on both the sides.


Solution

  • According to the Fine Source, Archive::Tar::File strips off the high bits from the mode. You can try the following magic incantation at the beginning of your script (before anything might have referenced Archive::Tar) and see if that subverts it:

    use Archive::Tar::Constant ();
    BEGIN {
        local $SIG{__WARN__} = sub{};
        *Archive::Tar::Constant::STRIP_MODE = sub(){ sub {shift} };
    }
    ...
    use Archive::Tar;
    ...
    

    Brief explanation: STRIP_MODE is a constant that contains a subroutine that can be passed the raw mode and returns the mode that should be stored. It is normally set to

    sub { shift() & 0777 }
    

    Because it is a constant, imported from Archive::Tar::Constant into Archive::Tar::File and used there, whatever it is set to will be inlined into Archive::Tar::File as it is compiled. So to change it, the constant must be changed before it is inlined, that is, before Archive::Tar::File ever gets loaded.

    N.B. Because changing an inlinable constant is prone to error (changing it after it is too late to have any effect), it normally generates a severe warning that can't be disabled by usual means.