Browse Source

* ability to use locks on database file

master
Alex 'AdUser' Z 8 years ago
parent
commit
43a3dca5f7
  1. 13
      src/database.c
  2. 4
      src/simdb.h

13
src/database.c

@ -81,6 +81,14 @@ simdb_open(const char *path, int mode, int *error) {
return NULL;
}
if ((mode & SIMDB_FLAG_WRITE) && (mode & (SIMDB_FLAG_LOCK|SIMDB_FLAG_LOCKNB))) {
int locktype = (mode & SIMDB_FLAG_LOCKNB) ? F_TLOCK : F_LOCK;
if (lockf(fd, locktype, 0) < 0) {
*error = SIMDB_ERR_LOCK;
return NULL;
}
}
errno = 0;
bytes = pread(fd, buf, SIMDB_REC_LEN, 0);
if (bytes < SIMDB_REC_LEN) {
@ -106,8 +114,7 @@ simdb_open(const char *path, int mode, int *error) {
return NULL;
}
if (mode & SIMDB_FLAG_WRITE)
flags |= SIMDB_FLAG_WRITE;
flags = mode & SIMDB_FLAGS_MASK;
/* all seems to be ok */
@ -166,6 +173,8 @@ simdb_error(int error) {
return "wrong parameters passed to finction";
} else if (error == SIMDB_ERR_SAMPLER) {
return "given file not an image, damaged or has unsupported format";
} else if (error == SIMDB_ERR_LOCK) {
return "can't add lock on database file";
}
return "unknown error";
}

4
src/simdb.h

@ -12,7 +12,10 @@
/**
* @defgroup SIMDBFlags Database runtime flags
* @{ */
#define SIMDB_FLAGS_MASK 0xFF
#define SIMDB_FLAG_WRITE 1 << (0 + 0) /**< database has write access */
#define SIMDB_FLAG_LOCK 1 << (0 + 1) /**< use locks for file with write access (only with @ref SIMDB_FLAG_WRITE) */
#define SIMDB_FLAG_LOCKNB 1 << (0 + 2) /**< same as above, but not wait for lock (only with @ref SIMDB_FLAG_WRITE) */
/** @} */
/**
@ -43,6 +46,7 @@
#define SIMDB_ERR_READONLY -6 /**< database opened in read-only mode */
#define SIMDB_ERR_USAGE -7 /**< wrong arguments passed */
#define SIMDB_ERR_SAMPLER -8 /**< given file not an image, damaged or has unsupported format */
#define SIMDB_ERR_LOCK -9 /**< can't add lock on database file */
/** @} */
/** opaque database handler */

Loading…
Cancel
Save