lha_arch_unix.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Copyright (c) 2012, Simon Howard
  3. Permission to use, copy, modify, and/or distribute this software
  4. for any purpose with or without fee is hereby granted, provided
  5. that the above copyright notice and this permission notice appear
  6. in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  8. WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  9. WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  10. AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  11. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. //
  17. // Architecture-specific files for compilation on Unix.
  18. //
  19. #define _GNU_SOURCE
  20. #include "lha_arch.h"
  21. #if LHA_ARCH == LHA_ARCH_UNIX
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #include <utime.h>
  27. #include <sys/stat.h>
  28. #include <sys/types.h>
  29. // TODO: This file depends on vasprintf(), which is a non-standard
  30. // function (_GNU_SOURCE above). Most modern Unix systems have an
  31. // implementation of it, but develop a compatible workaround for
  32. // operating systems that don't have it.
  33. int lha_arch_vasprintf(char **result, char *fmt, va_list args)
  34. {
  35. return vasprintf(result, fmt, args);
  36. }
  37. void lha_arch_set_binary(FILE *handle)
  38. {
  39. // No-op on Unix systems: there is no difference between
  40. // "text" and "binary" files.
  41. }
  42. int lha_arch_mkdir(char *path, unsigned int unix_perms)
  43. {
  44. return mkdir(path, unix_perms) == 0;
  45. }
  46. int lha_arch_chown(char *filename, int unix_uid, int unix_gid)
  47. {
  48. return chown(filename, unix_uid, unix_gid) == 0;
  49. }
  50. int lha_arch_chmod(char *filename, int unix_perms)
  51. {
  52. return chmod(filename, unix_perms) == 0;
  53. }
  54. int lha_arch_utime(char *filename, unsigned int timestamp)
  55. {
  56. struct utimbuf times;
  57. times.actime = (time_t) timestamp;
  58. times.modtime = (time_t) timestamp;
  59. return utime(filename, &times) == 0;
  60. }
  61. FILE *lha_arch_fopen(char *filename, int unix_uid, int unix_gid, int unix_perms)
  62. {
  63. FILE *fstream;
  64. int fileno;
  65. // The O_EXCL flag will cause the open() below to fail if the
  66. // file already exists. Remove it first.
  67. unlink(filename);
  68. // If we have file permissions, they must be set after the
  69. // file is created and UID/GID have been set. When open()ing
  70. // the file, create it with minimal permissions granted only
  71. // to the current user.
  72. // Use O_EXCL so that symlinks are not followed; this prevents
  73. // a malicious symlink from overwriting arbitrary filesystem
  74. // locations.
  75. fileno = open(filename, O_CREAT|O_WRONLY|O_EXCL, 0600);
  76. if (fileno < 0) {
  77. return NULL;
  78. }
  79. // Set owner and group.
  80. if (unix_uid >= 0) {
  81. if (fchown(fileno, unix_uid, unix_gid) != 0) {
  82. // On most Unix systems, only root can change
  83. // ownership. But if we can't change ownership,
  84. // it isn't a fatal error. So ignore the failure
  85. // and continue.
  86. // TODO: Implement some kind of alternate handling
  87. // here?
  88. /* close(fileno);
  89. remove(filename);
  90. return NULL; */
  91. }
  92. }
  93. // Set file permissions.
  94. // File permissions must be set *after* owner and group have
  95. // been set; otherwise, we might briefly be granting permissions
  96. // to the wrong group.
  97. if (unix_perms >= 0) {
  98. if (fchmod(fileno, unix_perms) != 0) {
  99. close(fileno);
  100. remove(filename);
  101. return NULL;
  102. }
  103. }
  104. // Create stdc FILE handle.
  105. fstream = fdopen(fileno, "wb");
  106. if (fstream == NULL) {
  107. close(fileno);
  108. remove(filename);
  109. return NULL;
  110. }
  111. return fstream;
  112. }
  113. LHAFileType lha_arch_exists(char *filename)
  114. {
  115. struct stat statbuf;
  116. if (stat(filename, &statbuf) != 0) {
  117. if (errno == ENOENT) {
  118. return LHA_FILE_NONE;
  119. } else {
  120. return LHA_FILE_ERROR;
  121. }
  122. }
  123. if (S_ISDIR(statbuf.st_mode)) {
  124. return LHA_FILE_DIRECTORY;
  125. } else {
  126. return LHA_FILE_FILE;
  127. }
  128. }
  129. int lha_arch_symlink(char *path, char *target)
  130. {
  131. unlink(path);
  132. return symlink(target, path) == 0;
  133. }
  134. #endif /* LHA_ARCH_UNIX */