Skip to content

Commit

Permalink
Added option to omar-data-mgr "--access-time-threshold <days>" for ch…
Browse files Browse the repository at this point in the history
…ecking file last accessed time before deleting.
  • Loading branch information
David Burken committed Apr 9, 2016
1 parent 45956a5 commit 534f566
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 51 deletions.
159 changes: 108 additions & 51 deletions apps/curl_apps/omarDataMgrUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <string>
#include <vector>

static std::string ACCESS_TIME_THRESHOLD_KW = "access_time_threshold";
static std::string CLEAN_KW = "clean";
static std::string DUMP_FILTERED_IMAGES_KW = "dump_filtered_images";
static std::string FALSE_KW = "false";
Expand Down Expand Up @@ -79,6 +80,9 @@ void omarDataMgrUtil::addArguments(ossimArgumentParser& ap)
au->setCommandLineUsage(usageString);

// Set the command line options:

au->addCommandLineOption("--access-time-threshold", "<days> For \"remove\" action. Only remove if last access is greater than days.");

au->addCommandLineOption("--clean", "Cleans/removes image file and associated files from file system if present.\n\"remove\" option only.\nLooks for associated files of image, e.g. .ovr, .his, .omd, .geom\nCAUTION: This command is irreversible once envoked!");

au->addCommandLineOption("--dump-filtered-image-list", "Outputs list of filtered images.");
Expand Down Expand Up @@ -127,6 +131,15 @@ bool omarDataMgrUtil::initialize(ossimArgumentParser& ap)
std::string ts1;
ossimArgumentParser::ossimParameter sp1(ts1);

if( ap.read("--access-time-threshold", sp1) )
{
m_kwl->addPair( ACCESS_TIME_THRESHOLD_KW, ts1 );
if ( ap.argc() < 3 )
{
break;
}
}

if( ap.read("--clean") )
{
addOption( CLEAN_KW, TRUE_KW );
Expand Down Expand Up @@ -455,6 +468,10 @@ void omarDataMgrUtil::usage(ossimArgumentParser& ap)
<< ap.getApplicationName()
<< " --clean --threads 32 remove /data1/imagery/2015/09/28/0000\n"

<< "Removing all images in a directory using 4 threads that have not been accessed in 30 days:\n"
<< ap.getApplicationName()
<< " --access-time-threshold 30 --clean --threads 32 remove /data1/imagery/2015/09/28/0000\n"

<< std::endl;
}

Expand All @@ -475,6 +492,42 @@ bool omarDataMgrUtil::isDirectoryBasedImage(const ossimImageHandler* ih) const
return result;
}

bool omarDataMgrUtil::isPastLastAccessedThreshold( const ossimFilename& file ) const
{
// Default to true.
bool result = true;

std::string value = m_kwl->findKey( ACCESS_TIME_THRESHOLD_KW );
if ( value.size() )
{
const ossim_int64 SECONDS_PER_DAY = 86400; // 60 * 60 * 24
ossim_int64 thresholdInDays = ossimString(value).toInt64();
if ( thresholdInDays )
{
// Returns -1 if does not exist.
ossim_int64 secondsSinceAccessed = file.lastAccessed();

if(traceDebug())
{
ossimNotify(ossimNotifyLevel_DEBUG)
<< ACCESS_TIME_THRESHOLD_KW << ": " << thresholdInDays
<< "\nfile_last_accessed: " << secondsSinceAccessed/SECONDS_PER_DAY
<< "\n";
}

if ( secondsSinceAccessed > 0 )
{
if ( secondsSinceAccessed/SECONDS_PER_DAY <= thresholdInDays )
{
result = false;
}
}
}
}

return result;
}

void omarDataMgrUtil::setNumberOfThreads( ossim_uint32 threads )
{
addOption( THREADS_KW, threads );
Expand Down Expand Up @@ -720,76 +773,80 @@ bool omarDataMgrUtil::callRemoveRasterService( const ossimFilename& file )
}

bool result = false;

