TR181-XPON  1.4.0
TR-181 PON manager.
file_utils.h File Reference
#include <stdbool.h>
#include <stddef.h>

Go to the source code of this file.

Functions

bool write_file (const char *const path, const char *const value)
 
bool read_first_line_from_file (const char *const path, char *line, size_t len)
 

Detailed Description

Helper functions to read and write files.

Definition in file file_utils.h.

Function Documentation

◆ read_first_line_from_file()

bool read_first_line_from_file ( const char *const  path,
char *  line,
size_t  len 
)

Read the first line from a file into a buffer passed by caller.

Parameters
[in]pathfile path
[in,out]linefunction reads 1st line from path and puts it in this parameter
[in]lenlength of line
Returns
true on success, else false

Definition at line 212 of file file_utils.c.

212  {
213  FILE* f = fopen(path, "r");
214  if(!f) {
215  SAH_TRACEZ_ERROR(ME, "Failed to open %s: %s", path, strerror(errno));
216  return false;
217  }
218  bool rv = true;
219  if(!fgets(line, (int) len, f)) {
220  SAH_TRACEZ_ERROR(ME, "Failed to read from %s", path);
221  rv = false;
222  }
223  if(fclose(f) == EOF) {
224  SAH_TRACEZ_ERROR(ME, "Failed to close %s: %s", path, strerror(errno));
225  }
226  return rv;
227 }
#define ME
Definition: xpon_trace.h:78
Here is the caller graph for this function:

◆ write_file()

bool write_file ( const char *const  path,
const char *const  value 
)

Write the string value to the file path.

Parameters
[in]pathfile path
[in]valuevalue to write to path

If path already exists and has value as contents, the function immediately returns true to avoid an unneeded write.

The function first writes the value to the file path with ".tmp" appended. Then it renames the file to path.

Returns
true on success, else false

Avoid writing the file if the file already exists and already has the value of the parameter 'value' as contents.

Definition at line 154 of file file_utils.c.

154  {
155 
156  SAH_TRACEZ_DEBUG2(ME, "path='%s' value='%s'", path, value);
157 
158  const size_t value_len = strlen(value);
159  if(value_len >= LINE_LEN) {
160  SAH_TRACEZ_ERROR(ME, "strlen(value) = %zd >= %d", value_len, LINE_LEN);
161  return false;
162  }
167  if(access(path, F_OK) == 0) {
168  SAH_TRACEZ_DEBUG2(ME, "%s already exists", path);
169  bool has_value = false;
170  if((check_file_has_value(path, value, &has_value) == true) && has_value) {
171  return true;
172  }
173  }
174 
175  char tmpfile[256];
176  if(snprintf(tmpfile, 256, "%s.tmp", path) < 0) {
177  SAH_TRACEZ_ERROR(ME, "%s: failed to create path with .tmp", path);
178  return false;
179  }
180 
181  FILE* f = fopen(tmpfile, "w");
182  if(!f) {
183  SAH_TRACEZ_ERROR(ME, "Failed to open %s: %s", tmpfile, strerror(errno));
184  return false;
185  }
186 
187  bool rv = true;
188  if(fputs(value, f) == EOF) {
189  SAH_TRACEZ_ERROR(ME, "Failed to write to %s", tmpfile);
190  rv = false;
191  }
192 
193  if(fclose(f) == EOF) {
194  SAH_TRACEZ_ERROR(ME, "Failed to close %s: %s", tmpfile, strerror(errno));
195  }
196  if(!rv) {
197  return false;
198  }
199  return rename_file(tmpfile, path);
200 }
static bool check_file_has_value(const char *const path, const char *const value, bool *has_value)
Definition: file_utils.c:114
#define LINE_LEN
Definition: file_utils.c:84
static bool rename_file(const char *const oldpath, const char *const newpath)
Definition: file_utils.c:86
#define SAH_TRACEZ_DEBUG2(zone, format,...)
Definition: xpon_trace.h:127
Here is the call graph for this function:
Here is the caller graph for this function: