michael@0: /****************************************************************************** michael@0: * Copyright (C) 2009-2013, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #if U_PLATFORM == U_PF_MINGW michael@0: // *cough* - for struct stat michael@0: #ifdef __STRICT_ANSI__ michael@0: #undef __STRICT_ANSI__ michael@0: #endif michael@0: #endif michael@0: michael@0: #include "filetools.h" michael@0: #include "filestrm.h" michael@0: #include "cstring.h" michael@0: #include "unicode/putil.h" michael@0: #include "putilimp.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #if U_HAVE_DIRENT_H michael@0: #include michael@0: typedef struct dirent DIRENT; michael@0: michael@0: #define MAX_PATH_SIZE 4096 /* Set the limit for the size of the path. */ michael@0: michael@0: #define SKIP1 "." michael@0: #define SKIP2 ".." michael@0: #endif michael@0: michael@0: static int32_t whichFileModTimeIsLater(const char *file1, const char *file2); michael@0: michael@0: /* michael@0: * Goes through the given directory recursive to compare each file's modification time with that of the file given. michael@0: * Also can be given just one file to check against. Default value for isDir is FALSE. michael@0: */ michael@0: U_CAPI UBool U_EXPORT2 michael@0: isFileModTimeLater(const char *filePath, const char *checkAgainst, UBool isDir) { michael@0: UBool isLatest = TRUE; michael@0: michael@0: if (filePath == NULL || checkAgainst == NULL) { michael@0: return FALSE; michael@0: } michael@0: michael@0: if (isDir == TRUE) { michael@0: #if U_HAVE_DIRENT_H michael@0: DIR *pDir = NULL; michael@0: if ((pDir= opendir(checkAgainst)) != NULL) { michael@0: DIR *subDirp = NULL; michael@0: DIRENT *dirEntry = NULL; michael@0: michael@0: while ((dirEntry = readdir(pDir)) != NULL) { michael@0: if (uprv_strcmp(dirEntry->d_name, SKIP1) != 0 && uprv_strcmp(dirEntry->d_name, SKIP2) != 0) { michael@0: char newpath[MAX_PATH_SIZE] = ""; michael@0: uprv_strcpy(newpath, checkAgainst); michael@0: uprv_strcat(newpath, U_FILE_SEP_STRING); michael@0: uprv_strcat(newpath, dirEntry->d_name); michael@0: michael@0: if ((subDirp = opendir(newpath)) != NULL) { michael@0: /* If this new path is a directory, make a recursive call with the newpath. */ michael@0: closedir(subDirp); michael@0: isLatest = isFileModTimeLater(filePath, newpath, isDir); michael@0: if (!isLatest) { michael@0: break; michael@0: } michael@0: } else { michael@0: int32_t latest = whichFileModTimeIsLater(filePath, newpath); michael@0: if (latest < 0 || latest == 2) { michael@0: isLatest = FALSE; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: } michael@0: } michael@0: closedir(pDir); michael@0: } else { michael@0: fprintf(stderr, "Unable to open directory: %s\n", checkAgainst); michael@0: return FALSE; michael@0: } michael@0: #endif michael@0: } else { michael@0: if (T_FileStream_file_exists(checkAgainst)) { michael@0: int32_t latest = whichFileModTimeIsLater(filePath, checkAgainst); michael@0: if (latest < 0 || latest == 2) { michael@0: isLatest = FALSE; michael@0: } michael@0: } else { michael@0: isLatest = FALSE; michael@0: } michael@0: } michael@0: michael@0: return isLatest; michael@0: } michael@0: michael@0: /* Compares the mod time of both files returning a number indicating which one is later. -1 if error ocurs. */ michael@0: static int32_t whichFileModTimeIsLater(const char *file1, const char *file2) { michael@0: int32_t result = 0; michael@0: struct stat stbuf1, stbuf2; michael@0: michael@0: if (stat(file1, &stbuf1) == 0 && stat(file2, &stbuf2) == 0) { michael@0: time_t modtime1, modtime2; michael@0: double diff; michael@0: michael@0: modtime1 = stbuf1.st_mtime; michael@0: modtime2 = stbuf2.st_mtime; michael@0: michael@0: diff = difftime(modtime1, modtime2); michael@0: if (diff < 0.0) { michael@0: result = 2; michael@0: } else if (diff > 0.0) { michael@0: result = 1; michael@0: } michael@0: michael@0: } else { michael@0: fprintf(stderr, "Unable to get stats from file: %s or %s\n", file1, file2); michael@0: result = -1; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: /* Swap the file separater character given with the new one in the file path. */ michael@0: U_CAPI void U_EXPORT2 michael@0: swapFileSepChar(char *filePath, const char oldFileSepChar, const char newFileSepChar) { michael@0: for (int32_t i = 0, length = uprv_strlen(filePath); i < length; i++) { michael@0: filePath[i] = (filePath[i] == oldFileSepChar ) ? newFileSepChar : filePath[i]; michael@0: } michael@0: }