+void get_cur_epoch() {
+ int fd;
+ fd = open("/tmp/fprobe_last_epoch",O_RDONLY);
+ if (fd != -1) {
+ char snum[7];
+ ssize_t len;
+ len = read(fd, snum, sizeof(snum)-1);
+ if (len != -1) {
+ snum[len]='\0';
+ sscanf(snum,"%d",&cur_epoch);
+ close(fd);
+ }
+ }
+ return;
+}
+
+
+void update_cur_epoch_file(int n) {
+ int fd, len;
+ char snum[7];
+ len=snprintf(snum,6,"%d",n);
+ fd = open("/tmp/fprobe_last_epoch",O_WRONLY|O_CREAT|O_TRUNC);
+ if (fd == -1) {
+ my_log(LOG_ERR, "open() failed: /tmp/fprobe_last_epoch.The next restart will resume logging from epoch id 0.");
+ return;
+ }
+ write(fd, snum, len);
+ close(fd);
+}
+
+unsigned get_log_fd(char *fname, unsigned cur_fd) {
+ struct Time now;
+ unsigned cur_uptime;
+ /* We check if the amount of space left on the disk < some threshold and start reusing logs, or bail out if that
+ * doesn't solve the problem */
+
+ struct statfs statfs;
+ int ret_fd;
+ gettime(&now);
+ cur_uptime = getuptime_minutes(&now);
+
+ if (fstatfs(cur_fd, &statfs) && cur_fd!=START_VALUE) {
+ my_log(LOG_ERR, "PANIC! Can't stat disk to calculate free blocks");
+ }
+ else {
+ if (min_free && statfs.f_bfree < min_free)
+ switch(cur_epoch) {
+ case 0: /* Uh oh. Our first file filled up all of the free space. Just bail out. */
+ my_log(LOG_ERR, "The first epoch filled up all the free space on disk. Bailing out.");
+ exit(1);
+ default:
+ my_log(LOG_INFO, "Disk almost full. I'm going to drop data. Max epochs = %d\n",cur_epoch);
+ cur_epoch = -1;
+ }
+ }
+
+ /* Epoch length in minutes */
+ if ((cur_uptime - prev_uptime) > epoch_length || cur_fd<0 || cur_epoch==-1) {
+ char nextname[MAX_PATH_LEN];
+ int write_fd;
+ prev_uptime = cur_uptime;
+ cur_epoch = (cur_epoch + 1) % log_epochs;
+ close(cur_fd);
+ snprintf(nextname,MAX_PATH_LEN,"%s.%d",fname,cur_epoch);
+ if ((write_fd = open(nextname, O_WRONLY|O_CREAT|O_TRUNC)) < 0) {
+ my_log(LOG_ERR, "open(): %s (%s)\n", nextname, strerror(errno));
+ exit(1);
+ }
+ update_cur_epoch_file(cur_epoch);
+ ret_fd = write_fd;
+ }
+ else
+ ret_fd = cur_fd;
+ return(ret_fd);
+}
+