软件可以包装gzip/gunzip来保留文件所有权



gzip/gunzip被发现会更改索引节点编号,因此如果由另一个所有者打开,则所有权将不会保留。

soluion posted by OP since thread is closed prematurely

Here is the solution to keep ownership, if anyone interested.

I modified the procedures, it passed the current basic tests.

# the original file information
ok 1 - /shared/shared/colourbar.nii.gz Found
ok 2 - /shared/shared/colourbar.nii.gz -- fileowner <504>
ok 3 - /shared/shared/colourbar.nii.gz -- inode <692254>
# copy file to /tmp, processed by uid 500
***cp -vf /shared/shared/colourbar.nii.gz /tmp
`/shared/shared/colourbar.nii.gz' -> `/tmp/colourbar.nii.gz'
***gunzip -f /tmp/colourbar.nii.gz
***gzip -f /tmp/colourbar.nii
ok 4 - /tmp/colourbar.nii.gz -- fileowner <500>
ok 5 - /tmp/colourbar.nii.gz -- inode <31>

# copy back to overwrite, and the ownership preserved
***cp -vf /tmp/colourbar.nii.gz /shared/shared
`/tmp/colourbar.nii.gz' -> `/shared/shared/colourbar.nii.gz'
ok 6 - /shared/shared/colourbar.nii.gz -- fileowner <504>
ok 7 - /shared/shared/colourbar.nii.gz -- inode <692254>

由于gzip/gunzip实用程序中的一个功能,单元测试失败。关于gzip/gunzip的讨论已经结束了,我想进一步推动

真正的问题需要真正的解决方案。我们能在test6上找到相同的文件所有者吗?

原所有者拥有uid 500,通过所有单元测试

ok 1 - /shared/shared/colourbar.nii.gz Found
ok 2 - original fileowner <500>
ok 3 - original inode<692254>
gunzip -f /shared/shared/colourbar.nii.gz
ok 4 - fileowner after gunzip <500>
ok 5 - inode after gunzip<692255>
gzip -f /shared/shared/colourbar.nii
ok 6 - fileowner after gzip <500>
ok 7 - inode after gzip<692254>

Joe的uid为504,测试6未通过

ok 1 - /shared/shared/colourbar.nii.gz Found
ok 2 - original fileowner <500>
ok 3 - original inode<692254>
gunzip -f /shared/shared/colourbar.nii.gz
not ok 4 - fileowner after gunzip <504>
ok 5 - inode after gunzip<692255>
gzip -f /shared/shared/colourbar.nii
not ok 6 - fileowner after gzip <504>
ok 7 - inode after gzip<692254>

原始测试脚本在这里:

#!/usr/bin/perl
use strict;
use warnings;
use Test::More ;
return 1  unless $0 eq __FILE__;
main(@ARGV) if $0 eq __FILE__;
sub mock_gzip{
    my $file = $_[0];
    my $cmd = "gzip -f $file";
    print "$cmdn";
    system($cmd);
}
sub mock_gunzip{
    my $file = $_[0];
    my $cmd = "gunzip -f $file";
    print "$cmdn";
    system($cmd);
}
sub fileowner{
    my $file = $_[0];
    my $uid = `stat -c %u $file`;
    chomp($uid);
    return $uid;
}
sub get_inode{
    my $file =$_[0];
    my $inode = `stat -c %i $file`;
    chomp($inode);
    return  $inode;
}
sub main{
    #simulate real life situation - user A
    my $file = "/shared/shared/colourbar.nii.gz";
    my $fileu = $file;
    $fileu =~ s/.gz$//g;
    ok(-e $file,"$file Foundn");
    my $fileowner = fileowner($file);
    ok($fileowner>0,"original fileowner <$fileowner>n");
    my $inode  = get_inode($file);
    ok($inode>0,"original inode<$inode>n");
    # user B - gunzip/gzip owner changed
    mock_gunzip($file);
    my $fileowner_gunzip = fileowner($fileu);
    ok($fileowner_gunzip==$fileowner,"fileowner after gunzip <$fileowner_gunzip>n");
    my $inode_gunzip  = get_inode($fileu);
    ok($inode_gunzip>0,"inode after gunzip<$inode_gunzip>n");
    mock_gzip($fileu);
    my $fileowner_gzip = fileowner($file);
    ok($fileowner_gzip==$fileowner,"fileowner after gzip <$fileowner_gzip>n");
    my $inode_gzip  = get_inode($file);
    ok($inode_gzip==$inode,"inode after gzip<$inode_gzip>n");
    # solution, or verified no solution to be decided
}

压缩文件时,将创建一个新文件,该文件归当前用户所有。使用-i选项可以显示文件的索引节点。

罗伯特:~>触摸测试

罗伯特:~>ls-li测试

93644038-rw-r-r-1 robert staff 0 Dec 26 20:42测试

如果压缩到位,旧文件就会被删除,所以看起来它可以替换文件,但实际上它确实创建了一个新文件。

罗伯特:~>gzip测试

罗伯特:~>ls-li测试*

93644048-rw-r-r-1 robert staff 12月25日20:42测试.gz

当您解压缩时,您还创建了新的文件,同样由当前用户所有。

罗伯特:~>gunzip测试.gz

罗伯特:~>ls-li测试*

93644052-rw-r-r-1 robert staff 0 Dec 26 20:42测试

因此,您的错误是假设它是同一个文件,但事实并非如此——请注意ls输出开头的inode编号不断变化。如果它试图使用同一个inode,它会对具有多个文件的归档做什么?

如果你只是重命名文件,它就是同一个文件,并且保留所有权:

罗伯特:~>sudo mv测试更名为

罗伯特:~>ls-li更名为

93644052-rw-r-r-1 robert staff 0 Dec 26 20:42更名为

与附件相同(更改内容):

罗伯特:~>sudo echo>gt;重命名为

罗伯特:~>ls-li更名为

93644052-rw-r-r-1 robert staff 4 Dec 26 20:51更名为

有关索引节点的更多详细信息,请参阅维基百科上的索引节点。

gzip格式不保留所有者。此外,只有root用户才能将文件的所有权设置给其他用户。

您可以使用tar记录文件的所有者以及文件内容:

:; echo hello > sym2.txt
:; ls -l sym2.txt
-rw-r--r--  1 mayoff  staff  6 Dec 26 20:46 sym2.txt
:; tar cvzf sym2.tar.gz sym2.txt 
a sym2.txt
:; tar tvf sym2.tar.gz
-rw-r--r--  0 mayoff staff       6 Dec 26 20:46 sym2.txt

要提取tar文件并保留所有权,您需要以root身份这样做:

sudo tar xvf sym2.tar.gz

或者,您可以在作为应该拥有提取文件的用户运行时提取tar文件。

最新更新