From 1c7a56a214519cf71c6132e4670ae6216b44ca90 Mon Sep 17 00:00:00 2001 From: obarun <eric@obarun.org> Date: Fri, 8 Apr 2022 17:57:04 +1100 Subject: [PATCH] split state function --- src/lib66/state/deps-lib/deps | 12 +- src/lib66/state/state.c | 179 ---------------------------- src/lib66/state/state_check.c | 38 ++++++ src/lib66/state/state_check_flags.c | 47 ++++++++ src/lib66/state/state_pack.c | 30 +++++ src/lib66/state/state_read.c | 41 +++++++ src/lib66/state/state_rmfile.c | 37 ++++++ src/lib66/state/state_setflag.c | 34 ++++++ src/lib66/state/state_unpack.c | 43 +++++++ src/lib66/state/state_write.c | 41 +++++++ src/lib66/state/template.c | 19 +++ 11 files changed, 338 insertions(+), 183 deletions(-) delete mode 100644 src/lib66/state/state.c create mode 100644 src/lib66/state/state_check.c create mode 100644 src/lib66/state/state_check_flags.c create mode 100644 src/lib66/state/state_pack.c create mode 100644 src/lib66/state/state_read.c create mode 100644 src/lib66/state/state_rmfile.c create mode 100644 src/lib66/state/state_setflag.c create mode 100644 src/lib66/state/state_unpack.c create mode 100644 src/lib66/state/state_write.c create mode 100644 src/lib66/state/template.c diff --git a/src/lib66/state/deps-lib/deps b/src/lib66/state/deps-lib/deps index ced15d0d..d897d6f5 100644 --- a/src/lib66/state/deps-lib/deps +++ b/src/lib66/state/deps-lib/deps @@ -1,7 +1,11 @@ -state.o --ls6rc --ls6 +state_check.o +state_check_flags.o +state_pack.o +state_read.o +state_rmfile.o +state_setflag.o +state_unpack.o +state_write.o -loblibs --lexecline -lskarnet diff --git a/src/lib66/state/state.c b/src/lib66/state/state.c deleted file mode 100644 index 2d89384d..00000000 --- a/src/lib66/state/state.c +++ /dev/null @@ -1,179 +0,0 @@ -/* - * state.c - * - * Copyright (c) 2018-2021 Eric Vidal <eric@obarun.org> - * - * All rights reserved. - * - * This file is part of Obarun. It is subject to the license terms in - * the LICENSE file found in the top-level directory of this - * distribution. - * This file may not be copied, modified, propagated, or distributed - * except according to the terms contained in the LICENSE file./ - */ - -#include <string.h> -#include <sys/stat.h> -#include <stdlib.h>//realpath -#include <stdio.h> - -#include <oblibs/types.h> -#include <oblibs/log.h> - -#include <skalibs/posixplz.h> -#include <skalibs/uint32.h> -#include <skalibs/uint64.h> -#include <skalibs/djbunix.h> - -#include <66/state.h> - -void state_rmfile(char const *src,char const *name) -{ - log_flow() ; - - size_t srclen = strlen(src) ; - size_t namelen = strlen(name) ; - - char tmp[srclen + 1 + namelen + 1] ; - memcpy(tmp,src,srclen) ; - tmp[srclen] = '/' ; - memcpy(tmp + srclen + 1, name, namelen) ; - tmp[srclen + 1 + namelen] = 0 ; - - unlink_void(tmp) ; -} - -void state_pack(char *pack, ss_state_t *sta) -{ - log_flow() ; - - uint32_pack_big(pack,sta->reload) ; - uint32_pack_big(pack+4,sta->init) ; - uint32_pack_big(pack+8,sta->unsupervise) ; - uint32_pack_big(pack+12,sta->state) ; - uint64_pack_big(pack+16,sta->pid) ; -} - -void state_unpack(char *pack,ss_state_t *sta) -{ - log_flow() ; - - uint32_t reload ; - uint32_t init ; - uint32_t unsupervise ; - uint32_t state ; - uint64_t pid ; - - uint32_unpack_big(pack,&reload) ; - sta->reload = reload ; - uint32_unpack_big(pack+4,&init) ; - sta->init = init ; - uint32_unpack_big(pack+8,&unsupervise) ; - sta->unsupervise = unsupervise ; - uint32_unpack_big(pack+12,&state) ; - sta->state = state ; - uint64_unpack_big(pack+16,&pid) ; - sta->pid = pid ; -} - -int state_write(ss_state_t *sta, char const *dst, char const *name) -{ - log_flow() ; - - char pack[SS_STATE_SIZE] ; - size_t dstlen = strlen(dst) ; - size_t namelen = strlen(name) ; - - char tmp[dstlen + 1 + namelen + 1] ; - memcpy(tmp,dst,dstlen) ; - tmp[dstlen] = '/' ; - memcpy(tmp + dstlen + 1, name, namelen) ; - tmp[dstlen + 1 + namelen] = 0 ; - - state_pack(pack,sta) ; - if (!openwritenclose_unsafe(tmp,pack,SS_STATE_SIZE)) return 0 ; - - return 1 ; -} - -int state_read(ss_state_t *sta, char const *src, char const *name) -{ - log_flow() ; - - char pack[SS_STATE_SIZE] ; - size_t srclen = strlen(src) ; - size_t namelen = strlen(name) ; - - char tmp[srclen + 1 + namelen + 1] ; - memcpy(tmp,src,srclen) ; - tmp[srclen] = '/' ; - memcpy(tmp + srclen + 1, name, namelen) ; - tmp[srclen + 1 + namelen] = 0 ; - - if (openreadnclose(tmp, pack, SS_STATE_SIZE) < SS_STATE_SIZE) return 0 ; - state_unpack(pack, sta) ; - - return 1 ; -} - -int state_check(char const *src, char const *name) -{ - log_flow() ; - - int r ; - size_t srclen = strlen(src) ; - size_t namelen = strlen(name) ; - char tmp[srclen + 1 + namelen + 1] ; - memcpy(tmp,src,srclen) ; - tmp[srclen] = '/' ; - memcpy(tmp + srclen + 1, name, namelen) ; - tmp[srclen + 1 + namelen] = 0 ; - r = scan_mode(tmp,S_IFREG) ; - if (r <= 0) return 0 ; - return 1 ; -} - -void state_setflag(ss_state_t *sta,int flags,int flags_val) -{ - log_flow() ; - - switch (flags) - { - case SS_FLAGS_RELOAD: sta->reload = flags_val ; break ; - case SS_FLAGS_INIT: sta->init = flags_val ; break ; - case SS_FLAGS_UNSUPERVISE: sta->unsupervise = flags_val ; break ; - case SS_FLAGS_STATE: sta->state = flags_val ; break ; - case SS_FLAGS_PID: sta->pid = (uint32_t)flags_val ; break ; - default: return ; - } -} - -int state_check_flags(char const *src, char const *name,int flags) -{ - log_flow() ; - - /** unitialized at all, all flags == 0. - * Return -1 to make a distinction between - * file absent and flag == 0. */ - if (!state_check(src,name)) - return -1 ; - - ss_state_t sta = STATE_ZERO ; - - if (!state_read(&sta,src,name)) - // should not happen - return -1 ; - - switch (flags) - { - case SS_FLAGS_RELOAD: return sta.reload ; - case SS_FLAGS_INIT: return sta.init ; - case SS_FLAGS_UNSUPERVISE: return sta.unsupervise ; - case SS_FLAGS_STATE: return sta.state ; - case SS_FLAGS_PID: return sta.pid ; - default: - // should never happen - return -1 ; - } - -} diff --git a/src/lib66/state/state_check.c b/src/lib66/state/state_check.c new file mode 100644 index 00000000..8f2cd210 --- /dev/null +++ b/src/lib66/state/state_check.c @@ -0,0 +1,38 @@ +/* + * state_check.c + * + * Copyright (c) 2018-2021 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#include <string.h> +#include <sys/stat.h> + +#include <oblibs/log.h> +#include <oblibs/types.h> + +#include <66/state.h> + +int state_check(char const *src, char const *name) +{ + log_flow() ; + + int r ; + size_t srclen = strlen(src) ; + size_t namelen = strlen(name) ; + char tmp[srclen + 1 + namelen + 1] ; + memcpy(tmp,src,srclen) ; + tmp[srclen] = '/' ; + memcpy(tmp + srclen + 1, name, namelen) ; + tmp[srclen + 1 + namelen] = 0 ; + r = scan_mode(tmp,S_IFREG) ; + if (r <= 0) return 0 ; + return 1 ; +} diff --git a/src/lib66/state/state_check_flags.c b/src/lib66/state/state_check_flags.c new file mode 100644 index 00000000..1daf6391 --- /dev/null +++ b/src/lib66/state/state_check_flags.c @@ -0,0 +1,47 @@ +/* + * state_check_flags.c + * + * Copyright (c) 2018-2021 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#include <oblibs/log.h> + +#include <66/state.h> + +int state_check_flags(char const *src, char const *name,int flags) +{ + log_flow() ; + + /** unitialized at all, all flags == 0. + * Return -1 to make a distinction between + * file absent and flag == 0. */ + if (!state_check(src,name)) + return -1 ; + + ss_state_t sta = STATE_ZERO ; + + if (!state_read(&sta,src,name)) + // should not happen + return -1 ; + + switch (flags) + { + case SS_FLAGS_RELOAD: return sta.reload ; + case SS_FLAGS_INIT: return sta.init ; + case SS_FLAGS_UNSUPERVISE: return sta.unsupervise ; + case SS_FLAGS_STATE: return sta.state ; + case SS_FLAGS_PID: return sta.pid ; + default: + // should never happen + return -1 ; + } + +} diff --git a/src/lib66/state/state_pack.c b/src/lib66/state/state_pack.c new file mode 100644 index 00000000..6479adfc --- /dev/null +++ b/src/lib66/state/state_pack.c @@ -0,0 +1,30 @@ +/* + * state_pack.c + * + * Copyright (c) 2018-2021 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#include <oblibs/log.h> + +#include <skalibs/uint32.h> + +#include <66/state.h> + +void state_pack(char *pack, ss_state_t *sta) +{ + log_flow() ; + + uint32_pack_big(pack,sta->reload) ; + uint32_pack_big(pack+4,sta->init) ; + uint32_pack_big(pack+8,sta->unsupervise) ; + uint32_pack_big(pack+12,sta->state) ; + uint64_pack_big(pack+16,sta->pid) ; +} diff --git a/src/lib66/state/state_read.c b/src/lib66/state/state_read.c new file mode 100644 index 00000000..b2bb622a --- /dev/null +++ b/src/lib66/state/state_read.c @@ -0,0 +1,41 @@ +/* + * state_read.c + * + * Copyright (c) 2018-2021 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#include <string.h> + +#include <oblibs/log.h> + +#include <skalibs/djbunix.h> + +#include <66/state.h> + +int state_read(ss_state_t *sta, char const *src, char const *name) +{ + log_flow() ; + + char pack[SS_STATE_SIZE] ; + size_t srclen = strlen(src) ; + size_t namelen = strlen(name) ; + + char tmp[srclen + 1 + namelen + 1] ; + memcpy(tmp,src,srclen) ; + tmp[srclen] = '/' ; + memcpy(tmp + srclen + 1, name, namelen) ; + tmp[srclen + 1 + namelen] = 0 ; + + if (openreadnclose(tmp, pack, SS_STATE_SIZE) < SS_STATE_SIZE) return 0 ; + state_unpack(pack, sta) ; + + return 1 ; +} diff --git a/src/lib66/state/state_rmfile.c b/src/lib66/state/state_rmfile.c new file mode 100644 index 00000000..84f860b9 --- /dev/null +++ b/src/lib66/state/state_rmfile.c @@ -0,0 +1,37 @@ +/* + * state_rmfile.c + * + * Copyright (c) 2018-2021 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#include <string.h> + +#include <oblibs/log.h> + +#include <skalibs/posixplz.h> + +#include <66/state.h> + +void state_rmfile(char const *src,char const *name) +{ + log_flow() ; + + size_t srclen = strlen(src) ; + size_t namelen = strlen(name) ; + + char tmp[srclen + 1 + namelen + 1] ; + memcpy(tmp,src,srclen) ; + tmp[srclen] = '/' ; + memcpy(tmp + srclen + 1, name, namelen) ; + tmp[srclen + 1 + namelen] = 0 ; + + unlink_void(tmp) ; +} diff --git a/src/lib66/state/state_setflag.c b/src/lib66/state/state_setflag.c new file mode 100644 index 00000000..946668ce --- /dev/null +++ b/src/lib66/state/state_setflag.c @@ -0,0 +1,34 @@ +/* + * state_setflag.c + * + * Copyright (c) 2018-2021 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#include <stdint.h> + +#include <oblibs/log.h> + +#include <66/state.h> + +void state_setflag(ss_state_t *sta,int flags,int flags_val) +{ + log_flow() ; + + switch (flags) + { + case SS_FLAGS_RELOAD: sta->reload = flags_val ; break ; + case SS_FLAGS_INIT: sta->init = flags_val ; break ; + case SS_FLAGS_UNSUPERVISE: sta->unsupervise = flags_val ; break ; + case SS_FLAGS_STATE: sta->state = flags_val ; break ; + case SS_FLAGS_PID: sta->pid = (uint32_t)flags_val ; break ; + default: return ; + } +} diff --git a/src/lib66/state/state_unpack.c b/src/lib66/state/state_unpack.c new file mode 100644 index 00000000..3d385f8c --- /dev/null +++ b/src/lib66/state/state_unpack.c @@ -0,0 +1,43 @@ +/* + * state_unpack.c + * + * Copyright (c) 2018-2021 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#include <stdint.h> + +#include <oblibs/log.h> + +#include <skalibs/uint32.h> + +#include <66/state.h> + +void state_unpack(char *pack,ss_state_t *sta) +{ + log_flow() ; + + uint32_t reload ; + uint32_t init ; + uint32_t unsupervise ; + uint32_t state ; + uint64_t pid ; + + uint32_unpack_big(pack,&reload) ; + sta->reload = reload ; + uint32_unpack_big(pack+4,&init) ; + sta->init = init ; + uint32_unpack_big(pack+8,&unsupervise) ; + sta->unsupervise = unsupervise ; + uint32_unpack_big(pack+12,&state) ; + sta->state = state ; + uint64_unpack_big(pack+16,&pid) ; + sta->pid = pid ; +} diff --git a/src/lib66/state/state_write.c b/src/lib66/state/state_write.c new file mode 100644 index 00000000..c5a5de61 --- /dev/null +++ b/src/lib66/state/state_write.c @@ -0,0 +1,41 @@ +/* + * state_write.c + * + * Copyright (c) 2018-2021 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#include <string.h> + +#include <oblibs/log.h> + +#include <skalibs/djbunix.h> + +#include <66/state.h> + +int state_write(ss_state_t *sta, char const *dst, char const *name) +{ + log_flow() ; + + char pack[SS_STATE_SIZE] ; + size_t dstlen = strlen(dst) ; + size_t namelen = strlen(name) ; + + char tmp[dstlen + 1 + namelen + 1] ; + memcpy(tmp,dst,dstlen) ; + tmp[dstlen] = '/' ; + memcpy(tmp + dstlen + 1, name, namelen) ; + tmp[dstlen + 1 + namelen] = 0 ; + + state_pack(pack,sta) ; + if (!openwritenclose_unsafe(tmp,pack,SS_STATE_SIZE)) return 0 ; + + return 1 ; +} diff --git a/src/lib66/state/template.c b/src/lib66/state/template.c new file mode 100644 index 00000000..b1130bed --- /dev/null +++ b/src/lib66/state/template.c @@ -0,0 +1,19 @@ +/* + * service.c + * + * Copyright (c) 2018-2021 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + + + + + +#include <66/state.h> -- GitLab