cp无法获取文件状态stat_Linux系统编程之文件系统和stat函数「62章节」

Linux学习中,每天学习一点点,你就是下一个Linux专家

一、Linux系统编程之文件系统

文件系统需要了解相关概念:inode,dentry,数据存储,文件系统

Inode:

其本质为结构体,存储文件的属性信息,如权限,属性,时间,块位置....,也叫作文件属性管理结构

如下是文件和Inode和磁盘的存储关系:

ec7184c38428ccb3af3458e522c84828.png

dentry,inode和磁盘之间的关系

备注:磁盘空间不会被删除,格式化磁盘数据也无法删除,只能覆盖磁盘

Dentry:

Dentry一个文件名和一个inode,组合起来就是dentry,如上图

二、Stat函数

获取文件属性,(从Inode结构体中获取)

Int stat(const char *path, struct *buf);成功返回0;失败返回 -1设置errno 为恰当值。

参数1: 文件名

参数2:inode 结构体指针(传出参数)

文件属性将通过传出参数返回给调用者。

在 linux 输入 man stat 可以看到该函数的原型。

如下函数可以尝试看stat的用法

#include

#include

#include

#include//需要引用此头文件

Int main(int argc, char *argv[]){

Strcut stat buf;

int ret = stat(argv[1], &buf);

If(ret == -1)

{

perror(“stat error”);

exit(1);

}

//如下打印stat各个属性,也可以获取其它文件属性

printf(“file size of=%ld”,buf.st_size); //获取文件大小

}

如下是struct stat的结构,可以通过上面的printf函数打印出来各个文件如下属性:

d1b85b1e93c5fd1ef23960dc7113b853.png

stat函数中struct *buf传出结构里的属性值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux C 语言,使用系统函数复制目录需要使用递归的方式遍历目录树,并对每个文件进行复制。下面是一个简单的示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <dirent.h> #define BUFFER_SIZE 1024 void copy_file(const char *src, const char *dst) { char buffer[BUFFER_SIZE]; size_t nread; FILE *fp_src = fopen(src, "rb"); if (fp_src == NULL) { perror("fopen"); exit(1); } FILE *fp_dst = fopen(dst, "wb"); if (fp_dst == NULL) { perror("fopen"); exit(1); } while ((nread = fread(buffer, 1, sizeof(buffer), fp_src)) > 0) { fwrite(buffer, 1, nread, fp_dst); } fclose(fp_src); fclose(fp_dst); } void copy_dir(const char *src, const char *dst) { DIR *dir = opendir(src); if (dir == NULL) { perror("opendir"); exit(1); } char src_path[BUFFER_SIZE], dst_path[BUFFER_SIZE]; struct dirent *entry; struct stat statbuf; while ((entry = readdir(dir)) != NULL) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } snprintf(src_path, sizeof(src_path), "%s/%s", src, entry->d_name); snprintf(dst_path, sizeof(dst_path), "%s/%s", dst, entry->d_name); if (lstat(src_path, &statbuf) == -1) { perror("lstat"); exit(1); } if (S_ISREG(statbuf.st_mode)) { copy_file(src_path, dst_path); } else if (S_ISDIR(statbuf.st_mode)) { if (mkdir(dst_path, statbuf.st_mode & 0777) == -1 && errno != EEXIST) { perror("mkdir"); exit(1); } copy_dir(src_path, dst_path); } else { fprintf(stderr, "Unsupported file type: %s\n", src_path); exit(1); } } closedir(dir); } int main(int argc, char *argv[]) { if (argc != 3) { fprintf(stderr, "Usage: %s <source> <destination>\n", argv[0]); exit(1); } if (mkdir(argv[2], 0777) == -1 && errno != EEXIST) { perror("mkdir"); exit(1); } copy_dir(argv[1], argv[2]); return 0; } ``` 这个程序的使用方法是: ``` $ ./copydir <source> <destination> ``` 其 `<source>` 是源目录的路径,`<destination>` 是目标目录的路径。 程序首先检查命令行参数的个数是否正确,如果不正确则输出用法信息并退出。然后创建目标目录,如果目录创建失败则输出错误信息并退出。接下来,程序使用递归的方式遍历源目录文件,并将它们复制到目标目录。如果遇到普通文件,则调用 `copy_file` 函数进行复制;如果遇到子目录,则递归调用 `copy_dir` 函数进行复制。 需要注意的是,在复制目录时,目标目录必须事先存在。如果目标目录不存在,则需要先创建目标目录。此外,如果遇到目录存在软链接等非普通文件类型,则需要根据实际情况进行处理。 如果要使用标准库来粘贴目录到其他目录,可以使用 `system` 函数调用 `cp` 命令来实现。例如: ```c #include <stdlib.h> int main() { system("cp -r /path/to/source /path/to/destination"); return 0; } ``` 这个程序的作用是将 `/path/to/source` 目录复制到 `/path/to/destination` 目录。需要注意的是,在使用 `system` 函数调用命令时,需要特别注意命令可能包含的特殊字符,如空格、单引号、双引号等。这些特殊字符可能会导致命令执行出错。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值