Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1.  
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <time.h>
  10. #include <string.h>
  11. #include <locale.h>
  12. #include <dirent.h>
  13.  
  14.  
  15. typedef struct tree_element {
  16. int type; //0=file 1=folder
  17. char *directory; //file directory
  18. struct tree_element *next;
  19. } tree_element;
  20.  
  21. tree_element *get_files(char *directory,tree_element *tree, int recursive) {
  22. if(strcmp(directory,".")==0)
  23. directory="./";
  24. DIR *d;
  25. struct dirent *dir;
  26. d = opendir(directory);
  27. if (d) {
  28. while ((dir = readdir(d)) != NULL) {
  29. if (dir->d_name[0] != '.' && dir->d_name[strlen(dir->d_name)-1] != '~') {
  30. char temp_dir[sizeof(directory)+sizeof(dir->d_name)];
  31. strcpy(temp_dir,directory);
  32. strcat(temp_dir,dir->d_name);
  33. strcat(temp_dir,"/");
  34. tree->next=malloc(sizeof(tree_element));
  35. tree=tree->next;
  36. tree->directory=strdup(temp_dir);
  37. tree->type=0;
  38. tree->next=NULL;
  39. if (opendir(temp_dir)) {
  40. tree->type=1;
  41. if(recursive)
  42. tree=get_files(temp_dir,tree,recursive);
  43. }
  44. }
  45. }
  46. closedir(d);
  47. }
  48. return tree;
  49. }
  50.  
  51. int cp(const char *to, const char *from){
  52. int fd_to, fd_from;
  53. char buf[4096];
  54. ssize_t nread;
  55. int saved_errno;
  56.  
  57. fd_from = open(from, O_RDONLY);
  58. if (fd_from < 0)
  59. return -1;
  60.  
  61. fd_to = open(to, O_WRONLY | O_CREAT | O_EXCL, 0666);
  62. if (fd_to < 0)
  63. goto out_error;
  64.  
  65. while (nread = read(fd_from, buf, sizeof buf), nread > 0)
  66. {
  67. char *out_ptr = buf;
  68. ssize_t nwritten;
  69.  
  70. do {
  71. nwritten = write(fd_to, out_ptr, nread);
  72.  
  73. if (nwritten >= 0)
  74. {
  75. nread -= nwritten;
  76. out_ptr += nwritten;
  77. }
  78. else if (errno != EINTR)
  79. {
  80. goto out_error;
  81. }
  82. } while (nread > 0);
  83. }
  84.  
  85. if (nread == 0)
  86. {
  87. if (close(fd_to) < 0)
  88. {
  89. fd_to = -1;
  90. goto out_error;
  91. }
  92. close(fd_from);
  93.  
  94. /* Success! */
  95. return 0;
  96. }
  97.  
  98. out_error:
  99. saved_errno = errno;
  100.  
  101. close(fd_from);
  102. if (fd_to >= 0)
  103. close(fd_to);
  104.  
  105. errno = saved_errno;
  106. return -1;
  107. }
  108.  
  109. int check (char *plik1, char *plik2){
  110. struct stat staty1;
  111. stat(plik1,&staty1);
  112. struct stat staty2;
  113. stat(plik2,&staty2);
  114. return staty1.st_mtime>staty2.st_mtime ? 0 : 1;//0 zamienic
  115. }
  116.  
  117.  
  118. int remove_directory(const char *path){
  119. DIR *d = opendir(path);
  120. size_t path_len = strlen(path);
  121. int r = -1;
  122.  
  123. if (d)
  124. {
  125. struct dirent *p;
  126.  
  127. r = 0;
  128.  
  129. while (!r && (p=readdir(d)))
  130. {
  131. int r2 = -1;
  132. char *buf;
  133. size_t len;
  134.  
  135. /* Skip the names "." and ".." as we don't want to recurse on them. */
  136. if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
  137. {
  138. continue;
  139. }
  140.  
  141. len = path_len + strlen(p->d_name) + 2;
  142. buf = malloc(len);
  143.  
  144. if (buf)
  145. {
  146. struct stat statbuf;
  147.  
  148. snprintf(buf, len, "%s/%s", path, p->d_name);
  149.  
  150. if (!stat(buf, &statbuf))
  151. {
  152. if (S_ISDIR(statbuf.st_mode))
  153. {
  154. r2 = remove_directory(buf);
  155. }
  156. else
  157. {
  158. r2 = unlink(buf);
  159. }
  160. }
  161.  
  162. free(buf);
  163. }
  164.  
  165. r = r2;
  166. }
  167.  
  168. closedir(d);
  169. }
  170.  
  171. if (!r)
  172. {
  173. r = rmdir(path);
  174. }
  175.  
  176. return r;
  177. }
  178. char *cutParentDir(char *dir) {
  179. int startIndex=1;
  180. if(dir[0]=='.')
  181. startIndex=2;
  182. int i=startIndex;
  183. while(dir[i]!='/')
  184. i++;
  185. return &dir[i];
  186. }
  187.  
  188. void DeleteFilesExcess(tree_element *wsk1, tree_element *wsk2){ // najpierw podajemy zrodlo a potem gdzie zapisujemy
  189. tree_element *copy1;
  190. tree_element *copy2=wsk2;
  191. do{
  192. copy1=wsk1;
  193. do{
  194. printf("%s %s\n",cutParentDir(copy1->directory),cutParentDir(copy2->directory));
  195. if(strcmp(cutParentDir(copy1->directory),cutParentDir(copy2->directory))==0)
  196. break;
  197. else{
  198. if(copy1->next==NULL){
  199. if(copy2->type==0){
  200. printf("Usuwam plik: %s\n",copy2->directory);
  201. char b[strlen(copy2->directory)-1];
  202. strcpy(b,copy2->directory);
  203. b[strlen(b)-1]='\0';
  204. remove(b);
  205. } else {
  206. printf("Usuwam plik : %s\n",copy2->directory);
  207. }
  208. copy1=copy1->next;
  209. } else
  210. copy1=copy1->next;
  211. }
  212. }while(!(copy1==NULL));
  213. if(copy2->next!=NULL)
  214. copy2=copy2->next;
  215. else
  216. break;
  217. }while(copy2!=NULL);
  218. }
  219.  
  220. void AddFilesExcess(tree_element *wsk1, tree_element *wsk2){ // najpierw podajemy zrodlo a potem gdzie zapisujemy
  221. tree_element *copy1=wsk1;
  222. tree_element *copy2;
  223. do{
  224. copy2=wsk2;
  225. do{
  226. printf("%s %s\n",cutParentDir(copy1->directory),cutParentDir(copy2->directory));
  227. if(strcmp(cutParentDir(copy1->directory),cutParentDir(copy2->directory))==0)
  228. if(!check(copy1->directory,copy2->directory)){
  229. cp(copy2->directory,copy1->directory);
  230. } else
  231. break;
  232. else{
  233.  
  234. }
  235. } while(copy2!=NULL);
  236. if(copy1->next!=NULL)
  237. copy1=copy1->next;
  238. else
  239. break;
  240. }while(copy1!=NULL);
  241. }
  242.  
  243. int mk_path(char *path ,int type, char *root) {
  244. if(type==1){
  245. char current_dir[strlen(path)];
  246. struct stat st = {0};
  247. memset(current_dir, '\0', sizeof(current_dir));
  248. int startIndex=1,i,pathParts=1;
  249. current_dir[0]='/';
  250. if(path[0]=='.') {
  251. startIndex=2;
  252. current_dir[0]='.';
  253. current_dir[1]='/';
  254. }
  255. for(i=startIndex;i<strlen(path);i++) {
  256. if(path[i]!='/') {
  257. current_dir[i]=path[i];
  258. }
  259. else {
  260. if (stat(current_dir, &st) == -1)
  261. mkdir(current_dir, 0705);
  262. current_dir[i]=path[i];
  263. }
  264. }
  265. }
  266. else{
  267. int t=strlen(path);
  268. char temp[t],dir_from[t+strlen(root)];
  269. char *dir_wo_root;
  270. memset(dir_from, '\0', sizeof(dir_from));
  271. strcpy(dir_from,root);
  272. strcpy(temp,path);
  273. int start=0;
  274. if(temp[0]=='.')
  275. start=2;
  276. else if(temp[0]=='/')
  277. start=1;
  278. while(temp[start++]!='/') {
  279. dir_wo_root=&temp[start];
  280. printf("%d\n",start);
  281. }
  282. strcat(dir_from,dir_wo_root);
  283. while(temp[--t]!='/')
  284. temp[t]='\0';
  285. mk_path(temp,1,"");
  286. cp(dir_from,path);
  287. }
  288. return 0;
  289. }
  290.  
  291. char *getFromDirRoot(char *path){
  292. char *root=malloc(sizeof(char)*strlen(path));
  293. strcpy(root,path);
  294. int start=1;
  295. if(path[0]=='.')
  296. start=2;
  297. while(root[start++]!='/');
  298. root[start]='\0';
  299. return root;
  300. }
  301.  
  302. int main(int argc, char* argv[] ) {
  303. int rflag = 0,c=-1;
  304. struct stat statinfo;
  305. char dir_from[strlen(argv[1])],dir_to[strlen(argv[2])];
  306.  
  307. opterr = 0;
  308.  
  309. if (stat(argv[1], &statinfo) != 0 || stat(argv[2], &statinfo)!=0) {
  310. printf("%s\n","Jedna ze sciezek jest niepoprawna.");
  311. return 0;
  312. }
  313.  
  314. strcpy(dir_from,argv[1]);
  315. strcpy(dir_to,argv[2]);
  316.  
  317. if(strcpy(dir_to,dir_from)) {
  318. printf("%s\n","Podane sciezki sa takie same");
  319. return 0;
  320. }
  321.  
  322. while ((c = getopt (argc, argv, "R")) != -1)
  323. switch (c){
  324. case 'R':
  325. rflag = 1;
  326. break;
  327. case '?':
  328. if (isprint (optopt))
  329. fprintf (stderr, "Unknown option `-%c'.\n", optopt);
  330. else
  331. fprintf (stderr,"Unknown option character `\\x%x'.\n",optopt);
  332. return 1;
  333. default:
  334. abort ();
  335. }
  336. printf("%s %s %d",dir_from,dir_to,rflag);
  337. return 0;
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement