0001-localedef-Add-hardlink-resolver-from-util-linux.patch 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. From c6dca721df6dd8c39ffe16e61623516bd8742d39 Mon Sep 17 00:00:00 2001
  2. From: Jason Wessel <jason.wessel@windriver.com>
  3. Date: Sat, 7 Dec 2019 09:59:22 -0800
  4. Subject: [PATCH] localedef: Add hardlink resolver from util-linux
  5. The hard link resolver that is built into localedef cannot be run in
  6. parallel. It will search sibling directories (which are be processed
  7. in parallel) and perform a creation of a .tmp file and remove the
  8. original and move the .tmp file in. The problem is that if a probe
  9. occurs a hard link can be requested to the file that is being removed.
  10. This will lead to a stray copy or potentially, on a loaded system
  11. cause race condition which pseudo cannot deal with, where it is left
  12. with a hard link request to a file that no longer exists. In this
  13. situation psuedo will inherit the permissions of what ever the target
  14. inode had to offer.
  15. In short, there are two problems:
  16. 1) You will be left with stray copies when using the hard link
  17. resolution that is built in while running in parallel with
  18. localedef.
  19. 2) When running under pseudo the possibility exists for uid/gid
  20. leakage when the source file is removed before the hard link can
  21. be completed.
  22. The solution is to call localedef with --no-hard-links and separately
  23. process the hardlinks at a later point. To do this requires the
  24. inclusion of the hardlink utility found in modern versions of
  25. util-linux. Most host systems do not have this, so it will be
  26. included with the cross-localedef binary.
  27. [YOCTO #11299]
  28. [YOCTO #12434]
  29. Upstream-Status: Pending
  30. Signed-off-by: Jason Wessel <jason.wessel@windriver.com>
  31. Signed-off-by: Khem Raj <raj.khem@gmail.com>
  32. ---
  33. locale/programs/c.h | 407 ++++++++++++++++
  34. locale/programs/cross-localedef-hardlink.c | 528 +++++++++++++++++++++
  35. locale/programs/xalloc.h | 129 +++++
  36. 3 files changed, 1064 insertions(+)
  37. create mode 100644 locale/programs/c.h
  38. create mode 100644 locale/programs/cross-localedef-hardlink.c
  39. create mode 100644 locale/programs/xalloc.h
  40. diff --git a/locale/programs/c.h b/locale/programs/c.h
  41. new file mode 100644
  42. index 0000000000..d0a402e90e
  43. --- /dev/null
  44. +++ b/locale/programs/c.h
  45. @@ -0,0 +1,407 @@
  46. +/*
  47. + * Fundamental C definitions.
  48. + */
  49. +
  50. +#ifndef UTIL_LINUX_C_H
  51. +#define UTIL_LINUX_C_H
  52. +
  53. +#include <limits.h>
  54. +#include <stddef.h>
  55. +#include <stdint.h>
  56. +#include <stdio.h>
  57. +#include <unistd.h>
  58. +#include <stdarg.h>
  59. +#include <stdlib.h>
  60. +#include <string.h>
  61. +#include <errno.h>
  62. +
  63. +#include <assert.h>
  64. +
  65. +#ifdef HAVE_ERR_H
  66. +# include <err.h>
  67. +#endif
  68. +
  69. +#ifdef HAVE_SYS_SYSMACROS_H
  70. +# include <sys/sysmacros.h> /* for major, minor */
  71. +#endif
  72. +
  73. +#ifndef LOGIN_NAME_MAX
  74. +# define LOGIN_NAME_MAX 256
  75. +#endif
  76. +
  77. +#ifndef NAME_MAX
  78. +# define NAME_MAX PATH_MAX
  79. +#endif
  80. +
  81. +/*
  82. + * __GNUC_PREREQ is deprecated in favour of __has_attribute() and
  83. + * __has_feature(). The __has macros are supported by clang and gcc>=5.
  84. + */
  85. +#ifndef __GNUC_PREREQ
  86. +# if defined __GNUC__ && defined __GNUC_MINOR__
  87. +# define __GNUC_PREREQ(maj, min) \
  88. + ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  89. +# else
  90. +# define __GNUC_PREREQ(maj, min) 0
  91. +# endif
  92. +#endif
  93. +
  94. +#ifdef __GNUC__
  95. +
  96. +/* &a[0] degrades to a pointer: a different type from an array */
  97. +# define __must_be_array(a) \
  98. + UL_BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(__typeof__(a), __typeof__(&a[0])))
  99. +
  100. +# define ignore_result(x) __extension__ ({ \
  101. + __typeof__(x) __dummy __attribute__((__unused__)) = (x); (void) __dummy; \
  102. +})
  103. +
  104. +#else /* !__GNUC__ */
  105. +# define __must_be_array(a) 0
  106. +# define __attribute__(_arg_)
  107. +# define ignore_result(x) ((void) (x))
  108. +#endif /* !__GNUC__ */
  109. +
  110. +/*
  111. + * It evaluates to 1 if the attribute/feature is supported by the current
  112. + * compilation targed. Fallback for old compilers.
  113. + */
  114. +#ifndef __has_attribute
  115. + #define __has_attribute(x) 0
  116. +#endif
  117. +
  118. +#ifndef __has_feature
  119. + #define __has_feature(x) 0
  120. +#endif
  121. +
  122. +/*
  123. + * Function attributes
  124. + */
  125. +#ifndef __ul_alloc_size
  126. +# if (__has_attribute(alloc_size) && __has_attribute(warn_unused_result)) || __GNUC_PREREQ (4, 3)
  127. +# define __ul_alloc_size(s) __attribute__((alloc_size(s), warn_unused_result))
  128. +# else
  129. +# define __ul_alloc_size(s)
  130. +# endif
  131. +#endif
  132. +
  133. +#ifndef __ul_calloc_size
  134. +# if (__has_attribute(alloc_size) && __has_attribute(warn_unused_result)) || __GNUC_PREREQ (4, 3)
  135. +# define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s), warn_unused_result))
  136. +# else
  137. +# define __ul_calloc_size(n, s)
  138. +# endif
  139. +#endif
  140. +
  141. +#if __has_attribute(returns_nonnull) || __GNUC_PREREQ (4, 9)
  142. +# define __ul_returns_nonnull __attribute__((returns_nonnull))
  143. +#else
  144. +# define __ul_returns_nonnull
  145. +#endif
  146. +
  147. +/*
  148. + * Force a compilation error if condition is true, but also produce a
  149. + * result (of value 0 and type size_t), so the expression can be used
  150. + * e.g. in a structure initializer (or wherever else comma expressions
  151. + * aren't permitted).
  152. + */
  153. +#define UL_BUILD_BUG_ON_ZERO(e) __extension__ (sizeof(struct { int:-!!(e); }))
  154. +#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
  155. +
  156. +#ifndef ARRAY_SIZE
  157. +# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
  158. +#endif
  159. +
  160. +#ifndef PATH_MAX
  161. +# define PATH_MAX 4096
  162. +#endif
  163. +
  164. +#ifndef TRUE
  165. +# define TRUE 1
  166. +#endif
  167. +
  168. +#ifndef FALSE
  169. +# define FALSE 0
  170. +#endif
  171. +
  172. +#ifndef min
  173. +# define min(x, y) __extension__ ({ \
  174. + __typeof__(x) _min1 = (x); \
  175. + __typeof__(y) _min2 = (y); \
  176. + (void) (&_min1 == &_min2); \
  177. + _min1 < _min2 ? _min1 : _min2; })
  178. +#endif
  179. +
  180. +#ifndef max
  181. +# define max(x, y) __extension__ ({ \
  182. + __typeof__(x) _max1 = (x); \
  183. + __typeof__(y) _max2 = (y); \
  184. + (void) (&_max1 == &_max2); \
  185. + _max1 > _max2 ? _max1 : _max2; })
  186. +#endif
  187. +
  188. +#ifndef cmp_numbers
  189. +# define cmp_numbers(x, y) __extension__ ({ \
  190. + __typeof__(x) _a = (x); \
  191. + __typeof__(y) _b = (y); \
  192. + (void) (&_a == &_b); \
  193. + _a == _b ? 0 : _a > _b ? 1 : -1; })
  194. +#endif
  195. +
  196. +#ifndef offsetof
  197. +#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  198. +#endif
  199. +
  200. +/*
  201. + * container_of - cast a member of a structure out to the containing structure
  202. + * @ptr: the pointer to the member.
  203. + * @type: the type of the container struct this is embedded in.
  204. + * @member: the name of the member within the struct.
  205. + */
  206. +#ifndef container_of
  207. +#define container_of(ptr, type, member) __extension__ ({ \
  208. + const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
  209. + (type *)( (char *)__mptr - offsetof(type,member) );})
  210. +#endif
  211. +
  212. +#ifndef HAVE_PROGRAM_INVOCATION_SHORT_NAME
  213. +# ifdef HAVE___PROGNAME
  214. +extern char *__progname;
  215. +# define program_invocation_short_name __progname
  216. +# else
  217. +# ifdef HAVE_GETEXECNAME
  218. +# define program_invocation_short_name \
  219. + prog_inv_sh_nm_from_file(getexecname(), 0)
  220. +# else
  221. +# define program_invocation_short_name \
  222. + prog_inv_sh_nm_from_file(__FILE__, 1)
  223. +# endif
  224. +static char prog_inv_sh_nm_buf[256];
  225. +static inline char *
  226. +prog_inv_sh_nm_from_file(char *f, char stripext)
  227. +{
  228. + char *t;
  229. +
  230. + if ((t = strrchr(f, '/')) != NULL)
  231. + t++;
  232. + else
  233. + t = f;
  234. +
  235. + strncpy(prog_inv_sh_nm_buf, t, sizeof(prog_inv_sh_nm_buf) - 1);
  236. + prog_inv_sh_nm_buf[sizeof(prog_inv_sh_nm_buf) - 1] = '\0';
  237. +
  238. + if (stripext && (t = strrchr(prog_inv_sh_nm_buf, '.')) != NULL)
  239. + *t = '\0';
  240. +
  241. + return prog_inv_sh_nm_buf;
  242. +}
  243. +# endif
  244. +#endif
  245. +
  246. +
  247. +#ifndef HAVE_ERR_H
  248. +static inline void
  249. +errmsg(char doexit, int excode, char adderr, const char *fmt, ...)
  250. +{
  251. + fprintf(stderr, "%s: ", program_invocation_short_name);
  252. + if (fmt != NULL) {
  253. + va_list argp;
  254. + va_start(argp, fmt);
  255. + vfprintf(stderr, fmt, argp);
  256. + va_end(argp);
  257. + if (adderr)
  258. + fprintf(stderr, ": ");
  259. + }
  260. + if (adderr)
  261. + fprintf(stderr, "%m");
  262. + fprintf(stderr, "\n");
  263. + if (doexit)
  264. + exit(excode);
  265. +}
  266. +
  267. +#ifndef HAVE_ERR
  268. +# define err(E, FMT...) errmsg(1, E, 1, FMT)
  269. +#endif
  270. +
  271. +#ifndef HAVE_ERRX
  272. +# define errx(E, FMT...) errmsg(1, E, 0, FMT)
  273. +#endif
  274. +
  275. +#ifndef HAVE_WARN
  276. +# define warn(FMT...) errmsg(0, 0, 1, FMT)
  277. +#endif
  278. +
  279. +#ifndef HAVE_WARNX
  280. +# define warnx(FMT...) errmsg(0, 0, 0, FMT)
  281. +#endif
  282. +#endif /* !HAVE_ERR_H */
  283. +
  284. +
  285. +/* Don't use inline function to avoid '#include "nls.h"' in c.h
  286. + */
  287. +#define errtryhelp(eval) __extension__ ({ \
  288. + fprintf(stderr, _("Try '%s --help' for more information.\n"), \
  289. + program_invocation_short_name); \
  290. + exit(eval); \
  291. +})
  292. +
  293. +/* After failed execvp() */
  294. +#define EX_EXEC_FAILED 126 /* Program located, but not usable. */
  295. +#define EX_EXEC_ENOENT 127 /* Could not find program to exec. */
  296. +#define errexec(name) err(errno == ENOENT ? EX_EXEC_ENOENT : EX_EXEC_FAILED, \
  297. + _("failed to execute %s"), name)
  298. +
  299. +
  300. +static inline __attribute__((const)) int is_power_of_2(unsigned long num)
  301. +{
  302. + return (num != 0 && ((num & (num - 1)) == 0));
  303. +}
  304. +
  305. +#ifndef HAVE_LOFF_T
  306. +typedef int64_t loff_t;
  307. +#endif
  308. +
  309. +#if !defined(HAVE_DIRFD) && (!defined(HAVE_DECL_DIRFD) || HAVE_DECL_DIRFD == 0) && defined(HAVE_DIR_DD_FD)
  310. +#include <sys/types.h>
  311. +#include <dirent.h>
  312. +static inline int dirfd(DIR *d)
  313. +{
  314. + return d->dd_fd;
  315. +}
  316. +#endif
  317. +
  318. +/*
  319. + * Fallback defines for old versions of glibc
  320. + */
  321. +#include <fcntl.h>
  322. +
  323. +#ifdef O_CLOEXEC
  324. +#define UL_CLOEXECSTR "e"
  325. +#else
  326. +#define UL_CLOEXECSTR ""
  327. +#endif
  328. +
  329. +#ifndef O_CLOEXEC
  330. +#define O_CLOEXEC 0
  331. +#endif
  332. +
  333. +#ifdef __FreeBSD_kernel__
  334. +#ifndef F_DUPFD_CLOEXEC
  335. +#define F_DUPFD_CLOEXEC 17 /* Like F_DUPFD, but FD_CLOEXEC is set */
  336. +#endif
  337. +#endif
  338. +
  339. +
  340. +#ifndef AI_ADDRCONFIG
  341. +#define AI_ADDRCONFIG 0x0020
  342. +#endif
  343. +
  344. +#ifndef IUTF8
  345. +#define IUTF8 0040000
  346. +#endif
  347. +
  348. +/*
  349. + * MAXHOSTNAMELEN replacement
  350. + */
  351. +static inline size_t get_hostname_max(void)
  352. +{
  353. + long len = sysconf(_SC_HOST_NAME_MAX);
  354. +
  355. + if (0 < len)
  356. + return len;
  357. +
  358. +#ifdef MAXHOSTNAMELEN
  359. + return MAXHOSTNAMELEN;
  360. +#elif HOST_NAME_MAX
  361. + return HOST_NAME_MAX;
  362. +#endif
  363. + return 64;
  364. +}
  365. +
  366. +
  367. +/*
  368. + * Constant strings for usage() functions. For more info see
  369. + * Documentation/{howto-usage-function.txt,boilerplate.c}
  370. + */
  371. +#define USAGE_HEADER ("\nUsage:\n")
  372. +#define USAGE_OPTIONS ("\nOptions:\n")
  373. +#define USAGE_FUNCTIONS ("\nFunctions:\n")
  374. +#define USAGE_COMMANDS ("\nCommands:\n")
  375. +#define USAGE_COLUMNS ("\nAvailable output columns:\n")
  376. +#define USAGE_SEPARATOR "\n"
  377. +
  378. +#define USAGE_OPTSTR_HELP ("display this help")
  379. +#define USAGE_OPTSTR_VERSION ("display version")
  380. +
  381. +#define USAGE_HELP_OPTIONS(marg_dsc) \
  382. + "%-" #marg_dsc "s%s\n" \
  383. + "%-" #marg_dsc "s%s\n" \
  384. + , " -h, --help", USAGE_OPTSTR_HELP \
  385. + , " -V, --version", USAGE_OPTSTR_VERSION
  386. +
  387. +#define USAGE_MAN_TAIL(_man) ("\nFor more details see %s.\n"), _man
  388. +
  389. +#define UTIL_LINUX_VERSION ("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING
  390. +
  391. +#define print_version(eval) __extension__ ({ \
  392. + printf(UTIL_LINUX_VERSION); \
  393. + exit(eval); \
  394. +})
  395. +
  396. +/*
  397. + * scanf modifiers for "strings allocation"
  398. + */
  399. +#ifdef HAVE_SCANF_MS_MODIFIER
  400. +#define UL_SCNsA "%ms"
  401. +#elif defined(HAVE_SCANF_AS_MODIFIER)
  402. +#define UL_SCNsA "%as"
  403. +#endif
  404. +
  405. +/*
  406. + * seek stuff
  407. + */
  408. +#ifndef SEEK_DATA
  409. +# define SEEK_DATA 3
  410. +#endif
  411. +#ifndef SEEK_HOLE
  412. +# define SEEK_HOLE 4
  413. +#endif
  414. +
  415. +
  416. +/*
  417. + * Macros to convert #define'itions to strings, for example
  418. + * #define XYXXY 42
  419. + * printf ("%s=%s\n", stringify(XYXXY), stringify_value(XYXXY));
  420. + */
  421. +#define stringify_value(s) stringify(s)
  422. +#define stringify(s) #s
  423. +
  424. +/*
  425. + * UL_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
  426. + * instrumentation shipped with Clang and GCC) to not instrument the
  427. + * annotated function. Furthermore, it will prevent the compiler from
  428. + * inlining the function because inlining currently breaks the blacklisting
  429. + * mechanism of AddressSanitizer.
  430. + */
  431. +#if __has_feature(address_sanitizer) && __has_attribute(no_sanitize_memory) && __has_attribute(no_sanitize_address)
  432. +# define UL_ASAN_BLACKLIST __attribute__((noinline)) __attribute__((no_sanitize_memory)) __attribute__((no_sanitize_address))
  433. +#else
  434. +# define UL_ASAN_BLACKLIST /* nothing */
  435. +#endif
  436. +
  437. +/*
  438. + * Note that sysconf(_SC_GETPW_R_SIZE_MAX) returns *initial* suggested size for
  439. + * pwd buffer and in some cases it is not large enough. See POSIX and
  440. + * getpwnam_r man page for more details.
  441. + */
  442. +#define UL_GETPW_BUFSIZ (16 * 1024)
  443. +
  444. +/*
  445. + * Darwin or other BSDs may only have MAP_ANON. To get it on Darwin we must
  446. + * define _DARWIN_C_SOURCE before including sys/mman.h. We do this in config.h.
  447. + */
  448. +#if !defined MAP_ANONYMOUS && defined MAP_ANON
  449. +# define MAP_ANONYMOUS (MAP_ANON)
  450. +#endif
  451. +
  452. +#endif /* UTIL_LINUX_C_H */
  453. diff --git a/locale/programs/cross-localedef-hardlink.c b/locale/programs/cross-localedef-hardlink.c
  454. new file mode 100644
  455. index 0000000000..63615896b0
  456. --- /dev/null
  457. +++ b/locale/programs/cross-localedef-hardlink.c
  458. @@ -0,0 +1,528 @@
  459. +/*
  460. + * hardlink - consolidate duplicate files via hardlinks
  461. + *
  462. + * Copyright (C) 2018 Red Hat, Inc. All rights reserved.
  463. + * Written by Jakub Jelinek <jakub@redhat.com>
  464. + *
  465. + * Copyright (C) 2019 Karel Zak <kzak@redhat.com>
  466. + *
  467. + * This program is free software; you can redistribute it and/or modify
  468. + * it under the terms of the GNU General Public License as published by
  469. + * the Free Software Foundation; either version 2 of the License, or
  470. + * (at your option) any later version.
  471. + *
  472. + * This program is distributed in the hope that it would be useful,
  473. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  474. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  475. + * GNU General Public License for more details.
  476. + *
  477. + * You should have received a copy of the GNU General Public License along
  478. + * with this program; if not, write to the Free Software Foundation, Inc.,
  479. + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  480. + */
  481. +#include <sys/types.h>
  482. +#include <stdlib.h>
  483. +#include <getopt.h>
  484. +#include <stdio.h>
  485. +#include <unistd.h>
  486. +#include <sys/stat.h>
  487. +#include <sys/mman.h>
  488. +#include <string.h>
  489. +#include <dirent.h>
  490. +#include <fcntl.h>
  491. +#include <errno.h>
  492. +#ifdef HAVE_PCRE
  493. +# define PCRE2_CODE_UNIT_WIDTH 8
  494. +# include <pcre2.h>
  495. +#endif
  496. +
  497. +#include "c.h"
  498. +#include "xalloc.h"
  499. +#include "nls.h"
  500. +#include "closestream.h"
  501. +
  502. +#define NHASH (1<<17) /* Must be a power of 2! */
  503. +#define NBUF 64
  504. +
  505. +struct hardlink_file;
  506. +
  507. +struct hardlink_hash {
  508. + struct hardlink_hash *next;
  509. + struct hardlink_file *chain;
  510. + off_t size;
  511. + time_t mtime;
  512. +};
  513. +
  514. +struct hardlink_dir {
  515. + struct hardlink_dir *next;
  516. + char name[];
  517. +};
  518. +
  519. +struct hardlink_file {
  520. + struct hardlink_file *next;
  521. + ino_t ino;
  522. + dev_t dev;
  523. + unsigned int cksum;
  524. + char name[];
  525. +};
  526. +
  527. +struct hardlink_dynstr {
  528. + char *buf;
  529. + size_t alloc;
  530. +};
  531. +
  532. +struct hardlink_ctl {
  533. + struct hardlink_dir *dirs;
  534. + struct hardlink_hash *hps[NHASH];
  535. + char iobuf1[BUFSIZ];
  536. + char iobuf2[BUFSIZ];
  537. + /* summary counters */
  538. + unsigned long long ndirs;
  539. + unsigned long long nobjects;
  540. + unsigned long long nregfiles;
  541. + unsigned long long ncomp;
  542. + unsigned long long nlinks;
  543. + unsigned long long nsaved;
  544. + /* current device */
  545. + dev_t dev;
  546. + /* flags */
  547. + unsigned int verbose;
  548. + unsigned int
  549. + no_link:1,
  550. + content_only:1,
  551. + force:1;
  552. +};
  553. +/* ctl is in global scope due use in atexit() */
  554. +struct hardlink_ctl global_ctl;
  555. +
  556. +__attribute__ ((always_inline))
  557. +static inline unsigned int hash(off_t size, time_t mtime)
  558. +{
  559. + return (size ^ mtime) & (NHASH - 1);
  560. +}
  561. +
  562. +__attribute__ ((always_inline))
  563. +static inline int stcmp(struct stat *st1, struct stat *st2, int content_scope)
  564. +{
  565. + if (content_scope)
  566. + return st1->st_size != st2->st_size;
  567. +
  568. + return st1->st_mode != st2->st_mode
  569. + || st1->st_uid != st2->st_uid
  570. + || st1->st_gid != st2->st_gid
  571. + || st1->st_size != st2->st_size
  572. + || st1->st_mtime != st2->st_mtime;
  573. +}
  574. +
  575. +static void print_summary(void)
  576. +{
  577. + struct hardlink_ctl const *const ctl = &global_ctl;
  578. +
  579. + if (!ctl->verbose)
  580. + return;
  581. +
  582. + if (ctl->verbose > 1 && ctl->nlinks)
  583. + fputc('\n', stdout);
  584. +
  585. + printf(_("Directories: %9lld\n"), ctl->ndirs);
  586. + printf(_("Objects: %9lld\n"), ctl->nobjects);
  587. + printf(_("Regular files: %9lld\n"), ctl->nregfiles);
  588. + printf(_("Comparisons: %9lld\n"), ctl->ncomp);
  589. + printf( "%s%9lld\n", (ctl->no_link ?
  590. + _("Would link: ") :
  591. + _("Linked: ")), ctl->nlinks);
  592. + printf( "%s %9lld\n", (ctl->no_link ?
  593. + _("Would save: ") :
  594. + _("Saved: ")), ctl->nsaved);
  595. +}
  596. +
  597. +static void __attribute__((__noreturn__)) usage(void)
  598. +{
  599. + fputs(USAGE_HEADER, stdout);
  600. + printf(_(" %s [options] directory...\n"), program_invocation_short_name);
  601. +
  602. + fputs(USAGE_SEPARATOR, stdout);
  603. + puts(_("Consolidate duplicate files using hardlinks."));
  604. +
  605. + fputs(USAGE_OPTIONS, stdout);
  606. + puts(_(" -c, --content compare only contents, ignore permission, etc."));
  607. + puts(_(" -n, --dry-run don't actually link anything"));
  608. + puts(_(" -v, --verbose print summary after hardlinking"));
  609. + puts(_(" -vv print every hardlinked file and summary"));
  610. + puts(_(" -f, --force force hardlinking across filesystems"));
  611. + puts(_(" -x, --exclude <regex> exclude files matching pattern"));
  612. +
  613. + fputs(USAGE_SEPARATOR, stdout);
  614. + printf(USAGE_HELP_OPTIONS(16)); /* char offset to align option descriptions */
  615. + printf(USAGE_MAN_TAIL("hardlink(1)"));
  616. + exit(EXIT_SUCCESS);
  617. +}
  618. +
  619. +__attribute__ ((always_inline))
  620. +static inline size_t add2(size_t a, size_t b)
  621. +{
  622. + size_t sum = a + b;
  623. +
  624. + if (sum < a)
  625. + errx(EXIT_FAILURE, _("integer overflow"));
  626. + return sum;
  627. +}
  628. +
  629. +__attribute__ ((always_inline))
  630. +static inline size_t add3(size_t a, size_t b, size_t c)
  631. +{
  632. + return add2(add2(a, b), c);
  633. +}
  634. +
  635. +static void growstr(struct hardlink_dynstr *str, size_t newlen)
  636. +{
  637. + if (newlen < str->alloc)
  638. + return;
  639. + str->buf = xrealloc(str->buf, str->alloc = add2(newlen, 1));
  640. +}
  641. +
  642. +static void process_path(struct hardlink_ctl *ctl, const char *name)
  643. +{
  644. + struct stat st, st2, st3;
  645. + const size_t namelen = strlen(name);
  646. +
  647. + ctl->nobjects++;
  648. + if (lstat(name, &st))
  649. + return;
  650. +
  651. + if (st.st_dev != ctl->dev && !ctl->force) {
  652. + if (ctl->dev)
  653. + errx(EXIT_FAILURE,
  654. + _("%s is on different filesystem than the rest "
  655. + "(use -f option to override)."), name);
  656. + ctl->dev = st.st_dev;
  657. + }
  658. + if (S_ISDIR(st.st_mode)) {
  659. + struct hardlink_dir *dp = xmalloc(add3(sizeof(*dp), namelen, 1));
  660. + memcpy(dp->name, name, namelen + 1);
  661. + dp->next = ctl->dirs;
  662. + ctl->dirs = dp;
  663. +
  664. + } else if (S_ISREG(st.st_mode)) {
  665. + int fd, i;
  666. + struct hardlink_file *fp, *fp2;
  667. + struct hardlink_hash *hp;
  668. + const char *n1, *n2;
  669. + unsigned int buf[NBUF];
  670. + int cksumsize = sizeof(buf);
  671. + unsigned int cksum;
  672. + time_t mtime = ctl->content_only ? 0 : st.st_mtime;
  673. + unsigned int hsh = hash(st.st_size, mtime);
  674. + off_t fsize;
  675. +
  676. + ctl->nregfiles++;
  677. + if (ctl->verbose > 1)
  678. + printf("%s\n", name);
  679. +
  680. + fd = open(name, O_RDONLY);
  681. + if (fd < 0)
  682. + return;
  683. +
  684. + if ((size_t)st.st_size < sizeof(buf)) {
  685. + cksumsize = st.st_size;
  686. + memset(((char *)buf) + cksumsize, 0,
  687. + (sizeof(buf) - cksumsize) % sizeof(buf[0]));
  688. + }
  689. + if (read(fd, buf, cksumsize) != cksumsize) {
  690. + close(fd);
  691. + return;
  692. + }
  693. + cksumsize = (cksumsize + sizeof(buf[0]) - 1) / sizeof(buf[0]);
  694. + for (i = 0, cksum = 0; i < cksumsize; i++) {
  695. + if (cksum + buf[i] < cksum)
  696. + cksum += buf[i] + 1;
  697. + else
  698. + cksum += buf[i];
  699. + }
  700. + for (hp = ctl->hps[hsh]; hp; hp = hp->next) {
  701. + if (hp->size == st.st_size && hp->mtime == mtime)
  702. + break;
  703. + }
  704. + if (!hp) {
  705. + hp = xmalloc(sizeof(*hp));
  706. + hp->size = st.st_size;
  707. + hp->mtime = mtime;
  708. + hp->chain = NULL;
  709. + hp->next = ctl->hps[hsh];
  710. + ctl->hps[hsh] = hp;
  711. + }
  712. + for (fp = hp->chain; fp; fp = fp->next) {
  713. + if (fp->cksum == cksum)
  714. + break;
  715. + }
  716. + for (fp2 = fp; fp2 && fp2->cksum == cksum; fp2 = fp2->next) {
  717. + if (fp2->ino == st.st_ino && fp2->dev == st.st_dev) {
  718. + close(fd);
  719. + return;
  720. + }
  721. + }
  722. + for (fp2 = fp; fp2 && fp2->cksum == cksum; fp2 = fp2->next) {
  723. +
  724. + if (!lstat(fp2->name, &st2) && S_ISREG(st2.st_mode) &&
  725. + !stcmp(&st, &st2, ctl->content_only) &&
  726. + st2.st_ino != st.st_ino &&
  727. + st2.st_dev == st.st_dev) {
  728. +
  729. + int fd2 = open(fp2->name, O_RDONLY);
  730. + if (fd2 < 0)
  731. + continue;
  732. +
  733. + if (fstat(fd2, &st2) || !S_ISREG(st2.st_mode)
  734. + || st2.st_size == 0) {
  735. + close(fd2);
  736. + continue;
  737. + }
  738. + ctl->ncomp++;
  739. + lseek(fd, 0, SEEK_SET);
  740. +
  741. + for (fsize = st.st_size; fsize > 0;
  742. + fsize -= (off_t)sizeof(ctl->iobuf1)) {
  743. + ssize_t xsz;
  744. + ssize_t rsize = fsize > (ssize_t) sizeof(ctl->iobuf1) ?
  745. + (ssize_t) sizeof(ctl->iobuf1) : fsize;
  746. +
  747. + if ((xsz = read(fd, ctl->iobuf1, rsize)) != rsize)
  748. + warn(_("cannot read %s"), name);
  749. + else if ((xsz = read(fd2, ctl->iobuf2, rsize)) != rsize)
  750. + warn(_("cannot read %s"), fp2->name);
  751. +
  752. + if (xsz != rsize) {
  753. + close(fd);
  754. + close(fd2);
  755. + return;
  756. + }
  757. + if (memcmp(ctl->iobuf1, ctl->iobuf2, rsize))
  758. + break;
  759. + }
  760. + close(fd2);
  761. + if (fsize > 0)
  762. + continue;
  763. + if (lstat(name, &st3)) {
  764. + warn(_("cannot stat %s"), name);
  765. + close(fd);
  766. + return;
  767. + }
  768. + st3.st_atime = st.st_atime;
  769. + if (stcmp(&st, &st3, 0)) {
  770. + warnx(_("file %s changed underneath us"), name);
  771. + close(fd);
  772. + return;
  773. + }
  774. + n1 = fp2->name;
  775. + n2 = name;
  776. +
  777. + if (!ctl->no_link) {
  778. + const char *suffix =
  779. + ".$$$___cleanit___$$$";
  780. + const size_t suffixlen = strlen(suffix);
  781. + size_t n2len = strlen(n2);
  782. + struct hardlink_dynstr nam2 = { NULL, 0 };
  783. +
  784. + growstr(&nam2, add2(n2len, suffixlen));
  785. + memcpy(nam2.buf, n2, n2len);
  786. + memcpy(&nam2.buf[n2len], suffix,
  787. + suffixlen + 1);
  788. + /* First create a temporary link to n1 under a new name */
  789. + if (link(n1, nam2.buf)) {
  790. + warn(_("failed to hardlink %s to %s (create temporary link as %s failed)"),
  791. + n1, n2, nam2.buf);
  792. + free(nam2.buf);
  793. + continue;
  794. + }
  795. + /* Then rename into place over the existing n2 */
  796. + if (rename(nam2.buf, n2)) {
  797. + warn(_("failed to hardlink %s to %s (rename temporary link to %s failed)"),
  798. + n1, n2, n2);
  799. + /* Something went wrong, try to remove the now redundant temporary link */
  800. + if (unlink(nam2.buf))
  801. + warn(_("failed to remove temporary link %s"), nam2.buf);
  802. + free(nam2.buf);
  803. + continue;
  804. + }
  805. + free(nam2.buf);
  806. + }
  807. + ctl->nlinks++;
  808. + if (st3.st_nlink > 1) {
  809. + /* We actually did not save anything this time, since the link second argument
  810. + had some other links as well. */
  811. + if (ctl->verbose > 1)
  812. + printf(_(" %s %s to %s\n"),
  813. + (ctl->no_link ? _("Would link") : _("Linked")),
  814. + n1, n2);
  815. + } else {
  816. + ctl->nsaved += ((st.st_size + 4095) / 4096) * 4096;
  817. + if (ctl->verbose > 1)
  818. + printf(_(" %s %s to %s, %s %jd\n"),
  819. + (ctl->no_link ? _("Would link") : _("Linked")),
  820. + n1, n2,
  821. + (ctl->no_link ? _("would save") : _("saved")),
  822. + (intmax_t)st.st_size);
  823. + }
  824. + close(fd);
  825. + return;
  826. + }
  827. + }
  828. + fp2 = xmalloc(add3(sizeof(*fp2), namelen, 1));
  829. + close(fd);
  830. + fp2->ino = st.st_ino;
  831. + fp2->dev = st.st_dev;
  832. + fp2->cksum = cksum;
  833. + memcpy(fp2->name, name, namelen + 1);
  834. +
  835. + if (fp) {
  836. + fp2->next = fp->next;
  837. + fp->next = fp2;
  838. + } else {
  839. + fp2->next = hp->chain;
  840. + hp->chain = fp2;
  841. + }
  842. + return;
  843. + }
  844. +}
  845. +
  846. +int main(int argc, char **argv)
  847. +{
  848. + int ch;
  849. + int i;
  850. +#ifdef HAVE_PCRE
  851. + int errornumber;
  852. + PCRE2_SIZE erroroffset;
  853. + pcre2_code *re = NULL;
  854. + PCRE2_SPTR exclude_pattern = NULL;
  855. + pcre2_match_data *match_data = NULL;
  856. +#endif
  857. + struct hardlink_dynstr nam1 = { NULL, 0 };
  858. + struct hardlink_ctl *ctl = &global_ctl;
  859. +
  860. + static const struct option longopts[] = {
  861. + { "content", no_argument, NULL, 'c' },
  862. + { "dry-run", no_argument, NULL, 'n' },
  863. + { "exclude", required_argument, NULL, 'x' },
  864. + { "force", no_argument, NULL, 'f' },
  865. + { "help", no_argument, NULL, 'h' },
  866. + { "verbose", no_argument, NULL, 'v' },
  867. + { "version", no_argument, NULL, 'V' },
  868. + { NULL, 0, NULL, 0 },
  869. + };
  870. +
  871. + setlocale(LC_ALL, "");
  872. + bindtextdomain(PACKAGE, LOCALEDIR);
  873. + textdomain(PACKAGE);
  874. + close_stdout_atexit();
  875. +
  876. + while ((ch = getopt_long(argc, argv, "cnvfx:Vh", longopts, NULL)) != -1) {
  877. + switch (ch) {
  878. + case 'n':
  879. + ctl->no_link = 1;
  880. + break;
  881. + case 'v':
  882. + ctl->verbose++;
  883. + break;
  884. + case 'c':
  885. + ctl->content_only = 1;
  886. + break;
  887. + case 'f':
  888. + ctl->force = 1;
  889. + break;
  890. + case 'x':
  891. +#ifdef HAVE_PCRE
  892. + exclude_pattern = (PCRE2_SPTR) optarg;
  893. +#else
  894. + errx(EXIT_FAILURE,
  895. + _("option --exclude not supported (built without pcre2)"));
  896. +#endif
  897. + break;
  898. + case 'V':
  899. + print_version(EXIT_SUCCESS);
  900. + case 'h':
  901. + usage();
  902. + default:
  903. + errtryhelp(EXIT_FAILURE);
  904. + }
  905. + }
  906. +
  907. + if (optind == argc) {
  908. + warnx(_("no directory specified"));
  909. + errtryhelp(EXIT_FAILURE);
  910. + }
  911. +
  912. +#ifdef HAVE_PCRE
  913. + if (exclude_pattern) {
  914. + re = pcre2_compile(exclude_pattern, /* the pattern */
  915. + PCRE2_ZERO_TERMINATED, /* indicates pattern is zero-terminate */
  916. + 0, /* default options */
  917. + &errornumber, &erroroffset, NULL); /* use default compile context */
  918. + if (!re) {
  919. + PCRE2_UCHAR buffer[256];
  920. + pcre2_get_error_message(errornumber, buffer,
  921. + sizeof(buffer));
  922. + errx(EXIT_FAILURE, _("pattern error at offset %d: %s"),
  923. + (int)erroroffset, buffer);
  924. + }
  925. + match_data = pcre2_match_data_create_from_pattern(re, NULL);
  926. + }
  927. +#endif
  928. + atexit(print_summary);
  929. +
  930. + for (i = optind; i < argc; i++)
  931. + process_path(ctl, argv[i]);
  932. +
  933. + while (ctl->dirs) {
  934. + DIR *dh;
  935. + struct dirent *di;
  936. + struct hardlink_dir *dp = ctl->dirs;
  937. + size_t nam1baselen = strlen(dp->name);
  938. +
  939. + ctl->dirs = dp->next;
  940. + growstr(&nam1, add2(nam1baselen, 1));
  941. + memcpy(nam1.buf, dp->name, nam1baselen);
  942. + free(dp);
  943. + nam1.buf[nam1baselen++] = '/';
  944. + nam1.buf[nam1baselen] = 0;
  945. + dh = opendir(nam1.buf);
  946. +
  947. + if (dh == NULL)
  948. + continue;
  949. + ctl->ndirs++;
  950. +
  951. + while ((di = readdir(dh)) != NULL) {
  952. + if (!di->d_name[0])
  953. + continue;
  954. + if (di->d_name[0] == '.') {
  955. + if (!di->d_name[1] || !strcmp(di->d_name, ".."))
  956. + continue;
  957. + }
  958. +#ifdef HAVE_PCRE
  959. + if (re && pcre2_match(re, /* compiled regex */
  960. + (PCRE2_SPTR) di->d_name, strlen(di->d_name), 0, /* start at offset 0 */
  961. + 0, /* default options */
  962. + match_data, /* block for storing the result */
  963. + NULL) /* use default match context */
  964. + >=0) {
  965. + if (ctl->verbose) {
  966. + nam1.buf[nam1baselen] = 0;
  967. + printf(_("Skipping %s%s\n"), nam1.buf, di->d_name);
  968. + }
  969. + continue;
  970. + }
  971. +#endif
  972. + {
  973. + size_t subdirlen;
  974. + growstr(&nam1,
  975. + add2(nam1baselen, subdirlen =
  976. + strlen(di->d_name)));
  977. + memcpy(&nam1.buf[nam1baselen], di->d_name,
  978. + add2(subdirlen, 1));
  979. + }
  980. + process_path(ctl, nam1.buf);
  981. + }
  982. + closedir(dh);
  983. + }
  984. +
  985. + return 0;
  986. +}
  987. diff --git a/locale/programs/xalloc.h b/locale/programs/xalloc.h
  988. new file mode 100644
  989. index 0000000000..0129a85e2e
  990. --- /dev/null
  991. +++ b/locale/programs/xalloc.h
  992. @@ -0,0 +1,129 @@
  993. +/*
  994. + * Copyright (C) 2010 Davidlohr Bueso <dave@gnu.org>
  995. + *
  996. + * This file may be redistributed under the terms of the
  997. + * GNU Lesser General Public License.
  998. + *
  999. + * General memory allocation wrappers for malloc, realloc, calloc and strdup
  1000. + */
  1001. +
  1002. +#ifndef UTIL_LINUX_XALLOC_H
  1003. +#define UTIL_LINUX_XALLOC_H
  1004. +
  1005. +#include <stdlib.h>
  1006. +#include <string.h>
  1007. +
  1008. +#include "c.h"
  1009. +
  1010. +#ifndef XALLOC_EXIT_CODE
  1011. +# define XALLOC_EXIT_CODE EXIT_FAILURE
  1012. +#endif
  1013. +
  1014. +static inline void __attribute__((__noreturn__))
  1015. +__err_oom(const char *file, unsigned int line)
  1016. +{
  1017. + err(XALLOC_EXIT_CODE, "%s: %u: cannot allocate memory", file, line);
  1018. +}
  1019. +
  1020. +#define err_oom() __err_oom(__FILE__, __LINE__)
  1021. +
  1022. +static inline __ul_alloc_size(1) __ul_returns_nonnull
  1023. +void *xmalloc(const size_t size)
  1024. +{
  1025. + void *ret = malloc(size);
  1026. +
  1027. + if (!ret && size)
  1028. + err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
  1029. + return ret;
  1030. +}
  1031. +
  1032. +static inline __ul_alloc_size(2) __ul_returns_nonnull
  1033. +void *xrealloc(void *ptr, const size_t size)
  1034. +{
  1035. + void *ret = realloc(ptr, size);
  1036. +
  1037. + if (!ret && size)
  1038. + err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
  1039. + return ret;
  1040. +}
  1041. +
  1042. +static inline __ul_calloc_size(1, 2) __ul_returns_nonnull
  1043. +void *xcalloc(const size_t nelems, const size_t size)
  1044. +{
  1045. + void *ret = calloc(nelems, size);
  1046. +
  1047. + if (!ret && size && nelems)
  1048. + err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
  1049. + return ret;
  1050. +}
  1051. +
  1052. +static inline char __attribute__((warn_unused_result)) __ul_returns_nonnull
  1053. +*xstrdup(const char *str)
  1054. +{
  1055. + char *ret;
  1056. +
  1057. + if (!str)
  1058. + return NULL;
  1059. +
  1060. + ret = strdup(str);
  1061. +
  1062. + if (!ret)
  1063. + err(XALLOC_EXIT_CODE, "cannot duplicate string");
  1064. + return ret;
  1065. +}
  1066. +
  1067. +static inline char * __attribute__((warn_unused_result)) __ul_returns_nonnull
  1068. +xstrndup(const char *str, size_t size)
  1069. +{
  1070. + char *ret;
  1071. +
  1072. + if (!str)
  1073. + return NULL;
  1074. +
  1075. + ret = strndup(str, size);
  1076. +
  1077. + if (!ret)
  1078. + err(XALLOC_EXIT_CODE, "cannot duplicate string");
  1079. + return ret;
  1080. +}
  1081. +
  1082. +
  1083. +static inline int __attribute__ ((__format__(printf, 2, 3)))
  1084. + xasprintf(char **strp, const char *fmt, ...)
  1085. +{
  1086. + int ret;
  1087. + va_list args;
  1088. + va_start(args, fmt);
  1089. + ret = vasprintf(&(*strp), fmt, args);
  1090. + va_end(args);
  1091. + if (ret < 0)
  1092. + err(XALLOC_EXIT_CODE, "cannot allocate string");
  1093. + return ret;
  1094. +}
  1095. +
  1096. +static inline int __attribute__ ((__format__(printf, 2, 0)))
  1097. +xvasprintf(char **strp, const char *fmt, va_list ap)
  1098. +{
  1099. + int ret = vasprintf(&(*strp), fmt, ap);
  1100. + if (ret < 0)
  1101. + err(XALLOC_EXIT_CODE, "cannot allocate string");
  1102. + return ret;
  1103. +}
  1104. +
  1105. +
  1106. +static inline char * __attribute__((warn_unused_result)) xgethostname(void)
  1107. +{
  1108. + char *name;
  1109. + size_t sz = get_hostname_max() + 1;
  1110. +
  1111. + name = xmalloc(sizeof(char) * sz);
  1112. +
  1113. + if (gethostname(name, sz) != 0) {
  1114. + free(name);
  1115. + return NULL;
  1116. + }
  1117. + name[sz - 1] = '\0';
  1118. + return name;
  1119. +}
  1120. +
  1121. +#endif