Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

settings fs: Adding support for csi_save_start/end handlers #42028

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions subsys/settings/include/settings/settings_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define __SETTINGS_FILE_H_

#include "settings/settings.h"
#include <fs/fs.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -21,6 +22,9 @@ struct settings_file {
const char *cf_name; /* filename */
int cf_maxlines; /* max # of lines before compressing */
int cf_lines; /* private */
bool wr_file_opened;
struct fs_file_t wr_file;
bool syncing; /* sync after writing value */
};

/* register file to be source of settings */
Expand Down
111 changes: 76 additions & 35 deletions subsys/settings/src/settings_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@ LOG_MODULE_DECLARE(settings, CONFIG_SETTINGS_LOG_LEVEL);
int settings_backend_init(void);
void settings_mount_fs_backend(struct settings_file *cf);

static int settings_file_save_start(struct settings_store *cs);
static int settings_file_save_end(struct settings_store *cs);
static int settings_file_load(struct settings_store *cs,
const struct settings_load_arg *arg);
static int settings_file_save(struct settings_store *cs, const char *name,
const char *value, size_t val_len);

static const struct settings_store_itf settings_file_itf = {
.csi_load = settings_file_load,
.csi_save_start = settings_file_save_start,
.csi_save = settings_file_save,
.csi_save_end = settings_file_save_end
};

/*
Expand Down Expand Up @@ -360,52 +364,94 @@ static int settings_file_save_and_compress(struct settings_file *cf,

}

static int settings_file_open(struct settings_store *cs)
{
struct settings_file *cf = (struct settings_file *)cs;
int rc;

if (cf->wr_file_opened) {
return 0;
}

fs_file_t_init(&cf->wr_file);
rc = fs_open(&cf->wr_file, cf->cf_name, FS_O_CREATE | FS_O_APPEND | FS_O_WRITE);
cf->wr_file_opened = true;
return rc;
}

static int settings_file_sync(struct settings_store *cs)
{
struct settings_file *cf = (struct settings_file *)cs;

if (!cf->syncing) {
return 0;
}
return fs_sync(&cf->wr_file);
}

static int settings_file_close(struct settings_store *cs)
{
struct settings_file *cf = (struct settings_file *)cs;

cf->wr_file_opened = false;
return fs_close(&cf->wr_file);
}

static int settings_file_save_start(struct settings_store *cs)
{
struct settings_file *cf = (struct settings_file *)cs;

cf->syncing = false;
return 0;
}

static int settings_file_save_end(struct settings_store *cs)
{
struct settings_file *cf = (struct settings_file *)cs;

cf->syncing = true;
return settings_file_sync(cs);
}

static int settings_file_save_priv(struct settings_store *cs, const char *name,
const char *value, size_t val_len)
{
struct settings_file *cf = (struct settings_file *)cs;
struct line_entry_ctx entry_ctx;
struct fs_file_t file;
int rc2;
int rc;

if (!name) {
return -EINVAL;
}

fs_file_t_init(&file);

if (cf->cf_maxlines && (cf->cf_lines + 1 >= cf->cf_maxlines)) {
/*
* Compress before config file size exceeds
* the max number of lines.
*/
return settings_file_save_and_compress(cf, name, value,
val_len);
rc = settings_file_close(cs);
if (rc) {
return rc;
}
rc = settings_file_save_and_compress(cf, name, value,
val_len);
return rc;
}

rc = settings_file_open(cs);
if (rc) {
return rc;
}
/*
* Open the file to add this one value.
* Open the file to add this one value. File has been open and seek to the end
*/
rc = fs_open(&file, cf->cf_name, FS_O_CREATE | FS_O_RDWR);
entry_ctx.stor_ctx = &cf->wr_file;
rc = settings_line_write(name, value, val_len, 0,
(void *)&entry_ctx);
if (rc == 0) {
rc = fs_seek(&file, 0, FS_SEEK_END);
if (rc == 0) {
entry_ctx.stor_ctx = &file;
rc = settings_line_write(name, value, val_len, 0,
(void *)&entry_ctx);
if (rc == 0) {
cf->cf_lines++;
}
}

rc2 = fs_close(&file);
if (rc == 0) {
rc = rc2;
}
cf->cf_lines++;
}

return rc;
return settings_file_sync(cs);
}


Expand Down Expand Up @@ -484,17 +530,10 @@ static int write_handler(void *ctx, off_t off, char const *buf, size_t len)
struct fs_file_t *file = entry_ctx->stor_ctx;
int rc;

/* append to file only */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this removal?

rc = fs_seek(file, 0, FS_SEEK_END);

if (rc == 0) {
rc = fs_write(file, buf, len);

if (rc > 0) {
rc = 0;
}
rc = fs_write(file, buf, len);
if (rc > 0) {
rc = 0;
}

return rc;
}

Expand All @@ -507,7 +546,9 @@ int settings_backend_init(void)
{
static struct settings_file config_init_settings_file = {
.cf_name = CONFIG_SETTINGS_FS_FILE,
.cf_maxlines = CONFIG_SETTINGS_FS_MAX_LINES
.cf_maxlines = CONFIG_SETTINGS_FS_MAX_LINES,
.wr_file_opened = false,
.syncing = true
};
struct fs_dirent entry;
int rc;
Expand Down