Linux系统编程学习笔记(四)文件和目录管理
文件和目录管理:
1、获得文件metadata的Stat家族:
#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>int lstat(const char * restrict path, struct stat * restrict buf);int stat(const char *restrict path, struct stat * restrict buf);int fstat(int fd,struct stat *buf);
struct stat{dev_t st_dev; /*包含文件的设备号*/ino_t st_ino; /*文件inode序列号 */mode_t st_mode;/*文件的mode*/nlink_t st_nlink; /*硬链接的数量*/uid_t st_uid; /*文件的用户id*/gid_t st_gid; /*文件的goup id*/off_t st_size;/*文件大小*/time_t st_atime;/*最后一次访问时间*/time_t st_mtime;/*最后一次修改时间*/time_t st_ctime;/*最后一次状态改变时间*/}
#include <stdio.h>#include <time.h>#include <sys/stat.h>int isdirectory(char *path) { struct stat statbuf; if (stat(path, &statbuf) == -1) return 0; else return S_ISDIR(statbuf.st_mode);}
#include <sys/types.h>#include <sys/stat.h>int chmod(const char *path, mode_t mode);int fchmod(int fd,mode_t mode);
ret = chmod("./map.png",S_IRUSR | S_IWUSR);if(ret)perror("chmod");
int ret;ret = fchmod(fd, S_IRUSR | S_IWUSR);if(ret)perror("fchmod");
#include <sys/types.h>#include <unistd.h>int chown(const char *path, uid_t owner, gid_t group);int lchown(const char *path, uid_t owner, gid_t group);int fchown(int fd, uid_t owner, gid_t group);
struct group *gr;int ret;gr = getgrnam("officers");if(! gr){perror("getgrnam");return 1;}ret = chmod("manifest.txt",-1,gr->gr_gid);if(ret){perror("chmod");return 1;}
int make_root_owner(int fd){int ret;ret = fchown(fd,0,0);if(ret)perror("fchown");return ret;}
#include <unistd.h>int chdir(const char *path);int fchdir(int fd);
char *dir = "/tmp";if(chdir(dir) == -1)perror("Failed to change current working directory to /tmp");
#include <unistd.h>char *getcwd(char *buf, size_t size);
#include <limits.h>#include <stdio.h>#include <unistd.h>#ifndef PATH_MAX#define PATH_MAX 255#endifint main(void){char mycwd[PATH_MAX];if(getcwd(mycwd,PATH_MAX) == NULL){perror("Failed to get current working directory");return 1;}printf("Current working directory: %s\n",mycwd);return 0;}
#include <unistd.h> long fpathconf(int fd, int name); long pathconf(const char *path,int name); sysconf(int name);
#include <sys/stat.h>#include <sys/types.h>int mkdir(const char *path, mode_t mode);
int ret;ret = rmdir("/home/fuliang/test");if(ret)perror("rmdir");
#include <unistd.h>int rmdir(const char *path);
#include <dirent.h>DIR *opendir(const char *dirname);struct dirent *readdir(DIR *dirp);int closedir(DIR *dirp);void rewinddir(DIR *dirp);
#include <dirent.h>#include <errno.h>#include <stdio.h>int main(int argc, char *argv[]){struct dirent *direntp;DIR *dirp;if(argc != 2){fprintf(stderr,"Usage %s directory_name\n", argv[0]);}if((dirp = opendir(argv[1])) == NULL){perror("Failed to open directory");return 1;}while((direntp = readdir(dirp)) != NULL)printf("%s\n", direntp->d_name);while((closedir(dirp) == -1) && (errno == EINTR)) ;return 0; }
struct dirent{ino_t d_ino; /* inode number */off_t d_off; /* offset to the next dirent */unsigned short d_reclen; /* length of this record */unsigned char d_type; /* type of file */char d_name[256]; /*filename*/};
#define _BSD_SOURCE#include <sys/types.h>#include <dirent.h>int dirfd(DIR *dir);
#include <unistd.h>int link(const char *path1, const char *path2);int unlink(const char *path);
#include <stdio.h>#include <unistd.h>if (link("/dirA/name1","dirB/name2") == -1)perror("Failed to make a new link in /dirB");
#include <unistd.h>int symlink(const char *path1, const char *path2);
#include <stdio.h>int rename(const char *oldpath, const char *newpath);
#include <stdio.h>char *tmpnam(char *ptr); /*返回执行路径的指针*/FILE *tmpfile(void); /*返回文件指针,错误则为NULL*/
#include <stdio.h>int main(void){char name[L_tmpnam],line[MAXLINE];FILE *fp;printf("%s\n",tmpnam(NULL));tmpname(name);printf("%s\n",name);if((fp = tmpfile()) == NULL){perror("tmpfile");return 1;}fputs("one line of output\n",fp);rewind(fp);if(fgets(line,sizeof(line),fp) == NULL){perror("fgets");return 1;}fputs(line,stdout);return 0;}
#include <stdio.h>char *tempnam(const char *directory, const char *prefix);int mkstemp(char *template);
#include <stdio.h>int main(int argc, char *argv[]){if(argc != 3){pintf("usage: %s <directory> <prefix>",argv[0]);return 1;}printf("%s\n",tempnam(argv[1][0] != ' ' ? argv[1] : NULL, argv[2][0] != ' ' ? argv[2] : NULL));return 0;}