diff options
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/file.c | 28 | ||||
| -rw-r--r-- | src/core/file.h | 6 | ||||
| -rw-r--r-- | src/core/platform.h | 12 |
3 files changed, 46 insertions, 0 deletions
diff --git a/src/core/file.c b/src/core/file.c index 4715be2a..b76fdcda 100644 --- a/src/core/file.c +++ b/src/core/file.c @@ -128,4 +128,32 @@ const char * nni_file_basename(const char *path) { return (nni_plat_file_basename(path)); +} + +struct nni_file_lockh { + nni_plat_flock lk; +}; + +int +nni_file_lock(const char *path, nni_file_lockh **hp) +{ + nni_file_lockh *h; + int rv; + if ((h = NNI_ALLOC_STRUCT(h)) == NULL) { + return (NNG_ENOMEM); + } + rv = nni_plat_file_lock(path, &h->lk); + if (rv != 0) { + NNI_FREE_STRUCT(h); + return (rv); + } + *hp = h; + return (0); +} + +void +nni_file_unlock(nni_file_lockh *h) +{ + nni_plat_file_unlock(&h->lk); + NNI_FREE_STRUCT(h); }
\ No newline at end of file diff --git a/src/core/file.h b/src/core/file.h index 7969ae5f..3baa1ccd 100644 --- a/src/core/file.h +++ b/src/core/file.h @@ -84,4 +84,10 @@ extern bool nni_file_is_file(const char *); // false if an error occurs, or the path references something else. extern bool nni_file_is_dir(const char *); +typedef struct nni_file_lockh nni_file_lockh; + +extern int nni_file_lock(const char *, nni_file_lockh **); + +extern void nni_file_unlock(nni_file_lockh *); + #endif // CORE_FILE_H diff --git a/src/core/platform.h b/src/core/platform.h index 3f336f11..d5fc40f7 100644 --- a/src/core/platform.h +++ b/src/core/platform.h @@ -434,6 +434,18 @@ typedef int (*nni_plat_file_walker)(const char *, void *); // with the path name, and the supplied void * argument. extern int nni_plat_file_walk(const char *, nni_plat_file_walker, void *, int); +typedef struct nni_plat_flock nni_plat_flock; + +// nni_plat_file_lock locks the file. This usually means open it (creating +// if it does not exist) and doing a lock operation. The nni_plat_flock +// is our handle for the lock, to unlock. Usually its just a file descriptor, +// and we can unlock by doing close(). Note that this is a "try-lock" +// operation -- if the file is already locked then NNG_EBUSY is returned. +extern int nni_plat_file_lock(const char *path, nni_plat_flock *); + +// nni_plat_file_unlock unlocks the previously locked file. +extern void nni_plat_file_unlock(nni_plat_flock *); + // nni_plat_dir_open attempts to "open a directory" for listing. The // handle for further operations is returned in the first argument, and // the directory name is supplied in the second. |
