mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] fs/parseopt: Add parseopt_llu_suffix
@ 2018-02-01 10:37 Philipp Zabel
  2018-02-01 10:37 ` [PATCH 2/3] filetype: Add type detection at an offset Philipp Zabel
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Philipp Zabel @ 2018-02-01 10:37 UTC (permalink / raw)
  To: barebox

Add an option parser using strtoull_suffix that can be used to
parse a loop mount offset option.

Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
---
 include/parseopt.h |  2 ++
 lib/parseopt.c     | 30 ++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/include/parseopt.h b/include/parseopt.h
index 1f9763f8c9..273a371ac3 100644
--- a/include/parseopt.h
+++ b/include/parseopt.h
@@ -1,5 +1,7 @@
 #ifndef __PARSEOPT_H__
 #define __PARSEOPT_H__
+void parseopt_llu_suffix(const char *options, const char *opt,
+			 unsigned long long *val);
 
 void parseopt_b(const char *options, const char *opt, bool *val);
 void parseopt_hu(const char *options, const char *opt, unsigned short *val);
diff --git a/lib/parseopt.c b/lib/parseopt.c
index 8211733e3b..70983066d9 100644
--- a/lib/parseopt.c
+++ b/lib/parseopt.c
@@ -122,3 +122,33 @@ again:
 
 	*val = xstrndup(parsed, endp - parsed);
 }
+
+void parseopt_llu_suffix(const char *options, const char *opt,
+			 unsigned long long *val)
+{
+	const char *start;
+	size_t optlen = strlen(opt);
+	unsigned long long v;
+	char *endp;
+
+again:
+	start = strstr(options, opt);
+
+	if (!start)
+		return;
+
+	if (start > options && start[-1] != ',') {
+		options = start;
+		goto again;
+	}
+
+	if (start[optlen] != '=') {
+		options = start;
+		goto again;
+	}
+
+	v = strtoull_suffix(start + optlen + 1, &endp, 0);
+
+	if (*endp == ',' || *endp == '\0')
+		*val = v;
+}
-- 
2.15.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] filetype: Add type detection at an offset
  2018-02-01 10:37 [PATCH 1/3] fs/parseopt: Add parseopt_llu_suffix Philipp Zabel
@ 2018-02-01 10:37 ` Philipp Zabel
  2018-02-01 10:37 ` [PATCH 3/3] fs: add support loop mount offset Philipp Zabel
  2018-02-05  7:26 ` [PATCH 1/3] fs/parseopt: Add parseopt_llu_suffix Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Philipp Zabel @ 2018-02-01 10:37 UTC (permalink / raw)
  To: barebox

Add file_name_detect_type_offset to allow file system detection for loop
mounts at an offset.

Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
---
 common/filetype.c  | 10 ++++++++--
 include/filetype.h |  1 +
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/common/filetype.c b/common/filetype.c
index f9c034ff2a..71691fd813 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -21,6 +21,7 @@
 #include <asm/unaligned.h>
 #include <fcntl.h>
 #include <fs.h>
+#include <libfile.h>
 #include <malloc.h>
 #include <errno.h>
 #include <envfs.h>
@@ -343,13 +344,13 @@ enum filetype file_detect_type(const void *_buf, size_t bufsize)
 	return filetype_unknown;
 }
 
-enum filetype file_name_detect_type(const char *filename)
+enum filetype file_name_detect_type_offset(const char *filename, loff_t pos)
 {
 	int fd, ret;
 	void *buf;
 	enum filetype type = filetype_unknown;
 
-	fd = open(filename, O_RDONLY);
+	fd = open_and_lseek(filename, O_RDONLY, pos);
 	if (fd < 0)
 		return fd;
 
@@ -368,6 +369,11 @@ err_out:
 	return type;
 }
 
