2012-03-20 10:35:22 +01:00
/*
* pilerpurge . c , SJ
*/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <sys/time.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <dirent.h>
# include <unistd.h>
# include <time.h>
# include <locale.h>
# include <syslog.h>
# include <piler.h>
extern char * optarg ;
extern int optind ;
int dryrun = 0 ;
2013-09-19 09:46:17 +02:00
unsigned long purged_size = 0 ;
2012-03-20 10:35:22 +01:00
2012-04-27 14:39:10 +02:00
# define SQL_STMT_SELECT_PURGE_FROM_OPTION_TABLE "SELECT `value` FROM `" SQL_OPTION_TABLE "` WHERE `key`='enable_purge'"
2012-03-20 10:35:22 +01:00
# define SQL_STMT_DELETE_FROM_META_TABLE "UPDATE `" SQL_METADATA_TABLE "` SET `deleted`=1 WHERE `id` IN ("
2012-03-26 14:59:01 +02:00
# define SQL_STMT_DELETE_FROM_META_TABLE_BY_PILER_ID "UPDATE `" SQL_METADATA_TABLE "` SET `deleted`=1 WHERE `piler_id` IN ('"
2012-03-20 10:35:22 +01:00
# define SQL_STMT_SELECT_NON_REFERENCED_ATTACHMENTS "SELECT `piler_id`, `attachment_id`, `i` FROM `" SQL_ATTACHMENTS_VIEW "` WHERE `refcount`=0 AND `piler_id` IN ('"
# define SQL_STMT_DELETE_FROM_ATTACHMENT_TABLE "DELETE FROM `" SQL_ATTACHMENT_TABLE "` WHERE `id` IN ("
2013-05-05 11:57:14 +02:00
int is_purge_allowed ( struct session_data * sdata , struct __data * data , struct __config * cfg ) {
2012-04-27 14:39:10 +02:00
int rc = 0 ;
2015-07-24 14:28:31 +02:00
if ( prepare_sql_statement ( sdata , & ( data - > stmt_generic ) , SQL_STMT_SELECT_PURGE_FROM_OPTION_TABLE , cfg ) = = ERR ) return rc ;
2013-05-05 11:57:14 +02:00
p_bind_init ( data ) ;
if ( p_exec_query ( sdata , data - > stmt_generic , data ) = = ERR ) goto ENDE ;
p_bind_init ( data ) ;
data - > sql [ data - > pos ] = ( char * ) & rc ; data - > type [ data - > pos ] = TYPE_LONG ; data - > len [ data - > pos ] = sizeof ( int ) ; data - > pos + + ;
p_store_results ( sdata , data - > stmt_generic , data ) ;
p_fetch_results ( data - > stmt_generic ) ;
p_free_results ( data - > stmt_generic ) ;
ENDE :
close_prepared_statement ( data - > stmt_generic ) ;
2012-04-27 14:39:10 +02:00
return rc ;
}
2012-03-20 10:35:22 +01:00
int remove_message_frame_files ( char * s , char * update_meta_sql , struct session_data * sdata , struct __config * cfg ) {
char * p , puf [ SMALLBUFSIZE ] , filename [ SMALLBUFSIZE ] ;
2014-07-22 16:00:03 +02:00
int n = 0 , result ;
2015-02-14 19:47:40 +01:00
# ifdef HAVE_SUPPORT_FOR_COMPAT_STORAGE_LAYOUT
2012-12-15 09:57:24 +01:00
struct stat st ;
2015-02-14 19:47:40 +01:00
# endif
2012-03-20 10:35:22 +01:00
p = s ;
do {
2014-07-22 16:00:03 +02:00
p = split ( p , ' ' , puf , sizeof ( puf ) - 1 , & result ) ;
2012-03-20 10:35:22 +01:00
if ( strlen ( puf ) = = RND_STR_LEN ) {
2013-02-15 20:58:58 +01:00
snprintf ( filename , sizeof ( filename ) - 1 , " %s/%02x/%c%c%c/%c%c/%c%c/%s.m " , cfg - > queuedir , cfg - > server_id , puf [ 8 ] , puf [ 9 ] , puf [ 10 ] , puf [ RND_STR_LEN - 4 ] , puf [ RND_STR_LEN - 3 ] , puf [ RND_STR_LEN - 2 ] , puf [ RND_STR_LEN - 1 ] , puf ) ;
2012-12-15 09:57:24 +01:00
2015-02-01 10:40:18 +01:00
# ifdef HAVE_SUPPORT_FOR_COMPAT_STORAGE_LAYOUT
2012-12-15 09:57:24 +01:00
if ( stat ( filename , & st ) ) {
2013-02-15 20:58:58 +01:00
snprintf ( filename , sizeof ( filename ) - 1 , " %s/%02x/%c%c/%c%c/%c%c/%s.m " , cfg - > queuedir , cfg - > server_id , puf [ RND_STR_LEN - 6 ] , puf [ RND_STR_LEN - 5 ] , puf [ RND_STR_LEN - 4 ] , puf [ RND_STR_LEN - 3 ] , puf [ RND_STR_LEN - 2 ] , puf [ RND_STR_LEN - 1 ] , puf ) ;
2012-12-15 09:57:24 +01:00
}
2015-02-01 10:40:18 +01:00
# endif
2012-03-20 10:35:22 +01:00
if ( dryrun = = 1 ) {
n + + ;
printf ( " removing messagefile: %s \n " , filename ) ;
}
else {
if ( unlink ( filename ) = = 0 ) n + + ;
}
}
} while ( p ) ;
update_meta_sql [ strlen ( update_meta_sql ) - 1 ] = ' ) ' ;
if ( dryrun = = 1 ) {
printf ( " running sql query: *%s* \n \n " , update_meta_sql ) ;
} else {
2015-07-24 15:01:22 +02:00
p_query ( sdata , update_meta_sql , cfg ) ;
2012-03-20 10:35:22 +01:00
}
return n ;
}
2013-05-05 11:57:14 +02:00
int remove_attachments ( char * in , struct session_data * sdata , struct __data * data , struct __config * cfg ) {
2012-03-20 10:35:22 +01:00
char filename [ SMALLBUFSIZE ] ;
char * a , buf [ BIGBUFSIZE - 300 ] , update_meta_sql [ BIGBUFSIZE ] , delete_attachment_stmt [ BIGBUFSIZE ] ;
2013-05-05 11:57:14 +02:00
char piler_id [ SMALLBUFSIZE ] , i [ BUFLEN ] ;
int n = 0 , len , attachment_id ;
2015-02-14 19:47:40 +01:00
# ifdef HAVE_SUPPORT_FOR_COMPAT_STORAGE_LAYOUT
2012-12-15 09:57:24 +01:00
struct stat st ;
2015-02-14 19:47:40 +01:00
# endif
2012-03-20 10:35:22 +01:00
if ( strlen ( in ) < 10 ) return 0 ;
len = strlen ( SQL_STMT_SELECT_NON_REFERENCED_ATTACHMENTS ) + strlen ( in ) + 2 ;
a = malloc ( len ) ;
if ( ! a ) return 0 ;
memset ( a , 0 , len ) ;
in [ strlen ( in ) - 2 ] = ' ) ' ;
in [ strlen ( in ) - 1 ] = ' \0 ' ;
snprintf ( a , len - 1 , " %s%s " , SQL_STMT_SELECT_NON_REFERENCED_ATTACHMENTS , in ) ;
2015-07-24 14:28:31 +02:00
if ( prepare_sql_statement ( sdata , & ( data - > stmt_select_non_referenced_attachments ) , a , cfg ) = = ERR ) { free ( a ) ; return n ; }
2013-05-05 11:57:14 +02:00
2012-03-20 10:35:22 +01:00
if ( dryrun = = 1 ) printf ( " running sql query: *%s* \n \n " , a ) ;
memset ( buf , 0 , sizeof ( buf ) ) ;
memset ( update_meta_sql , 0 , sizeof ( update_meta_sql ) ) ;
memset ( delete_attachment_stmt , 0 , sizeof ( delete_attachment_stmt ) ) ;
snprintf ( update_meta_sql , sizeof ( update_meta_sql ) - 1 , " %s " , SQL_STMT_DELETE_FROM_META_TABLE_BY_PILER_ID ) ;
snprintf ( delete_attachment_stmt , sizeof ( delete_attachment_stmt ) - 1 , " %s " , SQL_STMT_DELETE_FROM_ATTACHMENT_TABLE ) ;
2013-05-05 11:57:14 +02:00
p_bind_init ( data ) ;
if ( p_exec_query ( sdata , data - > stmt_select_non_referenced_attachments , data ) = = ERR ) goto ENDE ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
p_bind_init ( data ) ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
data - > sql [ data - > pos ] = & piler_id [ 0 ] ; data - > type [ data - > pos ] = TYPE_STRING ; data - > len [ data - > pos ] = sizeof ( piler_id ) - 2 ; data - > pos + + ;
data - > sql [ data - > pos ] = ( char * ) & attachment_id ; data - > type [ data - > pos ] = TYPE_LONG ; data - > len [ data - > pos ] = sizeof ( int ) ; data - > pos + + ;
data - > sql [ data - > pos ] = & i [ 0 ] ; data - > type [ data - > pos ] = TYPE_STRING ; data - > len [ data - > pos ] = sizeof ( i ) - 2 ; data - > pos + + ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
p_store_results ( sdata , data - > stmt_select_non_referenced_attachments , data ) ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
while ( p_fetch_results ( data - > stmt_select_non_referenced_attachments ) = = OK ) {
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
snprintf ( filename , sizeof ( filename ) - 1 , " %s/%02x/%c%c%c/%c%c/%c%c/%s.a%d " , cfg - > queuedir , cfg - > server_id , piler_id [ 8 ] , piler_id [ 9 ] , piler_id [ 10 ] , piler_id [ RND_STR_LEN - 4 ] , piler_id [ RND_STR_LEN - 3 ] , piler_id [ RND_STR_LEN - 2 ] , piler_id [ RND_STR_LEN - 1 ] , piler_id , attachment_id ) ;
2015-02-01 10:40:18 +01:00
# ifdef HAVE_SUPPORT_FOR_COMPAT_STORAGE_LAYOUT
2013-05-05 11:57:14 +02:00
if ( stat ( filename , & st ) ) {
snprintf ( filename , sizeof ( filename ) - 1 , " %s/%02x/%c%c/%c%c/%c%c/%s.a%d " , cfg - > queuedir , cfg - > server_id , piler_id [ RND_STR_LEN - 6 ] , piler_id [ RND_STR_LEN - 5 ] , piler_id [ RND_STR_LEN - 4 ] , piler_id [ RND_STR_LEN - 3 ] , piler_id [ RND_STR_LEN - 2 ] , piler_id [ RND_STR_LEN - 1 ] , piler_id , attachment_id ) ;
}
2015-02-01 10:40:18 +01:00
# endif
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
if ( dryrun = = 1 ) {
printf ( " removing attachment: *%s* \n " , filename ) ;
} else {
unlink ( filename ) ;
}
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
if ( strlen ( i ) > 0 ) {
memcpy ( & delete_attachment_stmt [ strlen ( delete_attachment_stmt ) ] , i , strlen ( i ) ) ;
memcpy ( & delete_attachment_stmt [ strlen ( delete_attachment_stmt ) ] , " , " , 1 ) ;
}
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
if ( attachment_id = = 1 ) {
memcpy ( & update_meta_sql [ strlen ( update_meta_sql ) ] , piler_id , strlen ( piler_id ) ) ;
memcpy ( & update_meta_sql [ strlen ( update_meta_sql ) ] , " ',' " , 3 ) ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
if ( strlen ( buf ) > = sizeof ( buf ) - RND_STR_LEN - 2 - 1 ) {
if ( strlen ( update_meta_sql ) > 10 ) {
update_meta_sql [ strlen ( update_meta_sql ) - 2 ] = ' ) ' ;
update_meta_sql [ strlen ( update_meta_sql ) - 1 ] = ' \0 ' ;
}
n + = remove_message_frame_files ( buf , update_meta_sql , sdata , cfg ) ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
if ( strlen ( delete_attachment_stmt ) > strlen ( SQL_STMT_DELETE_FROM_ATTACHMENT_TABLE ) ) {
delete_attachment_stmt [ strlen ( delete_attachment_stmt ) - 1 ] = ' ) ' ;
if ( dryrun = = 1 ) {
printf ( " delete sql: *%s* \n " , delete_attachment_stmt ) ;
} else {
2015-07-24 15:01:22 +02:00
p_query ( sdata , delete_attachment_stmt , cfg ) ;
2013-05-05 11:57:14 +02:00
}
2012-03-20 10:35:22 +01:00
}
2013-05-05 11:57:14 +02:00
memset ( buf , 0 , sizeof ( buf ) ) ;
memset ( update_meta_sql , 0 , sizeof ( update_meta_sql ) ) ;
memset ( delete_attachment_stmt , 0 , sizeof ( delete_attachment_stmt ) ) ;
snprintf ( update_meta_sql , sizeof ( update_meta_sql ) - 1 , " %s " , SQL_STMT_DELETE_FROM_META_TABLE_BY_PILER_ID ) ;
snprintf ( delete_attachment_stmt , sizeof ( delete_attachment_stmt ) - 1 , " %s " , SQL_STMT_DELETE_FROM_ATTACHMENT_TABLE ) ;
2012-03-20 10:35:22 +01:00
}
2013-05-05 11:57:14 +02:00
memcpy ( & buf [ strlen ( buf ) ] , piler_id , strlen ( piler_id ) ) ;
memcpy ( & buf [ strlen ( buf ) ] , " " , 1 ) ;
2012-03-20 10:35:22 +01:00
}
2013-05-05 11:57:14 +02:00
2012-03-20 10:35:22 +01:00
}
2013-05-05 11:57:14 +02:00
p_free_results ( data - > stmt_select_non_referenced_attachments ) ;
2012-03-20 10:35:22 +01:00
if ( strlen ( buf ) > 5 & & strlen ( update_meta_sql ) > strlen ( SQL_STMT_DELETE_FROM_META_TABLE_BY_PILER_ID ) + 10 ) {
update_meta_sql [ strlen ( update_meta_sql ) - 2 ] = ' ) ' ;
update_meta_sql [ strlen ( update_meta_sql ) - 1 ] = ' \0 ' ;
n + = remove_message_frame_files ( buf , update_meta_sql , sdata , cfg ) ;
}
if ( strlen ( delete_attachment_stmt ) > strlen ( SQL_STMT_DELETE_FROM_ATTACHMENT_TABLE ) ) {
delete_attachment_stmt [ strlen ( delete_attachment_stmt ) - 1 ] = ' ) ' ;
if ( dryrun = = 1 ) {
printf ( " delete sql: *%s* \n " , delete_attachment_stmt ) ;
} else {
2015-07-24 15:01:22 +02:00
p_query ( sdata , delete_attachment_stmt , cfg ) ;
2012-03-20 10:35:22 +01:00
}
}
2013-05-05 11:57:14 +02:00
ENDE :
free ( a ) ;
close_prepared_statement ( data - > stmt_select_non_referenced_attachments ) ;
2012-03-20 10:35:22 +01:00
return n ;
}
2013-10-15 12:52:13 +02:00
int purge_messages_round1 ( struct session_data * sdata , struct __data * data , char * attachment_condition , struct __config * cfg ) {
2013-09-19 09:46:17 +02:00
int purged = 0 , size ;
2013-05-05 11:57:14 +02:00
char id [ BUFLEN ] , s [ SMALLBUFSIZE ] , buf [ BIGBUFSIZE - 300 ] , update_meta_sql [ BIGBUFSIZE ] ;
2012-03-20 10:35:22 +01:00
memset ( buf , 0 , sizeof ( buf ) ) ;
memset ( update_meta_sql , 0 , sizeof ( update_meta_sql ) ) ;
snprintf ( update_meta_sql , sizeof ( update_meta_sql ) - 1 , " %s " , SQL_STMT_DELETE_FROM_META_TABLE ) ;
2015-03-29 09:59:41 +02:00
snprintf ( s , sizeof ( s ) - 1 , " SELECT `id`, `piler_id`, `size` FROM `%s` WHERE `deleted`=0 AND `retained` < %ld AND %s AND id NOT IN (SELECT id FROM `%s` WHERE `to` IN (SELECT email FROM `%s`)) AND id NOT IN (SELECT id FROM `%s` WHERE `from` IN (SELECT email FROM `%s`)) " , SQL_METADATA_TABLE , sdata - > now , attachment_condition , SQL_RECIPIENT_TABLE , SQL_LEGAL_HOLD_TABLE , SQL_METADATA_TABLE , SQL_LEGAL_HOLD_TABLE ) ;
2012-03-20 10:35:22 +01:00
if ( cfg - > verbosity > = _LOG_DEBUG ) syslog ( LOG_PRIORITY , " purge sql: *%s* " , s ) ;
2015-07-24 14:28:31 +02:00
if ( prepare_sql_statement ( sdata , & ( data - > stmt_select_from_meta_table ) , s , cfg ) = = ERR ) return purged ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
p_bind_init ( data ) ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
if ( p_exec_query ( sdata , data - > stmt_select_from_meta_table , data ) = = ERR ) goto ENDE ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
p_bind_init ( data ) ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
data - > sql [ data - > pos ] = & id [ 0 ] ; data - > type [ data - > pos ] = TYPE_STRING ; data - > len [ data - > pos ] = sizeof ( id ) - 2 ; data - > pos + + ;
data - > sql [ data - > pos ] = & s [ 0 ] ; data - > type [ data - > pos ] = TYPE_STRING ; data - > len [ data - > pos ] = sizeof ( s ) - 2 ; data - > pos + + ;
2013-09-19 09:46:17 +02:00
data - > sql [ data - > pos ] = ( char * ) & size ; data - > type [ data - > pos ] = TYPE_LONG ; data - > len [ data - > pos ] = sizeof ( int ) ; data - > pos + + ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
p_store_results ( sdata , data - > stmt_select_from_meta_table , data ) ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
while ( p_fetch_results ( data - > stmt_select_from_meta_table ) = = OK ) {
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
memcpy ( & update_meta_sql [ strlen ( update_meta_sql ) ] , id , strlen ( id ) ) ;
memcpy ( & update_meta_sql [ strlen ( update_meta_sql ) ] , " , " , 1 ) ;
2012-03-20 10:35:22 +01:00
2013-09-19 12:02:34 +02:00
purged_size + = size ;
2013-05-05 11:57:14 +02:00
if ( strlen ( buf ) > = sizeof ( buf ) - RND_STR_LEN - 2 - 1 ) {
purged + = remove_message_frame_files ( buf , update_meta_sql , sdata , cfg ) ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
memset ( buf , 0 , sizeof ( buf ) ) ;
memset ( update_meta_sql , 0 , sizeof ( update_meta_sql ) ) ;
snprintf ( update_meta_sql , sizeof ( update_meta_sql ) - 1 , " %s " , SQL_STMT_DELETE_FROM_META_TABLE ) ;
2012-03-20 10:35:22 +01:00
}
2013-05-05 11:57:14 +02:00
memcpy ( & buf [ strlen ( buf ) ] , s , strlen ( s ) ) ;
memcpy ( & buf [ strlen ( buf ) ] , " " , 1 ) ;
2012-03-20 10:35:22 +01:00
}
2013-05-05 11:57:14 +02:00
p_free_results ( data - > stmt_select_from_meta_table ) ;
2012-03-20 10:35:22 +01:00
if ( strlen ( buf ) > 5 & & strlen ( update_meta_sql ) > strlen ( SQL_STMT_DELETE_FROM_META_TABLE ) + 5 ) {
purged + = remove_message_frame_files ( buf , update_meta_sql , sdata , cfg ) ;
}
2013-05-05 11:57:14 +02:00
ENDE :
close_prepared_statement ( data - > stmt_select_from_meta_table ) ;
2012-03-20 10:35:22 +01:00
return purged ;
}
2013-05-05 11:57:14 +02:00
int purge_messages_with_attachments ( struct session_data * sdata , struct __data * data , struct __config * cfg ) {
2013-09-19 09:46:17 +02:00
int purged = 0 , size ;
2012-03-20 10:35:22 +01:00
char s [ SMALLBUFSIZE ] , idlist [ BIGBUFSIZE ] ;
memset ( idlist , 0 , sizeof ( idlist ) ) ;
2013-09-19 09:46:17 +02:00
snprintf ( s , sizeof ( s ) - 1 , " SELECT `piler_id`, `size` FROM `%s` WHERE `deleted`=0 AND `retained` < %ld AND attachments > 0 " , SQL_METADATA_TABLE , sdata - > now ) ;
2012-03-20 10:35:22 +01:00
if ( cfg - > verbosity > = _LOG_DEBUG ) syslog ( LOG_PRIORITY , " purge sql: *%s* " , s ) ;
2015-07-24 14:28:31 +02:00
if ( prepare_sql_statement ( sdata , & ( data - > stmt_select_from_meta_table ) , s , cfg ) = = ERR ) return purged ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
p_bind_init ( data ) ;
if ( p_exec_query ( sdata , data - > stmt_select_from_meta_table , data ) = = ERR ) goto ENDE ;
p_bind_init ( data ) ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
data - > sql [ data - > pos ] = & s [ 0 ] ; data - > type [ data - > pos ] = TYPE_STRING ; data - > len [ data - > pos ] = sizeof ( s ) - 2 ; data - > pos + + ;
2013-09-19 09:46:17 +02:00
data - > sql [ data - > pos ] = ( char * ) & size ; data - > type [ data - > pos ] = TYPE_LONG ; data - > len [ data - > pos ] = sizeof ( int ) ; data - > pos + + ;
2013-05-05 11:57:14 +02:00
p_store_results ( sdata , data - > stmt_select_from_meta_table , data ) ;
while ( p_fetch_results ( data - > stmt_select_from_meta_table ) = = OK ) {
memcpy ( & idlist [ strlen ( idlist ) ] , s , strlen ( s ) ) ;
memcpy ( & idlist [ strlen ( idlist ) ] , " ',' " , 3 ) ;
2013-09-19 12:02:34 +02:00
purged_size + = size ;
2013-05-05 11:57:14 +02:00
if ( strlen ( idlist ) > = sizeof ( idlist ) - 2 * RND_STR_LEN ) {
purged + = remove_attachments ( idlist , sdata , data , cfg ) ;
2013-09-19 09:46:17 +02:00
2013-05-05 11:57:14 +02:00
memset ( idlist , 0 , sizeof ( idlist ) ) ;
2012-03-20 10:35:22 +01:00
}
}
2013-05-05 11:57:14 +02:00
p_free_results ( data - > stmt_select_from_meta_table ) ;
2012-03-20 10:35:22 +01:00
if ( strlen ( idlist ) > 5 ) {
2013-05-05 11:57:14 +02:00
purged + = remove_attachments ( idlist , sdata , data , cfg ) ;
2012-03-20 10:35:22 +01:00
}
2013-05-05 11:57:14 +02:00
ENDE :
close_prepared_statement ( data - > stmt_select_from_meta_table ) ;
2012-03-20 10:35:22 +01:00
return purged ;
}
int main ( int argc , char * * argv ) {
int i , purged = 0 ;
2013-09-19 09:46:17 +02:00
char * configfile = CONFIG_FILE , buf [ SMALLBUFSIZE ] ;
2012-03-20 10:35:22 +01:00
struct session_data sdata ;
2013-05-05 11:57:14 +02:00
struct __data data ;
2012-03-20 10:35:22 +01:00
struct __config cfg ;
while ( ( i = getopt ( argc , argv , " c:dh? " ) ) > 0 ) {
switch ( i ) {
case ' c ' :
configfile = optarg ;
break ;
case ' d ' :
dryrun = 1 ;
break ;
case ' h ' :
case ' ? ' :
default :
break ;
}
}
( void ) openlog ( " pilerpurge " , LOG_PID , LOG_MAIL ) ;
cfg = read_config ( configfile ) ;
2013-04-28 14:18:09 +02:00
if ( open_database ( & sdata , & cfg ) = = ERR ) return 0 ;
2012-03-20 10:35:22 +01:00
setlocale ( LC_CTYPE , cfg . locale ) ;
2013-02-22 15:01:21 +01:00
init_session_data ( & sdata , & cfg ) ;
2012-03-20 10:35:22 +01:00
2013-05-05 11:57:14 +02:00
i = is_purge_allowed ( & sdata , & data , & cfg ) ;
2012-04-27 14:39:10 +02:00
if ( i = = 1 ) {
2013-10-15 12:52:13 +02:00
purged + = purge_messages_round1 ( & sdata , & data , " attachments=0 " , & cfg ) ;
2013-05-05 11:57:14 +02:00
purged + = purge_messages_with_attachments ( & sdata , & data , & cfg ) ;
2013-10-15 12:52:13 +02:00
purged + = purge_messages_round1 ( & sdata , & data , " attachments > 0 " , & cfg ) ;
2012-03-20 10:35:22 +01:00
2013-09-19 16:44:33 +02:00
syslog ( LOG_INFO , " purged %d messages, %ld bytes " , purged , purged_size ) ;
2012-04-27 14:39:10 +02:00
}
else printf ( " purge is not allowed by configuration, enable_purge=%d \n " , i ) ;
2012-03-20 10:35:22 +01:00
2013-09-19 09:46:17 +02:00
if ( purged_size > 100 ) {
2013-09-19 16:44:33 +02:00
snprintf ( buf , sizeof ( buf ) - 1 , " UPDATE `%s` SET size = size - %ld " , SQL_COUNTER_TABLE , purged_size ) ;
2013-09-19 09:46:17 +02:00
2015-07-24 15:01:22 +02:00
if ( dryrun = = 0 ) p_query ( & sdata , buf , & cfg ) ;
2013-09-19 09:46:17 +02:00
}
2013-04-28 14:18:09 +02:00
close_database ( & sdata ) ;
2012-03-20 10:35:22 +01:00
return 0 ;
}