[PATCH 1/5] ubi-utils: introduction of libubi_common

Alexander Schmidt alexs at linux.vnet.ibm.com
Fri Jul 27 11:28:48 EDT 2007


libubi_common provides helper functions for ubi utils.

Signed-off-by: Alexander Schmidt <alexs at linux.vnet.ibm.com>
---
ubi-utils/src/libubi_common.c |   93 ++++++++++++++++++++++++++++++++++++++++++
ubi-utils/src/libubi_common.h |   78 +++++++++++++++++++++++++++++++++++
2 files changed, 171 insertions(+)

--- /dev/null
+++ mtd-utils/ubi-utils/src/libubi_common.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright International Business Machines Corp., 2007
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+ * the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Author: Alexander Schmidt
+ *
+ * This library provides helper functions for accessing UBI volumes.
+ * The paths to UBI device nodes is hardcoded, see below.
+ *
+ * 1.0 Initial commit
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define DEFAULT_DEV_PATTERN    "/dev/ubi%d"
+#define DEFAULT_VOL_PATTERN    "/dev/ubi%d_%d"
+
+#define MAXPATH          1024
+
+int ubi_vol_open(int devn, int vol_id, int flags)
+{
+	char path[MAXPATH];
+	int fd;
+
+	snprintf(path, MAXPATH, DEFAULT_VOL_PATTERN, devn, vol_id);
+
+	fd = open(path, flags);
+
+	return fd;
+}
+
+int ubi_vol_close(int vol_fd)
+{
+	return close(vol_fd);
+}
+
+FILE* ubi_vol_fopen_read(int devn, int vol_id)
+{
+	FILE *fp;
+	int fd;
+
+	fd = ubi_vol_open(devn, vol_id, O_RDONLY);
+	if (fd == -1)
+		return NULL;
+
+	fp = fdopen(fd, "r");
+	if (fp == NULL)
+		ubi_vol_close(fd);
+
+	return fp;
+}
+
+int ubi_get_cdev_path(int devn, char *buf, size_t buf_size)
+{
+	int rc;
+
+	if (buf == NULL)
+		return -EINVAL;
+
+	rc = snprintf(buf, buf_size, DEFAULT_DEV_PATTERN, devn);
+
+	return rc;
+}
+
+
+int ubi_vol_get_used_bytes(int vol_fd, unsigned long long *bytes)
+{
+	off_t res;
+
+	res = lseek(vol_fd, 0, SEEK_END);
+	if (res == (off_t)-1)
+		return -1;
+	*bytes = (unsigned long long) res;
+	res = lseek(vol_fd, 0, SEEK_SET);
+	return res == (off_t)-1 ? -1 : 0;
+}
--- /dev/null
+++ mtd-utils/ubi-utils/src/libubi_common.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright International Business Machines Corp., 2007
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+ * the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Author: Alexander Schmidt
+ *
+ * This library provides helper functions for accessing UBI volumes.
+ * The paths to UBI device nodes is hardcoded.
+ *
+ * 1.0 Initial commit
+ */
+
+
+/**
+ * ubi_vol_open - open a UBI volume
+ *
+ * @devn	Number of UBI device on which to open the volume
+ * @vol_id	Number of UBI device on which to open the volume
+ * @flags	Flags to pass to open()
+ *
+ * This function opens a UBI volume on a given UBI device.  It returns
+ * the file descriptor of the opened volume device.  In case of an
+ * error %-1 is returned and errno is set appropriately.
+ */
+int ubi_vol_open(int devn, int vol_id, int flags);
+
+/**
+ * ubi_vol_close - close a UBI volume
+ *
+ * @vol_fd	file descriptor of UBI volume to close
+ *
+ * This function closes the given UBI device.
+ */
+int ubi_vol_close(int vol_fd);
+
+/**
+ * ubi_vol_fopen_read - open a volume for reading, returning a FILE *
+ * @devn	UBI device number
+ * @vol_id	volume ID to read
+ *
+ * Opens a volume for reading.  Reading itself can then be performed
+ * with fread().  The stream can be closed with fclose().  Returns a
+ * stream on success, else NULL.
+ */
+FILE *
+ubi_vol_fopen_read(int devn, uint32_t vol_id);
+
+/**
+ * ubi_get_cdev_path - get a device path for a given device number
+ *
+ * @devn	UBI device number
+ * @buf		buffer for device path
+ * @buf_size	size of buffer
+ */
+int ubi_get_cdev_path(int devn, char *buf, size_t buf_size);
+
+/**
+ * ubi_vol_get_used_bytes - determine used bytes in a UBI volume
+ * @vol_fd	File descriptor of UBI volume
+ * @bytes	Pointer to result
+ *
+ * Returns 0 on success, else error.
+ */
+int ubi_vol_get_used_bytes(int vol_fd, unsigned long long *bytes);
+



More information about the linux-mtd mailing list