+enum filetype file_name_detect_type(const char *filename)
+{
+	return file_name_detect_type_offset(filename, 0);
+}
+
 enum filetype cdev_detect_type(const char *name)
 {
 	enum filetype type = filetype_unknown;
diff --git a/include/filetype.h b/include/filetype.h
index b98dcb5014..ec5aea2635 100644
--- a/include/filetype.h
+++ b/include/filetype.h
@@ -50,6 +50,7 @@ const char *file_type_to_short_string(enum filetype f);
 enum filetype file_detect_partition_table(const void *_buf, size_t bufsize);
 enum filetype file_detect_type(const void *_buf, size_t bufsize);
 enum filetype file_name_detect_type(const char *filename);
+enum filetype file_name_detect_type_offset(const char *filename, loff_t pos);
 enum filetype cdev_detect_type(const char *name);
 enum filetype is_fat_or_mbr(const unsigned char *sector, unsigned long *bootsec);
 int is_fat_boot_sector(const void *_buf);
-- 
2.15.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] fs: add support loop mount offset
  2018-02-01 10:37 [PATCH 1/3] fs/parseopt: Add parseopt_llu_suffix Philipp Zabel
  2018-02-01 10:37 ` [PATCH 2/3] filetype: Add type detection at an offset Philipp Zabel
@ 2018-02-01 10:37 ` Philipp Zabel
  2018-02-05  7:26 ` [PATCH 1/3] fs/parseopt: Add parseopt_llu_suffix Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Philipp Zabel @ 2018-02-01 10:37 UTC (permalink / raw)
  To: barebox

When loop mounting, allow to specify an offset into the file, similarly
to the losetup offset option. Multiplicative suffixes are supported.

Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
---
 fs/devfs-core.c  |  8 ++++----
 fs/fs.c          | 12 +++++++++---
 include/driver.h |  2 +-
 3 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index be56edd18d..ea5887c720 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -464,7 +464,7 @@ static const struct file_operations loop_ops = {
 	.lseek = dev_lseek_default,
 };
 
-struct cdev *cdev_create_loop(const char *path, ulong flags)
+struct cdev *cdev_create_loop(const char *path, ulong flags, loff_t offset)
 {
 	struct cdev *new;
 	struct loop_priv *priv;
@@ -486,15 +486,15 @@ struct cdev *cdev_create_loop(const char *path, ulong flags)
 	new->priv = priv;
 
 	ofs = lseek(priv->fd, 0, SEEK_END);
-	if (ofs < 0) {
+	if (ofs < 0 || ofs <= offset) {
 		free(new);
 		free(priv);
 		return NULL;
 	}
-	lseek(priv->fd, 0, SEEK_SET);
+	lseek(priv->fd, offset, SEEK_SET);
 
 	new->size = ofs;
-	new->offset = 0;
+	new->offset = offset;
 	new->dev = NULL;
 	new->flags = 0;
 
diff --git a/fs/fs.c b/fs/fs.c
index 6f15e93ba9..56e67aac2a 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -1279,11 +1279,13 @@ static const char *detect_fs(const char *filename, const char *fsoptions)
 	enum filetype type;
 	struct driver_d *drv;
 	struct fs_driver_d *fdrv;
-	bool loop;
+	bool loop = false;
+	unsigned long long offset = 0;
 
 	parseopt_b(fsoptions, "loop", &loop);
+	parseopt_llu_suffix(fsoptions, "offset", &offset);
 	if (loop)
-		type = file_name_detect_type(filename);
+		type = file_name_detect_type_offset(filename, offset);
 	else
 		type = cdev_detect_type(filename);
 
@@ -1302,9 +1304,13 @@ static const char *detect_fs(const char *filename, const char *fsoptions)
 
 int fsdev_open_cdev(struct fs_device_d *fsdev)
 {
+	unsigned long long offset = 0;
+
 	parseopt_b(fsdev->options, "loop", &fsdev->loop);
+	parseopt_llu_suffix(fsdev->options, "offset", &offset);
 	if (fsdev->loop)
-		fsdev->cdev = cdev_create_loop(fsdev->backingstore, O_RDWR);
+		fsdev->cdev = cdev_create_loop(fsdev->backingstore, O_RDWR,
+					       offset);
 	else
 		fsdev->cdev = cdev_open(fsdev->backingstore, O_RDWR);
 	if (!fsdev->cdev)
diff --git a/include/driver.h b/include/driver.h
index e571fbbec5..f37805db17 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -475,7 +475,7 @@ struct cdev *cdev_readlink(struct cdev *cdev);
 struct cdev *cdev_by_device_node(struct device_node *node);
 struct cdev *cdev_by_partuuid(const char *partuuid);
 struct cdev *cdev_open(const char *name, unsigned long flags);
-struct cdev *cdev_create_loop(const char *path, ulong flags);
+struct cdev *cdev_create_loop(const char *path, ulong flags, loff_t offset);
 void cdev_remove_loop(struct cdev *cdev);
 int cdev_do_open(struct cdev *, unsigned long flags);
 void cdev_close(struct cdev *cdev);
-- 
2.15.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/3] fs/parseopt: Add parseopt_llu_suffix
  2018-02-01 10:37 [PATCH 1/3] fs/parseopt: Add parseopt_llu_suffix Philipp Zabel
  2018-02-01 10:37 ` [PATCH 2/3] filetype: Add type detection at an offset Philipp Zabel
  2018-02-01 10:37 ` [PATCH 3/3] fs: add support loop mount offset Philipp Zabel
@ 2018-02-05  7:26 ` Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2018-02-05  7:26 UTC (permalink / raw)
  To: Philipp Zabel; +Cc: barebox

