Skip to content

Commit

Permalink
mappedfile: only release the buffer if fd changed (github #5)
Browse files Browse the repository at this point in the history
  • Loading branch information
brong committed Jul 15, 2016
1 parent e776324 commit d4126e8
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions lib/mappedfile.c
Expand Up @@ -90,7 +90,6 @@ struct mappedfile {
size_t map_size;

/* the file itself */
ino_t map_ino;
int fd;

/* tracking */
Expand Down Expand Up @@ -226,6 +225,7 @@ EXPORTED int mappedfile_readlock(struct mappedfile *mf)
return -EIO;
}
if (sbuf.st_ino == sbuffile.st_ino) break;
buf_free(&mf->map_buf);

newfd = open(mf->fname, O_RDWR, 0644);
if (newfd == -1) {
Expand All @@ -240,11 +240,6 @@ EXPORTED int mappedfile_readlock(struct mappedfile *mf)

mf->lock_status = MF_READLOCKED;

/* XXX - can we guarantee the fd isn't reused? */
if (mf->map_ino != sbuf.st_ino) {
buf_free(&mf->map_buf);
}

_ensure_mapped(mf, sbuf.st_size, /*update*/0);

return 0;
Expand All @@ -255,23 +250,21 @@ EXPORTED int mappedfile_writelock(struct mappedfile *mf)
int r;
struct stat sbuf;
const char *lockfailaction;
int changed = 0;

assert(mf->lock_status == MF_UNLOCKED);
assert(mf->fd != -1);
assert(mf->is_rw);
assert(!mf->dirty);

r = lock_reopen(mf->fd, mf->fname, &sbuf, &lockfailaction);
r = lock_reopen_ex(mf->fd, mf->fname, &sbuf, &lockfailaction, &changed);
if (r < 0) {
syslog(LOG_ERR, "IOERROR: %s %s: %m", lockfailaction, mf->fname);
return r;
}
mf->lock_status = MF_WRITELOCKED;

/* XXX - can we guarantee the fd isn't reused? */
if (mf->map_ino != sbuf.st_ino) {
buf_free(&mf->map_buf);
}
if (changed) buf_free(&mf->map_buf);

_ensure_mapped(mf, sbuf.st_size, /*update*/0);

Expand Down

0 comments on commit d4126e8

Please sign in to comment.