From acaa0479d714a6b8469de0582d02148d9af2e43e Mon Sep 17 00:00:00 2001 From: Francois Laupretre Date: Thu, 15 May 2014 17:08:40 +0200 Subject: [PATCH 1/5] Doc: Change function link syntax and add new links --- src/02-Using.sh | 7 ++++--- src/sf_db.sh | 2 +- src/sf_disk.sh | 2 +- src/sf_error.sh | 2 +- src/sf_file.sh | 8 +++++--- src/sf_fs.sh | 20 ++++++++++---------- src/sf_os.sh | 4 ++-- src/sf_save.sh | 2 +- src/sf_tmp.sh | 8 ++++---- 9 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/02-Using.sh b/src/02-Using.sh index 23fe92b..da451f6 100644 --- a/src/02-Using.sh +++ b/src/02-Using.sh @@ -27,7 +27,8 @@ # # assuming that /usr/bin is in your path. # -# Once the library is loaded, every sysfunc commands are available. +# Once the library is loaded, every sysfunc commands are available, prefixed +# with the 'sf_' string. # # # Calling a single function # # @@ -58,10 +59,10 @@ # # # Checking whether the library is loaded # # -# The **sf_loaded** function checks if the library is loaded. When using this +# The [function:loaded] function checks if the library is loaded. When using this # function, you must redirect stderr to /dev/null to avoid displaying an error # when the library is not loaded (when the library is not loaded, the -# __sf_loaded__ function is not present). +# [function:loaded] function won't be found). # # The syntax to use is: # diff --git a/src/sf_db.sh b/src/sf_db.sh index daae6a9..db3e173 100644 --- a/src/sf_db.sh +++ b/src/sf_db.sh @@ -179,7 +179,7 @@ sf_db_unset "$1" } ##---------------------------------------------------------------------------- -# Set a variable with the "sf_now" timestamp value +# Set a variable with the value returned by [function:tm_now] # # Args: # $1: Variable name diff --git a/src/sf_disk.sh b/src/sf_disk.sh index f57f892..3ab3fb8 100644 --- a/src/sf_disk.sh +++ b/src/sf_disk.sh @@ -87,7 +87,7 @@ echo $ndsk # File system can be mounted or not. # # Note: In order to get the size of a mounted filesystem containing a given -# path, use [flink:fs_size]. +# path, use [function:fs_size]. # # Args: # $1: Device diff --git a/src/sf_error.sh b/src/sf_error.sh index 7bdcefd..a13fbfe 100644 --- a/src/sf_error.sh +++ b/src/sf_error.sh @@ -23,7 +23,7 @@ #----------------------------------------------------------------------------- # Cleanup error file # -# Called by sf_finish +# Called by [function:finish] #----------------------------------------------------------------------------- function _sf_error_cleanup diff --git a/src/sf_file.sh b/src/sf_file.sh index 50aebbd..fa23ae4 100644 --- a/src/sf_file.sh +++ b/src/sf_file.sh @@ -84,7 +84,7 @@ return 1 # file (any type except directory or symbolic link to a directory), the # program aborts with a fatal error. If you want to avoid # this (if you want to create the directory, even if something else is -# already existing in this path), call sf_delete first. +# already existing in this path), call [function:delete] first. # If the path given as arg contains a symbolic link pointing to an existing # directory, it is left as-is. # @@ -122,7 +122,9 @@ fi ##---------------------------------------------------------------------------- # Renames a file to '/old. -# +# +# ### This function is deprecated. Please use [function:save] instead. +# # Args: # $1 : Path # Returns: Always 0 @@ -331,7 +333,7 @@ fi } ##---------------------------------------------------------------------------- -# Checks if a file contains a block inserted by sf_check_block +# Checks if a file contains a block inserted by [function:check_block] # # # Args: diff --git a/src/sf_fs.sh b/src/sf_fs.sh index fcfbd67..460bb55 100644 --- a/src/sf_fs.sh +++ b/src/sf_fs.sh @@ -23,7 +23,7 @@ ##---------------------------------------------------------------------------- # Checks if a directory is a file system mount point # -# ### This function is deprecated. Please use [flink:fs_is_mount_point]. +# ### This function is deprecated. Please use [function:fs_is_mount_point]. # # Args: # $1: Directory to check @@ -58,7 +58,7 @@ function sf_fs_is_mount_point ##---------------------------------------------------------------------------- # Gets the mount point of the filesystem containing a given path # -# ### This function is deprecated. Please use [flink:fs_mount_point]. +# ### This function is deprecated. Please use [function:fs_mount_point]. # # Args: # $1: Path (must correspond to an existing element) @@ -101,7 +101,7 @@ esac ##---------------------------------------------------------------------------- # Gets the device of the filesystem containing a given path # -# ### This function is deprecated. Please use [flink:fs_device]. +# ### This function is deprecated. Please use [function:fs_device]. # # Args: # $1: Path (must correspond to an existing element) @@ -144,7 +144,7 @@ sf_disk_normalize_device $disk ##---------------------------------------------------------------------------- # Get the size of the filesystem containing a given path # -# ### This function is deprecated. Please use [flink:fs_size]. +# ### This function is deprecated. Please use [function:fs_size]. # # Args: # $1: Path (must correspond to an existing element) @@ -162,7 +162,7 @@ sf_fs_size $* # # Note: This function is to be used for a mounted filesystem. In order to get # the size of a file system contained in a given device (mounted or not), -# use [flink:disk_fs_size]. +# use [function:disk_fs_size]. # # Args: # $1: Path (must correspond to an existing element) @@ -195,7 +195,7 @@ echo `expr $sz / 1024` ##---------------------------------------------------------------------------- # Extend a file system to a given size # -# ### This function is deprecated. Please use [flink:fs_extend]. +# ### This function is deprecated. Please use [function:fs_extend]. # # Args: # $1: A path contained in the file system to extend @@ -254,7 +254,7 @@ return $rc ##---------------------------------------------------------------------------- # Create a file system, mount it, and register it (mount at boot) # -# ### This function is deprecated. Please use [flink:fs_create]. +# ### This function is deprecated. Please use [function:fs_create]. # # Refuses existing directory as mount point (security) # @@ -370,10 +370,10 @@ echo $type ##---------------------------------------------------------------------------- # Create a logical volume and a filesystem on it # -# ### This function is deprecated. Please use [flink:fs_create_lv_fs] +# ### This function is deprecated. Please use [function:fs_create_lv_fs] # ### Warning: Note that superseding function has argument 4 and 5 swapped (size and FS type). # -# Combines sf_create_lv and sf_create_fs +# Combines [function:create_lv] and [function:fs_create] # # Args: # $1: Mount point (directory) @@ -395,7 +395,7 @@ sf_fs_create_lv_fs "$1" "$2" "$3" "$5" "$4" "$6" ##---------------------------------------------------------------------------- # Create a logical volume and a filesystem on it # -# Combines sf_create_lv and sf_fs_create +# Combines [function:create_lv] and [function:fs_create] # # Args: # $1: Mount point (directory) diff --git a/src/sf_os.sh b/src/sf_os.sh index 1df2fce..245d3f8 100644 --- a/src/sf_os.sh +++ b/src/sf_os.sh @@ -68,9 +68,9 @@ echo $res } ##---------------------------------------------------------------------------- -# Alias of sf_os_id() (obsolete - for compatibility only) +# Alias of [function:os_id] # -# Other info: see sf_os_id() +# ### This function is deprecated. Please use [function:os_id] # # Args: None # Returns: Always 0 diff --git a/src/sf_save.sh b/src/sf_save.sh index 09096f8..408d20f 100644 --- a/src/sf_save.sh +++ b/src/sf_save.sh @@ -179,7 +179,7 @@ return 0 ##---------------------------------------------------------------------------- # Save system cleanup # -# Called by sf_cleanup() +# Called by [function:cleanup] # # Args: None # Returns: Always 0 diff --git a/src/sf_tmp.sh b/src/sf_tmp.sh index 21f60ea..15780d3 100644 --- a/src/sf_tmp.sh +++ b/src/sf_tmp.sh @@ -21,9 +21,9 @@ #============================================================================= ##---------------------------------------------------------------------------- -# Deletes all temporary files +# Deletes every temporary files # -# This function is automatically called by sf_cleanup() +# This function is automatically called by [function:cleanup] # # Args: none # Returns: Always 0 @@ -40,9 +40,9 @@ fi ##---------------------------------------------------------------------------- # Returns an unused temporary path # -# The returned path can then be used to create a directory or a file. +# ### This function is deprecated. Please use [function:tmpfile] or [function:tmpdir] instead # -# ** This function is deprecated. Please use sf_tmpfile or sf_tmpdir instead. +# The returned path can then be used to create a directory or a file. # # Args: none # Returns: Always 0 From 27a9303303bf623dfc118cee352438d7e44ae12b Mon Sep 17 00:00:00 2001 From: Francois Laupretre Date: Tue, 20 May 2014 12:25:45 +0200 Subject: [PATCH 2/5] sf_db: Use new temp file mechanism. --- src/sf_db.sh | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/src/sf_db.sh b/src/sf_db.sh index db3e173..35678c5 100644 --- a/src/sf_db.sh +++ b/src/sf_db.sh @@ -31,7 +31,7 @@ function sf_db_clear { -\rm -rf "$SF_DB_PATH" "$SF_DB_TMP_PATH" +\rm -rf "$SF_DB_PATH" } ##---------------------------------------------------------------------------- @@ -76,21 +76,6 @@ function sf_db_key sf_db_normalize "$1" | sed -e 's,\.,\.,g' } -##---------------------------------------------------------------------------- -# Replace the DB file with the temp DB file -# -# Args: -# $1: Variable name -# Returns: 0 -# Displays: Key string -#---------------------------------------------------------------------------- - -function _sf_db_tmp_replace -{ -\rm -rf "$SF_DB_PATH" -\mv "$SF_DB_TMP_PATH" "$SF_DB_PATH" -} - ##---------------------------------------------------------------------------- # Unset a variable # @@ -104,17 +89,19 @@ function _sf_db_tmp_replace function sf_db_unset { -typeset key name +typeset key name tmp _sf_db_exists || return 0 +tmp=`sf_tmpfile` for name do [ -z "$name" ] && break key=`sf_db_key "$name"` - grep -v "^$key " $SF_DB_PATH >$SF_DB_TMP_PATH - _sf_db_tmp_replace + grep -v "^$key " $SF_DB_PATH >$tmp + cat $tmp >$SF_DB_PATH done +/bin/rm -f $tmp } ##---------------------------------------------------------------------------- @@ -302,37 +289,36 @@ return 0 function sf_db_expand { -typeset name value esc_val _tmp1 _tmp2 +typeset name value esc_val _tmp1 _tmp2 _tmp -_tmp1=/tmp/.sf_db_expand.tmp1.$$ -_tmp2=/tmp/.sf_db_expand.tmp2.$$ +_tmp1=`sf_tmpfile` +_tmp2=`sf_tmpfile` +_tmp=`sf_tmpfile` sf_db_dump | while read name value do esc_val=`echo "$value" | sed -e 's!,!\,!g'` echo "s,{{%$name%}},$esc_val,g" -done >$SF_DB_TMP_PATH +done >$_tmp -\rm -rf $_tmp1 $_tmp2 cat >$_tmp1 while true do - sed -f $SF_DB_TMP_PATH <$_tmp1 >$_tmp2 + sed -f $_tmp <$_tmp1 >$_tmp2 diff $_tmp1 $_tmp2 >/dev/null && break cp $_tmp2 $_tmp1 done sed 's,{{%[^%]*%}},,g' <$_tmp2 # Suppress unresolved patterns -\rm -rf $_tmp1 $_tmp2 $SF_DB_TMP_PATH +\rm -rf $_tmp1 $_tmp2 $_tmp } #============================================================================= [ "X$SF_DB_PATH" = X ] && SF_DB_PATH=/etc/sysfunc.db -[ "X$SF_DB_TMP_PATH" = X ] && SF_DB_TMP_PATH=/etc/sysfunc.db.tmp -export SF_DB_PATH SF_DB_TMP_PATH +export SF_DB_PATH #============================================================================= From 6306cb6195018c9ff3dc56410bf4ab3df8cee15d Mon Sep 17 00:00:00 2001 From: Francois Laupretre Date: Tue, 20 May 2014 19:24:14 +0200 Subject: [PATCH 3/5] sf_db: Readonly functions can now get DB content from stdin This is used by new sf_cfg functions, as default values are spread among several files, but we want to manage them as if they were in a single DB. In order to have functions read from stdin, set $SF_DB_PATH to ''. --- src/sf_db.sh | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/src/sf_db.sh b/src/sf_db.sh index 35678c5..b431c90 100644 --- a/src/sf_db.sh +++ b/src/sf_db.sh @@ -182,6 +182,9 @@ sf_db_set "$1" "`sf_tm_now`" ##---------------------------------------------------------------------------- # Check if a variable is set # +# If global variable SF_DB_PATH contains '', DB content is read from +# standard input. +# # Args: # $1: Variable name # Returns: 0 if variable is set, <> 0 if not @@ -192,10 +195,8 @@ function sf_db_isset { typeset key -_sf_db_exists || return 1 - key=`sf_db_key "$1"` -grep "^$key " $SF_DB_PATH >/dev/null +_sf_get_clean_db | grep "^$key " >/dev/null } ##---------------------------------------------------------------------------- @@ -203,6 +204,9 @@ grep "^$key " $SF_DB_PATH >/dev/null # # If variable is not set, return an empty string (no error) # +# If global variable SF_DB_PATH contains '', DB content is read from +# standard input. +# # Args: # $1: Variable name # Returns: 0 @@ -211,14 +215,42 @@ grep "^$key " $SF_DB_PATH >/dev/null function sf_db_get { -typeset key name +typeset key -name="$1" key=`sf_db_key "$1"` # 'head -1' by security (should never be used) -grep "^$key " $SF_DB_PATH 2>/dev/null | sed 's,^[^ ][^ ]* ,,' | head -1 +_sf_get_clean_db | grep "^$key " | sed 's,^[^ ][^ ]* ,,' | head -1 +} + +##---------------------------------------------------------------------------- +# Provides a database whose comments and empty lines are removed +# +# Exists because sf_db can be used to read from a handcrafted DB file +# (by overriding $SF_DB_PATH) or from standard input. Such content can contain +# comments and empty lines. +# +# Standard input is read when $SF_DB_PATH contains ''. +# +# Args: None +# Returns: 0 +# Displays: Input with comments and empty lines removed +#----------------------------------------------------------------------------- + +function _sf_get_clean_db +{ +typeset dbpath + +if [ "X$SF_DB_PATH" = 'X' ] ; then + dbpath='' +else + dbpath="$SF_DB_PATH" + _sf_db_exists || return +fi + +sed -e 's/ */ /g' -e 's/ */ /g' -e 's/^ *//g' -e 's/^#.*$//g' \ + $dbpath | grep -v '^$' } ##---------------------------------------------------------------------------- @@ -233,8 +265,7 @@ grep "^$key " $SF_DB_PATH 2>/dev/null | sed 's,^[^ ][^ ]* ,,' | head -1 function sf_db_dump { -_sf_db_exists && sort <$SF_DB_PATH -return 0 +_sf_get_clean_db | sort } ##---------------------------------------------------------------------------- From 76da4c1244135c321fc7564920a4d9eb44999dd9 Mon Sep 17 00:00:00 2001 From: Francois Laupretre Date: Tue, 20 May 2014 19:29:06 +0200 Subject: [PATCH 4/5] Add sf_cfg module This module allows to manage configuration parameters. The main difference with plain DB entries is that the list of parameters is known and each parameter has a default value. External software define their parameters by adding a file to a given directory. This file contains the list of parameters and default values. --- src/sf_cfg.sh | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 src/sf_cfg.sh diff --git a/src/sf_cfg.sh b/src/sf_cfg.sh new file mode 100644 index 0000000..6b123d2 --- /dev/null +++ b/src/sf_cfg.sh @@ -0,0 +1,200 @@ +# +# Copyright 2009-2014 - Francois Laupretre +# +#============================================================================= +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License (LGPL) as +# published by the Free Software Foundation, either version 3 of the License, +# or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see . +#============================================================================= + + +#============================================================================= +# Section: Configuration parameters +#============================================================================= + +##---------------------------------------------------------------------------- +# Run an 'sf_db' command on default values +# +# Args: +# $*: An sf_db command along with its arguments +# Returns: 0 +# Displays: nothing +#----------------------------------------------------------------------------- + +function _sf_cfg_def_exec +{ +cat $SF_CFG_DEF_DIR/* 2>/dev/null | SF_DB_PATH='' $* +} + +##---------------------------------------------------------------------------- +# Check that the given parameter is a valid one +# +# Actually, parameter is valid if a default value is registered for this param. +# +# Args: +# $1: Parameter +# Returns: 0 if parameter exists, =!0 if not +# Displays: nothing +#----------------------------------------------------------------------------- + +function sf_cfg_exists +{ +_sf_cfg_def_exec sf_db_isset "$1" +} + +##---------------------------------------------------------------------------- +# Check if a parameter is not set (default value) +# +# Args: +# $1: Parameter +# Returns: 0 if parameter is not set (default value), 1 if set +# Displays: nothing +#----------------------------------------------------------------------------- + +function sf_cfg_is_default +{ +sf_db_isset "$1" && return 1 +return 0 +} + +##---------------------------------------------------------------------------- +# Aborts if the given parameter is not a valid one +# +# Args: +# $1: Parameter +# Returns: Only if parameter is valid +# Displays: If parameter is invalid, aborts with error message +#----------------------------------------------------------------------------- + +function sf_cfg_check +{ +sf_cfg_exists "$1" || sf_fatal "Invalid configuration parameter: $1" +} + +##---------------------------------------------------------------------------- +# List defined configuration parameters +# +# Args: None +# Returns: 0 +# Displays: Defined parameters, one by line +#----------------------------------------------------------------------------- + +function sf_cfg_list +{ +_sf_cfg_def_exec sf_db_list +} + +##---------------------------------------------------------------------------- +# Get parameter default value +# +# Args: +# $1: Parameter +# Returns: Only if parameter is valid +# Displays: Parameter default value +#----------------------------------------------------------------------------- + +function sf_cfg_get_default +{ +_sf_cfg_def_exec sf_db_get "$1" +} + +##---------------------------------------------------------------------------- +# Get parameter value +# +# If parameter is set, returns set value. Else, returns default value. +# +# Args: +# $1: Parameter +# Returns: Only if parameter is valid +# Displays: Parameter value +#----------------------------------------------------------------------------- + +function sf_cfg_get +{ +typeset var +var=$1 +sf_cfg_check "$var" + +if sf_cfg_is_default "$var" ; then + sf_cfg_get_default "$var" +else + sf_db_get "$var" +fi +} + +##---------------------------------------------------------------------------- +# Set an explicit parameter value +# +# To reset a parameter to its default value, see [function:cfg_unset] +# +# Args: +# $1: Parameter +# $2: Value +# Returns: Only if parameter is valid +# Displays: Nothing +#----------------------------------------------------------------------------- + +function sf_cfg_set +{ +sf_cfg_check "$1" +sf_db_set "$1" "$2" +} + +##---------------------------------------------------------------------------- +# Reset a parameter to its default value +# +# Args: +# $1: Parameter +# Returns: Only if parameter is valid +# Displays: Nothing +#----------------------------------------------------------------------------- + +function sf_cfg_unset +{ +sf_cfg_check "$1" +sf_db_unset "$1" +} + +##---------------------------------------------------------------------------- +# Display a list of parameters along with default and actual values +# +# Args: None +# Returns: Only if parameter is valid +# Displays: Nothing +#----------------------------------------------------------------------------- + +function sf_cfg_display +{ +typeset var value enhanced normal prefix + +enhanced="`tput rev`" +normal="`tput sgr0`" + +echo "Name Actual value Default value" +echo "----------------------- -------------------- --------------------" +sf_cfg_list | while read var ; do + value="`sf_cfg_get $var`" + prefix='' + sf_cfg_is_default "$var" || prefix="$enhanced" + printf "%-23s $prefix%-20s$normal %-20s\n" $var "$value" \ + "`sf_cfg_get_default $var`" +done +} + + +#============================================================================= + +[ "X$SF_CFG_DEF_DIR" = X ] && SF_CFG_DEF_DIR=/etc/sysfunc/cfg/defaults + +export SF_CFG_DEF_DIR + +#============================================================================= From c439c47e2b5d61bd982d262c8d7c5129f3330fd6 Mon Sep 17 00:00:00 2001 From: Francois Laupretre Date: Tue, 20 May 2014 19:41:10 +0200 Subject: [PATCH 5/5] Set version to 1.27.0 --- config.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.mk b/config.mk index b449a96..bfbd885 100644 --- a/config.mk +++ b/config.mk @@ -18,6 +18,6 @@ INSTALL_DIR = /opt/sysfunc -SOFTWARE_VERSION = 1.26.1 +SOFTWARE_VERSION = 1.27.0 #============================================================================