123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include "libubi.h"
- #define PROGRAM_NAME "rmvol"
- #include "common.h"
- #include "helpers.h"
- #define SYSFS_FILE "/sys/class/ubi/ubi%d_%d/usable_eb_size"
- int main(int argc, char * const argv[])
- {
- int ret, fd;
- char fname[sizeof(SYSFS_FILE) + 20];
- const char *node;
- libubi_t libubi;
- struct ubi_dev_info dev_info;
- struct ubi_mkvol_request req;
- char tmp[100];
- if (initial_check(argc, argv))
- return 1;
- node = argv[1];
- libubi = libubi_open();
- if (libubi == NULL) {
- failed("libubi_open");
- return 1;
- }
- if (ubi_get_dev_info(libubi, node, &dev_info)) {
- failed("ubi_get_dev_info");
- goto out_libubi;
- }
-
- req.vol_id = UBI_VOL_NUM_AUTO;
- req.alignment = dev_info.min_io_size;
- req.bytes = dev_info.leb_size;
- req.vol_type = UBI_DYNAMIC_VOLUME;
- req.name = "rmvol";
- req.flags = 0;
- if (ubi_mkvol(libubi, node, &req)) {
- failed("ubi_mkvol");
- goto out_libubi;
- }
-
- sprintf(fname, SYSFS_FILE, dev_info.dev_num, req.vol_id);
- fd = open(fname, O_RDONLY);
- if (fd == -1) {
- errorm("cannot open %s", fname);
- failed("open");
- goto out_rmvol;
- }
-
- if (ubi_rmvol(libubi, node, req.vol_id)) {
- failed("ubi_rmvol");
- perror("ubi_rmvol");
- goto out_close;
- }
-
- ret = read(fd, tmp, 100);
- if (ret != -1) {
- errorm("read returned %d, expected -1", ret);
- failed("read");
- goto out_close;
- }
-
- close(fd);
- fd = open(fname, O_RDONLY);
- if (fd != -1) {
- errorm("opened %s again, open returned %d, expected -1",
- fname, fd);
- failed("open");
- goto out_libubi;
- }
- libubi_close(libubi);
- return 0;
- out_rmvol:
- ubi_rmvol(libubi, node, req.vol_id);
- out_libubi:
- libubi_close(libubi);
- return 1;
- out_close:
- close(fd);
- libubi_close(libubi);
- return 1;
- }
|