From a85c80f25325c69579f3b6c6df847d631e68d0b5 Mon Sep 17 00:00:00 2001 From: José Bollo Date: Thu, 26 Jan 2017 19:05:28 +0100 Subject: Add utils-file and provide json-c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is an intermediate commit providing new features for common utils. Change-Id: Ide92167d8e4e5013ddadd718c02071ea837938e3 Signed-off-by: José Bollo --- src/CMakeLists.txt | 7 +-- src/utils-file.c | 129 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/utils-file.h | 24 ++++++++++ 3 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 src/utils-file.c create mode 100644 src/utils-file.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6b2b280..f126346 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -41,7 +41,7 @@ set(CMAKE_C_FLAGS_CCOV "-g -O2 --coverage") include(FindPkgConfig) -pkg_check_modules(EXTRAS REQUIRED libxml-2.0 openssl xmlsec1 xmlsec1-openssl) +pkg_check_modules(EXTRAS REQUIRED libxml-2.0 openssl xmlsec1 xmlsec1-openssl json-c) add_compile_options(${EXTRAS_CFLAGS}) include_directories(${EXTRAS_INCLUDE_DIRS}) link_libraries(${EXTRAS_LIBRARIES}) @@ -95,6 +95,8 @@ add_library(wgtpkg STATIC add_library(utils STATIC utils-dir.c + utils-file.c + utils-json.c verbose.c ) @@ -131,7 +133,7 @@ install(TARGETS wgtpkg-sign wgtpkg-pack wgtpkg-info wgtpkg-installer DESTINATION ########################################################################### # the targeted -pkg_check_modules(EXTRA2 json-c libsystemd) +pkg_check_modules(EXTRA2 libsystemd) if(EXTRA2_FOUND) add_compile_options(${EXTRA2_CFLAGS}) include_directories(${EXTRA2_INCLUDE_DIRS}) @@ -139,7 +141,6 @@ if(EXTRA2_FOUND) add_library(utils2 STATIC utils-jbus.c - utils-json.c ) add_library(afm STATIC diff --git a/src/utils-file.c b/src/utils-file.c new file mode 100644 index 0000000..09eaaed --- /dev/null +++ b/src/utils-file.c @@ -0,0 +1,129 @@ +/* + Copyright 2017 IoT.bzh + + author: José Bollo + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include + +#include "utils-file.h" + +/* alias for getfile_at(AT_FDCWD, file, content, size) */ +int getfile(const char *file, char **content, size_t *size) +{ + return getfile_at(AT_FDCWD, file, content, size); +} + +/* + * Reads the 'file' relative to 'dfd' (see openat) in a freshly + * allocated memory and returns it in 'content' and 'size' (if not NULL). + * To help in reading text files, a pending null is added at the + * end of the content. + * Return 0 in case of success or else -1 and set 'errno'. + */ +int getfile_at(int dfd, const char *file, char **content, size_t *size) +{ + int rc, f; + struct stat s; + char *r; + ssize_t rsz; + size_t sz, i; + + if (content) + *content = NULL; + rc = openat(dfd, file, O_RDONLY); + if (rc >= 0) { + f = rc; + rc = fstat(f, &s); + if (rc != 0) { + /* nothing */; + } else if (!S_ISREG(s.st_mode)) { + rc = -1; + errno = EBADF; + } else { + sz = (size_t)s.st_size; + if (content) { + r = malloc(sz + 1); + if (!r) { + errno = ENOMEM; + rc = -1; + } else { + i = 0; + while (!rc && i < sz) { + rsz = read(f, r + i, sz - i); + if (rsz == 0) + sz = i; + else if (rsz > 0) + i += (size_t)rsz; + else if (errno != EINTR && errno != EAGAIN) { + free(r); + rc = -1; + } + } + if (rc == 0) { + r[sz] = 0; + *content = r; + } + } + } + if (size) + *size = sz; + } + close(f); + } + return rc; +} + +/* alias for putfile_at(AT_FDCWD, file, content, size) */ +int putfile(const char *file, const void *content, size_t size) +{ + return putfile_at(AT_FDCWD, file, content, size); +} + +int putfile_at(int dfd, const char *file, const void *content, size_t size) +{ + int rc, f; + ssize_t wsz; + size_t i; + + rc = openat(dfd, file, O_WRONLY|O_CREAT|O_TRUNC, 0666); + if (rc >= 0) { + if (size == (size_t)(ssize_t)-1) + size = strlen(content); + f = rc; + if (rc < 0) + i = 0; + rc = 0; + while (!rc && i < size) { + wsz = write(f, content + i, size - i); + if (wsz >= 0) + i += (size_t)wsz; + else if (errno != EINTR && errno != EAGAIN) { + unlinkat(dfd, file, 0); + rc = -1; + } + } + close(f); + } + return rc; +} + diff --git a/src/utils-file.h b/src/utils-file.h new file mode 100644 index 0000000..a29b09a --- /dev/null +++ b/src/utils-file.h @@ -0,0 +1,24 @@ +/* + Copyright 2017 IoT.bzh + + author: José Bollo + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +extern int getfile_at(int dfd, const char *file, char **content, size_t *size); +extern int getfile(const char *file, char **content, size_t *size); + +extern int putfile_at(int dfd, const char *file, const void *content, size_t size); +extern int putfile(const char *file, const void *content, size_t size); + -- cgit 1.2.3-korg