@cryptotaxi247 / netdata-1 / commits / ccbb062ed

properly cleanup deleted files of obsolete charts from disk; fixes #1358

Costa Tsaousis (ktsaou) committed Apr 4, 2017 at 02:20 UTC ccbb062edade163bcdd70b53998d626bf3733198
5 files changed +58 -13
src/common.c
+42
@@ -1228,3 +1228,45 @@ unsigned long end_tsc(void) {
1228 return (((unsigned long)d << 32) | (unsigned long)a) - tsc;
1229 }
1230 */
1231 +
1232 +int recursively_delete_dir(const char *path, const char *reason) {
1233 + DIR *dir = opendir(path);
1234 + if(!dir) {
1235 + error("Cannot read %s directory to be deleted '%s'", reason?reason:"", path);
1236 + return -1;
1237 + }
1238 +
1239 + int ret = 0;
1240 + struct dirent *de = NULL;
1241 + while((de = readdir(dir))) {
1242 + if(de->d_type == DT_DIR
1243 + && (
1244 + (de->d_name[0] == '.' && de->d_name[1] == '\0')
1245 + || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1246 + ))
1247 + continue;
1248 +
1249 + char fullpath[FILENAME_MAX + 1];
1250 + snprintfz(fullpath, FILENAME_MAX, "%s/%s", path, de->d_name);
1251 +
1252 + if(de->d_type == DT_DIR) {
1253 + int r = recursively_delete_dir(fullpath, reason);
1254 + if(r > 0) ret += r;
1255 + continue;
1256 + }
1257 +
1258 + info("Deleting %s file '%s'", reason?reason:"", fullpath);
1259 + if(unlikely(unlink(fullpath) == -1))
1260 + error("Cannot delete %s file '%s'", reason?reason:"", fullpath);
1261 + else
1262 + ret++;
1263 + }
1264 +
1265 + info("Deleting empty directory '%s'", path);
1266 + if(unlikely(rmdir(path) == -1))
1267 + error("Cannot delete empty directory '%s'", path);
1268 + else
1269 + ret++;
1270 +
1271 + return ret;
1272 +}
src/common.h
+2
@@ -289,6 +289,8 @@ extern pid_t get_system_pid_max(void);
289 extern unsigned int hz;
290 extern void get_system_HZ(void);
291
292 +extern int recursively_delete_dir(const char *path, const char *reason);
293 +
294 extern volatile sig_atomic_t netdata_exit;
295 extern const char *os_type;
296
src/rrddim.c
-4
@@ -233,10 +233,6 @@ void rrddim_free(RRDSET *st, RRDDIM *rd)
233
234 switch(rd->rrd_memory_mode) {
235 case RRD_MEMORY_MODE_SAVE:
236 - debug(D_RRD_CALLS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
237 - savememory(rd->cache_filename, rd, rd->memsize);
238 - // continue to map mode - no break;
239 -
236 case RRD_MEMORY_MODE_MAP:
237 debug(D_RRD_CALLS, "Unmapping dimension '%s'.", rd->name);
238 freez((void *)rd->id);
src/rrdhost.c
+2
@@ -539,6 +539,8 @@ void rrdhost_delete(RRDHOST *host) {
539 rrdset_unlock(st);
540 }
541
542 + recursively_delete_dir(host->cache_dir, "left over host");
543 +
544 rrdhost_unlock(host);
545 }
546
src/rrdset.c
+12 -9
@@ -288,8 +288,6 @@ void rrdset_free(RRDSET *st) {
288 }
289
290 void rrdset_save(RRDSET *st) {
291 - RRDDIM *rd;
292 -
291 rrdset_check_rdlock(st);
292
293 // info("Saving chart '%s' ('%s')", st->id, st->name);
@@ -299,6 +297,7 @@ void rrdset_save(RRDSET *st) {
297 savememory(st->cache_filename, st, st->memsize);
298 }
299
300 + RRDDIM *rd;
301 rrddim_foreach_read(rd, st) {
302 if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
303 debug(D_RRD_STATS, "Saving dimension '%s' to '%s'.", rd->name, rd->cache_filename);
@@ -312,19 +311,23 @@ void rrdset_delete(RRDSET *st) {
311
312 rrdset_check_rdlock(st);
313
315 - // info("Deleting chart '%s' ('%s')", st->id, st->name);
314 + info("Deleting chart '%s' ('%s') from disk...", st->id, st->name);
315
317 - if(st->rrd_memory_mode == RRD_MEMORY_MODE_SAVE) {
318 - debug(D_RRD_STATS, "Deleting stats '%s' to '%s'.", st->name, st->cache_filename);
319 - unlink(st->cache_filename);
316 + if(st->rrd_memory_mode != RRD_MEMORY_MODE_RAM) {
317 + info("Deleting chart header file '%s'.", st->cache_filename);
318 + if(unlikely(unlink(st->cache_filename) == -1))
319 + error("Cannot delete chart header file '%s'", st->cache_filename);
320 }
321
322 rrddim_foreach_read(rd, st) {
323 - if(likely(rd->rrd_memory_mode == RRD_MEMORY_MODE_SAVE)) {
324 - debug(D_RRD_STATS, "Deleting dimension '%s' to '%s'.", rd->name, rd->cache_filename);
325 - unlink(rd->cache_filename);
323 + if(likely(rd->rrd_memory_mode != RRD_MEMORY_MODE_RAM)) {
324 + info("Deleting dimension file '%s'.", rd->cache_filename);
325 + if(unlikely(unlink(rd->cache_filename) == -1))
326 + error("Cannot delete dimension file '%s'", rd->cache_filename);
327 }
328 }
329 +
330 + recursively_delete_dir(st->cache_dir, "left-over chart");
331 }
332
333 // ----------------------------------------------------------------------------