Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sol-util-file.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Soletta (TM) Project
3  *
4  * Copyright (C) 2015 Intel Corporation. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <sol-macros.h>
22 #include <sol-buffer.h>
23 
24 #include <dirent.h>
25 #include <sys/types.h>
26 #include <stdarg.h>
27 #include <stdbool.h>
28 #include <strings.h>
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
52 #define SOL_UTIL_MAX_READ_ATTEMPTS 10
53 
65 };
66 
78 int sol_util_write_file(const char *path, const char *fmt, ...) SOL_ATTR_PRINTF(2, 3);
79 
95 int sol_util_vwrite_file(const char *path, const char *fmt, va_list args) SOL_ATTR_PRINTF(2, 0);
96 
106 ssize_t sol_util_write_file_slice(const char *path, struct sol_str_slice slice);
107 
119 int sol_util_read_file(const char *path, const char *fmt, ...) SOL_ATTR_SCANF(2, 3);
120 
136 int sol_util_vread_file(const char *path, const char *fmt, va_list args) SOL_ATTR_SCANF(2, 0);
137 
151 #ifndef DOXYGEN_RUN
153 #endif
154  ;
155 
168 char *sol_util_load_file_string(const char *filename, size_t *size)
169 #ifndef DOXYGEN_RUN
170  SOL_ATTR_WARN_UNUSED_RESULT
171 #endif
172  ;
173 
186 char *sol_util_load_file_fd_string(int fd, size_t *size)
187 #ifndef DOXYGEN_RUN
188  SOL_ATTR_WARN_UNUSED_RESULT
189 #endif
190  ;
191 
200 int sol_util_load_file_fd_buffer(int fd, struct sol_buffer *buf)
201 #ifndef DOXYGEN_RUN
202  SOL_ATTR_WARN_UNUSED_RESULT
203 #endif
204  ;
205 
214 int sol_util_load_file_buffer(const char *filename, struct sol_buffer *buf)
215 #ifndef DOXYGEN_RUN
216  SOL_ATTR_WARN_UNUSED_RESULT
217 #endif
218  ;
219 
228 int sol_util_get_rootdir(char *out, size_t size)
229 #ifndef DOXYGEN_RUN
230  SOL_ATTR_WARN_UNUSED_RESULT
231 #endif
232  ;
233 
242 int sol_util_fd_set_flag(int fd, int flag)
243 #ifndef DOXYGEN_RUN
244  SOL_ATTR_WARN_UNUSED_RESULT
245 #endif
246  ;
247 
268 ssize_t sol_util_fill_buffer(int fd, struct sol_buffer *buffer, size_t size);
269 
294 static inline int
295 sol_util_fill_buffer_exactly(int fd, struct sol_buffer *buffer, size_t size)
296 {
297  ssize_t ret = sol_util_fill_buffer(fd, buffer, size);
298 
299  if (ret < 0)
300  return (int)ret;
301  else if ((size_t)ret < size)
302  return -EIO;
303 
304  return 0;
305 }
306 
326 int sol_util_iterate_dir(const char *path,
327  enum sol_util_iterate_dir_reason (*iterate_dir_cb)(void *data,
328  const char *dir_path,
329  struct dirent *ent), const void *data);
330 
347 int sol_util_move_file(const char *old_path, const char *new_path, mode_t mode);
348 
365 bool sol_util_busy_wait_file(const char *path, uint64_t nanoseconds);
366 
378 
387 int sol_util_create_recursive_dirs(const struct sol_str_slice path, mode_t mode);
388 
396 int sol_util_get_user_config_dir(struct sol_buffer *buffer);
397 
409 int sol_util_file_encode_filename(struct sol_buffer *buf, const struct sol_str_slice value);
410 
415 #ifdef __cplusplus
416 }
417 #endif
int sol_util_move_file(const char *old_path, const char *new_path, mode_t mode)
Moves file on filesystem.
#define SOL_ATTR_PRINTF(fmt, arg)
Specifies that a function takes printf style arguments which should be type-checked against a format ...
Definition: sol-macros.h:189
int int ssize_t sol_util_write_file_slice(const char *path, struct sol_str_slice slice)
Write the slice content the file pointed by path.
char * sol_util_load_file_fd_string(int fd, size_t *size)
Reads the contents of a file.
int sol_util_get_user_config_dir(struct sol_buffer *buffer)
Get the user context config directory for current app.
bool sol_util_busy_wait_file(const char *path, uint64_t nanoseconds)
Wait for some file to become available.
int sol_util_get_rootdir(char *out, size_t size)
Gets the root directory.
int int struct sol_buffer * sol_util_load_file_fd_raw(int fd)
Reads the contents of a file.
char * sol_util_load_file_string(const char *filename, size_t *size)
Reads the contents of a file.
These are common Soletta macros.
int int sol_util_vwrite_file(const char *path, const char *fmt, va_list args) SOL_ATTR_PRINTF(2
Write the formatted string in the file pointed by path.
static struct sol_buffer value
Definition: server.c:42
int sol_util_read_file(const char *path, const char *fmt,...) SOL_ATTR_SCANF(2
Reads from a file the contents according with the formatted string.
sol_util_iterate_dir_reason
Return values (for non-error paths) expected from sol_util_iterate_dir() callbacks.
Definition: sol-util-file.h:62
String slice type.
Definition: sol-str-slice.h:84
These are routines that Soletta provides for its buffer implementation.
static FILE * fd
Definition: download.c:28
#define SOL_ATTR_WARN_UNUSED_RESULT
Causes a warning to be emitted if a caller of the function with this attribute does not use its retur...
Definition: sol-macros.h:187
int int sol_util_vread_file(const char *path, const char *fmt, va_list args) SOL_ATTR_SCANF(2
Reads from a file the contents according with the formatted string.
int sol_util_load_file_buffer(const char *filename, struct sol_buffer *buf)
Reads the contents of a file and append to a buffer.
static int sol_util_fill_buffer_exactly(int fd, struct sol_buffer *buffer, size_t size)
Fills buffer with data read from file fd with an exact amount of bytes.
Definition: sol-util-file.h:295
The directory iteration should continue.
Definition: sol-util-file.h:64
int sol_util_file_encode_filename(struct sol_buffer *buf, const struct sol_str_slice value)
Encode string to be used as a file name.
int sol_util_iterate_dir(const char *path, enum sol_util_iterate_dir_reason(*iterate_dir_cb)(void *data, const char *dir_path, struct dirent *ent), const void *data)
Iterate over a directory.
int sol_util_load_file_fd_buffer(int fd, struct sol_buffer *buf)
Reads the contents of a file and append to a buffer.
int sol_util_write_file(const char *path, const char *fmt,...) SOL_ATTR_PRINTF(2
Write the formatted string in the file pointed by path.
ssize_t sol_util_fill_buffer(int fd, struct sol_buffer *buffer, size_t size)
Fills buffer with data read from file fd.
int sol_util_create_recursive_dirs(const struct sol_str_slice path, mode_t mode)
Create directories recursively.
struct sol_str_slice sol_util_file_get_basename(struct sol_str_slice path)
Get the basename of a path.
A sol_buffer is a dynamic array, that can be resized if needed.
Definition: sol-buffer.h:130
The directory iteration should stop.
Definition: sol-util-file.h:63
int sol_util_fd_set_flag(int fd, int flag)
Set a flag into a file descriptor.
#define SOL_ATTR_SCANF(fmt, arg)
Specifies that a function takes scanf style arguments which should be type-checked against a format s...
Definition: sol-macros.h:190