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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| void backup (char*source, char*ref, char*dest)
{
DIR*dp1;
struct dirent *d1;
if ((access(source,F_OK)) == -1 )
raler("Source file inexistant");
if((access(source,X_OK)) == -1)
raler("Source non parcourable");
dp1=opendir(source);
if(dp1==NULL)
raler("Error opening source file");
if(access(ref,F_OK) == -1 )
mkdir(ref,0777);
DIR*dp2;
dp2=opendir(ref);
if(dp2==NULL)
raler("Error opening reference directory");
if ((access(dest,F_OK)) == 0 )
raler("Destination directory already exists");
int fp1;
fp1=mkdir(dest,0777);
if(fp1<0)
raler("Error creating destination directory");
DIR*dp3;
dp3=opendir(dest);
if(dp3==NULL)
raler("Error opening destination directory");
while((d1=readdir(dp1)) != NULL){
if((strcmp("..",d1->d_name) != 0) &&
(strcmp(".",d1->d_name) != 0)) {
char source_path[CHEMIN_MAX]="";
strcat(source_path,source);
strcat(source_path,"/");
strcat(source_path,d1->d_name);
char dest_path[CHEMIN_MAX]="";
strcat(dest_path,dest);
strcat(dest_path,"/");
strcat(dest_path,d1->d_name);
char ref_path[CHEMIN_MAX]="";
strcat(ref_path,ref);
strcat(ref_path,"/");
strcat(ref_path,d1->d_name);
int fptmp;
struct stat sttmp;
fptmp=stat(source_path, &sttmp);
if(fptmp == -1)
raler("Stat source path error");
printf("Source_path : %s ",source_path);
switch (sttmp.st_mode & S_IFMT){
case (S_IFDIR):{
backup(source_path,ref_path,dest_path);
break;
}
case(S_IFLNK) : {
char linked[CHEMIN_MAX];
strcat(linked,dest_path);
strcat(linked,"/");
ssize_t read = readlink(source_path,linked,CHEMIN_MAX);
if(read == -1 )
raler("Readlink");
strcat(linked,linked);
symlink(dest_path,linked);
break;
}
case(S_IFREG):{
printf("est un fichier régulier\n");
if(access(ref_path,F_OK) == 0){ int fptmp_ref;
struct stat sttmp_ref;
fptmp_ref=stat(ref_path, &sttmp_ref);
if(fptmp_ref == -1)
raler("Stat ref path error");
if(!regular_unchanged(&sttmp,&sttmp_ref)){
copy_reg(source_path,dest_path);
int fd_cp=open(dest_path,O_RDWR,0666);
if(fd_cp < 0)raler("Ouverture lien");
regular_set_amtime(&sttmp,fd_cp);
}
else {
link(ref_path,dest_path);
int fd_link=open(dest_path,O_RDWR,0666);
if(fd_link < 0)raler("Ouverture lien");
regular_set_amtime(&sttmp,fd_link);
}
}
else {
copy_reg(source_path,dest_path);
int fd_cp=open(dest_path,O_RDWR,0666);
if(fd_cp < 0)raler("Ouverture lien");
regular_set_amtime(&sttmp,fd_cp);
}
break;
}
default : {
printf("Fichier ignoré: %s\n",source_path);
break;
}
}
}
}
if((closedir(dp1) == -1) || (closedir(dp2) == -1) || (closedir(dp3) == -1) )
raler("Error closing files");
} |
Partager