From: Marc Kleine-Budde <mkl@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 6/9] state: backend_raw: factor out state_backend_raw_file_get_size() into separate function
Date: Tue, 26 May 2015 13:37:50 +0200 [thread overview]
Message-ID: <1432640273-3895-7-git-send-email-mkl@pengutronix.de> (raw)
In-Reply-To: <1432640273-3895-1-git-send-email-mkl@pengutronix.de>
This patch factors out the state_backend_raw_file_get_size() into a separate
function and adds some ifdefs to make this code work under Linux aswell.
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
common/state.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 64 insertions(+), 9 deletions(-)
diff --git a/common/state.c b/common/state.c
index 7e5a31ae00a5..749c8e906851 100644
--- a/common/state.c
+++ b/common/state.c
@@ -1216,6 +1216,60 @@ out_free:
return ret;
}
+#ifdef __BAREBOX__
+#define STAT_GIVES_SIZE(s) (S_ISREG(s.st_mode) || S_ISCHR(s.st_mode))
+#define BLKGET_GIVES_SIZE(s) 0
+#ifndef BLKGETSIZE64
+#define BLKGETSIZE64 -1
+#endif
+#else
+#define STAT_GIVES_SIZE(s) (S_ISREG(s.st_mode))
+#define BLKGET_GIVES_SIZE(s) (S_ISBLK(s.st_mode))
+#endif
+
+static int state_backend_raw_file_get_size(const char *path, size_t *out_size)
+{
+ struct mtd_info_user meminfo;
+ struct stat s;
+ int ret;
+
+ ret = stat(path, &s);
+ if (ret)
+ return -errno;
+
+ /*
+ * under Linux, stat() gives the size only on regular files
+ * under barebox, it works on char dev, too
+ */
+ if (STAT_GIVES_SIZE(s)) {
+ *out_size = s.st_size;
+ return 0;
+ }
+
+ /* this works under Linux on block devs */
+ if (BLKGET_GIVES_SIZE(s)) {
+ int fd;
+
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ return -errno;
+
+ ret = ioctl(fd, BLKGETSIZE64, out_size);
+ close(fd);
+ if (!ret)
+ return 0;
+ }
+
+ /* try mtd next */
+ ret = mtd_get_meminfo(path, &meminfo);
+ if (!ret) {
+ *out_size = meminfo.size;
+ return 0;
+ }
+
+ return ret;
+}
+
/*
* state_backend_raw_file - create a raw file backend store for a state instance
*
@@ -1239,20 +1293,21 @@ int state_backend_raw_file(struct state *state, const char *of_path,
struct state_backend_raw *backend_raw;
struct state_backend *backend;
struct state_variable *sv;
- int ret;
- struct stat s;
struct mtd_info_user meminfo;
+ size_t path_size = 0;
+ int ret;
if (state->backend)
return -EBUSY;
- ret = stat(path, &s);
- if (!ret && S_ISCHR(s.st_mode)) {
- if (size == 0)
- size = s.st_size;
- else if (offset + size > s.st_size)
- return -EINVAL;
- }
+ ret = state_backend_raw_file_get_size(path, &path_size);
+ if (ret)
+ return ret;
+
+ if (size == 0)
+ size = path_size;
+ else if (offset + size > path_size)
+ return -EINVAL;
backend_raw = xzalloc(sizeof(*backend_raw));
backend = &backend_raw->backend;
--
2.1.4
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2015-05-26 11:38 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-26 11:37 [PATCH 0/9] state: updates and cleanups to make code better sharable with linux Marc Kleine-Budde
2015-05-26 11:37 ` [PATCH 1/9] state: mtd_get_meminfo: open path readonly instead of readwrite Marc Kleine-Budde
2015-05-26 11:37 ` [PATCH 2/9] state: mtd_get_meminfo: make usage of mtd's meminfo compatible to linux Marc Kleine-Budde
2015-05-26 11:37 ` [PATCH 3/9] state: backend_raw: remove hard coded limit of two copies Marc Kleine-Budde
2015-05-26 11:37 ` [PATCH 4/9] state: backend_raw: rename struct state_backend_raw::step -> stride Marc Kleine-Budde
2015-05-26 11:37 ` [PATCH 5/9] state: backend_raw: properly align write and erase size Marc Kleine-Budde
2015-05-26 11:37 ` Marc Kleine-Budde [this message]
2015-05-26 11:37 ` [PATCH 7/9] state: backend_raw: rename backend_raw_write_one() -> backend_raw_save_one() Marc Kleine-Budde
2015-05-26 11:37 ` [PATCH 8/9] state: backend_raw: add sanity check of data_len during load Marc Kleine-Budde
2015-05-26 11:37 ` [PATCH 9/9] state: backend_raw: update documentation of struct state_backend_raw members Marc Kleine-Budde
2015-05-26 12:33 ` [PATCH 0/9] state: updates and cleanups to make code better sharable with linux Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1432640273-3895-7-git-send-email-mkl@pengutronix.de \
--to=mkl@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox