+static void log_commit(BlueSkyLog *log)
+{
+ int batchsize = 0;
+
+ if (log->fd < 0)
+ return;
+
+ fdatasync(log->fd);
+ while (log->committed != NULL) {
+ BlueSkyCloudLog *item = (BlueSkyCloudLog *)log->committed->data;
+ g_mutex_lock(item->lock);
+ bluesky_cloudlog_stats_update(item, -1);
+ item->pending_write &= ~CLOUDLOG_JOURNAL;
+ item->location_flags |= CLOUDLOG_JOURNAL;
+ bluesky_cloudlog_stats_update(item, 1);
+ g_cond_signal(item->cond);
+ g_mutex_unlock(item->lock);
+ log->committed = g_slist_delete_link(log->committed, log->committed);
+ bluesky_cloudlog_unref(item);
+ batchsize++;
+ }
+
+ if (bluesky_verbose && batchsize > 1)
+ g_print("Log batch size: %d\n", batchsize);
+}
+
+static gboolean log_open(BlueSkyLog *log)
+{
+ char logname[64];
+
+ if (log->fd >= 0) {
+ log_commit(log);
+ close(log->fd);
+ log->seq_num++;
+ log->fd = -1;
+ }
+
+ while (log->fd < 0) {
+ g_snprintf(logname, sizeof(logname), "log-%08d", log->seq_num);
+ log->fd = openat(log->dirfd, logname, O_CREAT|O_WRONLY|O_EXCL, 0600);
+ if (log->fd < 0 && errno == EEXIST) {
+ fprintf(stderr, "Log file %s already exists...\n", logname);
+ log->seq_num++;
+ continue;
+ } else if (log->fd < 0) {
+ fprintf(stderr, "Error opening logfile %s: %m\n", logname);
+ return FALSE;
+ }
+ }
+
+ if (ftruncate(log->fd, LOG_SEGMENT_SIZE) < 0) {
+ fprintf(stderr, "Unable to truncate logfile %s: %m\n", logname);
+ }
+ fsync(log->fd);
+ fsync(log->dirfd);
+ return TRUE;
+}
+