/* Copy a directory to Unix */ static int unix_copydir(direntry_t *entry, MainParam_t *mp) { Arg_t *arg=(Arg_t *) mp->arg; time_t mtime; Stream_t *File=mp->File; int ret; char *unixFile; if (!arg->recursive && mp->basenameHasWildcard) return 0; File->Class->get_data(File, &mtime, 0, 0, 0); if (!arg->preserveTime) mtime = 0L; if(!arg->type && arg->verbose) { fprintf(stderr,"Copying "); fprintPwd(stderr, entry,0); fprintf(stderr, "\n"); } if(got_signal) return ERROR_ONE; unixFile = mpBuildUnixFilename(mp); if(!unixFile) { printOom(); return ERROR_ONE; } if(arg->type || !*mpPickTargetName(mp) || !makeUnixDir(unixFile)) { Arg_t newArg; newArg = *arg; newArg.mp.arg = (void *) &newArg; newArg.mp.unixTarget = unixFile; newArg.mp.targetName = 0; newArg.mp.basenameHasWildcard = 1; ret = mp->loop(File, &newArg.mp, "*"); set_mtime(unixFile, mtime); free(unixFile); return ret | GOT_ONE; } else { perror("mkdir"); fprintf(stderr, "Failure to make directory %s\n", unixFile); free(unixFile); return ERROR_ONE; } }
void _groupcounter_record_requester(int32_t listener_id, record_t* result) { CMP_DEF_THIS(cmp_groupcounter_t, _cmp_groupcounter); groupcounters_holder_t* holder; groupcounter_t* groupcounter; slist_t* it; int32_t i; mutex_lock(this->mutex); memset(result, 0, sizeof(record_t)); holder = &this->holders[listener_id]; result->listener_id = listener_id; set_mtime(&result->timestamp); for(i = 0, it = holder->groupcounters; it; it = it->next, ++i){ groupcounter = it->data; result->items[i] = groupcounter->interface.get_counter(groupcounter); } mutex_unlock(this->mutex); }
static void set_mtimes(const char *cnames[], int diff) { int i=0; for(i=0; cnames[i]; i++) set_mtime(cnames[i], diff); }
/** * Download a file. * * @param url URL. * @param cache_key Cache key. * * If the file is present in the cache, the cached version * will be retrieved. Otherwise, the file will be downloaded. * * If the file was not found on the server, or it was not found * the last time it was requested, an empty string will be * returned, and a zero-byte file will be stored in the cache. * * @return Absolute path to the cached file. */ string CacheManager::download( const string &url, const string &cache_key) { // Check the main cache key. string cache_filename = getCacheFilename(cache_key); if (cache_filename.empty()) { // Error obtaining the cache key filename. return string(); } // Lock the semaphore to make sure we don't // download too many files at once. SemaphoreLocker locker(m_dlsem); // Check if the file already exists. if (!access(cache_filename, R_OK)) { // File exists. // Is it larger than 0 bytes? int64_t sz = filesize(cache_filename); if (sz == 0) { // File is 0 bytes, which indicates it didn't exist // on the server. If the file is older than a week, // try to redownload it. // TODO: Configurable time. // TODO: How should we handle errors? time_t filetime; if (get_mtime(cache_filename, &filetime) != 0) return string(); struct timeval systime; if (gettimeofday(&systime, nullptr) != 0) return string(); if ((systime.tv_sec - filetime) < (86400*7)) { // Less than a week old. return string(); } // More than a week old. // Delete the cache file and redownload it. if (delete_file(cache_filename) != 0) return string(); } else if (sz > 0) { // File is larger than 0 bytes, which indicates // it was cached successfully. return cache_filename; } } // Check if the URL is blank. // This is allowed for some databases that are only available offline. if (url.empty()) { // Blank URL. Don't try to download anything. // Don't mark the file as unavailable by creating a // 0-byte dummy file, either. return string(); } // Make sure the subdirectories exist. // NOTE: The filename portion MUST be kept in cache_filename, // since the last component is ignored by rmkdir(). if (rmkdir(cache_filename) != 0) { // Error creating subdirectories. return string(); } // TODO: Keep-alive cURL connections (one per server)? m_downloader->setUrl(url); m_downloader->setProxyUrl(m_proxyUrl); int ret = m_downloader->download(); // Write the file to the cache. unique_ptr<IRpFile> file(new RpFile(cache_filename, RpFile::FM_CREATE_WRITE)); if (ret != 0 || !file || !file->isOpen()) { // Error downloading the file, or error opening // the file in the local cache. // TODO: Only keep a negative cache if it's a 404. // Keep the cached file as a 0-byte file to indicate // a "negative" hit, but return an empty filename. return string(); } // Write the file. file->write((void*)m_downloader->data(), m_downloader->dataSize()); file->close(); // Set the file's mtime if it was obtained by the downloader. // TODO: IRpFile::set_mtime()? time_t mtime = m_downloader->mtime(); if (mtime >= 0) { set_mtime(cache_filename, mtime); } // Return the cache filename. return cache_filename; }
/* Write the Unix file */ static int unix_write(direntry_t *entry, MainParam_t *mp, int needfilter) { Arg_t *arg=(Arg_t *) mp->arg; time_t mtime; Stream_t *File=mp->File; Stream_t *Target, *Source; struct stat stbuf; int ret; char errmsg[80]; char *unixFile; File->Class->get_data(File, &mtime, 0, 0, 0); if (!arg->preserveTime) mtime = 0L; if(arg->type) unixFile = "-"; else unixFile = mpBuildUnixFilename(mp); if(!unixFile) { printOom(); return ERROR_ONE; } /* if we are creating a file, check whether it already exists */ if(!arg->type) { if (!arg->nowarn && &arg->type && !access(unixFile, 0)){ if( ask_confirmation("File \"%s\" exists, overwrite (y/n) ? ", unixFile,0)) { free(unixFile); return ERROR_ONE; } /* sanity checking */ if (!stat(unixFile, &stbuf) && !S_ISREG(stbuf.st_mode)) { fprintf(stderr,"\"%s\" is not a regular file\n", unixFile); free(unixFile); return ERROR_ONE; } } } if(!arg->type && arg->verbose) { fprintf(stderr,"Copying "); mpPrintFilename(stderr,mp); fprintf(stderr,"\n"); } if(got_signal) { free(unixFile); return ERROR_ONE; } if ((Target = SimpleFileOpen(0, 0, unixFile, O_WRONLY | O_CREAT | O_TRUNC, errmsg, 0, 0, 0))) { ret = 0; if(needfilter && arg->textmode){ Source = open_filter(COPY(File)); if (!Source) ret = -1; } else Source = COPY(File); if (ret == 0 ) ret = copyfile(Source, Target); FREE(&Source); FREE(&Target); if(ret <= -1){ if(!arg->type) { unlink(unixFile); free(unixFile); } return ERROR_ONE; } if(!arg->type) { set_mtime(unixFile, mtime); free(unixFile); } return GOT_ONE; } else { fprintf(stderr,"%s\n", errmsg); if(!arg->type) free(unixFile); return ERROR_ONE; } }