From 01dbecb2e93aa782685c87021a1bc6912b810c4d Mon Sep 17 00:00:00 2001 From: Calvin Morrison Date: Thu, 10 Apr 2014 11:16:07 -0400 Subject: c version of lock --- Makefile | 16 ++++++++++++++++ lock | 28 ---------------------------- lock.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lock.sh | 28 ++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 28 deletions(-) create mode 100644 Makefile delete mode 100755 lock create mode 100644 lock.c create mode 100755 lock.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0c3dbd8 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +VERSION=\"0.0.5\" +CC = gcc +CFLAGS = -Os -mtune=native -Wall -Wextra -pedantic +DESTDIR = /usr/local/ + +all: lock + +lock: + $(CC) $(CFLAGS) lock.c -o lock + + +install: all + @install lock $(DESTDIR)/bin/ + +clean: + rm lock diff --git a/lock b/lock deleted file mode 100755 index 22191fe..0000000 --- a/lock +++ /dev/null @@ -1,28 +0,0 @@ -lock() { - - if [[ -f "$1" ]]; then - echo "$1 is not a lock directory." - exit 1 - fi - - if mkdir "$1" &>/dev/null; then - echo "lock $1 created" >&2 - else - echo "lock $1 found, waiting for unlock" >&2 - while true; do - sleep 2; - if [[ ! -d "$1" ]]; then - return - fi - done - fi; - -} - -if [[ "$#" -eq "1" ]]; then - lock "$1" -else - echo "please supply one argument: the lock directory." - exit 1 -fi - diff --git a/lock.c b/lock.c new file mode 100644 index 0000000..12d560b --- /dev/null +++ b/lock.c @@ -0,0 +1,54 @@ +/* See LICENSE for licence details. */ + +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv){ + size_t pathlen, argvlen; + char *path; + + if(argc != 2){ + fprintf(stderr, "Please supply one argument: the lock directory.\n"); + return EXIT_FAILURE; + } + + if(access(argv[1], F_OK)){ + /* create lock */ + if(mkdir(argv[1], S_IRUSR | S_IXUSR)){ + fprintf(stderr, "Failed to create lock %s.\n", argv[1]); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; + } + + /* prepare path */ + argvlen = strlen(argv[1]); + pathlen = argvlen + sizeof("/..") + 1; + path = malloc(pathlen); + if(path == NULL) { + fprintf(stderr, "Error: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + + memcpy(path, argv[1], argvlen); + memcpy(path + argvlen, "/..\0", sizeof("/..") + 1); + + if(access(path, F_OK)){ + /* invalid lock */ + fprintf(stderr, "%s is not a lock directory.\n", argv[1]); + free(path); + return EXIT_FAILURE; + } + + /* lock exists */ + for(; !access(path, F_OK) ;){ + sleep(1); + } + + free(path); + return EXIT_SUCCESS; +} diff --git a/lock.sh b/lock.sh new file mode 100755 index 0000000..22191fe --- /dev/null +++ b/lock.sh @@ -0,0 +1,28 @@ +lock() { + + if [[ -f "$1" ]]; then + echo "$1 is not a lock directory." + exit 1 + fi + + if mkdir "$1" &>/dev/null; then + echo "lock $1 created" >&2 + else + echo "lock $1 found, waiting for unlock" >&2 + while true; do + sleep 2; + if [[ ! -d "$1" ]]; then + return + fi + done + fi; + +} + +if [[ "$#" -eq "1" ]]; then + lock "$1" +else + echo "please supply one argument: the lock directory." + exit 1 +fi + -- cgit v1.2.3