2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
25 typedef uint64_t blockaddr;
26 typedef int64_t blockidx;
29 #define BLOCK_HEAD_SIZE 4 // only one int
30 #define GETMSG_MAX_BLOCK_SIZE (65*1024-BLOCK_HEAD_SIZE)
31 #define GETMSG_MAX_HASH_SIZE 64 /* SHA512 */
32 #define GETMSG_MAX_MSG_SIZE (GETMSG_MAX_BLOCK_SIZE+GETMSG_MAX_HASH_SIZE+sizeof(uint32_t)+OFFSET_FADDR_SIZE+100)
36 class bmessage: public SMARTALLOC
39 enum { bm_none, bm_ready, bm_busy, bm_ref };
41 POOLMEM *msg; // exchanged with BSOCK
42 int32_t msglen; // length from BSOCK
43 int32_t origlen; // length before rehydration, to be compared with length in header
44 char *rbuf; // adjusted to point to data inside *msg
45 int32_t rbuflen; // adjusted from msglen
47 int ret; // return value from bget_msg()
48 int jobbytes; // must be added to jcr->JobBytes if block is downloaded
50 bmessage(int bufsize);
52 void swap(BSOCK *sock);
55 class GetMsg: public SMARTALLOC
60 const char *rec_header; /* Format of a header */
61 int32_t bufsize; /* "ideal" bufsize from JCR */
63 bool m_is_stop; /* set by the read thread when bsock->is_stop() */
64 bool m_is_done; /* set when the read thread finish (no more record will be pushed) */
65 bool m_is_error; /* set when the read thread get an error */
69 pthread_mutex_t mutex;
72 bmessage *bmsg; // local bmsg used by bget_msg(NULL)
73 int32_t msglen; // used to mimic BSOCK, updated by bget_msg()
74 POOLMEM *msg; // used to mimic BSOCK, updated by bget_msg()
76 void inc_use_count(void) {P(mutex); m_use_count++; V(mutex); };
77 void dec_use_count(void) {P(mutex); m_use_count--; V(mutex); };
78 int32_t use_count() { int32_t v; P(mutex); v = m_use_count; V(mutex); return v;};
81 GetMsg(JCR *a_jcr, BSOCK *a_bsock, const char *a_rec_header, int32_t a_bufsize);
84 virtual int bget_msg(bmessage **pbmsg=NULL);
85 inline virtual void *do_read_sock_thread(void) { return NULL; };
86 inline virtual int start_read_sock() { return 0; };
87 inline virtual void *wait_read_sock(int /*emergency_quit*/) { return NULL;};
89 virtual bool is_stop() { return (m_is_stop!=false); };
90 virtual bool is_done() { return (m_is_done!=false); };
91 virtual bool is_error(){ return (m_is_error!=false); };
93 bmessage *new_msg() { return New(bmessage(bufsize)); };
97 /* Call this function to release the memory associated with the message queue
98 * The reading thread is using the BufferedMsgBase to work, so we need to free
99 * the memory only when the main thread and the reading thread agree
101 inline void free_GetMsg(GetMsg *b)
104 ASSERT2(b->use_count() >= 0, "GetMsg use_count too low");
105 if (b->use_count() == 0) {