Skip to content
Browse files

Linux fsync updates

  • Loading branch information...
1 parent 23d42f5 commit 31e0c114994b277e3eb7e76fe2f0ef6f8f1d5594 Steve Dekorte committed
Showing with 37 additions and 5 deletions.
  1. +31 −4 libs/jfile/source/JFile.c
  2. +6 −1 libs/udb/source/UDB.c
View
35 libs/jfile/source/JFile.c
@@ -124,7 +124,13 @@ void JFile_open(JFile *self)
self->log = fopen(self->logPath, "w");
fclose(self->log);
self->log = fopen(self->logPath, "r+");
- fcntl(fileno(self->log), F_PREALLOCATE, self->logHighWaterMark);
+ #ifdef F_PREALLOCATE
+ // OSX
+ fcntl(fileno(self->log), F_PREALLOCATE, self->logHighWaterMark);
+ #else
+ // Linux
+ posix_fallocate(fileno(self->log), 0, self->logHighWaterMark);
+ #endif
}
else
{
@@ -255,24 +261,45 @@ long JFile_setPositionToEnd(JFile *self)
return self->pos;
}
+void JFile_hardSyncFileDescriptor_(JFile *self, int fd)
+{
+ #ifdef F_FULLFSYNC
+ fcntl(fd, F_FULLFSYNC, NULL);
+ #else
+ #warning Linux can't ensure data sync to physical media
+ fsync(fd);
+ #endif
+}
+
void JFile_syncFileWritesToDisk(JFile *self)
{
//printf("f"); fflush(stdout);
fflush(self->file);
- if(self->fullSync) fcntl(fileno(self->file), F_FULLFSYNC, NULL);
+
+ if(self->fullSync)
+ {
+ JFile_hardSyncFileDescriptor_(self, fileno(self->file));
+ }
}
void JFile_syncLogWritesToDisk(JFile *self)
{
//printf("j"); fflush(stdout);
fflush(self->log);
- if (self->fullSync) fcntl(fileno(self->log), F_FULLFSYNC, NULL);
+ if (self->fullSync)
+ {
+ JFile_hardSyncFileDescriptor_(self, fileno(self->log));
+ }
+
fseek(self->log, -1, SEEK_END);
fputc(JFILE_END_TRANSACTION, self->log);
fflush(self->log);
- if (self->fullSync) fcntl(fileno(self->log), F_FULLFSYNC, NULL);
+ if (self->fullSync)
+ {
+ JFile_hardSyncFileDescriptor_(self, fileno(self->log));
+ }
}
void JFile_writeLogToFileIfNeeded(JFile *self)
View
7 libs/udb/source/UDB.c
@@ -135,7 +135,12 @@ void UDB_setCommittedFlag_(UDB *self, int flag)
{
fseek(self->committedFile, 0, SEEK_SET);
fputc(flag, self->committedFile);
- fcntl(fileno(self->committedFile), F_FULLFSYNC, NULL);
+ #ifdef F_FULLFSYNC
+ fcntl(fileno(self->committedFile), F_FULLFSYNC, NULL);
+ #else
+ #warning Linux doesn't support syncing to physical media
+ fsync(fileno(self->committedFile));
+ #endif
}
void UDB_commitTransaction(UDB *self)

0 comments on commit 31e0c11

Please sign in to comment.
Something went wrong with that request. Please try again.