Skip to content

Commit

Permalink
Sync cache contents to disk before unlocking it (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
piste-jp-ibm committed Oct 4, 2018
1 parent 82ebc5b commit e3a6316
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions messages/libltfs/root.txt
Expand Up @@ -791,6 +791,7 @@ root:table {
17252W:string { "Invalid value (%s) in the %s tag in UID %lld." }
17253E:string { "Cannot get tape parameters : %s (%d)." }
17254E:string { "This cartridge cannot be reformatted in the drive (0x%02x, %d)." }
17255I:string { "Cannot open %s cache for sync (%d)." }

// For Debug 19999I:string { "%s %s %d." }

Expand Down
1 change: 1 addition & 0 deletions src/libltfs/xml_writer.c
Expand Up @@ -275,6 +275,7 @@ static int _copy_file_contents(int dest, int src)
}

free(buf);
fsync(dest);

if (len_read) {
ltfsmsg(LTFS_ERR, 17246E, "_copy_file unexpected read", errno);
Expand Down
25 changes: 24 additions & 1 deletion src/libltfs/xml_writer_libltfs.c
Expand Up @@ -62,6 +62,11 @@
#include "pathname.h"
#include "arch/time_internal.h"

/* O_BINARY is defined only in MinGW */
#ifndef O_BINARY
#define O_BINARY 0
#endif

/* Structure to control EE's file offset cache and sync file list */
struct ltfsee_cache
{
Expand Down Expand Up @@ -708,7 +713,7 @@ xmlBufferPtr xml_make_schema(const char *creator, const struct ltfs_index *idx)

static int _commit_offset_caches(const char* path, const struct ltfs_index *idx)
{
int ret = 0;
int ret = 0, fd = -1;
char *offset_name = NULL, *sync_name = NULL;
char *offset_new = NULL, *sync_new = NULL;

Expand All @@ -720,6 +725,15 @@ static int _commit_offset_caches(const char* path, const struct ltfs_index *idx)
if (ret > 0) {
unlink(offset_name);
rename(offset_new, offset_name);
fd = open(offset_name, O_RDWR | O_BINARY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fd >= 0) {
fsync(fd);
close(fd);
fd = -1;
} else {
ltfsmsg(LTFS_INFO, 17255I, offset_name, errno);
}
free(offset_name);
}
free(offset_new);
Expand All @@ -731,6 +745,15 @@ static int _commit_offset_caches(const char* path, const struct ltfs_index *idx)
if (ret > 0) {
unlink(sync_name);
rename(sync_new, sync_name);
fd = open(sync_name, O_RDWR | O_BINARY,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fd >= 0) {
fsync(fd);
close(fd);
fd = -1;
} else {
ltfsmsg(LTFS_INFO, 17255I, sync_name, errno);
}
free(sync_name);
}
free(sync_new);
Expand Down

0 comments on commit e3a6316

Please sign in to comment.