2013-12-08 19:46:40 +00:00
|
|
|
/*
|
|
|
|
* mbsync - mailbox synchronizer
|
|
|
|
* Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
|
|
|
|
* Copyright (C) 2002-2006,2010-2012 Oswald Buddenhagen <ossi@users.sf.net>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* As a special exception, mbsync may be linked with the OpenSSL library,
|
|
|
|
* despite that library's more restrictive license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COMMON_H
|
|
|
|
#define COMMON_H
|
|
|
|
|
|
|
|
#include <autodefs.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
2014-11-30 17:41:11 +00:00
|
|
|
#include <time.h>
|
2013-12-08 19:46:40 +00:00
|
|
|
|
2014-12-07 12:19:30 +00:00
|
|
|
typedef unsigned char uchar;
|
|
|
|
typedef unsigned short ushort;
|
|
|
|
typedef unsigned int uint;
|
2019-07-28 09:35:02 +00:00
|
|
|
typedef unsigned long ulong;
|
2014-12-07 12:19:30 +00:00
|
|
|
|
2013-12-08 19:46:40 +00:00
|
|
|
#define as(ar) (sizeof(ar)/sizeof(ar[0]))
|
|
|
|
|
2019-07-28 20:31:41 +00:00
|
|
|
#define stringify__(x) #x
|
|
|
|
#define stringify(x) stringify__(x)
|
2013-12-08 19:46:40 +00:00
|
|
|
|
2020-07-08 20:16:47 +00:00
|
|
|
// From https://stackoverflow.com/a/62984543/3685191
|
|
|
|
#define deparen(x) esc_(ish_ x)
|
|
|
|
#define esc_(...) esc__(__VA_ARGS__)
|
|
|
|
#define esc__(...) van_ ## __VA_ARGS__
|
|
|
|
#define ish_(...) ish_ __VA_ARGS__
|
|
|
|
#define van_ish_
|
|
|
|
|
2016-12-18 20:52:06 +00:00
|
|
|
#define shifted_bit(in, from, to) \
|
2020-07-08 15:27:37 +00:00
|
|
|
((int)(((uint)(in) / (from > to ? from / to : 1) * (to > from ? to / from : 1)) & to))
|
2016-12-18 20:52:06 +00:00
|
|
|
|
2013-12-08 19:46:40 +00:00
|
|
|
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
|
|
|
|
# define ATTR_UNUSED __attribute__((unused))
|
|
|
|
# define ATTR_NORETURN __attribute__((noreturn))
|
|
|
|
# define ATTR_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var)))
|
|
|
|
#else
|
|
|
|
# define ATTR_UNUSED
|
|
|
|
# define ATTR_NORETURN
|
|
|
|
# define ATTR_PRINTFLIKE(fmt,var)
|
|
|
|
#endif
|
|
|
|
|
2019-07-28 18:42:04 +00:00
|
|
|
#if defined(__clang__)
|
|
|
|
# define DO_PRAGMA__(text) _Pragma(#text)
|
|
|
|
# define DIAG_PUSH DO_PRAGMA__(clang diagnostic push)
|
|
|
|
# define DIAG_POP DO_PRAGMA__(clang diagnostic pop)
|
|
|
|
# define DIAG_DISABLE(text) DO_PRAGMA__(clang diagnostic ignored text)
|
|
|
|
#elif __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 5)
|
|
|
|
# define DO_PRAGMA__(text) _Pragma(#text)
|
|
|
|
# define DIAG_PUSH DO_PRAGMA__(GCC diagnostic push)
|
|
|
|
# define DIAG_POP DO_PRAGMA__(GCC diagnostic pop)
|
|
|
|
# define DIAG_DISABLE(text) DO_PRAGMA__(GCC diagnostic ignored text)
|
|
|
|
#else
|
|
|
|
# define DIAG_PUSH
|
|
|
|
# define DIAG_POP
|
|
|
|
# define DIAG_DISABLE(text)
|
|
|
|
#endif
|
|
|
|
|
2020-12-14 22:24:05 +00:00
|
|
|
#if __GNUC__ >= 7 || defined(__clang__)
|
2017-10-01 08:04:00 +00:00
|
|
|
# define FALLTHROUGH __attribute__((fallthrough));
|
|
|
|
#else
|
|
|
|
# define FALLTHROUGH
|
|
|
|
#endif
|
|
|
|
|
2013-12-08 19:46:40 +00:00
|
|
|
#ifdef __GNUC__
|
|
|
|
# define INLINE __inline__
|
|
|
|
#else
|
|
|
|
# define INLINE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define EXE "mbsync"
|
|
|
|
|
|
|
|
/* main.c */
|
|
|
|
|
2015-03-23 07:42:51 +00:00
|
|
|
#define DEBUG_CRASH 0x01
|
|
|
|
#define DEBUG_MAILDIR 0x02
|
|
|
|
#define DEBUG_NET 0x04
|
|
|
|
#define DEBUG_NET_ALL 0x08
|
|
|
|
#define DEBUG_SYNC 0x10
|
2015-03-26 17:09:25 +00:00
|
|
|
#define DEBUG_MAIN 0x20
|
2017-04-02 12:57:17 +00:00
|
|
|
#define DEBUG_DRV 0x40
|
|
|
|
#define DEBUG_DRV_ALL 0x80
|
|
|
|
#define DEBUG_ALL (0xFF & ~(DEBUG_NET_ALL | DEBUG_DRV_ALL))
|
2015-03-23 07:42:51 +00:00
|
|
|
#define QUIET 0x100
|
|
|
|
#define VERYQUIET 0x200
|
|
|
|
#define PROGRESS 0x400
|
|
|
|
#define VERBOSE 0x800
|
|
|
|
#define KEEPJOURNAL 0x1000
|
|
|
|
#define ZERODELAY 0x2000
|
2020-12-14 22:16:01 +00:00
|
|
|
#define FORCEASYNC 0x4000
|
2013-12-08 19:46:40 +00:00
|
|
|
|
|
|
|
extern int DFlags;
|
2017-03-19 10:53:16 +00:00
|
|
|
extern int JLimit;
|
2013-12-08 19:46:40 +00:00
|
|
|
extern int UseFSync;
|
2014-10-25 15:30:57 +00:00
|
|
|
extern char FieldDelimiter;
|
2013-12-08 19:46:40 +00:00
|
|
|
|
|
|
|
extern int Pid;
|
|
|
|
extern char Hostname[256];
|
|
|
|
extern const char *Home;
|
|
|
|
|
2020-07-08 15:27:37 +00:00
|
|
|
extern uint BufferLimit;
|
2015-02-15 17:13:05 +00:00
|
|
|
|
2015-03-28 16:26:08 +00:00
|
|
|
extern int new_total[2], new_done[2];
|
|
|
|
extern int flags_total[2], flags_done[2];
|
|
|
|
extern int trash_total[2], trash_done[2];
|
|
|
|
|
|
|
|
void stats( void );
|
|
|
|
|
2013-12-08 19:46:40 +00:00
|
|
|
/* util.c */
|
|
|
|
|
2019-07-28 17:52:41 +00:00
|
|
|
void ATTR_PRINTFLIKE(2, 0) vdebug( int, const char *, va_list va );
|
|
|
|
void ATTR_PRINTFLIKE(2, 0) vdebugn( int, const char *, va_list va );
|
2013-12-08 19:46:40 +00:00
|
|
|
void ATTR_PRINTFLIKE(1, 2) info( const char *, ... );
|
|
|
|
void ATTR_PRINTFLIKE(1, 2) infon( const char *, ... );
|
2015-03-23 07:42:51 +00:00
|
|
|
void ATTR_PRINTFLIKE(1, 2) progress( const char *, ... );
|
|
|
|
void ATTR_PRINTFLIKE(1, 2) notice( const char *, ... );
|
2013-12-08 19:46:40 +00:00
|
|
|
void ATTR_PRINTFLIKE(1, 2) warn( const char *, ... );
|
|
|
|
void ATTR_PRINTFLIKE(1, 2) error( const char *, ... );
|
2020-08-04 14:54:28 +00:00
|
|
|
void ATTR_PRINTFLIKE(1, 0) vsys_error( const char *, va_list va );
|
2013-12-08 19:46:40 +00:00
|
|
|
void ATTR_PRINTFLIKE(1, 2) sys_error( const char *, ... );
|
|
|
|
void flushn( void );
|
|
|
|
|
|
|
|
typedef struct string_list {
|
|
|
|
struct string_list *next;
|
|
|
|
char string[1];
|
2019-11-15 19:20:45 +00:00
|
|
|
} string_list_t;
|
2013-12-08 19:46:40 +00:00
|
|
|
|
2020-07-08 15:27:37 +00:00
|
|
|
void add_string_list_n( string_list_t **list, const char *str, uint len );
|
2013-12-08 19:46:40 +00:00
|
|
|
void add_string_list( string_list_t **list, const char *str );
|
|
|
|
void free_string_list( string_list_t *list );
|
|
|
|
|
|
|
|
#ifndef HAVE_MEMRCHR
|
|
|
|
void *memrchr( const void *s, int c, size_t n );
|
|
|
|
#endif
|
2016-12-29 13:34:30 +00:00
|
|
|
#ifndef HAVE_STRNLEN
|
|
|
|
size_t strnlen( const char *str, size_t maxlen );
|
|
|
|
#endif
|
2013-12-08 19:46:40 +00:00
|
|
|
|
2020-07-08 15:27:37 +00:00
|
|
|
int starts_with( const char *str, int strl, const char *cmp, uint cmpl );
|
|
|
|
int starts_with_upper( const char *str, int strl, const char *cmp, uint cmpl );
|
|
|
|
int equals( const char *str, int strl, const char *cmp, uint cmpl );
|
2014-10-04 15:07:17 +00:00
|
|
|
|
2014-01-02 19:50:42 +00:00
|
|
|
#ifndef HAVE_TIMEGM
|
|
|
|
time_t timegm( struct tm *tm );
|
|
|
|
#endif
|
|
|
|
|
2013-12-08 19:46:40 +00:00
|
|
|
void *nfmalloc( size_t sz );
|
|
|
|
void *nfcalloc( size_t sz );
|
|
|
|
void *nfrealloc( void *mem, size_t sz );
|
2015-03-26 16:16:37 +00:00
|
|
|
char *nfstrndup( const char *str, size_t nchars );
|
2013-12-08 19:46:40 +00:00
|
|
|
char *nfstrdup( const char *str );
|
2019-07-28 17:52:41 +00:00
|
|
|
int ATTR_PRINTFLIKE(2, 0) nfvasprintf( char **str, const char *fmt, va_list va );
|
2013-12-08 19:46:40 +00:00
|
|
|
int ATTR_PRINTFLIKE(2, 3) nfasprintf( char **str, const char *fmt, ... );
|
|
|
|
int ATTR_PRINTFLIKE(3, 4) nfsnprintf( char *buf, int blen, const char *fmt, ... );
|
|
|
|
void ATTR_NORETURN oob( void );
|
2020-07-29 18:23:54 +00:00
|
|
|
void ATTR_NORETURN oom( void );
|
2013-12-08 19:46:40 +00:00
|
|
|
|
|
|
|
char *expand_strdup( const char *s );
|
|
|
|
|
2020-07-08 15:27:37 +00:00
|
|
|
int map_name( const char *arg, char **result, uint reserve, const char *in, const char *out );
|
2013-12-08 19:46:40 +00:00
|
|
|
|
2016-11-04 20:48:58 +00:00
|
|
|
#define DEFINE_ARRAY_TYPE(T) \
|
2017-04-02 13:42:18 +00:00
|
|
|
typedef struct { \
|
2016-11-04 20:48:58 +00:00
|
|
|
T *data; \
|
2020-07-08 15:27:37 +00:00
|
|
|
uint size; \
|
2019-11-15 19:20:45 +00:00
|
|
|
} T##_array_t; \
|
|
|
|
typedef union { \
|
2016-11-04 20:48:58 +00:00
|
|
|
T##_array_t array; \
|
2019-11-15 19:20:45 +00:00
|
|
|
struct { \
|
2020-12-17 14:53:40 +00:00
|
|
|
T *data; \
|
|
|
|
uint size; \
|
2019-11-15 19:20:45 +00:00
|
|
|
uint alloc; \
|
2020-12-17 14:53:40 +00:00
|
|
|
}; \
|
2019-11-15 19:20:45 +00:00
|
|
|
} T##_array_alloc_t; \
|
2016-11-04 20:48:58 +00:00
|
|
|
static INLINE T *T##_array_append( T##_array_alloc_t *arr ) \
|
|
|
|
{ \
|
2020-12-17 14:53:40 +00:00
|
|
|
if (arr->size == arr->alloc) { \
|
|
|
|
arr->alloc = arr->alloc * 2 + 100; \
|
|
|
|
arr->data = nfrealloc( arr->data, arr->alloc * sizeof(T) ); \
|
2016-11-04 20:48:58 +00:00
|
|
|
} \
|
2020-12-17 14:53:40 +00:00
|
|
|
return &arr->data[arr->size++]; \
|
2016-11-04 20:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define ARRAY_INIT(arr) \
|
2020-12-17 14:53:40 +00:00
|
|
|
do { (arr)->data = NULL; (arr)->size = (arr)->alloc = 0; } while (0)
|
2016-11-04 20:48:58 +00:00
|
|
|
|
|
|
|
#define ARRAY_SQUEEZE(arr) \
|
|
|
|
do { \
|
|
|
|
(arr)->data = nfrealloc( (arr)->data, (arr)->size * sizeof((arr)->data[0]) ); \
|
|
|
|
} while (0)
|
|
|
|
|
2017-03-21 19:05:29 +00:00
|
|
|
DEFINE_ARRAY_TYPE(uint)
|
|
|
|
void sort_uint_array( uint_array_t array );
|
|
|
|
int find_uint_array( const uint_array_t array, uint value );
|
2013-12-08 19:46:40 +00:00
|
|
|
|
|
|
|
void arc4_init( void );
|
2014-12-07 12:19:30 +00:00
|
|
|
uchar arc4_getbyte( void );
|
2013-12-08 19:46:40 +00:00
|
|
|
|
2020-07-08 15:27:37 +00:00
|
|
|
uint bucketsForSize( uint size );
|
2013-12-08 19:46:40 +00:00
|
|
|
|
2014-11-30 17:41:11 +00:00
|
|
|
typedef struct list_head {
|
|
|
|
struct list_head *next, *prev;
|
|
|
|
} list_head_t;
|
|
|
|
|
2014-11-29 18:15:50 +00:00
|
|
|
typedef struct notifier {
|
|
|
|
struct notifier *next;
|
|
|
|
void (*cb)( int what, void *aux );
|
|
|
|
void *aux;
|
2021-02-07 19:26:32 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
2020-07-08 15:27:37 +00:00
|
|
|
uint index;
|
2014-11-29 18:15:50 +00:00
|
|
|
#else
|
2019-07-28 19:24:17 +00:00
|
|
|
int fd;
|
|
|
|
short events;
|
2014-11-29 18:15:50 +00:00
|
|
|
#endif
|
|
|
|
} notifier_t;
|
|
|
|
|
2021-02-07 19:26:32 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
# include <poll.h>
|
2013-12-08 19:46:40 +00:00
|
|
|
#else
|
|
|
|
# define POLLIN 1
|
|
|
|
# define POLLOUT 4
|
|
|
|
# define POLLERR 8
|
|
|
|
#endif
|
|
|
|
|
2014-11-29 18:15:50 +00:00
|
|
|
void init_notifier( notifier_t *sn, int fd, void (*cb)( int, void * ), void *aux );
|
2019-07-28 19:24:17 +00:00
|
|
|
void conf_notifier( notifier_t *sn, short and_events, short or_events );
|
2019-11-16 15:00:06 +00:00
|
|
|
short notifier_config( notifier_t *sn );
|
2014-11-29 18:15:50 +00:00
|
|
|
void wipe_notifier( notifier_t *sn );
|
|
|
|
|
2014-11-30 17:41:11 +00:00
|
|
|
typedef struct {
|
|
|
|
list_head_t links;
|
|
|
|
void (*cb)( void *aux );
|
|
|
|
void *aux;
|
|
|
|
time_t timeout;
|
|
|
|
} wakeup_t;
|
|
|
|
|
|
|
|
void init_wakeup( wakeup_t *tmr, void (*cb)( void * ), void *aux );
|
|
|
|
void conf_wakeup( wakeup_t *tmr, int timeout );
|
|
|
|
void wipe_wakeup( wakeup_t *tmr );
|
2019-07-28 18:50:31 +00:00
|
|
|
static INLINE int ATTR_UNUSED pending_wakeup( wakeup_t *tmr ) { return tmr->links.next != NULL; }
|
2014-11-30 17:41:11 +00:00
|
|
|
|
2013-12-08 19:46:40 +00:00
|
|
|
void main_loop( void );
|
|
|
|
|
|
|
|
#endif
|