是否可以使用struct stat描述符获取和设置文件名?
是否可以在获取或设置(重命名)文件名时,将文件的绝对路径和struct stat实例作为lstat函数的参数。正如我在文档中发现的,struct似乎是这样的是否可以使用struct stat描述符获取和设置文件名?,c,unix,C,Unix,是否可以在获取或设置(重命名)文件名时,将文件的绝对路径和struct stat实例作为lstat函数的参数。正如我在文档中发现的,struct似乎是这样的 struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlin
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
struct stat *s;
char *path; // assigning the ablosute path of file
int res = lstat(path, s);
if(res == -1)
return errno;
char *name = s->(someFielAboutFileName);
or
s->(someFieldAboutFileName) = name; // setting name
我想要的是这样的东西struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* protection */
nlink_t st_nlink; /* number of hard links */
uid_t st_uid; /* user ID of owner */
gid_t st_gid; /* group ID of owner */
dev_t st_rdev; /* device ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of blocks allocated */
time_t st_atime; /* time of last access */
time_t st_mtime; /* time of last modification */
time_t st_ctime; /* time of last status change */
};
struct stat *s;
char *path; // assigning the ablosute path of file
int res = lstat(path, s);
if(res == -1)
return errno;
char *name = s->(someFielAboutFileName);
or
s->(someFieldAboutFileName) = name; // setting name
不可以。在unix中,名称不是文件的固有属性。一个文件可以有多个名称(请参见硬链接),甚至可以没有名称
名称只是目录中的条目。在Unix中,文件名属于目录,而不是文件。也就是说,同一个文件可以有多个名称(在Unix中称为“硬链接”)。这也意味着您无法从
stat
中找到名称,因为它处理的是文件,而不是名称。您可以使用realpath
查找文件的真实名称,并解析符号链接等。似乎要修改符号链接。这对您没有帮助,因为它只返回链接的状态,甚至不返回它指向的文件的名称,并且无论如何都不能用于修改有关该文件的任何内容
您可能需要创建符号链接,然后调用以重新创建它并使其指向另一个文件:
unlink(path);
symlink(name, path);