if ( isPastLastAccessedThreshold( file ) )
{
std::string service;
getService( service );

std::string service;
getService( service );

std::string url;
getUrl( url );
std::string url;
getUrl( url );

if ( service.size() && url.size() )
{
CURL* curl = curl_easy_init();
if ( curl )
if ( service.size() && url.size() )
{
// Data for POST:
std::string data = std::string("filename=") + file.string();
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, data.c_str() );
CURL* curl = curl_easy_init();
if ( curl )
{
// Data for POST:
std::string data = std::string("filename=") + file.string();
curl_easy_setopt( curl, CURLOPT_POSTFIELDS, data.c_str() );

// Create the URL string:
std::string urlString = url + service;
curl_easy_setopt( curl, CURLOPT_URL, urlString.c_str() );
// Create the URL string:
std::string urlString = url + service;
curl_easy_setopt( curl, CURLOPT_URL, urlString.c_str() );

ossimNotify(ossimNotifyLevel_INFO)
<< "data: " << data
<< "\nurl: " << urlString.c_str()
<< std::endl;
ossimNotify(ossimNotifyLevel_INFO)
<< "data: " << data
<< "\nurl: " << urlString.c_str()
<< std::endl;

// Tell libcurl to follow redirection
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
// Tell libcurl to follow redirection
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

// Run it:
CURLcode res = curl_easy_perform(curl);
// Run it:
CURLcode res = curl_easy_perform(curl);

// Check for errors:
if ( res == CURLE_OK )
{
// Response code of the http transaction:
long respcode;
res = curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &respcode);
// Check for errors:
if ( res == CURLE_OK )
{
if ( respcode == 200 ) // OK 200 "The request was fulfilled."
// Response code of the http transaction:
long respcode;
res = curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &respcode);
if ( res == CURLE_OK )
{
result = true; // Set return status for caller.

// Check for clean flag:
if ( getCleanFlag() )
if ( respcode == 200 ) // OK 200 "The request was fulfilled."
{
clean( file );
result = true; // Set return status for caller.

// Check for clean flag:
if ( getCleanFlag() )
{
clean( file );
}
}
}
}
}
else
{
ossimNotify(ossimNotifyLevel_WARN)
<< "curl_easy_perform() failed: \n"
<< curl_easy_strerror(res)
<< std::endl;
setErrorStatus( (ossim_int32)res );
}
else
{
ossimNotify(ossimNotifyLevel_WARN)
<< "curl_easy_perform() failed: \n"
<< curl_easy_strerror(res)
<< std::endl;
setErrorStatus( (ossim_int32)res );
}

ossimNotify(ossimNotifyLevel_WARN) << std::endl;
ossimNotify(ossimNotifyLevel_WARN) << std::endl;

// Always cleanup:
curl_easy_cleanup(curl);
}
// Always cleanup:
curl_easy_cleanup(curl);
}

// Cleanup curl:
curl_global_cleanup();

// Cleanup curl:
curl_global_cleanup();
} // Matches: if ( service.size() )

} // Matches: if ( service.size() )
} // Matches: if ( isPastLastAccessedThreshold( file ) )

if(traceDebug())
{
Expand Down
11 changes: 11 additions & 0 deletions apps/curl_apps/omarDataMgrUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,17 @@ class omarDataMgrUtil :
*/
bool isDirectoryBasedImage(const ossimImageHandler* ih) const;

/**
* @brief Checks file last access time against threshold.
*
* This check for option key "access_time_threshold", value in days. If
* found will return true if files last access time is greater than the
* threshold days. If key is not set the default return is true.
*
* @return true if file is past last accessed threshold.
*/
bool isPastLastAccessedThreshold( const ossimFilename& file ) const;

/** @return true if key is set to true; false, if not. */
bool keyIsTrue( const std::string& key ) const;

Expand Down
13 changes: 13 additions & 0 deletions include/ossim/base/ossimCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,19 @@ namespace ossim
bool gmtFlag,
std::string& result );

/**
* @brief Gets the current time.
*
* Wrapper around time.h time_t time(time_t *t) with a mutex lock.
*
* Note that time_t is a long int. Returning ossim_int64 to avoid include
* of time.h.
*
* @return The time as the number of seconds since the Epoch,
* 1970-01-01 00:00:00 +0000 (UTC).
*/
OSSIM_DLL ossim_int64 getTime();

/**
* @brief Computes the number of decimation levels to get to the overview
* stop dimension.
Expand Down
7 changes: 7 additions & 0 deletions include/ossim/base/ossimFilename.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class OSSIM_DLL ossimFilename : public ossimString
bool getTimes(ossimLocalTm* accessTime,
ossimLocalTm* modTime,
ossimLocalTm* createTime)const;
/**
* @brief Time in seconds since last accessed.
*
* @return The number of seconds since last accessed or -1 if file does not
* exist.
*/
ossim_int64 lastAccessed() const;

bool touch()const;

Expand Down
11 changes: 11 additions & 0 deletions src/base/ossimCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,17 @@ void ossim::getFormattedTime(
timeMutex.unlock();
}

ossim_int64 ossim::getTime()
{
time_t rawTime;

timeMutex.lock();
time(&rawTime);
timeMutex.unlock();

return (ossim_int64)rawTime;
}

ossim_uint32 ossim::computeLevels(const ossimIrect& rect)
{
ossim_uint32 result = 0;
Expand Down
25 changes: 25 additions & 0 deletions src/base/ossimFilename.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,31 @@ bool ossimFilename::getTimes(ossimLocalTm *accessTime,
return false;
}

// Time in seconds since last accessed.
ossim_int64 ossimFilename::lastAccessed() const
{
ossim_int64 result = -1;

if( expand().exists() )
{
ossim_int64 currentTime = ossim::getTime();

#if defined(_WIN32)
cerr << "ossimFilename::lastAccessed() not implemented for windows!" << endl;
#else
struct stat sbuf;
stat(c_str(), &sbuf);
if ( stat( expand().c_str(), &sbuf) == 0 )
{
time_t atime = sbuf.st_atime; // This cast to seconds(time_t).
result = currentTime - (ossim_int64)atime;
}
#endif // platforms
}

return result;
}

bool ossimFilename::touch()const
{
#if defined( _WIN32 )
Expand Down

0 comments on commit 534f566

Please sign in to comment.