On Thu, Feb 01, 2018 at 11:37:15AM +0100, Philipp Zabel wrote:
> Add an option parser using strtoull_suffix that can be used to
> parse a loop mount offset option.
> 
> Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
> ---
>  include/parseopt.h |  2 ++
>  lib/parseopt.c     | 30 ++++++++++++++++++++++++++++++
>  2 files changed, 32 insertions(+)

Applied, thanks

Sascha

> 
> diff --git a/include/parseopt.h b/include/parseopt.h
> index 1f9763f8c9..273a371ac3 100644
> --- a/include/parseopt.h
> +++ b/include/parseopt.h
> @@ -1,5 +1,7 @@
>  #ifndef __PARSEOPT_H__
>  #define __PARSEOPT_H__
> +void parseopt_llu_suffix(const char *options, const char *opt,
> +			 unsigned long long *val);
>  
>  void parseopt_b(const char *options, const char *opt, bool *val);
>  void parseopt_hu(const char *options, const char *opt, unsigned short *val);
> diff --git a/lib/parseopt.c b/lib/parseopt.c
> index 8211733e3b..70983066d9 100644
> --- a/lib/parseopt.c
> +++ b/lib/parseopt.c
> @@ -122,3 +122,33 @@ again:
>  
>  	*val = xstrndup(parsed, endp - parsed);
>  }
> +
> +void parseopt_llu_suffix(const char *options, const char *opt,
> +			 unsigned long long *val)
> +{
> +	const char *start;
> +	size_t optlen = strlen(opt);
> +	unsigned long long v;
> +	char *endp;
> +
> +again:
> +	start = strstr(options, opt);
> +
> +	if (!start)
> +		return;
> +
> +	if (start > options && start[-1] != ',') {
> +		options = start;
> +		goto again;
> +	}
> +
> +	if (start[optlen] != '=') {
> +		options = start;
> +		goto again;
> +	}
> +
> +	v = strtoull_suffix(start + optlen + 1, &endp, 0);
> +
> +	if (*endp == ',' || *endp == '\0')
> +		*val = v;
> +}
> -- 
> 2.15.1
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-02-05  7:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-01 10:37 [PATCH 1/3] fs/parseopt: Add parseopt_llu_suffix Philipp Zabel
2018-02-01 10:37 ` [PATCH 2/3] filetype: Add type detection at an offset Philipp Zabel
2018-02-01 10:37 ` [PATCH 3/3] fs: add support loop mount offset Philipp Zabel
2018-02-05  7:26 ` [PATCH 1/3] fs/parseopt: Add parseopt_llu_suffix Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox