gpt4 book ai didi

linux - 软件能否包装 gzip/gunzip 以保留文件所有权?

转载 作者:太空宇宙 更新时间:2023-11-04 09:27:45 24 4
gpt4 key购买 nike

<分区>

gzip/gunzip 被发现会改变 inode 号,因此如果被其他所有者打开,则所有权不会保留。

OP 发布的解决方案,因为线程过早关闭

如果有人感兴趣,这里是保持所有权的解决方案。

我修改了程序,通过了当前的基本测试。

# 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 "$cmd\n";
system($cmd);
}
sub mock_gunzip{
my $file = $_[0];
my $cmd = "gunzip -f $file";
print "$cmd\n";
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 Found\n");
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

}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com