static int ScanPathForTarFiles(const char *path, size_t basepath_length) { extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb); uint num = 0; struct stat sb; struct dirent *dirent; DIR *dir; if (path == NULL || (dir = ttd_opendir(path)) == NULL) return 0; while ((dirent = readdir(dir)) != NULL) { const char *d_name = FS2OTTD(dirent->d_name); char filename[MAX_PATH]; if (!FiosIsValidFile(path, dirent, &sb)) continue; snprintf(filename, lengthof(filename), "%s%s", path, d_name); if (S_ISDIR(sb.st_mode)) { /* Directory */ if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue; AppendPathSeparator(filename, lengthof(filename)); num += ScanPathForTarFiles(filename, basepath_length); } else if (S_ISREG(sb.st_mode)) { /* File */ char *ext = strrchr(filename, '.'); /* If no extension or extension isn't .tar, skip the file */ if (ext == NULL) continue; if (strcasecmp(ext, ".tar") != 0) continue; if (TarListAddFile(filename)) num++; } } closedir(dir); return num; }
/** * Handle the closing and extracting of a file after * downloading it has been done. */ void ClientNetworkContentSocketHandler::AfterDownload() { /* We read nothing; that's our marker for end-of-stream. * Now gunzip the tar and make it known. */ fclose(this->curFile); this->curFile = NULL; if (GunzipFile(this->curInfo)) { unlink(GetFullFilename(this->curInfo, true)); TarListAddFile(GetFullFilename(this->curInfo, false)); if (this->curInfo->type == CONTENT_TYPE_BASE_MUSIC) { /* Music can't be in a tar. So extract the tar! */ ExtractTar(GetFullFilename(this->curInfo, false)); unlink(GetFullFilename(this->curInfo, false)); } this->OnDownloadComplete(this->curInfo->id); } else { ShowErrorMessage(STR_CONTENT_ERROR_COULD_NOT_EXTRACT, INVALID_STRING_ID, WL_ERROR); } }