我正在使用
FindFirst
和 FindNext
函数读取各种文件和目录,如 RTL 文档中所述。
我唯一的问题是我无法确定该文件是否是符号链接。在文件属性中没有常量或标志,我找不到用于测试符号链接的函数。
您使用 findfirst 的最初想法是最好的,因为它是一个可移植的解决方案(现在 Windows 也有符号链接)。唯一需要调整的是在传递给 findfirst 的属性中请求符号链接检查:
uses sysutils;
var info : TSearchrec;
begin
// the or fasymlink in the next file is necessary so that findfirst
// uses (fp)lstat instead of (fp)stat
If FindFirst ('../*',faAnyFile or fasymlink ,Info)=0 then
begin
Repeat
With Info do
begin
If (Attr and fasymlink) = fasymlink then
Writeln('found symlink: ', info.name)
else
writeln('not a symlink: ', info.name,' ',attr);
end;
Until FindNext(info)<>0;
end;
FindClose(Info);
end.
您可以使用 BaseUnix 中的 fpstat:
类似这样的事情
uses baseUnix;
var s: stat;
fpstat(filname, s);
if s.st_mode = S_IFLNK then
writeln('is link');
这还为您提供了有关文件的许多其他信息(时间、大小......)
函数 fpLStat 就是答案:
var
fileStat: stat;
begin
if fpLStat('path/to/file', fileStat) = 0 then
begin
if fpS_ISLNK(fileStat.st_mode) then
Writeln ('File is a link');
if fpS_ISREG(fileStat.st_mode) then
Writeln ('File is a regular file');
if fpS_ISDIR(fileStat.st_mode) then
Writeln ('File is a directory');
if fpS_ISCHR(fileStat.st_mode) then
Writeln ('File is a character device file');
if fpS_ISBLK(fileStat.st_mode) then
Writeln ('File is a block device file');
if fpS_ISFIFO(fileStat.st_mode) then
Writeln ('File is a named pipe (FIFO)');
if fpS_ISSOCK(fileStat.st_mode) then
Writeln ('File is a socket');
end;
end.
打印出来:
test_symlink
File is a link
test
File is a directory
感谢使用 fpstat 的提示。但这似乎不起作用。 我有两个文件,一个目录和一个目录的符号链接:
drwxrwxr-x 2 marc marc 4096 Okt 1 09:40 test
lrwxrwxrwx 1 marc marc 11 Dez 5 13:49 test_symlink -> /home/marc/
如果我对这些文件使用 fpstat,我会得到:
Result of fstat on file test
Inode : 23855105
Mode : 16877
nlink : 92
uid : 1000
gid : 1000
rdev : 0
Size : 12288
Blksize : 4096
Blocks : 24
atime : 1354711751
mtime : 1354711747
ctime : 1354711747
Result of fstat on file test_symlink
Inode : 23855105
Mode : 16877
nlink : 92
uid : 1000
gid : 1000
rdev : 0
Size : 12288
Blksize : 4096
Blocks : 24
atime : 1354711751
mtime : 1354711747
ctime : 1354711747
属性 st_mode 没有区别。我认为 fpstat 获取链接目的地的统计信息,这确实是一个目录......