+/* this is the function called by the worker threads to perform the download
+ * signal state changes, and prepare the cache file for reading
+ */
+static void download(void *data)
+{
+ int tid, res;
+ struct file_info *file = data;
+
+ tid = ass_tpool_thread_id(tpool);
+
+ curl_easy_setopt(curl[tid], CURLOPT_URL, file->url);
+ curl_easy_setopt(curl[tid], CURLOPT_WRITEDATA, file);
+ res = curl_easy_perform(curl[tid]);
+
+ pthread_mutex_lock(&file->state_mutex);
+ if(res == CURLE_OK) {
+ file->state = DL_DONE;
+ fclose(file->cache_file);
+ if(!(file->cache_file = fopen(file->cache_fname, "rb"))) {
+ fprintf(stderr, "assman: failed to reopen cache file (%s) for reading: %s\n",
+ file->cache_fname, strerror(errno));
+ file->state = DL_ERROR;
+ }
+ } else {
+ file->state = DL_ERROR;
+ }
+ pthread_cond_broadcast(&file->state_cond);
+ pthread_mutex_unlock(&file->state_mutex);
+}
+
+/* this function is called by curl to pass along downloaded data chunks */
+static size_t recv_callback(char *ptr, size_t size, size_t count, void *udata)
+{
+ struct file_info *file = udata;
+
+ pthread_mutex_lock(&file->state_mutex);
+ if(file->state == DL_UNKNOWN) {
+ file->state = DL_STARTED;
+ pthread_cond_broadcast(&file->state_cond);
+ }
+ pthread_mutex_unlock(&file->state_mutex);
+
+ return fwrite(ptr, size, count, file->cache_file);
+}
+
+#ifdef WIN32
+#include <windows.h>
+
+static const char *get_temp_dir(void)
+{
+ static char buf[MAX_PATH + 1];
+ GetTempPathA(MAX_PATH + 1, buf);
+ return buf;
+}
+#else /* UNIX */
+static const char *get_temp_dir(void)
+{
+ char *env = getenv("TMPDIR");
+ return env ? env : "/tmp";
+}
+#endif
+
+
+
+#else /* don't build mod_url */