Perl chmod文件的路径长度超过260个字符的限制



我已经按照上的说明启用了Windows 10长路径https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation#enable-长路径-windows中的-10版本-1607和拉特我这样做是为了让我可以在perl中chmod超过260个字符的Windows最大路径长度限制的文件。

my $ret = chmod(oct(0555), '<path_length_greater_than_260_characters>somefile.txt');
print "$retn";

它无法chmod,$ret就是0。我有什么选择?

每个函数有两个版本接受/返回字符串,一个是a(NSI(版本,接受/返回使用ANSI/Active Code Page编码的字符串,另一个是W(ide(版本,接收/返回使用UTF-16le编码的字符串。

Perl使用A版本的函数。

您所做的更改仅适用于某些W函数。

如果选择长路径行为,这些目录管理函数将不再具有MAX_PATH限制:CreateDirectoryW、CreateDirectoryExW GetCurrentDirectoryW RemoveDirectoryW SetCurrentDirectoryW。

如果您选择长路径行为,这些文件管理函数将不再具有MAX_PATH限制:CopyFileW、CopyFile2、CopyFileExW、CreateFileW、CreateFile2、CreateHardLinkW、CreateSymbolicLinkW、DeleteFileW、FindFirstFileW、FindFirstFileExW,MoveFileExW、MoveFileWithProgressW、ReplaceFileW、SearchPathW、FindFirstFileNameW、FindNextFileName W、FindFirst StreamW、FindNext StreamW、GetCompressedFileSizeW、GetFinalPathNameByHandleW。

您可以使用Win32::Unicode来访问其中的大部分。您也可以使用Win32::API或FFI::Platypus访问它们。


顺便说一句,您也可以通过在路径前面加上\?来绕过限制(或者对于已经以\开头的路径,用\?UNC替换\(。例如,\?d:dirfile形式的某些内容将被限制为32767个字符,而不是260个字符。即使不启用OP中提到的功能,这也能工作。也就是说,这也只适用于W功能。

由于@ikegami解释的原因,您可以使用Win32::LongPath。来自模块简介:

use File::Spec::Functions;
use Win32::LongPath;
use utf8;

# make a really long path w/Unicode from around the world
$path = 'c:';
while (length ($path) < 5000) {
$path = catdir ($path, 'ελληνικά-русский-日本語-한국-中國的-עִברִית-عربي');
if (!testL ('e', $path)) {
mkdirL ($path) or die "unable to create $path ($^E)";
}
}
print 'ShortPath: ' . shortpathL ($path) . "n";

# next, create a file in the path
$file = catfile ('more interesting characters فارسی-தமிழர்-​ພາສາ​ລາວ');
openL ($FH, '>:encoding(UTF-8)', $file)
or die ("unable to open $file ($^E)");
print $FH "writing some more Unicode charactersn";
print $FH "דאס שרייבט אַ שורה אין ייִדיש.n";
close $FH;

# now undo everything
unlinkL ($file) or die "unable to delete file ($^E)";
while ($path =~ /[/\]/) {
rmdirL ($path) or die "unable to remove $path ($^E)";
$path =~ s#[/\][^/\]+$##;
}

对于Windows上的chmod,请注意来自perlport:的警告

(Win32(只适用于更改";所有者;读写访问"组";以及";其他";比特是没有意义的。

有关设置Windows特定的文件访问权限,请参阅文件安全和访问权限以及SetNamedSecurityInfo。我还没有使用该模块,但Win32::Security::NamedObject在这里可能会有所帮助。

最新更新