summaryrefslogtreecommitdiff
path: root/legacy/ecore/src/lib/ecore_file/ecore_file.c
diff options
context:
space:
mode:
authorVincent Torri <vincent.torri@gmail.com>2010-09-30 07:25:06 +0000
committerVincent Torri <vincent.torri@gmail.com>2010-09-30 07:25:06 +0000
commit519007d376d6736154c4f8619aea4dbb176b8bf4 (patch)
treefd5cc5047918c3a5eaf62f6c484cc715c09353c6 /legacy/ecore/src/lib/ecore_file/ecore_file.c
parent586334710d39f761b3bef30b8e1f8d8832aa536f (diff)
tab--
SVN revision: 52920
Diffstat (limited to '')
-rw-r--r--legacy/ecore/src/lib/ecore_file/ecore_file.c488
1 files changed, 244 insertions, 244 deletions
diff --git a/legacy/ecore/src/lib/ecore_file/ecore_file.c b/legacy/ecore/src/lib/ecore_file/ecore_file.c
index 041ad6e8b4..a746570463 100644
--- a/legacy/ecore/src/lib/ecore_file/ecore_file.c
+++ b/legacy/ecore/src/lib/ecore_file/ecore_file.c
@@ -223,8 +223,8 @@ ecore_file_mksubdirs(const char *base, const char **subdirs)
223 223
224 if (buf[baselen - 1] != '/') 224 if (buf[baselen - 1] != '/')
225 { 225 {
226 buf[baselen] = '/'; 226 buf[baselen] = '/';
227 baselen++; 227 baselen++;
228 } 228 }
229#else 229#else
230 dir = opendir(base); 230 dir = opendir(base);
@@ -236,36 +236,36 @@ ecore_file_mksubdirs(const char *base, const char **subdirs)
236 i = 0; 236 i = 0;
237 for (; *subdirs; subdirs++) 237 for (; *subdirs; subdirs++)
238 { 238 {
239 struct stat st; 239 struct stat st;
240 240
241#ifndef HAVE_ATFILE_SOURCE 241#ifndef HAVE_ATFILE_SOURCE
242 eina_strlcpy(buf + baselen, *subdirs, sizeof(buf) - baselen); 242 eina_strlcpy(buf + baselen, *subdirs, sizeof(buf) - baselen);
243 if (stat(buf, &st) == 0) 243 if (stat(buf, &st) == 0)
244#else 244#else
245 if (fstatat(fd, *subdirs, &st, 0) == 0) 245 if (fstatat(fd, *subdirs, &st, 0) == 0)
246#endif 246#endif
247 { 247 {
248 if (S_ISDIR(st.st_mode)) 248 if (S_ISDIR(st.st_mode))
249 { 249 {
250 i++; 250 i++;
251 continue; 251 continue;
252 } 252 }
253 } 253 }
254 else 254 else
255 { 255 {
256 if (errno == ENOENT) 256 if (errno == ENOENT)
257 { 257 {
258#ifndef HAVE_ATFILE_SOURCE 258#ifndef HAVE_ATFILE_SOURCE
259 if (mkdir(buf, default_mode) == 0) 259 if (mkdir(buf, default_mode) == 0)
260#else 260#else
261 if (mkdirat(fd, *subdirs, default_mode) == 0) 261 if (mkdirat(fd, *subdirs, default_mode) == 0)
262#endif 262#endif
263 { 263 {
264 i++; 264 i++;
265 continue; 265 continue;
266 } 266 }
267 } 267 }
268 } 268 }
269 } 269 }
270 270
271#ifdef HAVE_ATFILE_SOURCE 271#ifdef HAVE_ATFILE_SOURCE
@@ -333,23 +333,23 @@ ecore_file_recursive_rm(const char *dir)
333 ret = stat(dir, &st); 333 ret = stat(dir, &st);
334 if ((ret == 0) && (S_ISDIR(st.st_mode))) 334 if ((ret == 0) && (S_ISDIR(st.st_mode)))
335 { 335 {
336 ret = 1; 336 ret = 1;
337 if (stat(dir, &st) == -1) return EINA_FALSE; 337 if (stat(dir, &st) == -1) return EINA_FALSE;
338 dirp = opendir(dir); 338 dirp = opendir(dir);
339 if (dirp) 339 if (dirp)
340 { 340 {
341 while ((dp = readdir(dirp))) 341 while ((dp = readdir(dirp)))
342 { 342 {
343 if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, ".."))) 343 if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, "..")))
344 { 344 {
345 snprintf(path, PATH_MAX, "%s/%s", dir, dp->d_name); 345 snprintf(path, PATH_MAX, "%s/%s", dir, dp->d_name);
346 if (!ecore_file_recursive_rm(path)) 346 if (!ecore_file_recursive_rm(path))
347 ret = 0; 347 ret = 0;
348 } 348 }
349 } 349 }
350 closedir(dirp); 350 closedir(dirp);
351 } 351 }
352 if (!ecore_file_rmdir(dir)) ret = 0; 352 if (!ecore_file_rmdir(dir)) ret = 0;
353 if (ret) 353 if (ret)
354 return EINA_TRUE; 354 return EINA_TRUE;
355 else 355 else
@@ -357,8 +357,8 @@ ecore_file_recursive_rm(const char *dir)
357 } 357 }
358 else 358 else
359 { 359 {
360 if (ret == -1) return EINA_FALSE; 360 if (ret == -1) return EINA_FALSE;
361 return ecore_file_unlink(dir); 361 return ecore_file_unlink(dir);
362 } 362 }
363} 363}
364 364
@@ -393,13 +393,13 @@ ecore_file_mkpath(const char *path)
393 393
394 for (i = 0; path[i] != '\0'; ss[i] = path[i], i++) 394 for (i = 0; path[i] != '\0'; ss[i] = path[i], i++)
395 { 395 {
396 if (i == sizeof(ss) - 1) return EINA_FALSE; 396 if (i == sizeof(ss) - 1) return EINA_FALSE;
397 if ((path[i] == '/') && (i > 0)) 397 if ((path[i] == '/') && (i > 0))
398 { 398 {
399 ss[i] = '\0'; 399 ss[i] = '\0';
400 if (!_ecore_file_mkpath_if_not_exists(ss)) 400 if (!_ecore_file_mkpath_if_not_exists(ss))
401 return EINA_FALSE; 401 return EINA_FALSE;
402 } 402 }
403 } 403 }
404 ss[i] = '\0'; 404 ss[i] = '\0';
405 return _ecore_file_mkpath_if_not_exists(ss); 405 return _ecore_file_mkpath_if_not_exists(ss);
@@ -449,12 +449,12 @@ ecore_file_cp(const char *src, const char *dst)
449 f2 = fopen(dst, "wb"); 449 f2 = fopen(dst, "wb");
450 if (!f2) 450 if (!f2)
451 { 451 {
452 fclose(f1); 452 fclose(f1);
453 return EINA_FALSE; 453 return EINA_FALSE;
454 } 454 }
455 while ((num = fread(buf, 1, sizeof(buf), f1)) > 0) 455 while ((num = fread(buf, 1, sizeof(buf), f1)) > 0)
456 { 456 {
457 if (fwrite(buf, 1, num, f2) != num) ret = EINA_FALSE; 457 if (fwrite(buf, 1, num, f2) != num) ret = EINA_FALSE;
458 } 458 }
459 fclose(f1); 459 fclose(f1);
460 fclose(f2); 460 fclose(f2);
@@ -477,55 +477,55 @@ ecore_file_mv(const char *src, const char *dst)
477 { 477 {
478 // File cannot be moved directly because 478 // File cannot be moved directly because
479 // it resides on a different mount point. 479 // it resides on a different mount point.
480 if (errno == EXDEV) 480 if (errno == EXDEV)
481 { 481 {
482 struct stat st; 482 struct stat st;
483 483
484 // Make sure this is a regular file before 484 // Make sure this is a regular file before
485 // we do anything fancy. 485 // we do anything fancy.
486 stat(src, &st); 486 stat(src, &st);
487 if (S_ISREG(st.st_mode)) 487 if (S_ISREG(st.st_mode))
488 { 488 {
489 char *dir; 489 char *dir;
490 490
491 dir = ecore_file_dir_get(dst); 491 dir = ecore_file_dir_get(dst);
492 // Since we can't directly rename, try to 492 // Since we can't directly rename, try to
493 // copy to temp file in the dst directory 493 // copy to temp file in the dst directory
494 // and then rename. 494 // and then rename.
495 snprintf(buf, sizeof(buf), "%s/.%s.tmp.XXXXXX", 495 snprintf(buf, sizeof(buf), "%s/.%s.tmp.XXXXXX",
496 dir, ecore_file_file_get(dst)); 496 dir, ecore_file_file_get(dst));
497 free(dir); 497 free(dir);
498 fd = mkstemp(buf); 498 fd = mkstemp(buf);
499 if (fd < 0) 499 if (fd < 0)
500 { 500 {
501 perror("mkstemp"); 501 perror("mkstemp");
502 goto FAIL; 502 goto FAIL;
503 } 503 }
504 close(fd); 504 close(fd);
505 505
506 // Copy to temp file 506 // Copy to temp file
507 if (!ecore_file_cp(src, buf)) 507 if (!ecore_file_cp(src, buf))
508 goto FAIL; 508 goto FAIL;
509 509
510 // Set file permissions of temp file to match src 510 // Set file permissions of temp file to match src
511 chmod(buf, st.st_mode); 511 chmod(buf, st.st_mode);
512 512
513 // Try to atomically move temp file to dst 513 // Try to atomically move temp file to dst
514 if (rename(buf, dst)) 514 if (rename(buf, dst))
515 { 515 {
516 // If we still cannot atomically move 516 // If we still cannot atomically move
517 // do a normal copy and hope for the best. 517 // do a normal copy and hope for the best.
518 if (!ecore_file_cp(buf, dst)) 518 if (!ecore_file_cp(buf, dst))
519 goto FAIL; 519 goto FAIL;
520 } 520 }
521 521
522 // Delete temporary file and src 522 // Delete temporary file and src
523 ecore_file_unlink(buf); 523 ecore_file_unlink(buf);
524 ecore_file_unlink(src); 524 ecore_file_unlink(src);
525 goto PASS; 525 goto PASS;
526 } 526 }
527 } 527 }
528 goto FAIL; 528 goto FAIL;
529 } 529 }
530 530
531PASS: 531PASS:
@@ -682,11 +682,11 @@ ecore_file_ls(const char *dir)
682 682
683 while ((dp = readdir(dirp))) 683 while ((dp = readdir(dirp)))
684 { 684 {
685 if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, ".."))) 685 if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, "..")))
686 { 686 {
687 f = strdup(dp->d_name); 687 f = strdup(dp->d_name);
688 list = eina_list_append(list, f); 688 list = eina_list_append(list, f);
689 } 689 }
690 } 690 }
691 closedir(dirp); 691 closedir(dirp);
692 692
@@ -713,61 +713,61 @@ restart:
713 exe1 = p; 713 exe1 = p;
714 while (*p) 714 while (*p)
715 { 715 {
716 if (in_quot_sing) 716 if (in_quot_sing)
717 { 717 {
718 if (*p == '\'') 718 if (*p == '\'')
719 in_quot_sing = 0; 719 in_quot_sing = 0;
720 } 720 }
721 else if (in_quot_dbl) 721 else if (in_quot_dbl)
722 { 722 {
723 if (*p == '\"') 723 if (*p == '\"')
724 in_quot_dbl = 0; 724 in_quot_dbl = 0;
725 } 725 }
726 else 726 else
727 { 727 {
728 if (*p == '\'') 728 if (*p == '\'')
729 in_quot_sing = 1; 729 in_quot_sing = 1;
730 else if (*p == '\"') 730 else if (*p == '\"')
731 in_quot_dbl = 1; 731 in_quot_dbl = 1;
732 if ((isspace(*p)) && (!((p > app) && (p[-1] != '\\')))) 732 if ((isspace(*p)) && (!((p > app) && (p[-1] != '\\'))))
733 break; 733 break;
734 } 734 }
735 p++; 735 p++;
736 } 736 }
737 exe2 = p; 737 exe2 = p;
738 if (exe2 == exe1) return NULL; 738 if (exe2 == exe1) return NULL;
739 if (*exe1 == '~') 739 if (*exe1 == '~')
740 { 740 {
741 char *homedir; 741 char *homedir;
742 int len; 742 int len;
743 743
744 /* Skip ~ */ 744 /* Skip ~ */
745 exe1++; 745 exe1++;
746 746
747 homedir = getenv("HOME"); 747 homedir = getenv("HOME");
748 if (!homedir) return NULL; 748 if (!homedir) return NULL;
749 len = strlen(homedir); 749 len = strlen(homedir);
750 if (exe) free(exe); 750 if (exe) free(exe);
751 exe = malloc(len + exe2 - exe1 + 2); 751 exe = malloc(len + exe2 - exe1 + 2);
752 if (!exe) return NULL; 752 if (!exe) return NULL;
753 pp = exe; 753 pp = exe;
754 if (len) 754 if (len)
755 { 755 {
756 strcpy(exe, homedir); 756 strcpy(exe, homedir);
757 pp += len; 757 pp += len;
758 if (*(pp - 1) != '/') 758 if (*(pp - 1) != '/')
759 { 759 {
760 *pp = '/'; 760 *pp = '/';
761 pp++; 761 pp++;
762 } 762 }
763 } 763 }
764 } 764 }
765 else 765 else
766 { 766 {
767 if (exe) free(exe); 767 if (exe) free(exe);
768 exe = malloc(exe2 - exe1 + 1); 768 exe = malloc(exe2 - exe1 + 1);
769 if (!exe) return NULL; 769 if (!exe) return NULL;
770 pp = exe; 770 pp = exe;
771 } 771 }
772 p = exe1; 772 p = exe1;
773 restart = 0; 773 restart = 0;
@@ -775,69 +775,69 @@ restart:
775 in_quot_sing = 0; 775 in_quot_sing = 0;
776 while (*p) 776 while (*p)
777 { 777 {
778 if (in_quot_sing) 778 if (in_quot_sing)
779 { 779 {
780 if (*p == '\'') 780 if (*p == '\'')
781 in_quot_sing = 0; 781 in_quot_sing = 0;
782 else 782 else
783 { 783 {
784 *pp = *p; 784 *pp = *p;
785 pp++; 785 pp++;
786 } 786 }
787 } 787 }
788 else if (in_quot_dbl) 788 else if (in_quot_dbl)
789 { 789 {
790 if (*p == '\"') 790 if (*p == '\"')
791 in_quot_dbl = 0; 791 in_quot_dbl = 0;
792 else 792 else
793 { 793 {
794 /* techcincally this is wrong. double quotes also accept 794 /* techcincally this is wrong. double quotes also accept
795 * special chars: 795 * special chars:
796 * 796 *
797 * $, `, \ 797 * $, `, \
798 */ 798 */
799 *pp = *p; 799 *pp = *p;
800 pp++; 800 pp++;
801 } 801 }
802 } 802 }
803 else 803 else
804 { 804 {
805 /* technically we should handle special chars: 805 /* technically we should handle special chars:
806 * 806 *
807 * $, `, \, etc. 807 * $, `, \, etc.
808 */ 808 */
809 if ((p > exe1) && (p[-1] == '\\')) 809 if ((p > exe1) && (p[-1] == '\\'))
810 { 810 {
811 if (*p != '\n') 811 if (*p != '\n')
812 { 812 {
813 *pp = *p; 813 *pp = *p;
814 pp++; 814 pp++;
815 } 815 }
816 } 816 }
817 else if ((p > exe1) && (*p == '=')) 817 else if ((p > exe1) && (*p == '='))
818 { 818 {
819 restart = 1; 819 restart = 1;
820 *pp = *p; 820 *pp = *p;
821 pp++; 821 pp++;
822 } 822 }
823 else if (*p == '\'') 823 else if (*p == '\'')
824 in_quot_sing = 1; 824 in_quot_sing = 1;
825 else if (*p == '\"') 825 else if (*p == '\"')
826 in_quot_dbl = 1; 826 in_quot_dbl = 1;
827 else if (isspace(*p)) 827 else if (isspace(*p))
828 { 828 {
829 if (restart) 829 if (restart)
830 goto restart; 830 goto restart;
831 else 831 else
832 break; 832 break;
833 } 833 }
834 else 834 else
835 { 835 {
836 *pp = *p; 836 *pp = *p;
837 pp++; 837 pp++;
838 } 838 }
839 } 839 }
840 p++; 840 p++;
841 } 841 }
842 *pp = 0; 842 *pp = 0;
843 return exe; 843 return exe;
@@ -860,24 +860,24 @@ ecore_file_escape_name(const char *filename)
860 q = buf; 860 q = buf;
861 while (*p) 861 while (*p)
862 { 862 {
863 if ((q - buf) > (PATH_MAX - 6)) return NULL; 863 if ((q - buf) > (PATH_MAX - 6)) return NULL;
864 if ( 864 if (
865 (*p == ' ') || (*p == '\t') || (*p == '\n') || 865 (*p == ' ') || (*p == '\t') || (*p == '\n') ||
866 (*p == '\\') || (*p == '\'') || (*p == '\"') || 866 (*p == '\\') || (*p == '\'') || (*p == '\"') ||
867 (*p == ';') || (*p == '!') || (*p == '#') || 867 (*p == ';') || (*p == '!') || (*p == '#') ||
868 (*p == '$') || (*p == '%') || (*p == '&') || 868 (*p == '$') || (*p == '%') || (*p == '&') ||
869 (*p == '*') || (*p == '(') || (*p == ')') || 869 (*p == '*') || (*p == '(') || (*p == ')') ||
870 (*p == '[') || (*p == ']') || (*p == '{') || 870 (*p == '[') || (*p == ']') || (*p == '{') ||
871 (*p == '}') || (*p == '|') || (*p == '<') || 871 (*p == '}') || (*p == '|') || (*p == '<') ||
872 (*p == '>') || (*p == '?') 872 (*p == '>') || (*p == '?')
873 ) 873 )
874 { 874 {
875 *q = '\\'; 875 *q = '\\';
876 q++; 876 q++;
877 } 877 }
878 *q = *p; 878 *q = *p;
879 q++; 879 q++;
880 p++; 880 p++;
881 } 881 }
882 *q = 0; 882 *q = 0;
883 return strdup(buf); 883 return strdup(buf);
@@ -898,12 +898,12 @@ ecore_file_strip_ext(const char *path)
898 file = strdup(path); 898 file = strdup(path);
899 else if (p != path) 899 else if (p != path)
900 { 900 {
901 file = malloc(((p - path) + 1) * sizeof(char)); 901 file = malloc(((p - path) + 1) * sizeof(char));
902 if (file) 902 if (file)
903 { 903 {
904 memcpy(file, path, (p - path)); 904 memcpy(file, path, (p - path));
905 file[p - path] = 0; 905 file[p - path] = 0;
906 } 906 }
907 } 907 }
908 908
909 return file; 909 return file;
@@ -925,11 +925,11 @@ ecore_file_dir_is_empty(const char *dir)
925 925
926 while ((dp = readdir(dirp))) 926 while ((dp = readdir(dirp)))
927 { 927 {
928 if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, ".."))) 928 if ((strcmp(dp->d_name, ".")) && (strcmp(dp->d_name, "..")))
929 { 929 {
930 closedir(dirp); 930 closedir(dirp);
931 return 0; 931 return 0;
932 } 932 }
933 } 933 }
934 934
935 closedir(dirp); 935 closedir(dirp);