@@ -888,18 +888,20 @@ xmldb_clear(clixon_handle h,
888888 return 0 ;
889889}
890890
891- /*! Delete database, clear cache if any. Remove file and dir
891+ /*! Delete database, clear cache if any. Remove or truncate file and dir
892892 *
893893 * @param[in] h Clixon handle
894894 * @param[in] db Database
895+ * @param[in] rm Remove file if non-zero, else truncate file
895896 * @retval 0 OK
896897 * @retval -1 Error
897898 * @note Datastores / dirs are not actually deleted so that a backend after dropping priviliges
898899 * can re-create them
899900 */
900901int
901902xmldb_delete (clixon_handle h ,
902- const char * db )
903+ const char * db ,
904+ int rm )
903905{
904906 int retval = -1 ;
905907 char * filename = NULL ;
@@ -916,12 +918,21 @@ xmldb_delete(clixon_handle h,
916918 goto done ;
917919 if (xmldb_db2file (h , db , & filename ) < 0 )
918920 goto done ;
919- if (lstat (filename , & st ) == 0 )
920- if (truncate (filename , 0 ) < 0 ){
921- clixon_err (OE_DB , errno , "truncate %s" , filename );
922- goto done ;
921+ if (lstat (filename , & st ) == 0 ){
922+ if (!rm ){
923+ if (truncate (filename , 0 ) < 0 ){
924+ clixon_err (OE_DB , errno , "truncate %s" , filename );
925+ goto done ;
926+ }
923927 }
924- if (clicon_option_bool (h , "CLICON_XMLDB_MULTI" )){
928+ else {
929+ if (unlink (filename ) < 0 ){
930+ clixon_err (OE_DB , errno , "unlink %s" , filename );
931+ goto done ;
932+ }
933+ }
934+ }
935+ if (clicon_option_bool (h , "CLICON_XMLDB_MULTI" )){
925936 if (xmldb_db2subdir (h , db , & subdir ) < 0 )
926937 goto done ;
927938 if (stat (subdir , & st ) == 0 ){
@@ -934,9 +945,17 @@ xmldb_delete(clixon_handle h,
934945 for (i = 0 ; i < ndp ; i ++ ){
935946 cbuf_reset (cb );
936947 cprintf (cb , "%s/%s" , subdir , dp [i ].d_name );
937- if (truncate (cbuf_get (cb ), 0 ) < 0 ){
938- clixon_err (OE_DB , errno , "truncate %s" , filename );
939- goto done ;
948+ if (!rm ){
949+ if (truncate (cbuf_get (cb ), 0 ) < 0 ){
950+ clixon_err (OE_DB , errno , "truncate %s" , filename );
951+ goto done ;
952+ }
953+ }
954+ else {
955+ if (unlink (filename ) < 0 ){
956+ clixon_err (OE_DB , errno , "unlink %s" , filename );
957+ goto done ;
958+ }
940959 }
941960 }
942961 }
@@ -1012,7 +1031,7 @@ xmldb_db_reset(clixon_handle h,
10121031 const char * db )
10131032{
10141033 if (xmldb_exists (h , db ) == 1 ){
1015- if (xmldb_delete (h , db ) != 0 && errno != ENOENT )
1034+ if (xmldb_delete (h , db , 0 ) != 0 && errno != ENOENT )
10161035 return -1 ;
10171036 }
10181037 if (xmldb_create (h , db ) < 0 )
0 commit comments