uint32_t HashFile(const char *file, int *mtime, uint64_t *size) { uint32_t hash; char fileData[MAX_PATH_STR_LEN]; #ifndef WIN32 STAT_TYPE buf; #else BOOL fOk; WIN32_FILE_ATTRIBUTE_DATA fileInfo; #endif *mtime = 0; *size = 0; #ifdef WIN32 fOk = GetFileAttributesEx(file, GetFileExInfoStandard, (void *)&fileInfo); *mtime = fileInfo.ftLastWriteTime.dwLowDateTime; *size = ((uint64_t)fileInfo.nFileSizeHigh << 32) | fileInfo.nFileSizeLow; #else if (STAT_FUNC(file, &buf) != -1) { *mtime = (int)buf.st_mtime; *size = (uint64_t)buf.st_size; } else { LOG_ERROR("stat error on file %s, errno=%d\n", file, errno); } #endif // Generate a hash of the full file path, modified time, and file size memset(fileData, 0, sizeof(fileData)); snprintf(fileData, sizeof(fileData) - 1, "%s%d%llu", file, *mtime, *size); hash = hashlittle(fileData, strlen(fileData), 0); return hash; } /* HashFile() */
/* Check whether we have enough space in a given directory. */ void CheckDirSpace(char *dirname, char *envVar, int warnSize, int exitSize) { struct STAT_STRUCT stats; if (STAT_FUNC(dirname, &stats #if defined(SGI) , sizeof(stats), 0 #endif ) != 0) { reportErrSys("Can't get status of file system"); } else { int bytesFree = stats.STAT_BAVAIL * stats.STAT_FRSIZE; if (bytesFree < exitSize) { char errBuf[1024]; sprintf(errBuf, "%s directory (%s) has less than %d bytes free\n", envVar, dirname, exitSize); Exit::DoAbort(errBuf, __FILE__, __LINE__); } else if (bytesFree < warnSize) { fprintf(stderr, "Warning: %s directory (%s) has less than %d bytes free\n", envVar, dirname, warnSize); } } return; }
static gint64 calc_recursively (const char *path, GError **calc_error) { gint64 sum = 0; GError *error = NULL; GDir *folder = g_dir_open(path, 0, &error); if (!folder) { g_set_error (calc_error, CCNET_DOMAIN, 0, "g_open() dir %s failed:%s\n", path, error->message); return -1; } const char *name = NULL; while ((name = g_dir_read_name(folder)) != NULL) { STAT_STRUCT sb; char *full_path= g_build_filename (path, name, NULL); if (STAT_FUNC(full_path, &sb) < 0) { g_set_error (calc_error, CCNET_DOMAIN, 0, "failed to stat on %s: %s\n", full_path, strerror(errno)); g_free(full_path); g_dir_close(folder); return -1; } if (S_ISDIR(sb.st_mode)) { gint64 size = calc_recursively(full_path, calc_error); if (size < 0) { g_free (full_path); g_dir_close (folder); return -1; } sum += size; g_free(full_path); } else if (S_ISREG(sb.st_mode)) { sum += sb.st_size; g_free(full_path); } } g_dir_close (folder); return sum; }