int fallocate_main(int argc UNUSED_PARAM, char **argv) { const char *str_l; const char *str_o = "0"; off_t ofs, len; unsigned opts; int fd; /* exactly one non-option arg */ opt_complementary = "=1"; opts = getopt32(argv, "l:o:", &str_l, &str_o); if (!(opts & 1)) bb_show_usage(); ofs = xatoull_sfx(str_o, kmg_i_suffixes); len = xatoull_sfx(str_l, kmg_i_suffixes); argv += optind; fd = xopen3(*argv, O_RDWR | O_CREAT, 0666); /* posix_fallocate has unusual method of returning error */ /* maybe use Linux-specific fallocate(int fd, int mode, off_t offset, off_t len) instead? */ if ((errno = posix_fallocate(fd, ofs, len)) != 0) bb_perror_msg_and_die("fallocate '%s'", *argv); /* util-linux also performs fsync(fd); */ return EXIT_SUCCESS; }
int blkdiscard_main(int argc UNUSED_PARAM, char **argv) { unsigned opts; const char *offset_str = "0"; const char *length_str; uint64_t offset; /* Leaving these two variables out does not */ uint64_t length; /* shrink code size and hampers readability. */ uint64_t range[2]; // struct stat st; int fd; enum { OPT_OFFSET = (1 << 0), OPT_LENGTH = (1 << 1), OPT_SECURE = (1 << 2), }; opt_complementary = "=1"; opts = getopt32(argv, "o:l:s", &offset_str, &length_str); argv += optind; fd = xopen(argv[0], O_RDWR|O_EXCL); //Why bother, BLK[SEC]DISCARD will fail on non-blockdevs anyway? // xfstat(fd, &st); // if (!S_ISBLK(st.st_mode)) // bb_error_msg_and_die("%s: not a block device", argv[0]); offset = xatoull_sfx(offset_str, kMG_suffixes); if (opts & OPT_LENGTH) length = xatoull_sfx(length_str, kMG_suffixes); else { xioctl(fd, BLKGETSIZE64, &length); length -= offset; } range[0] = offset; range[1] = length; ioctl_or_perror_and_die(fd, (opts & OPT_SECURE) ? BLKSECDISCARD : BLKDISCARD, &range, "%s: %s failed", argv[0], (opts & OPT_SECURE) ? "BLKSECDISCARD" : "BLKDISCARD" ); if (ENABLE_FEATURE_CLEAN_UP) close(fd); return EXIT_SUCCESS; }
int split_main(int argc UNUSED_PARAM, char **argv) { unsigned suffix_len = 2; char *pfx; char *count_p; const char *sfx; off_t cnt = 1000; off_t remaining = 0; unsigned opt; ssize_t bytes_read, to_write; char *src; opt_complementary = "?2:a+"; /* max 2 args; -a N */ opt = getopt32(argv, "l:b:a:", &count_p, &count_p, &suffix_len); if (opt & SPLIT_OPT_l) cnt = XATOOFF(count_p); if (opt & SPLIT_OPT_b) // FIXME: also needs XATOOFF cnt = xatoull_sfx(count_p, IF_FEATURE_SPLIT_FANCY(split_suffixes) IF_NOT_FEATURE_SPLIT_FANCY(km_suffixes) ); sfx = "x"; argv += optind; if (argv[0]) { int fd; if (argv[1]) sfx = argv[1]; fd = xopen_stdin(argv[0]); xmove_fd(fd, STDIN_FILENO); } else { argv[0] = (char *) bb_msg_standard_input; } if (NAME_MAX < strlen(sfx) + suffix_len) bb_error_msg_and_die("suffix too long"); { char *char_p = xzalloc(suffix_len + 1); memset(char_p, 'a', suffix_len); pfx = xasprintf("%s%s", sfx, char_p); if (ENABLE_FEATURE_CLEAN_UP) free(char_p); } while (1) { bytes_read = safe_read(STDIN_FILENO, read_buffer, READ_BUFFER_SIZE); if (!bytes_read) break; if (bytes_read < 0) bb_simple_perror_msg_and_die(argv[0]); src = read_buffer; do { if (!remaining) { if (!pfx) bb_error_msg_and_die("suffixes exhausted"); xmove_fd(xopen(pfx, O_WRONLY | O_CREAT | O_TRUNC), 1); pfx = next_file(pfx, suffix_len); remaining = cnt; } if (opt & SPLIT_OPT_b) { /* split by bytes */ to_write = (bytes_read < remaining) ? bytes_read : remaining; remaining -= to_write; } else { /* split by lines */ /* can be sped up by using _memrchr_ * and writing many lines at once... */ char *end = memchr(src, '\n', bytes_read); if (end) { --remaining; to_write = end - src + 1; } else { to_write = bytes_read; } } xwrite(STDOUT_FILENO, src, to_write); bytes_read -= to_write; src += to_write; } while (bytes_read); } return EXIT_SUCCESS; }
int ubi_tools_main(int argc UNUSED_PARAM, char **argv) { static const struct suffix_mult size_suffixes[] = { { "KiB", 1024 }, { "MiB", 1024*1024 }, { "GiB", 1024*1024*1024 }, { "", 0 } }; unsigned opts; char *ubi_ctrl; int fd; int mtd_num; int dev_num = UBI_DEV_NUM_AUTO; int vol_id = UBI_VOL_NUM_AUTO; char *vol_name; unsigned long long size_bytes = size_bytes; /* for compiler */ char *size_bytes_str; int alignment = 1; char *type; union { struct ubi_attach_req attach_req; struct ubi_mkvol_req mkvol_req; struct ubi_rsvol_req rsvol_req; } req_structs; #define attach_req req_structs.attach_req #define mkvol_req req_structs.mkvol_req #define rsvol_req req_structs.rsvol_req char path[sizeof("/sys/class/ubi/ubi%d_%d/usable_eb_size") + 2 * sizeof(int)*3 + /*just in case:*/ 16]; #define path_sys_class_ubi_ubi (path + sizeof("/sys/class/ubi/ubi")-1) strcpy(path, "/sys/class/ubi/ubi"); memset(&req_structs, 0, sizeof(req_structs)); if (do_mkvol) { opt_complementary = "-1:d+:n+:a+"; opts = getopt32(argv, "md:n:N:s:a:t:", &dev_num, &vol_id, &vol_name, &size_bytes_str, &alignment, &type ); } else { opt_complementary = "-1:m+:d+:n+:a+"; opts = getopt32(argv, "m:d:n:N:s:a:t:", &mtd_num, &dev_num, &vol_id, &vol_name, &size_bytes_str, &alignment, &type ); } #define OPTION_m (1 << 0) #define OPTION_d (1 << 1) #define OPTION_n (1 << 2) #define OPTION_N (1 << 3) #define OPTION_s (1 << 4) #define OPTION_a (1 << 5) #define OPTION_t (1 << 6) if (opts & OPTION_s) size_bytes = xatoull_sfx(size_bytes_str, size_suffixes); argv += optind; ubi_ctrl = *argv++; fd = xopen(ubi_ctrl, O_RDWR); //xfstat(fd, &st, ubi_ctrl); //if (!S_ISCHR(st.st_mode)) // bb_error_msg_and_die("%s: not a char device", ubi_ctrl); //usage:#define ubiattach_trivial_usage //usage: "-m MTD_NUM [-d UBI_NUM] UBI_CTRL_DEV" //usage:#define ubiattach_full_usage "\n\n" //usage: "Attach MTD device to UBI\n" //usage: "\n -m MTD_NUM MTD device number to attach" //usage: "\n -d UBI_NUM UBI device number to assign" if (do_attach) { if (!(opts & OPTION_m)) bb_error_msg_and_die("%s device not specified", "MTD"); attach_req.mtd_num = mtd_num; attach_req.ubi_num = dev_num; xioctl(fd, UBI_IOCATT, &attach_req); } else //usage:#define ubidetach_trivial_usage //usage: "-d UBI_NUM UBI_CTRL_DEV" //usage:#define ubidetach_full_usage "\n\n" //usage: "Detach MTD device from UBI\n" //usage: "\n -d UBI_NUM UBI device number" if (do_detach) { if (!(opts & OPTION_d)) bb_error_msg_and_die("%s device not specified", "UBI"); /* FIXME? kernel expects int32_t* here: */ xioctl(fd, UBI_IOCDET, &dev_num); } else //usage:#define ubimkvol_trivial_usage //usage: "UBI_DEVICE -N NAME [-s SIZE | -m]" //usage:#define ubimkvol_full_usage "\n\n" //usage: "Create UBI volume\n" //usage: "\n -a ALIGNMENT Volume alignment (default 1)" //usage: "\n -m Set volume size to maximum available" //usage: "\n -n VOLID Volume ID. If not specified," //usage: "\n assigned automatically" //usage: "\n -N NAME Volume name" //usage: "\n -s SIZE Size in bytes" //usage: "\n -t TYPE Volume type (static|dynamic)" if (do_mkvol) { if (opts & OPTION_m) { unsigned leb_avail; unsigned leb_size; unsigned num; char *p; if (sscanf(ubi_ctrl, "/dev/ubi%u", &num) != 1) bb_error_msg_and_die("wrong format of UBI device name"); p = path_sys_class_ubi_ubi + sprintf(path_sys_class_ubi_ubi, "%u/", num); strcpy(p, "avail_eraseblocks"); leb_avail = get_num_from_file(path, UINT_MAX, "Can't get available eraseblocks from '%s'"); strcpy(p, "eraseblock_size"); leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get eraseblock size from '%s'"); size_bytes = leb_avail * (unsigned long long)leb_size; //if (size_bytes <= 0) // bb_error_msg_and_die("%s invalid maximum size calculated", "UBI"); } else if (!(opts & OPTION_s)) bb_error_msg_and_die("size not specified"); if (!(opts & OPTION_N)) bb_error_msg_and_die("name not specified"); mkvol_req.vol_id = vol_id; mkvol_req.vol_type = UBI_DYNAMIC_VOLUME; if ((opts & OPTION_t) && type[0] == 's') mkvol_req.vol_type = UBI_STATIC_VOLUME; mkvol_req.alignment = alignment; mkvol_req.bytes = size_bytes; /* signed int64_t */ strncpy(mkvol_req.name, vol_name, UBI_MAX_VOLUME_NAME); mkvol_req.name_len = strlen(vol_name); if (mkvol_req.name_len > UBI_MAX_VOLUME_NAME) bb_error_msg_and_die("volume name too long: '%s'", vol_name); xioctl(fd, UBI_IOCMKVOL, &mkvol_req); } else //usage:#define ubirmvol_trivial_usage //usage: "UBI_DEVICE -n VOLID" //usage:#define ubirmvol_full_usage "\n\n" //usage: "Remove UBI volume\n" //usage: "\n -n VOLID Volume ID" if (do_rmvol) { if (!(opts & OPTION_n)) bb_error_msg_and_die("volume id not specified"); /* FIXME? kernel expects int32_t* here: */ xioctl(fd, UBI_IOCRMVOL, &vol_id); } else //usage:#define ubirsvol_trivial_usage //usage: "UBI_DEVICE -n VOLID -s SIZE" //usage:#define ubirsvol_full_usage "\n\n" //usage: "Resize UBI volume\n" //usage: "\n -n VOLID Volume ID" //usage: "\n -s SIZE Size in bytes" if (do_rsvol) { if (!(opts & OPTION_s)) bb_error_msg_and_die("size not specified"); if (!(opts & OPTION_n)) bb_error_msg_and_die("volume id not specified"); rsvol_req.bytes = size_bytes; /* signed int64_t */ rsvol_req.vol_id = vol_id; xioctl(fd, UBI_IOCRSVOL, &rsvol_req); } else //usage:#define ubiupdatevol_trivial_usage //usage: "UBI_DEVICE [-t | [-s SIZE] IMG_FILE]" //usage:#define ubiupdatevol_full_usage "\n\n" //usage: "Update UBI volume\n" //usage: "\n -t Truncate to zero size" //usage: "\n -s SIZE Size in bytes to resize to" if (do_update) { int64_t bytes64; if (opts & OPTION_t) { /* truncate the volume by starting an update for size 0 */ bytes64 = 0; /* this ioctl expects int64_t* parameter */ xioctl(fd, UBI_IOCVOLUP, &bytes64); } else { struct stat st; unsigned ubinum, volnum; unsigned leb_size; ssize_t len; char *input_data; /* Assume that device is in normal format. */ /* Removes need for scanning sysfs tree as full libubi does. */ if (sscanf(ubi_ctrl, "/dev/ubi%u_%u", &ubinum, &volnum) != 2) bb_error_msg_and_die("wrong format of UBI device name"); sprintf(path_sys_class_ubi_ubi, "%u_%u/usable_eb_size", ubinum, volnum); leb_size = get_num_from_file(path, MAX_SANE_ERASEBLOCK, "Can't get usable eraseblock size from '%s'"); if (!(opts & OPTION_s)) { if (!*argv) bb_show_usage(); xstat(*argv, &st); size_bytes = st.st_size; xmove_fd(xopen(*argv, O_RDONLY), STDIN_FILENO); } bytes64 = size_bytes; /* this ioctl expects signed int64_t* parameter */ xioctl(fd, UBI_IOCVOLUP, &bytes64); input_data = xmalloc(leb_size); while ((len = full_read(STDIN_FILENO, input_data, leb_size)) > 0) { xwrite(fd, input_data, len); } if (len < 0) bb_perror_msg_and_die("UBI volume update failed"); } } if (ENABLE_FEATURE_CLEAN_UP) close(fd); return EXIT_SUCCESS; }