Commit c3dc9043 authored by Mark Lindner's avatar Mark Lindner

Added new 'fsync' option.

parent a6797196
2017-11-15 Mark Lindner <markl@avalon>
* lib/wincompat.c, lib/wincompat.h - added fsync() implementation for Windows
* lib/libconfig.c, lib/libconfig.h - added CONFIG_OPTION_FSYNC
* lib/libconfigcpp.c++, lib/libconfig.h++ - added Config::OptionFsync
* doc/libconfig.texi - Documentation updates
----- version 1.7 ------
2017-10-24 Mark Lindner <markl@avalon>
......
dnl Process this file with autoconf to produce a configure script.
AC_INIT(libconfig, 1.7, hyperrealm@gmail.com, libconfig,
AC_INIT(libconfig, 1.7.1, hyperrealm@gmail.com, libconfig,
[https://hyperrealm.github.io/libconfig/])
AC_CONFIG_AUX_DIR([aux-build])
AC_CONFIG_MACRO_DIR([m4])
......
......@@ -6,8 +6,8 @@
@setfilename libconfig.info
@settitle libconfig
@set edition 1.7
@set update-date 31 Oct 2017
@set edition 1.7.1
@set update-date ?? Nov 2017
@set subtitle-text A Library For Processing Structured Configuration Files
@set author-text Mark A.@: Lindner
......@@ -945,6 +945,11 @@ when writing floating point values (corresponding to @code{printf()}
@samp{%g} format) or should never be used (corresponding to @code{printf()}
@samp{%f} format). By default this option is turned off.
@item CONFIG_OPTION_FSYNC
(@b{Since @i{v1.7.1}})
This option controls whether the @code{config_write_file()} function performs
an @i{fsync} operation after writing the configuration and before closing the
file. By default this option is turned off.
@end table
@end deftypefun
......@@ -1580,6 +1585,12 @@ when writing floating point values (corresponding to @code{printf()}
@samp{%g} format) or should never be used (corresponding to @code{printf()}
@samp{%f} format). By default this option is turned off.
@item Config::OptionFsync
(@b{Since @i{v1.7.1}})
This option controls whether the @code{writeFile()} method performs an @i{fsync}
operation after writing the configuration and before closing the file. By
default this option is turned off.
@end table
@end deftypemethod
......
......@@ -38,6 +38,11 @@ int main(int argc, char **argv)
Config cfg;
cfg.setOptions(Config::OptionFsync
| Config::OptionSemicolonSeparators
| Config::OptionColonAssignmentForGroups
| Config::OptionOpenBraceOnSeparateLine);
// Read the file. If there is an error, report it and exit.
try
{
......
......@@ -36,6 +36,11 @@ int main(int argc, char **argv)
config_setting_t *root, *setting, *movie;
config_init(&cfg);
config_set_options(&cfg,
(CONFIG_OPTION_FSYNC
| CONFIG_OPTION_SEMICOLON_SEPARATORS
| CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS
| CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE));
/* Read the file. If there is an error, report it and exit. */
if(! config_read_file(&cfg, "example.cfg"))
......@@ -74,8 +79,6 @@ int main(int argc, char **argv)
setting = config_setting_add(movie, "qty", CONFIG_TYPE_INT);
config_setting_set_float(setting, 20);
config_set_options(&cfg, 0);
/* Write out the updated configuration. */
if(! config_write_file(&cfg, output_file))
{
......
......@@ -18,7 +18,7 @@
#
# For more info see section 6.3 of the GNU Libtool Manual.
VERINFO = -version-info 11:0:0
VERINFO = -version-info 11:1:0
## Flex
PARSER_PREFIX = libconfig_yy
......@@ -28,8 +28,8 @@ AM_LFLAGS = --header-file=scanner.h --prefix=$(PARSER_PREFIX)
AM_YFLAGS = -d -p $(PARSER_PREFIX)
libsrc = libconfig.c scanner.l grammar.y parsectx.h wincompat.h \
scanctx.c scanctx.h strbuf.c strbuf.h strvec.c strvec.h util.c util.h
libsrc = grammar.y libconfig.c parsectx.h scanctx.c scanctx.h scanner.l \
strbuf.c strbuf.h strvec.c strvec.h util.c util.h wincompat.c wincompat.h
libinc = libconfig.h
libsrc_cpp = $(libsrc) libconfigcpp.c++
......
......@@ -198,6 +198,10 @@
RelativePath=".\util.c"
>
</File>
<File
RelativePath=".\wincompat.c"
>
</File>
</Filter>
<Filter
Name="Header Files"
......
......@@ -92,6 +92,7 @@
<ClCompile Include="strbuf.c" />
<ClCompile Include="strvec.c" />
<ClCompile Include="util.c" />
<ClCompile Include="wincompat.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\ac_config.h" />
......@@ -112,4 +113,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
</Project>
......@@ -691,6 +691,23 @@ int config_write_file(config_t *config, const char *filename)
}
config_write(config, stream);
if(config_get_option(config, CONFIG_OPTION_FSYNC))
{
int fd = fileno(stream);
if(fd >= 0)
{
if(fsync(fd) != 0)
{
fclose(stream);
config->error_text = __io_error;
config->error_type = CONFIG_ERR_FILE_IO;
return(CONFIG_FALSE);
}
}
}
fclose(stream);
config->error_type = CONFIG_ERR_NONE;
return(CONFIG_TRUE);
......
......@@ -64,6 +64,7 @@ extern "C" {
#define CONFIG_OPTION_COLON_ASSIGNMENT_FOR_NON_GROUPS 0x08
#define CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE 0x10
#define CONFIG_OPTION_ALLOW_SCIENTIFIC_NOTATION 0x20
#define CONFIG_OPTION_FSYNC 0x40
#define CONFIG_TRUE (1)
#define CONFIG_FALSE (0)
......
......@@ -448,7 +448,8 @@ class LIBCONFIGXX_API Config
OptionColonAssignmentForGroups = 0x04,
OptionColonAssignmentForNonGroups = 0x08,
OptionOpenBraceOnSeparateLine = 0x10,
OptionAllowScientificNotation = 0x20
OptionAllowScientificNotation = 0x20,
OptionFsync = 0x40
};
Config();
......
......@@ -185,6 +185,10 @@
RelativePath=".\util.c"
>
</File>
<File
RelativePath=".\wincompat.c"
>
</File>
</Filter>
<Filter
Name="Header Files"
......
......@@ -81,6 +81,7 @@
<ClCompile Include="strbuf.c" />
<ClCompile Include="strvec.c" />
<ClCompile Include="util.c" />
<ClCompile Include="wincompat.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\ac_config.h" />
......@@ -99,4 +100,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
\ No newline at end of file
</Project>
/* ----------------------------------------------------------------------------
libconfig - A library for processing structured configuration files
Copyright (C) 2005-2018 Mark A Lindner
This file is part of libconfig.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
This library 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 Library General Public
License along with this library; if not, see
<http://www.gnu.org/licenses/>.
----------------------------------------------------------------------------
*/
#include "wincompat.h"
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) \
|| defined(WIN64) || defined(_WIN64) || defined(__WIN64__)
#include <errno.h>
#include <io.h>
int fsync(int fd)
{
HANDLE h = (HANDLE)_get_osfhandle(fd);
if(h == INVALID_HANDLE_VALUE)
{
errno = EBADF;
return(-1);
}
if(!FlushFileBuffers(h))
{
DWORD err = GetLastError();
switch(err)
{
case ERROR_ACCESS_DENIED:
return(0);
case ERROR_INVALID_HANDLE:
errno = EINVAL;
break;
default:
errno = EIO;
}
return(-1);
}
return(0);
}
#endif // WIN32 || WIN64
......@@ -101,6 +101,8 @@
#define IS_RELATIVE_PATH(P) \
(PathIsRelativeA(P))
extern int fsync(int fd);
#else /* defined(WIN32/WIN64) && ! defined(__MINGW32__) */
#define INT64_CONST(I) (I ## LL)
......@@ -109,6 +111,8 @@
#define IS_RELATIVE_PATH(P) \
((P)[0] != '/')
#include <unistd.h> /* for fsync() */
#endif /* defined(WIN32/WIN64) && ! defined(__MINGW32__) */
#endif /* __wincompat_h */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment