truncate.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/ArgsParser.h>
  27. #include <stdio.h>
  28. #include <sys/stat.h>
  29. #include <unistd.h>
  30. enum TruncateOperation {
  31. OP_Set,
  32. OP_Grow,
  33. OP_Shrink,
  34. };
  35. int main(int argc, char** argv)
  36. {
  37. const char* resize = nullptr;
  38. const char* reference = nullptr;
  39. const char* file = nullptr;
  40. Core::ArgsParser args_parser;
  41. args_parser.add_option(resize, "Resize the target file to (or by) this size. Prefix with + or - to expand or shrink the file, or a bare number to set the size exactly", "size", 's', "size");
  42. args_parser.add_option(reference, "Resize the target file to match the size of this one", "reference", 'r', "file");
  43. args_parser.add_positional_argument(file, "File path", "file");
  44. args_parser.parse(argc, argv);
  45. if (!resize && !reference) {
  46. args_parser.print_usage(stderr, argv[0]);
  47. return 1;
  48. }
  49. if (resize && reference) {
  50. args_parser.print_usage(stderr, argv[0]);
  51. return 1;
  52. }
  53. auto op = OP_Set;
  54. off_t size = 0;
  55. if (resize) {
  56. String str = resize;
  57. switch (str[0]) {
  58. case '+':
  59. op = OP_Grow;
  60. str = str.substring(1, str.length() - 1);
  61. break;
  62. case '-':
  63. op = OP_Shrink;
  64. str = str.substring(1, str.length() - 1);
  65. break;
  66. }
  67. auto size_opt = str.to_int<off_t>();
  68. if (!size_opt.has_value()) {
  69. args_parser.print_usage(stderr, argv[0]);
  70. return 1;
  71. }
  72. size = size_opt.value();
  73. }
  74. if (reference) {
  75. struct stat st;
  76. int rc = stat(reference, &st);
  77. if (rc < 0) {
  78. perror("stat");
  79. return 1;
  80. }
  81. size = st.st_size;
  82. }
  83. int fd = open(file, O_RDWR | O_CREAT, 0666);
  84. if (fd < 0) {
  85. perror("open");
  86. return 1;
  87. }
  88. struct stat st;
  89. if (fstat(fd, &st) < 0) {
  90. perror("fstat");
  91. return 1;
  92. }
  93. switch (op) {
  94. case OP_Set:
  95. break;
  96. case OP_Grow:
  97. size = st.st_size + size;
  98. break;
  99. case OP_Shrink:
  100. size = st.st_size - size;
  101. break;
  102. }
  103. if (ftruncate(fd, size) < 0) {
  104. perror("ftruncate");
  105. return 1;
  106. }
  107. if (close(fd) < 0) {
  108. perror("close");
  109. return 1;
  110. }
  111. return 0;
  112. }