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) {
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
} 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
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) */
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;
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
}
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);
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,
/* 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);
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
}
}
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++;
rrd_free(&rrdold);
rrd_close(rrd_file);
- rrd_free(&rrdnew);
rrd_close(rrd_out_file);
return (0);