From 074035cb8c68834d87ce33292e0654a1effa6b86 Mon Sep 17 00:00:00 2001 From: oetiker Date: Fri, 1 Jun 2007 19:11:49 +0000 Subject: [PATCH] Bernhard: - implement resize SHRINK for FD (mmap is to be done) - indent git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk@1100 a5681a0c-68f1-0310-ab6d-d61299d08faa --- program/src/rrd_open.c | 27 ++++++++++++++------------- program/src/rrd_resize.c | 26 +++++++++++++++----------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/program/src/rrd_open.c b/program/src/rrd_open.c index e8391845..6e8a0425 100644 --- a/program/src/rrd_open.c +++ b/program/src/rrd_open.c @@ -145,8 +145,10 @@ rrd_file_t *rrd_open( rrd_file_t *rrd_file = NULL; off_t newfile_size = 0; - if (rdwr & RRD_CREAT) - newfile_size = (off_t) rrd->stat_head; + if (rdwr & RRD_CREAT) { + newfile_size = (off_t) rrd->stat_head->float_cookie; + free(rrd->stat_head); + } rrd_init(rrd); rrd_file = malloc(sizeof(rrd_file_t)); if (rrd_file == NULL) { @@ -189,7 +191,6 @@ rrd_file_t *rrd_open( mm_flags |= MAP_POPULATE; /* populate ptes and data */ #endif #if defined MAP_NONBLOCK -// if (!(rdwr & RRD_COPY)) mm_flags |= MAP_NONBLOCK; /* just populate ptes */ #endif #ifdef USE_DIRECT_IO @@ -217,7 +218,7 @@ rrd_file_t *rrd_open( } else { rrd_file->file_len = newfile_size; lseek(rrd_file->fd, newfile_size - 1, SEEK_SET); - write(rrd_file->fd, "\0", 1); /* poke */ + write(rrd_file->fd, "\0", 1); /* poke */ lseek(rrd_file->fd, 0, SEEK_SET); } #ifdef HAVE_POSIX_FADVISE @@ -257,6 +258,8 @@ rrd_file_t *rrd_open( goto out_done; } #endif + if (rdwr & RRD_CREAT) + goto out_done; #ifdef USE_MADVISE if (rdwr & RRD_COPY) { /* We will read everything in a moment (copying) */ @@ -356,9 +359,7 @@ rrd_file_t *rrd_open( rrd_file->header_len = offset; rrd_file->pos = offset; -#ifdef USE_MADVISE out_done: -#endif return (rrd_file); out_nullify_head: rrd->stat_head = NULL; @@ -487,23 +488,24 @@ inline ssize_t rrd_read( size_t count) { #ifdef HAVE_MMAP - size_t _cnt = count; - ssize_t _surplus = rrd_file->pos + _cnt - rrd_file->file_len; + size_t _cnt = count; + ssize_t _surplus = rrd_file->pos + _cnt - rrd_file->file_len; + if (_surplus > 0) { /* short read */ _cnt -= _surplus; } if (_cnt == 0) - return 0; /* EOF */ + return 0; /* EOF */ buf = memcpy(buf, rrd_file->file_start + rrd_file->pos, _cnt); - rrd_file->pos += _cnt; /* mimmic read() semantics */ + rrd_file->pos += _cnt; /* mimmic read() semantics */ return _cnt; #else ssize_t ret; ret = read(rrd_file->fd, buf, count); if (ret > 0) - rrd_file->pos += ret; /* mimmic read() semantics */ + rrd_file->pos += ret; /* mimmic read() semantics */ return ret; #endif } @@ -571,8 +573,7 @@ inline void rrd_free( void rrd_free( rrd_t *rrd) { - if (atoi(rrd->stat_head->version) < 3) - free(rrd->live_head); + free(rrd->live_head); free(rrd->stat_head); free(rrd->ds_def); free(rrd->rra_def); diff --git a/program/src/rrd_resize.c b/program/src/rrd_resize.c index b242b482..9dcfccab 100644 --- a/program/src/rrd_resize.c +++ b/program/src/rrd_resize.c @@ -74,18 +74,22 @@ int rrd_resize( return (-1); } - if (modify < 0) { + if (modify < 0) if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) { rrd_set_error("This RRA is not that big"); rrd_free(&rrdold); rrd_close(rrd_file); return (-1); } - } else { - /* the size of the new file */ - rrdnew.stat_head = rrd_file->file_len + - (rrdold.stat_head->ds_cnt * sizeof(rrd_value_t) * modify); + /* the size of the new file */ + if ((rrdnew.stat_head = calloc(1, sizeof(stat_head_t))) == NULL) { + rrd_set_error("allocating stat_head for new RRD"); + rrd_free(&rrdold); + rrd_close(rrd_file); + return (-1); } + rrdnew.stat_head->float_cookie = rrd_file->file_len + + (rrdold.stat_head->ds_cnt * sizeof(rrd_value_t) * modify); rrd_out_file = rrd_open(outfilename, &rrdnew, RRD_READWRITE | RRD_CREAT); if (rrd_out_file == NULL) { rrd_set_error("Can't create '%s': %s", outfilename, @@ -162,8 +166,8 @@ int rrd_resize( /* Adding extra rows; insert unknown values just after the ** current row number. */ - l = rrdnew.stat_head->ds_cnt * (rrdnew.rra_ptr[target_rra].cur_row + - 1); + l = rrdnew.stat_head->ds_cnt * + (rrdnew.rra_ptr[target_rra].cur_row + 1); while (l > 0) { rrd_read(rrd_file, &buffer, sizeof(rrd_value_t) * 1); rrd_write(rrd_out_file, &buffer, sizeof(rrd_value_t) * 1); @@ -177,8 +181,8 @@ int rrd_resize( l--; } #else - /* for the mmap case, we did already fill the whole new file with DNAN - * before we copied the old values, so nothing to do here. */ + /* for the mmap case, we did already fill the whole new file with DNAN + * before we copied the old values, so nothing to do here. */ #endif } else { /* Removing rows. Normally this would be just after the cursor @@ -212,7 +216,8 @@ int rrd_resize( } } while (modify < 0) { - rrd_seek(rrd_file, sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt, + rrd_seek(rrd_file, + sizeof(rrd_value_t) * rrdnew.stat_head->ds_cnt, SEEK_CUR); rrdnew.rra_def[target_rra].row_cnt--; modify++; @@ -243,7 +248,6 @@ int rrd_resize( rrd_free(&rrdold); rrd_close(rrd_file); - rrd_free(&rrdnew); rrd_close(rrd_out_file); return (0); -- 2.30.2