editenv.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* editenv.c - tool to edit environment block. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008,2009,2010,2013 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <grub/types.h>
  21. #include <grub/emu/misc.h>
  22. #include <grub/util/misc.h>
  23. #include <grub/util/install.h>
  24. #include <grub/lib/envblk.h>
  25. #include <grub/i18n.h>
  26. #include <grub/emu/hostfile.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #if !defined(_WIN32)
  30. #include <libgen.h>
  31. #endif
  32. #define DEFAULT_ENVBLK_SIZE 1024
  33. #define GRUB_ENVBLK_MESSAGE "# WARNING: Do not edit this file by tools other than "PACKAGE"-editenv!!!\n"
  34. void
  35. grub_util_create_envblk_file (const char *name)
  36. {
  37. FILE *fp;
  38. char *buf, *pbuf, *namenew;
  39. #if !defined(_WIN32)
  40. ssize_t size = 1;
  41. char *rename_target = xstrdup (name);
  42. int rc;
  43. #endif
  44. buf = xmalloc (DEFAULT_ENVBLK_SIZE);
  45. namenew = xasprintf ("%s.new", name);
  46. fp = grub_util_fopen (namenew, "wb");
  47. if (! fp)
  48. grub_util_error (_("cannot open `%s': %s"), namenew,
  49. strerror (errno));
  50. pbuf = buf;
  51. memcpy (pbuf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
  52. pbuf += sizeof (GRUB_ENVBLK_SIGNATURE) - 1;
  53. memcpy (pbuf, GRUB_ENVBLK_MESSAGE, sizeof (GRUB_ENVBLK_MESSAGE) - 1);
  54. pbuf += sizeof (GRUB_ENVBLK_MESSAGE) - 1;
  55. memset (pbuf , '#',
  56. DEFAULT_ENVBLK_SIZE - sizeof (GRUB_ENVBLK_SIGNATURE) - sizeof (GRUB_ENVBLK_MESSAGE) + 2);
  57. if (fwrite (buf, 1, DEFAULT_ENVBLK_SIZE, fp) != DEFAULT_ENVBLK_SIZE)
  58. grub_util_error (_("cannot write to `%s': %s"), namenew,
  59. strerror (errno));
  60. if (grub_util_file_sync (fp) < 0)
  61. grub_util_error (_("cannot sync `%s': %s"), namenew, strerror (errno));
  62. free (buf);
  63. fclose (fp);
  64. #if defined(_WIN32)
  65. if (grub_util_rename (namenew, name) < 0)
  66. grub_util_error (_("cannot rename the file %s to %s"), namenew, name);
  67. #else
  68. while (1)
  69. {
  70. char *linkbuf;
  71. ssize_t retsize;
  72. linkbuf = xmalloc (size + 1);
  73. retsize = grub_util_readlink (rename_target, linkbuf, size);
  74. if (retsize < 0 && (errno == ENOENT || errno == EINVAL))
  75. {
  76. free (linkbuf);
  77. break;
  78. }
  79. else if (retsize < 0)
  80. {
  81. free (linkbuf);
  82. grub_util_error (_("cannot rename the file %s to %s: %s"), namenew, name, strerror (errno));
  83. }
  84. else if (retsize == size)
  85. {
  86. free (linkbuf);
  87. size += 128;
  88. continue;
  89. }
  90. linkbuf[retsize] = '\0';
  91. if (linkbuf[0] == '/')
  92. {
  93. free (rename_target);
  94. rename_target = linkbuf;
  95. }
  96. else
  97. {
  98. char *dbuf = xstrdup (rename_target);
  99. const char *dir = dirname (dbuf);
  100. free (rename_target);
  101. rename_target = xasprintf ("%s/%s", dir, linkbuf);
  102. free (dbuf);
  103. free (linkbuf);
  104. }
  105. }
  106. rc = grub_util_rename (namenew, rename_target);
  107. if (rc < 0 && errno == EXDEV)
  108. {
  109. rc = grub_install_copy_file (namenew, rename_target, 1);
  110. grub_util_unlink (namenew);
  111. }
  112. free (rename_target);
  113. if (rc < 0)
  114. grub_util_error (_("cannot rename the file %s to %s: %s"), namenew, name, strerror (errno));
  115. #endif
  116. free (namenew);
  117. }