ubirename.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2008 Logitech.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 51
  15. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. /*
  18. * An utility to get rename UBI volumes.
  19. *
  20. * Author: Richard Titmuss
  21. */
  22. #define PROGRAM_NAME "ubirename"
  23. #include <stdio.h>
  24. #include <stdint.h>
  25. #include <getopt.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <libubi.h>
  29. #include "common.h"
  30. static const char usage[] =
  31. "Usage: " PROGRAM_NAME " <UBI device node file name> [<old name> <new name>|...]\n\n"
  32. "Example: " PROGRAM_NAME " /dev/ubi0 A B C D - rename volume A to B, and C to D\n\n"
  33. "This utility allows re-naming several volumes in one go atomically.\n"
  34. "For example, if you have volumes A and B, then you may rename A into B\n"
  35. "and B into A at one go, and the operation will be atomic. This allows\n"
  36. "implementing atomic UBI volumes upgrades. E.g., if you have volume A\n"
  37. "and want to upgrade it atomically, you create a temporary volume B,\n"
  38. "put your new data to B, then rename A to B and B to A, and then you\n"
  39. "may remove old volume B.\n"
  40. "It is also allowed to re-name multiple volumes at a time, but 16 max.\n"
  41. "renames at once, which means you may specify up to 32 volume names.\n"
  42. "If you have volumes A and B, and re-name A to B, but do not re-name\n"
  43. "B to something else in the same request, old volume B will be removed\n"
  44. "and A will be renamed into B.\n";
  45. static int get_vol_id(libubi_t libubi, struct ubi_dev_info *dev_info,
  46. char *name)
  47. {
  48. int err, i;
  49. struct ubi_vol_info vol_info;
  50. for (i=dev_info->lowest_vol_id; i<=dev_info->highest_vol_id; i++) {
  51. err = ubi_get_vol_info1(libubi, dev_info->dev_num, i, &vol_info);
  52. if (err == -1) {
  53. if (errno == ENOENT)
  54. continue;
  55. return -1;
  56. }
  57. if (strcmp(name, vol_info.name) == 0)
  58. return vol_info.vol_id;
  59. }
  60. return -1;
  61. }
  62. int main(int argc, char * const argv[])
  63. {
  64. int i, err;
  65. int count = 0;
  66. libubi_t libubi;
  67. struct ubi_dev_info dev_info;
  68. struct ubi_rnvol_req rnvol;
  69. const char *node;
  70. if (argc < 3 || (argc & 1) == 1) {
  71. errmsg("too few arguments");
  72. fprintf(stderr, "%s\n", usage);
  73. return -1;
  74. }
  75. if (argc > UBI_MAX_RNVOL + 2) {
  76. errmsg("too many volumes to re-name, max. is %d",
  77. UBI_MAX_RNVOL);
  78. return -1;
  79. }
  80. node = argv[1];
  81. libubi = libubi_open();
  82. if (!libubi) {
  83. if (errno == 0)
  84. return errmsg("UBI is not present in the system");
  85. return sys_errmsg("cannot open libubi");
  86. }
  87. err = ubi_probe_node(libubi, node);
  88. if (err == 2) {
  89. errmsg("\"%s\" is an UBI volume node, not an UBI device node",
  90. node);
  91. goto out_libubi;
  92. } else if (err < 0) {
  93. if (errno == ENODEV)
  94. errmsg("\"%s\" is not an UBI device node", node);
  95. else
  96. sys_errmsg("error while probing \"%s\"", node);
  97. goto out_libubi;
  98. }
  99. err = ubi_get_dev_info(libubi, node, &dev_info);
  100. if (err == -1) {
  101. sys_errmsg("cannot get information about UBI device \"%s\"", node);
  102. goto out_libubi;
  103. }
  104. for (i = 2; i < argc; i += 2) {
  105. err = get_vol_id(libubi, &dev_info, argv[i]);
  106. if (err == -1) {
  107. errmsg("\"%s\" volume not found", argv[i]);
  108. goto out_libubi;
  109. }
  110. rnvol.ents[count].vol_id = err;
  111. rnvol.ents[count].name_len = strlen(argv[i + 1]);
  112. if (rnvol.ents[count].name_len >=
  113. sizeof(rnvol.ents[count].name)) {
  114. errmsg("\"%s\" volume name too long", argv[i + 1]);
  115. goto out_libubi;
  116. }
  117. strcpy(rnvol.ents[count++].name, argv[i + 1]);
  118. }
  119. rnvol.count = count;
  120. err = ubi_rnvols(libubi, node, &rnvol);
  121. if (err == -1) {
  122. sys_errmsg("cannot rename volumes");
  123. goto out_libubi;
  124. }
  125. libubi_close(libubi);
  126. return 0;
  127. out_libubi:
  128. libubi_close(libubi);
  129. return -1;
  130. }