1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
int checkAllFilesInDir(char * directory){
DIR * dir;
struct dirent * file;
if ((dir = opendir(directory))==NULL){
return EXIT_FAILURE;
}
while ((file = readdir(dir))!=NULL){
if (strcmp(file->d_name,".")!=0 && strcmp(file->d_name,"..")!= 0){
struct stat file_info;
char file_path[0x100];
strncpy(file_path, directory, sizeof(file_path));
strncat(file_path, "/", sizeof(file_path));
strncat(file_path, file->d_name, sizeof(file_path));
if (lstat(file_path, &file_info)!=-1){
if (S_ISDIR(file_info.st_mode)){
closedir(dir);
checkAllFilesInDir(file_path);
}
if (file_info.st_nlink > 1){
int fh;
if ((fh = open("./data", O_CREAT|O_APPEND|O_RDWR,0777))==-1){
perror("Unable to open data");
return EXIT_FAILURE;
}
if ((fh = write(fh, file_path, strlen(file_path)))==-1){
perror("Unable to write to data");
return EXIT_FAILURE;
}
if (close(fh)==-1){
perror("Close error");
return EXIT_FAILURE;
}
}
int fh2;
ino_t inode;
if ((fh2 = open("data2", O_CREAT|O_APPEND|O_RDWR,0777))==-1){
perror("Unable to open inos");
return EXIT_FAILURE;
}
inode = file_info.st_ino;
if (fh2 = write(fh2, inode, sizeof(ino_t))==-1){
perror("Unable to write to inos");
return EXIT_FAILURE;
}
if (close(fh2)==-1){
perror("Close error");
return EXIT_FAILURE;
}
}
}
}
closedir(dir);
return EXIT_SUCCESS;
} |
Partager