Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /****************************************************************************** |
michael@0 | 2 | * Copyright (C) 2009-2013, International Business Machines |
michael@0 | 3 | * Corporation and others. All Rights Reserved. |
michael@0 | 4 | ******************************************************************************* |
michael@0 | 5 | */ |
michael@0 | 6 | |
michael@0 | 7 | #if U_PLATFORM == U_PF_MINGW |
michael@0 | 8 | // *cough* - for struct stat |
michael@0 | 9 | #ifdef __STRICT_ANSI__ |
michael@0 | 10 | #undef __STRICT_ANSI__ |
michael@0 | 11 | #endif |
michael@0 | 12 | #endif |
michael@0 | 13 | |
michael@0 | 14 | #include "filetools.h" |
michael@0 | 15 | #include "filestrm.h" |
michael@0 | 16 | #include "cstring.h" |
michael@0 | 17 | #include "unicode/putil.h" |
michael@0 | 18 | #include "putilimp.h" |
michael@0 | 19 | |
michael@0 | 20 | #include <stdio.h> |
michael@0 | 21 | #include <stdlib.h> |
michael@0 | 22 | #include <sys/stat.h> |
michael@0 | 23 | #include <time.h> |
michael@0 | 24 | #include <string.h> |
michael@0 | 25 | |
michael@0 | 26 | #if U_HAVE_DIRENT_H |
michael@0 | 27 | #include <dirent.h> |
michael@0 | 28 | typedef struct dirent DIRENT; |
michael@0 | 29 | |
michael@0 | 30 | #define MAX_PATH_SIZE 4096 /* Set the limit for the size of the path. */ |
michael@0 | 31 | |
michael@0 | 32 | #define SKIP1 "." |
michael@0 | 33 | #define SKIP2 ".." |
michael@0 | 34 | #endif |
michael@0 | 35 | |
michael@0 | 36 | static int32_t whichFileModTimeIsLater(const char *file1, const char *file2); |
michael@0 | 37 | |
michael@0 | 38 | /* |
michael@0 | 39 | * Goes through the given directory recursive to compare each file's modification time with that of the file given. |
michael@0 | 40 | * Also can be given just one file to check against. Default value for isDir is FALSE. |
michael@0 | 41 | */ |
michael@0 | 42 | U_CAPI UBool U_EXPORT2 |
michael@0 | 43 | isFileModTimeLater(const char *filePath, const char *checkAgainst, UBool isDir) { |
michael@0 | 44 | UBool isLatest = TRUE; |
michael@0 | 45 | |
michael@0 | 46 | if (filePath == NULL || checkAgainst == NULL) { |
michael@0 | 47 | return FALSE; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | if (isDir == TRUE) { |
michael@0 | 51 | #if U_HAVE_DIRENT_H |
michael@0 | 52 | DIR *pDir = NULL; |
michael@0 | 53 | if ((pDir= opendir(checkAgainst)) != NULL) { |
michael@0 | 54 | DIR *subDirp = NULL; |
michael@0 | 55 | DIRENT *dirEntry = NULL; |
michael@0 | 56 | |
michael@0 | 57 | while ((dirEntry = readdir(pDir)) != NULL) { |
michael@0 | 58 | if (uprv_strcmp(dirEntry->d_name, SKIP1) != 0 && uprv_strcmp(dirEntry->d_name, SKIP2) != 0) { |
michael@0 | 59 | char newpath[MAX_PATH_SIZE] = ""; |
michael@0 | 60 | uprv_strcpy(newpath, checkAgainst); |
michael@0 | 61 | uprv_strcat(newpath, U_FILE_SEP_STRING); |
michael@0 | 62 | uprv_strcat(newpath, dirEntry->d_name); |
michael@0 | 63 | |
michael@0 | 64 | if ((subDirp = opendir(newpath)) != NULL) { |
michael@0 | 65 | /* If this new path is a directory, make a recursive call with the newpath. */ |
michael@0 | 66 | closedir(subDirp); |
michael@0 | 67 | isLatest = isFileModTimeLater(filePath, newpath, isDir); |
michael@0 | 68 | if (!isLatest) { |
michael@0 | 69 | break; |
michael@0 | 70 | } |
michael@0 | 71 | } else { |
michael@0 | 72 | int32_t latest = whichFileModTimeIsLater(filePath, newpath); |
michael@0 | 73 | if (latest < 0 || latest == 2) { |
michael@0 | 74 | isLatest = FALSE; |
michael@0 | 75 | break; |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | closedir(pDir); |
michael@0 | 82 | } else { |
michael@0 | 83 | fprintf(stderr, "Unable to open directory: %s\n", checkAgainst); |
michael@0 | 84 | return FALSE; |
michael@0 | 85 | } |
michael@0 | 86 | #endif |
michael@0 | 87 | } else { |
michael@0 | 88 | if (T_FileStream_file_exists(checkAgainst)) { |
michael@0 | 89 | int32_t latest = whichFileModTimeIsLater(filePath, checkAgainst); |
michael@0 | 90 | if (latest < 0 || latest == 2) { |
michael@0 | 91 | isLatest = FALSE; |
michael@0 | 92 | } |
michael@0 | 93 | } else { |
michael@0 | 94 | isLatest = FALSE; |
michael@0 | 95 | } |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | return isLatest; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | /* Compares the mod time of both files returning a number indicating which one is later. -1 if error ocurs. */ |
michael@0 | 102 | static int32_t whichFileModTimeIsLater(const char *file1, const char *file2) { |
michael@0 | 103 | int32_t result = 0; |
michael@0 | 104 | struct stat stbuf1, stbuf2; |
michael@0 | 105 | |
michael@0 | 106 | if (stat(file1, &stbuf1) == 0 && stat(file2, &stbuf2) == 0) { |
michael@0 | 107 | time_t modtime1, modtime2; |
michael@0 | 108 | double diff; |
michael@0 | 109 | |
michael@0 | 110 | modtime1 = stbuf1.st_mtime; |
michael@0 | 111 | modtime2 = stbuf2.st_mtime; |
michael@0 | 112 | |
michael@0 | 113 | diff = difftime(modtime1, modtime2); |
michael@0 | 114 | if (diff < 0.0) { |
michael@0 | 115 | result = 2; |
michael@0 | 116 | } else if (diff > 0.0) { |
michael@0 | 117 | result = 1; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | } else { |
michael@0 | 121 | fprintf(stderr, "Unable to get stats from file: %s or %s\n", file1, file2); |
michael@0 | 122 | result = -1; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | return result; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | /* Swap the file separater character given with the new one in the file path. */ |
michael@0 | 129 | U_CAPI void U_EXPORT2 |
michael@0 | 130 | swapFileSepChar(char *filePath, const char oldFileSepChar, const char newFileSepChar) { |
michael@0 | 131 | for (int32_t i = 0, length = uprv_strlen(filePath); i < length; i++) { |
michael@0 | 132 | filePath[i] = (filePath[i] == oldFileSepChar ) ? newFileSepChar : filePath[i]; |
michael@0 | 133 | } |
michael@0 | 134 | } |