@@ -50,6 +50,7 @@ gchar *assign_persist_name;
50
50
gboolean relocate_all ;
51
51
gboolean display_version ;
52
52
gboolean assign_help ;
53
+ gboolean truncate_confirm ;
53
54
54
55
static GOptionEntry cat_options [] =
55
56
{
@@ -65,6 +66,12 @@ static GOptionEntry info_options[] =
65
66
{ NULL , 0 , 0 , G_OPTION_ARG_NONE , NULL , NULL }
66
67
};
67
68
69
+ static GOptionEntry truncate_options [] =
70
+ {
71
+ { "force" , 'f' , 0 , G_OPTION_ARG_NONE , & truncate_confirm },
72
+ { NULL , 0 , 0 , G_OPTION_ARG_NONE , NULL , NULL }
73
+ };
74
+
68
75
static GOptionEntry relocate_options [] =
69
76
{
70
77
{
@@ -100,9 +107,9 @@ static GOptionEntry assign_options[] =
100
107
};
101
108
102
109
static gboolean
103
- open_queue (char * filename , LogQueue * * lq , DiskQueueOptions * options )
110
+ open_queue (char * filename , LogQueue * * lq , DiskQueueOptions * options , gboolean read_only )
104
111
{
105
- options -> read_only = TRUE ;
112
+ options -> read_only = read_only ;
106
113
options -> reliable = FALSE;
107
114
FILE * f = fopen (filename , "rb" );
108
115
if (f )
@@ -142,6 +149,51 @@ open_queue(char *filename, LogQueue **lq, DiskQueueOptions *options)
142
149
return TRUE;
143
150
}
144
151
152
+ static inline off_t
153
+ get_file_size (const char * path )
154
+ {
155
+ struct stat s ;
156
+ if (stat (path , & s ) != 0 )
157
+ return 0 ;
158
+
159
+ return s .st_size ;
160
+ }
161
+
162
+ static gint
163
+ dqtool_truncate (int argc , char * argv [])
164
+ {
165
+ if (!truncate_confirm )
166
+ {
167
+ printf ("Truncating disk-buffer files is not recommended in case they are actively used by syslog-ng.\n"
168
+ "Repeat this command with the --force flag if you are sure you want to truncate them.\n" );
169
+ return 1 ;
170
+ }
171
+
172
+ for (gint i = optind ; i < argc ; i ++ )
173
+ {
174
+ LogQueue * lq ;
175
+ DiskQueueOptions options = {0 };
176
+ disk_queue_options_set_default_options (& options );
177
+
178
+ options .truncate_size_ratio = 0 ;
179
+ off_t orig_size = get_file_size (argv [i ]);
180
+
181
+ if (!open_queue (argv [i ], & lq , & options , FALSE))
182
+ continue ;
183
+
184
+ gboolean persistent ;
185
+ log_queue_disk_stop (lq , & persistent );
186
+ log_queue_unref (lq );
187
+
188
+ off_t truncated_size = get_file_size (argv [i ]);
189
+
190
+ double reclaimed_gib = (orig_size - truncated_size ) / 1024.0 / 1024.0 / 1024.0 ;
191
+ printf ("Disk-buffer %s has been truncated, reclaimed space: %f GiB\n" , argv [i ], reclaimed_gib );
192
+ }
193
+
194
+ return 0 ;
195
+ }
196
+
145
197
static gint
146
198
dqtool_cat (int argc , char * argv [])
147
199
{
@@ -178,7 +230,7 @@ dqtool_cat(int argc, char *argv[])
178
230
LogPathOptions local_options = LOG_PATH_OPTIONS_INIT ;
179
231
LogQueue * lq ;
180
232
181
- if (!open_queue (argv [i ], & lq , & options ))
233
+ if (!open_queue (argv [i ], & lq , & options , TRUE ))
182
234
continue ;
183
235
184
236
log_queue_rewind_backlog_all (lq );
@@ -200,7 +252,6 @@ dqtool_cat(int argc, char *argv[])
200
252
}
201
253
g_string_free (msg , TRUE);
202
254
return 0 ;
203
-
204
255
}
205
256
206
257
static gint
@@ -213,7 +264,7 @@ dqtool_info(int argc, char *argv[])
213
264
DiskQueueOptions options = {0 };
214
265
disk_queue_options_set_default_options (& options );
215
266
216
- if (!open_queue (argv [i ], & lq , & options ))
267
+ if (!open_queue (argv [i ], & lq , & options , TRUE ))
217
268
continue ;
218
269
219
270
gboolean persistent ;
@@ -629,6 +680,7 @@ static struct
629
680
{ "info" , info_options , "Print infos about the given disk queue file" , dqtool_info },
630
681
{ "relocate" , relocate_options , "Relocate(rename) diskq file. Note that this option modifies the persist file." , dqtool_relocate },
631
682
{ "assign" , assign_options , "Assign diskq file to the given persist file with the given persist name." , dqtool_assign },
683
+ { "truncate" , truncate_options , "Truncate unused space in abandoned disk queues" , dqtool_truncate },
632
684
{ NULL , NULL },
633
685
};
634
686
0 commit comments