Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fcntl.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <time.h>
- #include <string.h>
- #include <locale.h>
- #include <dirent.h>
- typedef struct tree_element {
- int type; //0=file 1=folder
- char *directory; //file directory
- struct tree_element *next;
- } tree_element;
- tree_element *get_files(char *directory,tree_element *tree, int recursive) {
- if(strcmp(directory,".")==0)
- directory="./";
- DIR *d;
- struct dirent *dir;
- d = opendir(directory);
- if (d) {
- while ((dir = readdir(d)) != NULL) {
- if (dir->d_name[0] != '.' && dir->d_name[strlen(dir->d_name)-1] != '~') {
- char temp_dir[sizeof(directory)+sizeof(dir->d_name)];
- strcpy(temp_dir,directory);
- strcat(temp_dir,dir->d_name);
- strcat(temp_dir,"/");
- tree->next=malloc(sizeof(tree_element));
- tree=tree->next;
- tree->directory=strdup(temp_dir);
- tree->type=0;
- tree->next=NULL;
- if (opendir(temp_dir)) {
- tree->type=1;
- if(recursive)
- tree=get_files(temp_dir,tree,recursive);
- }
- }
- }
- closedir(d);
- }
- return tree;
- }
- int cp(const char *to, const char *from){
- int fd_to, fd_from;
- char buf[4096];
- ssize_t nread;
- int saved_errno;
- fd_from = open(from, O_RDONLY);
- if (fd_from < 0)
- return -1;
- fd_to = open(to, O_WRONLY | O_CREAT | O_EXCL, 0666);
- if (fd_to < 0)
- goto out_error;
- while (nread = read(fd_from, buf, sizeof buf), nread > 0)
- {
- char *out_ptr = buf;
- ssize_t nwritten;
- do {
- nwritten = write(fd_to, out_ptr, nread);
- if (nwritten >= 0)
- {
- nread -= nwritten;
- out_ptr += nwritten;
- }
- else if (errno != EINTR)
- {
- goto out_error;
- }
- } while (nread > 0);
- }
- if (nread == 0)
- {
- if (close(fd_to) < 0)
- {
- fd_to = -1;
- goto out_error;
- }
- close(fd_from);
- /* Success! */
- return 0;
- }
- out_error:
- saved_errno = errno;
- close(fd_from);
- if (fd_to >= 0)
- close(fd_to);
- errno = saved_errno;
- return -1;
- }
- int check (char *plik1, char *plik2){
- struct stat staty1;
- stat(plik1,&staty1);
- struct stat staty2;
- stat(plik2,&staty2);
- return staty1.st_mtime>staty2.st_mtime ? 0 : 1;//0 zamienic
- }
- int remove_directory(const char *path){
- DIR *d = opendir(path);
- size_t path_len = strlen(path);
- int r = -1;
- if (d)
- {
- struct dirent *p;
- r = 0;
- while (!r && (p=readdir(d)))
- {
- int r2 = -1;
- char *buf;
- size_t len;
- /* Skip the names "." and ".." as we don't want to recurse on them. */
- if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
- {
- continue;
- }
- len = path_len + strlen(p->d_name) + 2;
- buf = malloc(len);
- if (buf)
- {
- struct stat statbuf;
- snprintf(buf, len, "%s/%s", path, p->d_name);
- if (!stat(buf, &statbuf))
- {
- if (S_ISDIR(statbuf.st_mode))
- {
- r2 = remove_directory(buf);
- }
- else
- {
- r2 = unlink(buf);
- }
- }
- free(buf);
- }
- r = r2;
- }
- closedir(d);
- }
- if (!r)
- {
- r = rmdir(path);
- }
- return r;
- }
- char *cutParentDir(char *dir) {
- int startIndex=1;
- if(dir[0]=='.')
- startIndex=2;
- int i=startIndex;
- while(dir[i]!='/')
- i++;
- return &dir[i];
- }
- void DeleteFilesExcess(tree_element *wsk1, tree_element *wsk2){ // najpierw podajemy zrodlo a potem gdzie zapisujemy
- tree_element *copy1;
- tree_element *copy2=wsk2;
- do{
- copy1=wsk1;
- do{
- printf("%s %s\n",cutParentDir(copy1->directory),cutParentDir(copy2->directory));
- if(strcmp(cutParentDir(copy1->directory),cutParentDir(copy2->directory))==0)
- break;
- else{
- if(copy1->next==NULL){
- if(copy2->type==0){
- printf("Usuwam plik: %s\n",copy2->directory);
- char b[strlen(copy2->directory)-1];
- strcpy(b,copy2->directory);
- b[strlen(b)-1]='\0';
- remove(b);
- } else {
- printf("Usuwam plik : %s\n",copy2->directory);
- }
- copy1=copy1->next;
- } else
- copy1=copy1->next;
- }
- }while(!(copy1==NULL));
- if(copy2->next!=NULL)
- copy2=copy2->next;
- else
- break;
- }while(copy2!=NULL);
- }
- void AddFilesExcess(tree_element *wsk1, tree_element *wsk2){ // najpierw podajemy zrodlo a potem gdzie zapisujemy
- tree_element *copy1=wsk1;
- tree_element *copy2;
- do{
- copy2=wsk2;
- do{
- printf("%s %s\n",cutParentDir(copy1->directory),cutParentDir(copy2->directory));
- if(strcmp(cutParentDir(copy1->directory),cutParentDir(copy2->directory))==0)
- if(!check(copy1->directory,copy2->directory)){
- cp(copy2->directory,copy1->directory);
- } else
- break;
- else{
- }
- } while(copy2!=NULL);
- if(copy1->next!=NULL)
- copy1=copy1->next;
- else
- break;
- }while(copy1!=NULL);
- }
- int mk_path(char *path ,int type, char *root) {
- if(type==1){
- char current_dir[strlen(path)];
- struct stat st = {0};
- memset(current_dir, '\0', sizeof(current_dir));
- int startIndex=1,i,pathParts=1;
- current_dir[0]='/';
- if(path[0]=='.') {
- startIndex=2;
- current_dir[0]='.';
- current_dir[1]='/';
- }
- for(i=startIndex;i<strlen(path);i++) {
- if(path[i]!='/') {
- current_dir[i]=path[i];
- }
- else {
- if (stat(current_dir, &st) == -1)
- mkdir(current_dir, 0705);
- current_dir[i]=path[i];
- }
- }
- }
- else{
- int t=strlen(path);
- char temp[t],dir_from[t+strlen(root)];
- char *dir_wo_root;
- memset(dir_from, '\0', sizeof(dir_from));
- strcpy(dir_from,root);
- strcpy(temp,path);
- int start=0;
- if(temp[0]=='.')
- start=2;
- else if(temp[0]=='/')
- start=1;
- while(temp[start++]!='/') {
- dir_wo_root=&temp[start];
- printf("%d\n",start);
- }
- strcat(dir_from,dir_wo_root);
- while(temp[--t]!='/')
- temp[t]='\0';
- mk_path(temp,1,"");
- cp(dir_from,path);
- }
- return 0;
- }
- char *getFromDirRoot(char *path){
- char *root=malloc(sizeof(char)*strlen(path));
- strcpy(root,path);
- int start=1;
- if(path[0]=='.')
- start=2;
- while(root[start++]!='/');
- root[start]='\0';
- return root;
- }
- int main(int argc, char* argv[] ) {
- int rflag = 0,c=-1;
- struct stat statinfo;
- char dir_from[strlen(argv[1])],dir_to[strlen(argv[2])];
- opterr = 0;
- if (stat(argv[1], &statinfo) != 0 || stat(argv[2], &statinfo)!=0) {
- printf("%s\n","Jedna ze sciezek jest niepoprawna.");
- return 0;
- }
- strcpy(dir_from,argv[1]);
- strcpy(dir_to,argv[2]);
- if(strcpy(dir_to,dir_from)) {
- printf("%s\n","Podane sciezki sa takie same");
- return 0;
- }
- while ((c = getopt (argc, argv, "R")) != -1)
- switch (c){
- case 'R':
- rflag = 1;
- break;
- case '?':
- if (isprint (optopt))
- fprintf (stderr, "Unknown option `-%c'.\n", optopt);
- else
- fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt);
- return 1;
- default:
- abort ();
- }
- printf("%s %s %d",dir_from,dir_to,rflag);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement