0014-Add-OSX-and-FreeBSD-support.patch 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. From 514985ae786dcde9842e46899ef5b6218662a119 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?=C3=81lvaro=20Fern=C3=A1ndez=20Rojas?= <noltari@gmail.com>
  3. Date: Sat, 7 Mar 2015 16:32:51 +0100
  4. Subject: [PATCH 14/14] Add OSX and FreeBSD support
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
  9. ---
  10. Makefile | 9 ++++++++-
  11. src/boot.c | 1 +
  12. src/check.c | 1 +
  13. src/endian.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  14. src/fat.c | 1 +
  15. src/fatlabel.c | 1 +
  16. src/fsck.fat.h | 3 +--
  17. src/io.h | 2 +-
  18. src/lfn.c | 1 +
  19. src/mkfs.fat.c | 19 ++++++++++++++++---
  20. src/types.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  21. 11 files changed, 145 insertions(+), 7 deletions(-)
  22. create mode 100644 src/endian.h
  23. create mode 100644 src/types.h
  24. diff --git a/Makefile b/Makefile
  25. index 1593f3d..7359a79 100644
  26. --- a/Makefile
  27. +++ b/Makefile
  28. @@ -28,12 +28,19 @@ DOCDIR = $(PREFIX)/share/doc
  29. MANDIR = $(PREFIX)/share/man
  30. #OPTFLAGS = -O2 -fomit-frame-pointer -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
  31. -OPTFLAGS = -O2 -fomit-frame-pointer -D_GNU_SOURCE $(shell getconf LFS_CFLAGS)
  32. +OPTFLAGS = -O2 -fomit-frame-pointer -D_GNU_SOURCE
  33. #WARNFLAGS = -Wall -pedantic -std=c99
  34. WARNFLAGS = -Wall -Wextra -Wno-sign-compare -Wno-missing-field-initializers -Wmissing-prototypes -Wstrict-prototypes -Wwrite-strings
  35. DEBUGFLAGS = -g
  36. CFLAGS += $(OPTFLAGS) $(WARNFLAGS) $(DEBUGFLAGS)
  37. +UNAME_S := $(shell uname -s)
  38. +ifeq ($(UNAME_S),Darwin)
  39. + LDLIBS += -liconv
  40. +else
  41. + OPTFLAGS += $(shell getconf LFS_CFLAGS)
  42. +endif
  43. +
  44. VPATH = src
  45. all: build
  46. diff --git a/src/boot.c b/src/boot.c
  47. index 0c0918f..1da9889 100644
  48. --- a/src/boot.c
  49. +++ b/src/boot.c
  50. @@ -31,6 +31,7 @@
  51. #include <time.h>
  52. #include "common.h"
  53. +#include "endian.h"
  54. #include "fsck.fat.h"
  55. #include "fat.h"
  56. #include "io.h"
  57. diff --git a/src/check.c b/src/check.c
  58. index 488f715..17ff16a 100644
  59. --- a/src/check.c
  60. +++ b/src/check.c
  61. @@ -31,6 +31,7 @@
  62. #include <time.h>
  63. #include "common.h"
  64. +#include "endian.h"
  65. #include "fsck.fat.h"
  66. #include "io.h"
  67. #include "fat.h"
  68. diff --git a/src/endian.h b/src/endian.h
  69. new file mode 100644
  70. index 0000000..6613e65
  71. --- /dev/null
  72. +++ b/src/endian.h
  73. @@ -0,0 +1,57 @@
  74. +/* endian.h - Endian functions
  75. +
  76. + Copyright (C) 2015 Álvaro Fernández Rojas <noltari@gmail.com>
  77. +
  78. + This program is free software: you can redistribute it and/or modify
  79. + it under the terms of the GNU General Public License as published by
  80. + the Free Software Foundation, either version 3 of the License, or
  81. + (at your option) any later version.
  82. +
  83. + This program is distributed in the hope that it will be useful,
  84. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  85. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  86. + GNU General Public License for more details.
  87. +
  88. + You should have received a copy of the GNU General Public License
  89. + along with this program. If not, see <http://www.gnu.org/licenses/>.
  90. +
  91. + The complete text of the GNU General Public License
  92. + can be found in /usr/share/common-licenses/GPL-3 file.
  93. +*/
  94. +
  95. +#ifndef _ENDIAN_H
  96. +#define _ENDIAN_H
  97. +
  98. +#if defined(__linux__)
  99. + #include <endian.h>
  100. +#elif defined(__APPLE__)
  101. + #include <libkern/OSByteOrder.h>
  102. +
  103. + #define htobe16(x) OSSwapHostToBigInt16(x)
  104. + #define htole16(x) OSSwapHostToLittleInt16(x)
  105. + #define be16toh(x) OSSwapBigToHostInt16(x)
  106. + #define le16toh(x) OSSwapLittleToHostInt16(x)
  107. +
  108. + #define htobe32(x) OSSwapHostToBigInt32(x)
  109. + #define htole32(x) OSSwapHostToLittleInt32(x)
  110. + #define be32toh(x) OSSwapBigToHostInt32(x)
  111. + #define le32toh(x) OSSwapLittleToHostInt32(x)
  112. +
  113. + #define htobe64(x) OSSwapHostToBigInt64(x)
  114. + #define htole64(x) OSSwapHostToLittleInt64(x)
  115. + #define be64toh(x) OSSwapBigToHostInt64(x)
  116. + #define le64toh(x) OSSwapLittleToHostInt64(x)
  117. +#elif defined(__FreeBSD__)
  118. + #include <sys/endian.h>
  119. +
  120. + #define be16toh(x) betoh16(x)
  121. + #define le16toh(x) letoh16(x)
  122. +
  123. + #define be32toh(x) betoh32(x)
  124. + #define le32toh(x) letoh32(x)
  125. +
  126. + #define be64toh(x) betoh64(x)
  127. + #define le64toh(x) letoh64(x)
  128. +#endif
  129. +
  130. +#endif /* _ENDIAN_H */
  131. diff --git a/src/fat.c b/src/fat.c
  132. index 5a92f56..481c08a 100644
  133. --- a/src/fat.c
  134. +++ b/src/fat.c
  135. @@ -30,6 +30,7 @@
  136. #include <unistd.h>
  137. #include "common.h"
  138. +#include "endian.h"
  139. #include "fsck.fat.h"
  140. #include "io.h"
  141. #include "check.h"
  142. diff --git a/src/fatlabel.c b/src/fatlabel.c
  143. index 1484ba5..6de831c 100644
  144. --- a/src/fatlabel.c
  145. +++ b/src/fatlabel.c
  146. @@ -33,6 +33,7 @@
  147. #include <ctype.h>
  148. #include "common.h"
  149. +#include "types.h"
  150. #include "fsck.fat.h"
  151. #include "io.h"
  152. #include "boot.h"
  153. diff --git a/src/fsck.fat.h b/src/fsck.fat.h
  154. index e5f6178..8b0ccb9 100644
  155. --- a/src/fsck.fat.h
  156. +++ b/src/fsck.fat.h
  157. @@ -27,11 +27,10 @@
  158. #ifndef _DOSFSCK_H
  159. #define _DOSFSCK_H
  160. -#include <fcntl.h>
  161. #include <stddef.h>
  162. #include <stdint.h>
  163. -#include <endian.h>
  164. +#include "types.h"
  165. #include "msdos_fs.h"
  166. #define VFAT_LN_ATTR (ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME)
  167. diff --git a/src/io.h b/src/io.h
  168. index d23d07e..eecfdc5 100644
  169. --- a/src/io.h
  170. +++ b/src/io.h
  171. @@ -27,7 +27,7 @@
  172. #ifndef _IO_H
  173. #define _IO_H
  174. -#include <fcntl.h> /* for loff_t */
  175. +#include "types.h"
  176. loff_t llseek(int fd, loff_t offset, int whence);
  177. diff --git a/src/lfn.c b/src/lfn.c
  178. index 2601172..f679168 100644
  179. --- a/src/lfn.c
  180. +++ b/src/lfn.c
  181. @@ -28,6 +28,7 @@
  182. #include <time.h>
  183. #include "common.h"
  184. +#include "endian.h"
  185. #include "io.h"
  186. #include "fsck.fat.h"
  187. #include "lfn.h"
  188. diff --git a/src/mkfs.fat.c b/src/mkfs.fat.c
  189. index 02e0918..f2cee09 100644
  190. --- a/src/mkfs.fat.c
  191. +++ b/src/mkfs.fat.c
  192. @@ -48,8 +48,6 @@
  193. #include <fcntl.h>
  194. #include <sys/mount.h>
  195. -#include <endian.h>
  196. -#include <mntent.h>
  197. #include <signal.h>
  198. #include <string.h>
  199. #include <stdio.h>
  200. @@ -61,13 +59,14 @@
  201. #include <errno.h>
  202. #include <ctype.h>
  203. #include <stdint.h>
  204. -#include <endian.h>
  205. #if defined(__linux__)
  206. + #include <mntent.h>
  207. #include <linux/hdreg.h>
  208. #include <linux/fs.h>
  209. #include <linux/fd.h>
  210. #elif defined(__FreeBSD__) || defined(__APPLE__)
  211. + #include <sys/mount.h>
  212. #include <sys/disk.h>
  213. #define BLOCK_SIZE_BITS 10
  214. @@ -97,7 +96,9 @@
  215. };
  216. #endif
  217. +#include "endian.h"
  218. #include "msdos_fs.h"
  219. +#include "types.h"
  220. /* In earlier versions, an own llseek() was used, but glibc lseek() is
  221. * sufficient (or even better :) for 64 bit offsets in the meantime */
  222. @@ -525,6 +526,7 @@ static uint64_t count_blocks(char *filename, int *remainder)
  223. static void check_mount(char *device_name)
  224. {
  225. +#if defined(__linux__)
  226. FILE *f;
  227. struct mntent *mnt;
  228. @@ -534,6 +536,17 @@ static void check_mount(char *device_name)
  229. if (strcmp(device_name, mnt->mnt_fsname) == 0)
  230. die("%s contains a mounted filesystem.");
  231. endmntent(f);
  232. +#elif defined(__APPLE__) || defined(__FreeBSD__)
  233. + struct statfs* mounts;
  234. + int num_mounts = getmntinfo(&mounts, MNT_WAIT);
  235. + if (num_mounts < 0)
  236. + return;
  237. + for ( int i = 0; i < num_mounts; i++ )
  238. + {
  239. + if (strcmp(device_name, mounts[i].f_mntfromname) == 0)
  240. + die("%s contains a mounted filesystem.");
  241. + }
  242. +#endif
  243. }
  244. /* Establish the geometry and media parameters for the device */
  245. diff --git a/src/types.h b/src/types.h
  246. new file mode 100644
  247. index 0000000..a3f1a47
  248. --- /dev/null
  249. +++ b/src/types.h
  250. @@ -0,0 +1,57 @@
  251. +/* types.h - Missing types
  252. +
  253. + Copyright (C) 2015 Álvaro Fernández Rojas <noltari@gmail.com>
  254. +
  255. + This program is free software: you can redistribute it and/or modify
  256. + it under the terms of the GNU General Public License as published by
  257. + the Free Software Foundation, either version 3 of the License, or
  258. + (at your option) any later version.
  259. +
  260. + This program is distributed in the hope that it will be useful,
  261. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  262. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  263. + GNU General Public License for more details.
  264. +
  265. + You should have received a copy of the GNU General Public License
  266. + along with this program. If not, see <http://www.gnu.org/licenses/>.
  267. +
  268. + The complete text of the GNU General Public License
  269. + can be found in /usr/share/common-licenses/GPL-3 file.
  270. +*/
  271. +
  272. +#ifndef _TYPES_H
  273. +#define _TYPES_H
  274. +
  275. +#if defined(__linux__)
  276. + #include <fcntl.h>
  277. +#elif defined(__APPLE__)
  278. + #ifndef loff_t
  279. + typedef long long loff_t;
  280. + #endif /* loff_t */
  281. +
  282. + #ifndef lseek64
  283. + #define lseek64 lseek
  284. + #endif /* lseek64 */
  285. +
  286. + #ifndef off64_t
  287. + #ifdef _LP64
  288. + typedef off_t off64_t;
  289. + #else
  290. + typedef __longlong_t off64_t;
  291. + #endif /* _LP64 */
  292. + #endif /* off64_t */
  293. +#elif defined(__FreeBSD__)
  294. + #ifndef loff_t
  295. + typedef long long loff_t;
  296. + #endif /* loff_t */
  297. +
  298. + #ifndef lseek64
  299. + #define lseek64 lseek
  300. + #endif /* lseek64 */
  301. +
  302. + #ifndef off64_t
  303. + typedef off_t off64_t;
  304. + #endif /* off64_t */
  305. +#endif
  306. +
  307. +#endif /* _TYPES_H */
  308. --
  309. 1.9.1