sha1-file.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  1. /*
  2. * GIT - The information manager from hell
  3. *
  4. * Copyright (C) Linus Torvalds, 2005
  5. *
  6. * This handles basic git sha1 object files - packing, unpacking,
  7. * creation etc.
  8. */
  9. #include "cache.h"
  10. #include "config.h"
  11. #include "string-list.h"
  12. #include "lockfile.h"
  13. #include "delta.h"
  14. #include "pack.h"
  15. #include "blob.h"
  16. #include "commit.h"
  17. #include "run-command.h"
  18. #include "tag.h"
  19. #include "tree.h"
  20. #include "tree-walk.h"
  21. #include "refs.h"
  22. #include "pack-revindex.h"
  23. #include "sha1-lookup.h"
  24. #include "bulk-checkin.h"
  25. #include "repository.h"
  26. #include "replace-object.h"
  27. #include "streaming.h"
  28. #include "dir.h"
  29. #include "list.h"
  30. #include "mergesort.h"
  31. #include "quote.h"
  32. #include "packfile.h"
  33. #include "object-store.h"
  34. #include "promisor-remote.h"
  35. /* The maximum size for an object header. */
  36. #define MAX_HEADER_LEN 32
  37. #define EMPTY_TREE_SHA1_BIN_LITERAL \
  38. "\x4b\x82\x5d\xc6\x42\xcb\x6e\xb9\xa0\x60" \
  39. "\xe5\x4b\xf8\xd6\x92\x88\xfb\xee\x49\x04"
  40. #define EMPTY_TREE_SHA256_BIN_LITERAL \
  41. "\x6e\xf1\x9b\x41\x22\x5c\x53\x69\xf1\xc1" \
  42. "\x04\xd4\x5d\x8d\x85\xef\xa9\xb0\x57\xb5" \
  43. "\x3b\x14\xb4\xb9\xb9\x39\xdd\x74\xde\xcc" \
  44. "\x53\x21"
  45. #define EMPTY_BLOB_SHA1_BIN_LITERAL \
  46. "\xe6\x9d\xe2\x9b\xb2\xd1\xd6\x43\x4b\x8b" \
  47. "\x29\xae\x77\x5a\xd8\xc2\xe4\x8c\x53\x91"
  48. #define EMPTY_BLOB_SHA256_BIN_LITERAL \
  49. "\x47\x3a\x0f\x4c\x3b\xe8\xa9\x36\x81\xa2" \
  50. "\x67\xe3\xb1\xe9\xa7\xdc\xda\x11\x85\x43" \
  51. "\x6f\xe1\x41\xf7\x74\x91\x20\xa3\x03\x72" \
  52. "\x18\x13"
  53. const struct object_id null_oid;
  54. static const struct object_id empty_tree_oid = {
  55. EMPTY_TREE_SHA1_BIN_LITERAL
  56. };
  57. static const struct object_id empty_blob_oid = {
  58. EMPTY_BLOB_SHA1_BIN_LITERAL
  59. };
  60. static const struct object_id empty_tree_oid_sha256 = {
  61. EMPTY_TREE_SHA256_BIN_LITERAL
  62. };
  63. static const struct object_id empty_blob_oid_sha256 = {
  64. EMPTY_BLOB_SHA256_BIN_LITERAL
  65. };
  66. static void git_hash_sha1_init(git_hash_ctx *ctx)
  67. {
  68. git_SHA1_Init(&ctx->sha1);
  69. }
  70. static void git_hash_sha1_clone(git_hash_ctx *dst, const git_hash_ctx *src)
  71. {
  72. git_SHA1_Clone(&dst->sha1, &src->sha1);
  73. }
  74. static void git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len)
  75. {
  76. git_SHA1_Update(&ctx->sha1, data, len);
  77. }
  78. static void git_hash_sha1_final(unsigned char *hash, git_hash_ctx *ctx)
  79. {
  80. git_SHA1_Final(hash, &ctx->sha1);
  81. }
  82. static void git_hash_sha256_init(git_hash_ctx *ctx)
  83. {
  84. git_SHA256_Init(&ctx->sha256);
  85. }
  86. static void git_hash_sha256_clone(git_hash_ctx *dst, const git_hash_ctx *src)
  87. {
  88. git_SHA256_Clone(&dst->sha256, &src->sha256);
  89. }
  90. static void git_hash_sha256_update(git_hash_ctx *ctx, const void *data, size_t len)
  91. {
  92. git_SHA256_Update(&ctx->sha256, data, len);
  93. }
  94. static void git_hash_sha256_final(unsigned char *hash, git_hash_ctx *ctx)
  95. {
  96. git_SHA256_Final(hash, &ctx->sha256);
  97. }
  98. static void git_hash_unknown_init(git_hash_ctx *ctx)
  99. {
  100. BUG("trying to init unknown hash");
  101. }
  102. static void git_hash_unknown_clone(git_hash_ctx *dst, const git_hash_ctx *src)
  103. {
  104. BUG("trying to clone unknown hash");
  105. }
  106. static void git_hash_unknown_update(git_hash_ctx *ctx, const void *data, size_t len)
  107. {
  108. BUG("trying to update unknown hash");
  109. }
  110. static void git_hash_unknown_final(unsigned char *hash, git_hash_ctx *ctx)
  111. {
  112. BUG("trying to finalize unknown hash");
  113. }
  114. const struct git_hash_algo hash_algos[GIT_HASH_NALGOS] = {
  115. {
  116. NULL,
  117. 0x00000000,
  118. 0,
  119. 0,
  120. 0,
  121. git_hash_unknown_init,
  122. git_hash_unknown_clone,
  123. git_hash_unknown_update,
  124. git_hash_unknown_final,
  125. NULL,
  126. NULL,
  127. },
  128. {
  129. "sha1",
  130. /* "sha1", big-endian */
  131. 0x73686131,
  132. GIT_SHA1_RAWSZ,
  133. GIT_SHA1_HEXSZ,
  134. GIT_SHA1_BLKSZ,
  135. git_hash_sha1_init,
  136. git_hash_sha1_clone,
  137. git_hash_sha1_update,
  138. git_hash_sha1_final,
  139. &empty_tree_oid,
  140. &empty_blob_oid,
  141. },
  142. {
  143. "sha256",
  144. /* "s256", big-endian */
  145. 0x73323536,
  146. GIT_SHA256_RAWSZ,
  147. GIT_SHA256_HEXSZ,
  148. GIT_SHA256_BLKSZ,
  149. git_hash_sha256_init,
  150. git_hash_sha256_clone,
  151. git_hash_sha256_update,
  152. git_hash_sha256_final,
  153. &empty_tree_oid_sha256,
  154. &empty_blob_oid_sha256,
  155. }
  156. };
  157. const char *empty_tree_oid_hex(void)
  158. {
  159. static char buf[GIT_MAX_HEXSZ + 1];
  160. return oid_to_hex_r(buf, the_hash_algo->empty_tree);
  161. }
  162. const char *empty_blob_oid_hex(void)
  163. {
  164. static char buf[GIT_MAX_HEXSZ + 1];
  165. return oid_to_hex_r(buf, the_hash_algo->empty_blob);
  166. }
  167. int hash_algo_by_name(const char *name)
  168. {
  169. int i;
  170. if (!name)
  171. return GIT_HASH_UNKNOWN;
  172. for (i = 1; i < GIT_HASH_NALGOS; i++)
  173. if (!strcmp(name, hash_algos[i].name))
  174. return i;
  175. return GIT_HASH_UNKNOWN;
  176. }
  177. int hash_algo_by_id(uint32_t format_id)
  178. {
  179. int i;
  180. for (i = 1; i < GIT_HASH_NALGOS; i++)
  181. if (format_id == hash_algos[i].format_id)
  182. return i;
  183. return GIT_HASH_UNKNOWN;
  184. }
  185. int hash_algo_by_length(int len)
  186. {
  187. int i;
  188. for (i = 1; i < GIT_HASH_NALGOS; i++)
  189. if (len == hash_algos[i].rawsz)
  190. return i;
  191. return GIT_HASH_UNKNOWN;
  192. }
  193. /*
  194. * This is meant to hold a *small* number of objects that you would
  195. * want read_object_file() to be able to return, but yet you do not want
  196. * to write them into the object store (e.g. a browse-only
  197. * application).
  198. */
  199. static struct cached_object {
  200. struct object_id oid;
  201. enum object_type type;
  202. void *buf;
  203. unsigned long size;
  204. } *cached_objects;
  205. static int cached_object_nr, cached_object_alloc;
  206. static struct cached_object empty_tree = {
  207. { EMPTY_TREE_SHA1_BIN_LITERAL },
  208. OBJ_TREE,
  209. "",
  210. 0
  211. };
  212. static struct cached_object *find_cached_object(const struct object_id *oid)
  213. {
  214. int i;
  215. struct cached_object *co = cached_objects;
  216. for (i = 0; i < cached_object_nr; i++, co++) {
  217. if (oideq(&co->oid, oid))
  218. return co;
  219. }
  220. if (oideq(oid, the_hash_algo->empty_tree))
  221. return &empty_tree;
  222. return NULL;
  223. }
  224. static int get_conv_flags(unsigned flags)
  225. {
  226. if (flags & HASH_RENORMALIZE)
  227. return CONV_EOL_RENORMALIZE;
  228. else if (flags & HASH_WRITE_OBJECT)
  229. return global_conv_flags_eol | CONV_WRITE_OBJECT;
  230. else
  231. return 0;
  232. }
  233. int mkdir_in_gitdir(const char *path)
  234. {
  235. if (mkdir(path, 0777)) {
  236. int saved_errno = errno;
  237. struct stat st;
  238. struct strbuf sb = STRBUF_INIT;
  239. if (errno != EEXIST)
  240. return -1;
  241. /*
  242. * Are we looking at a path in a symlinked worktree
  243. * whose original repository does not yet have it?
  244. * e.g. .git/rr-cache pointing at its original
  245. * repository in which the user hasn't performed any
  246. * conflict resolution yet?
  247. */
  248. if (lstat(path, &st) || !S_ISLNK(st.st_mode) ||
  249. strbuf_readlink(&sb, path, st.st_size) ||
  250. !is_absolute_path(sb.buf) ||
  251. mkdir(sb.buf, 0777)) {
  252. strbuf_release(&sb);
  253. errno = saved_errno;
  254. return -1;
  255. }
  256. strbuf_release(&sb);
  257. }
  258. return adjust_shared_perm(path);
  259. }
  260. enum scld_error safe_create_leading_directories(char *path)
  261. {
  262. char *next_component = path + offset_1st_component(path);
  263. enum scld_error ret = SCLD_OK;
  264. while (ret == SCLD_OK && next_component) {
  265. struct stat st;
  266. char *slash = next_component, slash_character;
  267. while (*slash && !is_dir_sep(*slash))
  268. slash++;
  269. if (!*slash)
  270. break;
  271. next_component = slash + 1;
  272. while (is_dir_sep(*next_component))
  273. next_component++;
  274. if (!*next_component)
  275. break;
  276. slash_character = *slash;
  277. *slash = '\0';
  278. if (!stat(path, &st)) {
  279. /* path exists */
  280. if (!S_ISDIR(st.st_mode)) {
  281. errno = ENOTDIR;
  282. ret = SCLD_EXISTS;
  283. }
  284. } else if (mkdir(path, 0777)) {
  285. if (errno == EEXIST &&
  286. !stat(path, &st) && S_ISDIR(st.st_mode))
  287. ; /* somebody created it since we checked */
  288. else if (errno == ENOENT)
  289. /*
  290. * Either mkdir() failed because
  291. * somebody just pruned the containing
  292. * directory, or stat() failed because
  293. * the file that was in our way was
  294. * just removed. Either way, inform
  295. * the caller that it might be worth
  296. * trying again:
  297. */
  298. ret = SCLD_VANISHED;
  299. else
  300. ret = SCLD_FAILED;
  301. } else if (adjust_shared_perm(path)) {
  302. ret = SCLD_PERMS;
  303. }
  304. *slash = slash_character;
  305. }
  306. return ret;
  307. }
  308. enum scld_error safe_create_leading_directories_const(const char *path)
  309. {
  310. int save_errno;
  311. /* path points to cache entries, so xstrdup before messing with it */
  312. char *buf = xstrdup(path);
  313. enum scld_error result = safe_create_leading_directories(buf);
  314. save_errno = errno;
  315. free(buf);
  316. errno = save_errno;
  317. return result;
  318. }
  319. int raceproof_create_file(const char *path, create_file_fn fn, void *cb)
  320. {
  321. /*
  322. * The number of times we will try to remove empty directories
  323. * in the way of path. This is only 1 because if another
  324. * process is racily creating directories that conflict with
  325. * us, we don't want to fight against them.
  326. */
  327. int remove_directories_remaining = 1;
  328. /*
  329. * The number of times that we will try to create the
  330. * directories containing path. We are willing to attempt this
  331. * more than once, because another process could be trying to
  332. * clean up empty directories at the same time as we are
  333. * trying to create them.
  334. */
  335. int create_directories_remaining = 3;
  336. /* A scratch copy of path, filled lazily if we need it: */
  337. struct strbuf path_copy = STRBUF_INIT;
  338. int ret, save_errno;
  339. /* Sanity check: */
  340. assert(*path);
  341. retry_fn:
  342. ret = fn(path, cb);
  343. save_errno = errno;
  344. if (!ret)
  345. goto out;
  346. if (errno == EISDIR && remove_directories_remaining-- > 0) {
  347. /*
  348. * A directory is in the way. Maybe it is empty; try
  349. * to remove it:
  350. */
  351. if (!path_copy.len)
  352. strbuf_addstr(&path_copy, path);
  353. if (!remove_dir_recursively(&path_copy, REMOVE_DIR_EMPTY_ONLY))
  354. goto retry_fn;
  355. } else if (errno == ENOENT && create_directories_remaining-- > 0) {
  356. /*
  357. * Maybe the containing directory didn't exist, or
  358. * maybe it was just deleted by a process that is
  359. * racing with us to clean up empty directories. Try
  360. * to create it:
  361. */
  362. enum scld_error scld_result;
  363. if (!path_copy.len)
  364. strbuf_addstr(&path_copy, path);
  365. do {
  366. scld_result = safe_create_leading_directories(path_copy.buf);
  367. if (scld_result == SCLD_OK)
  368. goto retry_fn;
  369. } while (scld_result == SCLD_VANISHED && create_directories_remaining-- > 0);
  370. }
  371. out:
  372. strbuf_release(&path_copy);
  373. errno = save_errno;
  374. return ret;
  375. }
  376. static void fill_loose_path(struct strbuf *buf, const struct object_id *oid)
  377. {
  378. int i;
  379. for (i = 0; i < the_hash_algo->rawsz; i++) {
  380. static char hex[] = "0123456789abcdef";
  381. unsigned int val = oid->hash[i];
  382. strbuf_addch(buf, hex[val >> 4]);
  383. strbuf_addch(buf, hex[val & 0xf]);
  384. if (!i)
  385. strbuf_addch(buf, '/');
  386. }
  387. }
  388. static const char *odb_loose_path(struct object_directory *odb,
  389. struct strbuf *buf,
  390. const struct object_id *oid)
  391. {
  392. strbuf_reset(buf);
  393. strbuf_addstr(buf, odb->path);
  394. strbuf_addch(buf, '/');
  395. fill_loose_path(buf, oid);
  396. return buf->buf;
  397. }
  398. const char *loose_object_path(struct repository *r, struct strbuf *buf,
  399. const struct object_id *oid)
  400. {
  401. return odb_loose_path(r->objects->odb, buf, oid);
  402. }
  403. /*
  404. * Return non-zero iff the path is usable as an alternate object database.
  405. */
  406. static int alt_odb_usable(struct raw_object_store *o,
  407. struct strbuf *path,
  408. const char *normalized_objdir)
  409. {
  410. struct object_directory *odb;
  411. /* Detect cases where alternate disappeared */
  412. if (!is_directory(path->buf)) {
  413. error(_("object directory %s does not exist; "
  414. "check .git/objects/info/alternates"),
  415. path->buf);
  416. return 0;
  417. }
  418. /*
  419. * Prevent the common mistake of listing the same
  420. * thing twice, or object directory itself.
  421. */
  422. for (odb = o->odb; odb; odb = odb->next) {
  423. if (!fspathcmp(path->buf, odb->path))
  424. return 0;
  425. }
  426. if (!fspathcmp(path->buf, normalized_objdir))
  427. return 0;
  428. return 1;
  429. }
  430. /*
  431. * Prepare alternate object database registry.
  432. *
  433. * The variable alt_odb_list points at the list of struct
  434. * object_directory. The elements on this list come from
  435. * non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT
  436. * environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates,
  437. * whose contents is similar to that environment variable but can be
  438. * LF separated. Its base points at a statically allocated buffer that
  439. * contains "/the/directory/corresponding/to/.git/objects/...", while
  440. * its name points just after the slash at the end of ".git/objects/"
  441. * in the example above, and has enough space to hold 40-byte hex
  442. * SHA1, an extra slash for the first level indirection, and the
  443. * terminating NUL.
  444. */
  445. static void read_info_alternates(struct repository *r,
  446. const char *relative_base,
  447. int depth);
  448. static int link_alt_odb_entry(struct repository *r, const char *entry,
  449. const char *relative_base, int depth, const char *normalized_objdir)
  450. {
  451. struct object_directory *ent;
  452. struct strbuf pathbuf = STRBUF_INIT;
  453. if (!is_absolute_path(entry) && relative_base) {
  454. strbuf_realpath(&pathbuf, relative_base, 1);
  455. strbuf_addch(&pathbuf, '/');
  456. }
  457. strbuf_addstr(&pathbuf, entry);
  458. if (strbuf_normalize_path(&pathbuf) < 0 && relative_base) {
  459. error(_("unable to normalize alternate object path: %s"),
  460. pathbuf.buf);
  461. strbuf_release(&pathbuf);
  462. return -1;
  463. }
  464. /*
  465. * The trailing slash after the directory name is given by
  466. * this function at the end. Remove duplicates.
  467. */
  468. while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
  469. strbuf_setlen(&pathbuf, pathbuf.len - 1);
  470. if (!alt_odb_usable(r->objects, &pathbuf, normalized_objdir)) {
  471. strbuf_release(&pathbuf);
  472. return -1;
  473. }
  474. ent = xcalloc(1, sizeof(*ent));
  475. ent->path = xstrdup(pathbuf.buf);
  476. /* add the alternate entry */
  477. *r->objects->odb_tail = ent;
  478. r->objects->odb_tail = &(ent->next);
  479. ent->next = NULL;
  480. /* recursively add alternates */
  481. read_info_alternates(r, pathbuf.buf, depth + 1);
  482. strbuf_release(&pathbuf);
  483. return 0;
  484. }
  485. static const char *parse_alt_odb_entry(const char *string,
  486. int sep,
  487. struct strbuf *out)
  488. {
  489. const char *end;
  490. strbuf_reset(out);
  491. if (*string == '#') {
  492. /* comment; consume up to next separator */
  493. end = strchrnul(string, sep);
  494. } else if (*string == '"' && !unquote_c_style(out, string, &end)) {
  495. /*
  496. * quoted path; unquote_c_style has copied the
  497. * data for us and set "end". Broken quoting (e.g.,
  498. * an entry that doesn't end with a quote) falls
  499. * back to the unquoted case below.
  500. */
  501. } else {
  502. /* normal, unquoted path */
  503. end = strchrnul(string, sep);
  504. strbuf_add(out, string, end - string);
  505. }
  506. if (*end)
  507. end++;
  508. return end;
  509. }
  510. static void link_alt_odb_entries(struct repository *r, const char *alt,
  511. int sep, const char *relative_base, int depth)
  512. {
  513. struct strbuf objdirbuf = STRBUF_INIT;
  514. struct strbuf entry = STRBUF_INIT;
  515. if (!alt || !*alt)
  516. return;
  517. if (depth > 5) {
  518. error(_("%s: ignoring alternate object stores, nesting too deep"),
  519. relative_base);
  520. return;
  521. }
  522. strbuf_add_absolute_path(&objdirbuf, r->objects->odb->path);
  523. if (strbuf_normalize_path(&objdirbuf) < 0)
  524. die(_("unable to normalize object directory: %s"),
  525. objdirbuf.buf);
  526. while (*alt) {
  527. alt = parse_alt_odb_entry(alt, sep, &entry);
  528. if (!entry.len)
  529. continue;
  530. link_alt_odb_entry(r, entry.buf,
  531. relative_base, depth, objdirbuf.buf);
  532. }
  533. strbuf_release(&entry);
  534. strbuf_release(&objdirbuf);
  535. }
  536. static void read_info_alternates(struct repository *r,
  537. const char *relative_base,
  538. int depth)
  539. {
  540. char *path;
  541. struct strbuf buf = STRBUF_INIT;
  542. path = xstrfmt("%s/info/alternates", relative_base);
  543. if (strbuf_read_file(&buf, path, 1024) < 0) {
  544. warn_on_fopen_errors(path);
  545. free(path);
  546. return;
  547. }
  548. link_alt_odb_entries(r, buf.buf, '\n', relative_base, depth);
  549. strbuf_release(&buf);
  550. free(path);
  551. }
  552. void add_to_alternates_file(const char *reference)
  553. {
  554. struct lock_file lock = LOCK_INIT;
  555. char *alts = git_pathdup("objects/info/alternates");
  556. FILE *in, *out;
  557. int found = 0;
  558. hold_lock_file_for_update(&lock, alts, LOCK_DIE_ON_ERROR);
  559. out = fdopen_lock_file(&lock, "w");
  560. if (!out)
  561. die_errno(_("unable to fdopen alternates lockfile"));
  562. in = fopen(alts, "r");
  563. if (in) {
  564. struct strbuf line = STRBUF_INIT;
  565. while (strbuf_getline(&line, in) != EOF) {
  566. if (!strcmp(reference, line.buf)) {
  567. found = 1;
  568. break;
  569. }
  570. fprintf_or_die(out, "%s\n", line.buf);
  571. }
  572. strbuf_release(&line);
  573. fclose(in);
  574. }
  575. else if (errno != ENOENT)
  576. die_errno(_("unable to read alternates file"));
  577. if (found) {
  578. rollback_lock_file(&lock);
  579. } else {
  580. fprintf_or_die(out, "%s\n", reference);
  581. if (commit_lock_file(&lock))
  582. die_errno(_("unable to move new alternates file into place"));
  583. if (the_repository->objects->loaded_alternates)
  584. link_alt_odb_entries(the_repository, reference,
  585. '\n', NULL, 0);
  586. }
  587. free(alts);
  588. }
  589. void add_to_alternates_memory(const char *reference)
  590. {
  591. /*
  592. * Make sure alternates are initialized, or else our entry may be
  593. * overwritten when they are.
  594. */
  595. prepare_alt_odb(the_repository);
  596. link_alt_odb_entries(the_repository, reference,
  597. '\n', NULL, 0);
  598. }
  599. /*
  600. * Compute the exact path an alternate is at and returns it. In case of
  601. * error NULL is returned and the human readable error is added to `err`
  602. * `path` may be relative and should point to $GIT_DIR.
  603. * `err` must not be null.
  604. */
  605. char *compute_alternate_path(const char *path, struct strbuf *err)
  606. {
  607. char *ref_git = NULL;
  608. const char *repo;
  609. int seen_error = 0;
  610. ref_git = real_pathdup(path, 0);
  611. if (!ref_git) {
  612. seen_error = 1;
  613. strbuf_addf(err, _("path '%s' does not exist"), path);
  614. goto out;
  615. }
  616. repo = read_gitfile(ref_git);
  617. if (!repo)
  618. repo = read_gitfile(mkpath("%s/.git", ref_git));
  619. if (repo) {
  620. free(ref_git);
  621. ref_git = xstrdup(repo);
  622. }
  623. if (!repo && is_directory(mkpath("%s/.git/objects", ref_git))) {
  624. char *ref_git_git = mkpathdup("%s/.git", ref_git);
  625. free(ref_git);
  626. ref_git = ref_git_git;
  627. } else if (!is_directory(mkpath("%s/objects", ref_git))) {
  628. struct strbuf sb = STRBUF_INIT;
  629. seen_error = 1;
  630. if (get_common_dir(&sb, ref_git)) {
  631. strbuf_addf(err,
  632. _("reference repository '%s' as a linked "
  633. "checkout is not supported yet."),
  634. path);
  635. goto out;
  636. }
  637. strbuf_addf(err, _("reference repository '%s' is not a "
  638. "local repository."), path);
  639. goto out;
  640. }
  641. if (!access(mkpath("%s/shallow", ref_git), F_OK)) {
  642. strbuf_addf(err, _("reference repository '%s' is shallow"),
  643. path);
  644. seen_error = 1;
  645. goto out;
  646. }
  647. if (!access(mkpath("%s/info/grafts", ref_git), F_OK)) {
  648. strbuf_addf(err,
  649. _("reference repository '%s' is grafted"),
  650. path);
  651. seen_error = 1;
  652. goto out;
  653. }
  654. out:
  655. if (seen_error) {
  656. FREE_AND_NULL(ref_git);
  657. }
  658. return ref_git;
  659. }
  660. static void fill_alternate_refs_command(struct child_process *cmd,
  661. const char *repo_path)
  662. {
  663. const char *value;
  664. if (!git_config_get_value("core.alternateRefsCommand", &value)) {
  665. cmd->use_shell = 1;
  666. strvec_push(&cmd->args, value);
  667. strvec_push(&cmd->args, repo_path);
  668. } else {
  669. cmd->git_cmd = 1;
  670. strvec_pushf(&cmd->args, "--git-dir=%s", repo_path);
  671. strvec_push(&cmd->args, "for-each-ref");
  672. strvec_push(&cmd->args, "--format=%(objectname)");
  673. if (!git_config_get_value("core.alternateRefsPrefixes", &value)) {
  674. strvec_push(&cmd->args, "--");
  675. strvec_split(&cmd->args, value);
  676. }
  677. }
  678. cmd->env = local_repo_env;
  679. cmd->out = -1;
  680. }
  681. static void read_alternate_refs(const char *path,
  682. alternate_ref_fn *cb,
  683. void *data)
  684. {
  685. struct child_process cmd = CHILD_PROCESS_INIT;
  686. struct strbuf line = STRBUF_INIT;
  687. FILE *fh;
  688. fill_alternate_refs_command(&cmd, path);
  689. if (start_command(&cmd))
  690. return;
  691. fh = xfdopen(cmd.out, "r");
  692. while (strbuf_getline_lf(&line, fh) != EOF) {
  693. struct object_id oid;
  694. const char *p;
  695. if (parse_oid_hex(line.buf, &oid, &p) || *p) {
  696. warning(_("invalid line while parsing alternate refs: %s"),
  697. line.buf);
  698. break;
  699. }
  700. cb(&oid, data);
  701. }
  702. fclose(fh);
  703. finish_command(&cmd);
  704. strbuf_release(&line);
  705. }
  706. struct alternate_refs_data {
  707. alternate_ref_fn *fn;
  708. void *data;
  709. };
  710. static int refs_from_alternate_cb(struct object_directory *e,
  711. void *data)
  712. {
  713. struct strbuf path = STRBUF_INIT;
  714. size_t base_len;
  715. struct alternate_refs_data *cb = data;
  716. if (!strbuf_realpath(&path, e->path, 0))
  717. goto out;
  718. if (!strbuf_strip_suffix(&path, "/objects"))
  719. goto out;
  720. base_len = path.len;
  721. /* Is this a git repository with refs? */
  722. strbuf_addstr(&path, "/refs");
  723. if (!is_directory(path.buf))
  724. goto out;
  725. strbuf_setlen(&path, base_len);
  726. read_alternate_refs(path.buf, cb->fn, cb->data);
  727. out:
  728. strbuf_release(&path);
  729. return 0;
  730. }
  731. void for_each_alternate_ref(alternate_ref_fn fn, void *data)
  732. {
  733. struct alternate_refs_data cb;
  734. cb.fn = fn;
  735. cb.data = data;
  736. foreach_alt_odb(refs_from_alternate_cb, &cb);
  737. }
  738. int foreach_alt_odb(alt_odb_fn fn, void *cb)
  739. {
  740. struct object_directory *ent;
  741. int r = 0;
  742. prepare_alt_odb(the_repository);
  743. for (ent = the_repository->objects->odb->next; ent; ent = ent->next) {
  744. r = fn(ent, cb);
  745. if (r)
  746. break;
  747. }
  748. return r;
  749. }
  750. void prepare_alt_odb(struct repository *r)
  751. {
  752. if (r->objects->loaded_alternates)
  753. return;
  754. link_alt_odb_entries(r, r->objects->alternate_db, PATH_SEP, NULL, 0);
  755. read_info_alternates(r, r->objects->odb->path, 0);
  756. r->objects->loaded_alternates = 1;
  757. }
  758. /* Returns 1 if we have successfully freshened the file, 0 otherwise. */
  759. static int freshen_file(const char *fn)
  760. {
  761. return !utime(fn, NULL);
  762. }
  763. /*
  764. * All of the check_and_freshen functions return 1 if the file exists and was
  765. * freshened (if freshening was requested), 0 otherwise. If they return
  766. * 0, you should not assume that it is safe to skip a write of the object (it
  767. * either does not exist on disk, or has a stale mtime and may be subject to
  768. * pruning).
  769. */
  770. int check_and_freshen_file(const char *fn, int freshen)
  771. {
  772. if (access(fn, F_OK))
  773. return 0;
  774. if (freshen && !freshen_file(fn))
  775. return 0;
  776. return 1;
  777. }
  778. static int check_and_freshen_odb(struct object_directory *odb,
  779. const struct object_id *oid,
  780. int freshen)
  781. {
  782. static struct strbuf path = STRBUF_INIT;
  783. odb_loose_path(odb, &path, oid);
  784. return check_and_freshen_file(path.buf, freshen);
  785. }
  786. static int check_and_freshen_local(const struct object_id *oid, int freshen)
  787. {
  788. return check_and_freshen_odb(the_repository->objects->odb, oid, freshen);
  789. }
  790. static int check_and_freshen_nonlocal(const struct object_id *oid, int freshen)
  791. {
  792. struct object_directory *odb;
  793. prepare_alt_odb(the_repository);
  794. for (odb = the_repository->objects->odb->next; odb; odb = odb->next) {
  795. if (check_and_freshen_odb(odb, oid, freshen))
  796. return 1;
  797. }
  798. return 0;
  799. }
  800. static int check_and_freshen(const struct object_id *oid, int freshen)
  801. {
  802. return check_and_freshen_local(oid, freshen) ||
  803. check_and_freshen_nonlocal(oid, freshen);
  804. }
  805. int has_loose_object_nonlocal(const struct object_id *oid)
  806. {
  807. return check_and_freshen_nonlocal(oid, 0);
  808. }
  809. static int has_loose_object(const struct object_id *oid)
  810. {
  811. return check_and_freshen(oid, 0);
  812. }
  813. static void mmap_limit_check(size_t length)
  814. {
  815. static size_t limit = 0;
  816. if (!limit) {
  817. limit = git_env_ulong("GIT_MMAP_LIMIT", 0);
  818. if (!limit)
  819. limit = SIZE_MAX;
  820. }
  821. if (length > limit)
  822. die(_("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX),
  823. (uintmax_t)length, (uintmax_t)limit);
  824. }
  825. void *xmmap_gently(void *start, size_t length,
  826. int prot, int flags, int fd, off_t offset)
  827. {
  828. void *ret;
  829. mmap_limit_check(length);
  830. ret = mmap(start, length, prot, flags, fd, offset);
  831. if (ret == MAP_FAILED && !length)
  832. ret = NULL;
  833. return ret;
  834. }
  835. void *xmmap(void *start, size_t length,
  836. int prot, int flags, int fd, off_t offset)
  837. {
  838. void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
  839. if (ret == MAP_FAILED)
  840. die_errno(_("mmap failed"));
  841. return ret;
  842. }
  843. /*
  844. * With an in-core object data in "map", rehash it to make sure the
  845. * object name actually matches "oid" to detect object corruption.
  846. * With "map" == NULL, try reading the object named with "oid" using
  847. * the streaming interface and rehash it to do the same.
  848. */
  849. int check_object_signature(struct repository *r, const struct object_id *oid,
  850. void *map, unsigned long size, const char *type)
  851. {
  852. struct object_id real_oid;
  853. enum object_type obj_type;
  854. struct git_istream *st;
  855. git_hash_ctx c;
  856. char hdr[MAX_HEADER_LEN];
  857. int hdrlen;
  858. if (map) {
  859. hash_object_file(r->hash_algo, map, size, type, &real_oid);
  860. return !oideq(oid, &real_oid) ? -1 : 0;
  861. }
  862. st = open_istream(r, oid, &obj_type, &size, NULL);
  863. if (!st)
  864. return -1;
  865. /* Generate the header */
  866. hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(obj_type), (uintmax_t)size) + 1;
  867. /* Sha1.. */
  868. r->hash_algo->init_fn(&c);
  869. r->hash_algo->update_fn(&c, hdr, hdrlen);
  870. for (;;) {
  871. char buf[1024 * 16];
  872. ssize_t readlen = read_istream(st, buf, sizeof(buf));
  873. if (readlen < 0) {
  874. close_istream(st);
  875. return -1;
  876. }
  877. if (!readlen)
  878. break;
  879. r->hash_algo->update_fn(&c, buf, readlen);
  880. }
  881. r->hash_algo->final_fn(real_oid.hash, &c);
  882. close_istream(st);
  883. return !oideq(oid, &real_oid) ? -1 : 0;
  884. }
  885. int git_open_cloexec(const char *name, int flags)
  886. {
  887. int fd;
  888. static int o_cloexec = O_CLOEXEC;
  889. fd = open(name, flags | o_cloexec);
  890. if ((o_cloexec & O_CLOEXEC) && fd < 0 && errno == EINVAL) {
  891. /* Try again w/o O_CLOEXEC: the kernel might not support it */
  892. o_cloexec &= ~O_CLOEXEC;
  893. fd = open(name, flags | o_cloexec);
  894. }
  895. #if defined(F_GETFD) && defined(F_SETFD) && defined(FD_CLOEXEC)
  896. {
  897. static int fd_cloexec = FD_CLOEXEC;
  898. if (!o_cloexec && 0 <= fd && fd_cloexec) {
  899. /* Opened w/o O_CLOEXEC? try with fcntl(2) to add it */
  900. int flags = fcntl(fd, F_GETFD);
  901. if (fcntl(fd, F_SETFD, flags | fd_cloexec))
  902. fd_cloexec = 0;
  903. }
  904. }
  905. #endif
  906. return fd;
  907. }
  908. /*
  909. * Find "oid" as a loose object in the local repository or in an alternate.
  910. * Returns 0 on success, negative on failure.
  911. *
  912. * The "path" out-parameter will give the path of the object we found (if any).
  913. * Note that it may point to static storage and is only valid until another
  914. * call to stat_loose_object().
  915. */
  916. static int stat_loose_object(struct repository *r, const struct object_id *oid,
  917. struct stat *st, const char **path)
  918. {
  919. struct object_directory *odb;
  920. static struct strbuf buf = STRBUF_INIT;
  921. prepare_alt_odb(r);
  922. for (odb = r->objects->odb; odb; odb = odb->next) {
  923. *path = odb_loose_path(odb, &buf, oid);
  924. if (!lstat(*path, st))
  925. return 0;
  926. }
  927. return -1;
  928. }
  929. /*
  930. * Like stat_loose_object(), but actually open the object and return the
  931. * descriptor. See the caveats on the "path" parameter above.
  932. */
  933. static int open_loose_object(struct repository *r,
  934. const struct object_id *oid, const char **path)
  935. {
  936. int fd;
  937. struct object_directory *odb;
  938. int most_interesting_errno = ENOENT;
  939. static struct strbuf buf = STRBUF_INIT;
  940. prepare_alt_odb(r);
  941. for (odb = r->objects->odb; odb; odb = odb->next) {
  942. *path = odb_loose_path(odb, &buf, oid);
  943. fd = git_open(*path);
  944. if (fd >= 0)
  945. return fd;
  946. if (most_interesting_errno == ENOENT)
  947. most_interesting_errno = errno;
  948. }
  949. errno = most_interesting_errno;
  950. return -1;
  951. }
  952. static int quick_has_loose(struct repository *r,
  953. const struct object_id *oid)
  954. {
  955. struct object_directory *odb;
  956. prepare_alt_odb(r);
  957. for (odb = r->objects->odb; odb; odb = odb->next) {
  958. if (oid_array_lookup(odb_loose_cache(odb, oid), oid) >= 0)
  959. return 1;
  960. }
  961. return 0;
  962. }
  963. /*
  964. * Map the loose object at "path" if it is not NULL, or the path found by
  965. * searching for a loose object named "oid".
  966. */
  967. static void *map_loose_object_1(struct repository *r, const char *path,
  968. const struct object_id *oid, unsigned long *size)
  969. {
  970. void *map;
  971. int fd;
  972. if (path)
  973. fd = git_open(path);
  974. else
  975. fd = open_loose_object(r, oid, &path);
  976. map = NULL;
  977. if (fd >= 0) {
  978. struct stat st;
  979. if (!fstat(fd, &st)) {
  980. *size = xsize_t(st.st_size);
  981. if (!*size) {
  982. /* mmap() is forbidden on empty files */
  983. error(_("object file %s is empty"), path);
  984. close(fd);
  985. return NULL;
  986. }
  987. map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
  988. }
  989. close(fd);
  990. }
  991. return map;
  992. }
  993. void *map_loose_object(struct repository *r,
  994. const struct object_id *oid,
  995. unsigned long *size)
  996. {
  997. return map_loose_object_1(r, NULL, oid, size);
  998. }
  999. static int unpack_loose_short_header(git_zstream *stream,
  1000. unsigned char *map, unsigned long mapsize,
  1001. void *buffer, unsigned long bufsiz)
  1002. {
  1003. int ret;
  1004. /* Get the data stream */
  1005. memset(stream, 0, sizeof(*stream));
  1006. stream->next_in = map;
  1007. stream->avail_in = mapsize;
  1008. stream->next_out = buffer;
  1009. stream->avail_out = bufsiz;
  1010. git_inflate_init(stream);
  1011. obj_read_unlock();
  1012. ret = git_inflate(stream, 0);
  1013. obj_read_lock();
  1014. return ret;
  1015. }
  1016. int unpack_loose_header(git_zstream *stream,
  1017. unsigned char *map, unsigned long mapsize,
  1018. void *buffer, unsigned long bufsiz)
  1019. {
  1020. int status = unpack_loose_short_header(stream, map, mapsize,
  1021. buffer, bufsiz);
  1022. if (status < Z_OK)
  1023. return status;
  1024. /* Make sure we have the terminating NUL */
  1025. if (!memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
  1026. return -1;
  1027. return 0;
  1028. }
  1029. static int unpack_loose_header_to_strbuf(git_zstream *stream, unsigned char *map,
  1030. unsigned long mapsize, void *buffer,
  1031. unsigned long bufsiz, struct strbuf *header)
  1032. {
  1033. int status;
  1034. status = unpack_loose_short_header(stream, map, mapsize, buffer, bufsiz);
  1035. if (status < Z_OK)
  1036. return -1;
  1037. /*
  1038. * Check if entire header is unpacked in the first iteration.
  1039. */
  1040. if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
  1041. return 0;
  1042. /*
  1043. * buffer[0..bufsiz] was not large enough. Copy the partial
  1044. * result out to header, and then append the result of further
  1045. * reading the stream.
  1046. */
  1047. strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
  1048. stream->next_out = buffer;
  1049. stream->avail_out = bufsiz;
  1050. do {
  1051. obj_read_unlock();
  1052. status = git_inflate(stream, 0);
  1053. obj_read_lock();
  1054. strbuf_add(header, buffer, stream->next_out - (unsigned char *)buffer);
  1055. if (memchr(buffer, '\0', stream->next_out - (unsigned char *)buffer))
  1056. return 0;
  1057. stream->next_out = buffer;
  1058. stream->avail_out = bufsiz;
  1059. } while (status != Z_STREAM_END);
  1060. return -1;
  1061. }
  1062. static void *unpack_loose_rest(git_zstream *stream,
  1063. void *buffer, unsigned long size,
  1064. const struct object_id *oid)
  1065. {
  1066. int bytes = strlen(buffer) + 1;
  1067. unsigned char *buf = xmallocz(size);
  1068. unsigned long n;
  1069. int status = Z_OK;
  1070. n = stream->total_out - bytes;
  1071. if (n > size)
  1072. n = size;
  1073. memcpy(buf, (char *) buffer + bytes, n);
  1074. bytes = n;
  1075. if (bytes <= size) {
  1076. /*
  1077. * The above condition must be (bytes <= size), not
  1078. * (bytes < size). In other words, even though we
  1079. * expect no more output and set avail_out to zero,
  1080. * the input zlib stream may have bytes that express
  1081. * "this concludes the stream", and we *do* want to
  1082. * eat that input.
  1083. *
  1084. * Otherwise we would not be able to test that we
  1085. * consumed all the input to reach the expected size;
  1086. * we also want to check that zlib tells us that all
  1087. * went well with status == Z_STREAM_END at the end.
  1088. */
  1089. stream->next_out = buf + bytes;
  1090. stream->avail_out = size - bytes;
  1091. while (status == Z_OK) {
  1092. obj_read_unlock();
  1093. status = git_inflate(stream, Z_FINISH);
  1094. obj_read_lock();
  1095. }
  1096. }
  1097. if (status == Z_STREAM_END && !stream->avail_in) {
  1098. git_inflate_end(stream);
  1099. return buf;
  1100. }
  1101. if (status < 0)
  1102. error(_("corrupt loose object '%s'"), oid_to_hex(oid));
  1103. else if (stream->avail_in)
  1104. error(_("garbage at end of loose object '%s'"),
  1105. oid_to_hex(oid));
  1106. free(buf);
  1107. return NULL;
  1108. }
  1109. /*
  1110. * We used to just use "sscanf()", but that's actually way
  1111. * too permissive for what we want to check. So do an anal
  1112. * object header parse by hand.
  1113. */
  1114. static int parse_loose_header_extended(const char *hdr, struct object_info *oi,
  1115. unsigned int flags)
  1116. {
  1117. const char *type_buf = hdr;
  1118. unsigned long size;
  1119. int type, type_len = 0;
  1120. /*
  1121. * The type can be of any size but is followed by
  1122. * a space.
  1123. */
  1124. for (;;) {
  1125. char c = *hdr++;
  1126. if (!c)
  1127. return -1;
  1128. if (c == ' ')
  1129. break;
  1130. type_len++;
  1131. }
  1132. type = type_from_string_gently(type_buf, type_len, 1);
  1133. if (oi->type_name)
  1134. strbuf_add(oi->type_name, type_buf, type_len);
  1135. /*
  1136. * Set type to 0 if its an unknown object and
  1137. * we're obtaining the type using '--allow-unknown-type'
  1138. * option.
  1139. */
  1140. if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE) && (type < 0))
  1141. type = 0;
  1142. else if (type < 0)
  1143. die(_("invalid object type"));
  1144. if (oi->typep)
  1145. *oi->typep = type;
  1146. /*
  1147. * The length must follow immediately, and be in canonical
  1148. * decimal format (ie "010" is not valid).
  1149. */
  1150. size = *hdr++ - '0';
  1151. if (size > 9)
  1152. return -1;
  1153. if (size) {
  1154. for (;;) {
  1155. unsigned long c = *hdr - '0';
  1156. if (c > 9)
  1157. break;
  1158. hdr++;
  1159. size = size * 10 + c;
  1160. }
  1161. }
  1162. if (oi->sizep)
  1163. *oi->sizep = size;
  1164. /*
  1165. * The length must be followed by a zero byte
  1166. */
  1167. return *hdr ? -1 : type;
  1168. }
  1169. int parse_loose_header(const char *hdr, unsigned long *sizep)
  1170. {
  1171. struct object_info oi = OBJECT_INFO_INIT;
  1172. oi.sizep = sizep;
  1173. return parse_loose_header_extended(hdr, &oi, 0);
  1174. }
  1175. static int loose_object_info(struct repository *r,
  1176. const struct object_id *oid,
  1177. struct object_info *oi, int flags)
  1178. {
  1179. int status = 0;
  1180. unsigned long mapsize;
  1181. void *map;
  1182. git_zstream stream;
  1183. char hdr[MAX_HEADER_LEN];
  1184. struct strbuf hdrbuf = STRBUF_INIT;
  1185. unsigned long size_scratch;
  1186. if (oi->delta_base_oid)
  1187. oidclr(oi->delta_base_oid);
  1188. /*
  1189. * If we don't care about type or size, then we don't
  1190. * need to look inside the object at all. Note that we
  1191. * do not optimize out the stat call, even if the
  1192. * caller doesn't care about the disk-size, since our
  1193. * return value implicitly indicates whether the
  1194. * object even exists.
  1195. */
  1196. if (!oi->typep && !oi->type_name && !oi->sizep && !oi->contentp) {
  1197. const char *path;
  1198. struct stat st;
  1199. if (!oi->disk_sizep && (flags & OBJECT_INFO_QUICK))
  1200. return quick_has_loose(r, oid) ? 0 : -1;
  1201. if (stat_loose_object(r, oid, &st, &path) < 0)
  1202. return -1;
  1203. if (oi->disk_sizep)
  1204. *oi->disk_sizep = st.st_size;
  1205. return 0;
  1206. }
  1207. map = map_loose_object(r, oid, &mapsize);
  1208. if (!map)
  1209. return -1;
  1210. if (!oi->sizep)
  1211. oi->sizep = &size_scratch;
  1212. if (oi->disk_sizep)
  1213. *oi->disk_sizep = mapsize;
  1214. if ((flags & OBJECT_INFO_ALLOW_UNKNOWN_TYPE)) {
  1215. if (unpack_loose_header_to_strbuf(&stream, map, mapsize, hdr, sizeof(hdr), &hdrbuf) < 0)
  1216. status = error(_("unable to unpack %s header with --allow-unknown-type"),
  1217. oid_to_hex(oid));
  1218. } else if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0)
  1219. status = error(_("unable to unpack %s header"),
  1220. oid_to_hex(oid));
  1221. if (status < 0)
  1222. ; /* Do nothing */
  1223. else if (hdrbuf.len) {
  1224. if ((status = parse_loose_header_extended(hdrbuf.buf, oi, flags)) < 0)
  1225. status = error(_("unable to parse %s header with --allow-unknown-type"),
  1226. oid_to_hex(oid));
  1227. } else if ((status = parse_loose_header_extended(hdr, oi, flags)) < 0)
  1228. status = error(_("unable to parse %s header"), oid_to_hex(oid));
  1229. if (status >= 0 && oi->contentp) {
  1230. *oi->contentp = unpack_loose_rest(&stream, hdr,
  1231. *oi->sizep, oid);
  1232. if (!*oi->contentp) {
  1233. git_inflate_end(&stream);
  1234. status = -1;
  1235. }
  1236. } else
  1237. git_inflate_end(&stream);
  1238. munmap(map, mapsize);
  1239. if (status && oi->typep)
  1240. *oi->typep = status;
  1241. if (oi->sizep == &size_scratch)
  1242. oi->sizep = NULL;
  1243. strbuf_release(&hdrbuf);
  1244. oi->whence = OI_LOOSE;
  1245. return (status < 0) ? status : 0;
  1246. }
  1247. int obj_read_use_lock = 0;
  1248. pthread_mutex_t obj_read_mutex;
  1249. void enable_obj_read_lock(void)
  1250. {
  1251. if (obj_read_use_lock)
  1252. return;
  1253. obj_read_use_lock = 1;
  1254. init_recursive_mutex(&obj_read_mutex);
  1255. }
  1256. void disable_obj_read_lock(void)
  1257. {
  1258. if (!obj_read_use_lock)
  1259. return;
  1260. obj_read_use_lock = 0;
  1261. pthread_mutex_destroy(&obj_read_mutex);
  1262. }
  1263. int fetch_if_missing = 1;
  1264. static int do_oid_object_info_extended(struct repository *r,
  1265. const struct object_id *oid,
  1266. struct object_info *oi, unsigned flags)
  1267. {
  1268. static struct object_info blank_oi = OBJECT_INFO_INIT;
  1269. struct cached_object *co;
  1270. struct pack_entry e;
  1271. int rtype;
  1272. const struct object_id *real = oid;
  1273. int already_retried = 0;
  1274. if (flags & OBJECT_INFO_LOOKUP_REPLACE)
  1275. real = lookup_replace_object(r, oid);
  1276. if (is_null_oid(real))
  1277. return -1;
  1278. if (!oi)
  1279. oi = &blank_oi;
  1280. co = find_cached_object(real);
  1281. if (co) {
  1282. if (oi->typep)
  1283. *(oi->typep) = co->type;
  1284. if (oi->sizep)
  1285. *(oi->sizep) = co->size;
  1286. if (oi->disk_sizep)
  1287. *(oi->disk_sizep) = 0;
  1288. if (oi->delta_base_oid)
  1289. oidclr(oi->delta_base_oid);
  1290. if (oi->type_name)
  1291. strbuf_addstr(oi->type_name, type_name(co->type));
  1292. if (oi->contentp)
  1293. *oi->contentp = xmemdupz(co->buf, co->size);
  1294. oi->whence = OI_CACHED;
  1295. return 0;
  1296. }
  1297. while (1) {
  1298. if (find_pack_entry(r, real, &e))
  1299. break;
  1300. if (flags & OBJECT_INFO_IGNORE_LOOSE)
  1301. return -1;
  1302. /* Most likely it's a loose object. */
  1303. if (!loose_object_info(r, real, oi, flags))
  1304. return 0;
  1305. /* Not a loose object; someone else may have just packed it. */
  1306. if (!(flags & OBJECT_INFO_QUICK)) {
  1307. reprepare_packed_git(r);
  1308. if (find_pack_entry(r, real, &e))
  1309. break;
  1310. }
  1311. /* Check if it is a missing object */
  1312. if (fetch_if_missing && has_promisor_remote() &&
  1313. !already_retried && r == the_repository &&
  1314. !(flags & OBJECT_INFO_SKIP_FETCH_OBJECT)) {
  1315. /*
  1316. * TODO Investigate checking promisor_remote_get_direct()
  1317. * TODO return value and stopping on error here.
  1318. * TODO Pass a repository struct through
  1319. * promisor_remote_get_direct(), such that arbitrary
  1320. * repositories work.
  1321. */
  1322. promisor_remote_get_direct(r, real, 1);
  1323. already_retried = 1;
  1324. continue;
  1325. }
  1326. return -1;
  1327. }
  1328. if (oi == &blank_oi)
  1329. /*
  1330. * We know that the caller doesn't actually need the
  1331. * information below, so return early.
  1332. */
  1333. return 0;
  1334. rtype = packed_object_info(r, e.p, e.offset, oi);
  1335. if (rtype < 0) {
  1336. mark_bad_packed_object(e.p, real->hash);
  1337. return do_oid_object_info_extended(r, real, oi, 0);
  1338. } else if (oi->whence == OI_PACKED) {
  1339. oi->u.packed.offset = e.offset;
  1340. oi->u.packed.pack = e.p;
  1341. oi->u.packed.is_delta = (rtype == OBJ_REF_DELTA ||
  1342. rtype == OBJ_OFS_DELTA);
  1343. }
  1344. return 0;
  1345. }
  1346. int oid_object_info_extended(struct repository *r, const struct object_id *oid,
  1347. struct object_info *oi, unsigned flags)
  1348. {
  1349. int ret;
  1350. obj_read_lock();
  1351. ret = do_oid_object_info_extended(r, oid, oi, flags);
  1352. obj_read_unlock();
  1353. return ret;
  1354. }
  1355. /* returns enum object_type or negative */
  1356. int oid_object_info(struct repository *r,
  1357. const struct object_id *oid,
  1358. unsigned long *sizep)
  1359. {
  1360. enum object_type type;
  1361. struct object_info oi = OBJECT_INFO_INIT;
  1362. oi.typep = &type;
  1363. oi.sizep = sizep;
  1364. if (oid_object_info_extended(r, oid, &oi,
  1365. OBJECT_INFO_LOOKUP_REPLACE) < 0)
  1366. return -1;
  1367. return type;
  1368. }
  1369. static void *read_object(struct repository *r,
  1370. const struct object_id *oid, enum object_type *type,
  1371. unsigned long *size)
  1372. {
  1373. struct object_info oi = OBJECT_INFO_INIT;
  1374. void *content;
  1375. oi.typep = type;
  1376. oi.sizep = size;
  1377. oi.contentp = &content;
  1378. if (oid_object_info_extended(r, oid, &oi, 0) < 0)
  1379. return NULL;
  1380. return content;
  1381. }
  1382. int pretend_object_file(void *buf, unsigned long len, enum object_type type,
  1383. struct object_id *oid)
  1384. {
  1385. struct cached_object *co;
  1386. hash_object_file(the_hash_algo, buf, len, type_name(type), oid);
  1387. if (has_object_file_with_flags(oid, OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT) ||
  1388. find_cached_object(oid))
  1389. return 0;
  1390. ALLOC_GROW(cached_objects, cached_object_nr + 1, cached_object_alloc);
  1391. co = &cached_objects[cached_object_nr++];
  1392. co->size = len;
  1393. co->type = type;
  1394. co->buf = xmalloc(len);
  1395. memcpy(co->buf, buf, len);
  1396. oidcpy(&co->oid, oid);
  1397. return 0;
  1398. }
  1399. /*
  1400. * This function dies on corrupt objects; the callers who want to
  1401. * deal with them should arrange to call read_object() and give error
  1402. * messages themselves.
  1403. */
  1404. void *read_object_file_extended(struct repository *r,
  1405. const struct object_id *oid,
  1406. enum object_type *type,
  1407. unsigned long *size,
  1408. int lookup_replace)
  1409. {
  1410. void *data;
  1411. const struct packed_git *p;
  1412. const char *path;
  1413. struct stat st;
  1414. const struct object_id *repl = lookup_replace ?
  1415. lookup_replace_object(r, oid) : oid;
  1416. errno = 0;
  1417. data = read_object(r, repl, type, size);
  1418. if (data)
  1419. return data;
  1420. obj_read_lock();
  1421. if (errno && errno != ENOENT)
  1422. die_errno(_("failed to read object %s"), oid_to_hex(oid));
  1423. /* die if we replaced an object with one that does not exist */
  1424. if (repl != oid)
  1425. die(_("replacement %s not found for %s"),
  1426. oid_to_hex(repl), oid_to_hex(oid));
  1427. if (!stat_loose_object(r, repl, &st, &path))
  1428. die(_("loose object %s (stored in %s) is corrupt"),
  1429. oid_to_hex(repl), path);
  1430. if ((p = has_packed_and_bad(r, repl->hash)) != NULL)
  1431. die(_("packed object %s (stored in %s) is corrupt"),
  1432. oid_to_hex(repl), p->pack_name);
  1433. obj_read_unlock();
  1434. return NULL;
  1435. }
  1436. void *read_object_with_reference(struct repository *r,
  1437. const struct object_id *oid,
  1438. const char *required_type_name,
  1439. unsigned long *size,
  1440. struct object_id *actual_oid_return)
  1441. {
  1442. enum object_type type, required_type;
  1443. void *buffer;
  1444. unsigned long isize;
  1445. struct object_id actual_oid;
  1446. required_type = type_from_string(required_type_name);
  1447. oidcpy(&actual_oid, oid);
  1448. while (1) {
  1449. int ref_length = -1;
  1450. const char *ref_type = NULL;
  1451. buffer = repo_read_object_file(r, &actual_oid, &type, &isize);
  1452. if (!buffer)
  1453. return NULL;
  1454. if (type == required_type) {
  1455. *size = isize;
  1456. if (actual_oid_return)
  1457. oidcpy(actual_oid_return, &actual_oid);
  1458. return buffer;
  1459. }
  1460. /* Handle references */
  1461. else if (type == OBJ_COMMIT)
  1462. ref_type = "tree ";
  1463. else if (type == OBJ_TAG)
  1464. ref_type = "object ";
  1465. else {
  1466. free(buffer);
  1467. return NULL;
  1468. }
  1469. ref_length = strlen(ref_type);
  1470. if (ref_length + the_hash_algo->hexsz > isize ||
  1471. memcmp(buffer, ref_type, ref_length) ||
  1472. get_oid_hex((char *) buffer + ref_length, &actual_oid)) {
  1473. free(buffer);
  1474. return NULL;
  1475. }
  1476. free(buffer);
  1477. /* Now we have the ID of the referred-to object in
  1478. * actual_oid. Check again. */
  1479. }
  1480. }
  1481. static void write_object_file_prepare(const struct git_hash_algo *algo,
  1482. const void *buf, unsigned long len,
  1483. const char *type, struct object_id *oid,
  1484. char *hdr, int *hdrlen)
  1485. {
  1486. git_hash_ctx c;
  1487. /* Generate the header */
  1488. *hdrlen = xsnprintf(hdr, *hdrlen, "%s %"PRIuMAX , type, (uintmax_t)len)+1;
  1489. /* Sha1.. */
  1490. algo->init_fn(&c);
  1491. algo->update_fn(&c, hdr, *hdrlen);
  1492. algo->update_fn(&c, buf, len);
  1493. algo->final_fn(oid->hash, &c);
  1494. }
  1495. /*
  1496. * Move the just written object into its final resting place.
  1497. */
  1498. int finalize_object_file(const char *tmpfile, const char *filename)
  1499. {
  1500. int ret = 0;
  1501. if (object_creation_mode == OBJECT_CREATION_USES_RENAMES)
  1502. goto try_rename;
  1503. else if (link(tmpfile, filename))
  1504. ret = errno;
  1505. /*
  1506. * Coda hack - coda doesn't like cross-directory links,
  1507. * so we fall back to a rename, which will mean that it
  1508. * won't be able to check collisions, but that's not a
  1509. * big deal.
  1510. *
  1511. * The same holds for FAT formatted media.
  1512. *
  1513. * When this succeeds, we just return. We have nothing
  1514. * left to unlink.
  1515. */
  1516. if (ret && ret != EEXIST) {
  1517. try_rename:
  1518. if (!rename(tmpfile, filename))
  1519. goto out;
  1520. ret = errno;
  1521. }
  1522. unlink_or_warn(tmpfile);
  1523. if (ret) {
  1524. if (ret != EEXIST) {
  1525. return error_errno(_("unable to write file %s"), filename);
  1526. }
  1527. /* FIXME!!! Collision check here ? */
  1528. }
  1529. out:
  1530. if (adjust_shared_perm(filename))
  1531. return error(_("unable to set permission to '%s'"), filename);
  1532. return 0;
  1533. }
  1534. static int write_buffer(int fd, const void *buf, size_t len)
  1535. {
  1536. if (write_in_full(fd, buf, len) < 0)
  1537. return error_errno(_("file write error"));
  1538. return 0;
  1539. }
  1540. int hash_object_file(const struct git_hash_algo *algo, const void *buf,
  1541. unsigned long len, const char *type,
  1542. struct object_id *oid)
  1543. {
  1544. char hdr[MAX_HEADER_LEN];
  1545. int hdrlen = sizeof(hdr);
  1546. write_object_file_prepare(algo, buf, len, type, oid, hdr, &hdrlen);
  1547. return 0;
  1548. }
  1549. /* Finalize a file on disk, and close it. */
  1550. static void close_loose_object(int fd)
  1551. {
  1552. if (fsync_object_files)
  1553. fsync_or_die(fd, "loose object file");
  1554. if (close(fd) != 0)
  1555. die_errno(_("error when closing loose object file"));
  1556. }
  1557. /* Size of directory component, including the ending '/' */
  1558. static inline int directory_size(const char *filename)
  1559. {
  1560. const char *s = strrchr(filename, '/');
  1561. if (!s)
  1562. return 0;
  1563. return s - filename + 1;
  1564. }
  1565. /*
  1566. * This creates a temporary file in the same directory as the final
  1567. * 'filename'
  1568. *
  1569. * We want to avoid cross-directory filename renames, because those
  1570. * can have problems on various filesystems (FAT, NFS, Coda).
  1571. */
  1572. static int create_tmpfile(struct strbuf *tmp, const char *filename)
  1573. {
  1574. int fd, dirlen = directory_size(filename);
  1575. strbuf_reset(tmp);
  1576. strbuf_add(tmp, filename, dirlen);
  1577. strbuf_addstr(tmp, "tmp_obj_XXXXXX");
  1578. fd = git_mkstemp_mode(tmp->buf, 0444);
  1579. if (fd < 0 && dirlen && errno == ENOENT) {
  1580. /*
  1581. * Make sure the directory exists; note that the contents
  1582. * of the buffer are undefined after mkstemp returns an
  1583. * error, so we have to rewrite the whole buffer from
  1584. * scratch.
  1585. */
  1586. strbuf_reset(tmp);
  1587. strbuf_add(tmp, filename, dirlen - 1);
  1588. if (mkdir(tmp->buf, 0777) && errno != EEXIST)
  1589. return -1;
  1590. if (adjust_shared_perm(tmp->buf))
  1591. return -1;
  1592. /* Try again */
  1593. strbuf_addstr(tmp, "/tmp_obj_XXXXXX");
  1594. fd = git_mkstemp_mode(tmp->buf, 0444);
  1595. }
  1596. return fd;
  1597. }
  1598. static int write_loose_object(const struct object_id *oid, char *hdr,
  1599. int hdrlen, const void *buf, unsigned long len,
  1600. time_t mtime)
  1601. {
  1602. int fd, ret;
  1603. unsigned char compressed[4096];
  1604. git_zstream stream;
  1605. git_hash_ctx c;
  1606. struct object_id parano_oid;
  1607. static struct strbuf tmp_file = STRBUF_INIT;
  1608. static struct strbuf filename = STRBUF_INIT;
  1609. loose_object_path(the_repository, &filename, oid);
  1610. fd = create_tmpfile(&tmp_file, filename.buf);
  1611. if (fd < 0) {
  1612. if (errno == EACCES)
  1613. return error(_("insufficient permission for adding an object to repository database %s"), get_object_directory());
  1614. else
  1615. return error_errno(_("unable to create temporary file"));
  1616. }
  1617. /* Set it up */
  1618. git_deflate_init(&stream, zlib_compression_level);
  1619. stream.next_out = compressed;
  1620. stream.avail_out = sizeof(compressed);
  1621. the_hash_algo->init_fn(&c);
  1622. /* First header.. */
  1623. stream.next_in = (unsigned char *)hdr;
  1624. stream.avail_in = hdrlen;
  1625. while (git_deflate(&stream, 0) == Z_OK)
  1626. ; /* nothing */
  1627. the_hash_algo->update_fn(&c, hdr, hdrlen);
  1628. /* Then the data itself.. */
  1629. stream.next_in = (void *)buf;
  1630. stream.avail_in = len;
  1631. do {
  1632. unsigned char *in0 = stream.next_in;
  1633. ret = git_deflate(&stream, Z_FINISH);
  1634. the_hash_algo->update_fn(&c, in0, stream.next_in - in0);
  1635. if (write_buffer(fd, compressed, stream.next_out - compressed) < 0)
  1636. die(_("unable to write loose object file"));
  1637. stream.next_out = compressed;
  1638. stream.avail_out = sizeof(compressed);
  1639. } while (ret == Z_OK);
  1640. if (ret != Z_STREAM_END)
  1641. die(_("unable to deflate new object %s (%d)"), oid_to_hex(oid),
  1642. ret);
  1643. ret = git_deflate_end_gently(&stream);
  1644. if (ret != Z_OK)
  1645. die(_("deflateEnd on object %s failed (%d)"), oid_to_hex(oid),
  1646. ret);
  1647. the_hash_algo->final_fn(parano_oid.hash, &c);
  1648. if (!oideq(oid, &parano_oid))
  1649. die(_("confused by unstable object source data for %s"),
  1650. oid_to_hex(oid));
  1651. close_loose_object(fd);
  1652. if (mtime) {
  1653. struct utimbuf utb;
  1654. utb.actime = mtime;
  1655. utb.modtime = mtime;
  1656. if (utime(tmp_file.buf, &utb) < 0)
  1657. warning_errno(_("failed utime() on %s"), tmp_file.buf);
  1658. }
  1659. return finalize_object_file(tmp_file.buf, filename.buf);
  1660. }
  1661. static int freshen_loose_object(const struct object_id *oid)
  1662. {
  1663. return check_and_freshen(oid, 1);
  1664. }
  1665. static int freshen_packed_object(const struct object_id *oid)
  1666. {
  1667. struct pack_entry e;
  1668. if (!find_pack_entry(the_repository, oid, &e))
  1669. return 0;
  1670. if (e.p->freshened)
  1671. return 1;
  1672. if (!freshen_file(e.p->pack_name))
  1673. return 0;
  1674. e.p->freshened = 1;
  1675. return 1;
  1676. }
  1677. int write_object_file(const void *buf, unsigned long len, const char *type,
  1678. struct object_id *oid)
  1679. {
  1680. char hdr[MAX_HEADER_LEN];
  1681. int hdrlen = sizeof(hdr);
  1682. /* Normally if we have it in the pack then we do not bother writing
  1683. * it out into .git/objects/??/?{38} file.
  1684. */
  1685. write_object_file_prepare(the_hash_algo, buf, len, type, oid, hdr,
  1686. &hdrlen);
  1687. if (freshen_packed_object(oid) || freshen_loose_object(oid))
  1688. return 0;
  1689. return write_loose_object(oid, hdr, hdrlen, buf, len, 0);
  1690. }
  1691. int hash_object_file_literally(const void *buf, unsigned long len,
  1692. const char *type, struct object_id *oid,
  1693. unsigned flags)
  1694. {
  1695. char *header;
  1696. int hdrlen, status = 0;
  1697. /* type string, SP, %lu of the length plus NUL must fit this */
  1698. hdrlen = strlen(type) + MAX_HEADER_LEN;
  1699. header = xmalloc(hdrlen);
  1700. write_object_file_prepare(the_hash_algo, buf, len, type, oid, header,
  1701. &hdrlen);
  1702. if (!(flags & HASH_WRITE_OBJECT))
  1703. goto cleanup;
  1704. if (freshen_packed_object(oid) || freshen_loose_object(oid))
  1705. goto cleanup;
  1706. status = write_loose_object(oid, header, hdrlen, buf, len, 0);
  1707. cleanup:
  1708. free(header);
  1709. return status;
  1710. }
  1711. int force_object_loose(const struct object_id *oid, time_t mtime)
  1712. {
  1713. void *buf;
  1714. unsigned long len;
  1715. enum object_type type;
  1716. char hdr[MAX_HEADER_LEN];
  1717. int hdrlen;
  1718. int ret;
  1719. if (has_loose_object(oid))
  1720. return 0;
  1721. buf = read_object(the_repository, oid, &type, &len);
  1722. if (!buf)
  1723. return error(_("cannot read object for %s"), oid_to_hex(oid));
  1724. hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(type), (uintmax_t)len) + 1;
  1725. ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime);
  1726. free(buf);
  1727. return ret;
  1728. }
  1729. int has_object(struct repository *r, const struct object_id *oid,
  1730. unsigned flags)
  1731. {
  1732. int quick = !(flags & HAS_OBJECT_RECHECK_PACKED);
  1733. unsigned object_info_flags = OBJECT_INFO_SKIP_FETCH_OBJECT |
  1734. (quick ? OBJECT_INFO_QUICK : 0);
  1735. if (!startup_info->have_repository)
  1736. return 0;
  1737. return oid_object_info_extended(r, oid, NULL, object_info_flags) >= 0;
  1738. }
  1739. int repo_has_object_file_with_flags(struct repository *r,
  1740. const struct object_id *oid, int flags)
  1741. {
  1742. if (!startup_info->have_repository)
  1743. return 0;
  1744. return oid_object_info_extended(r, oid, NULL, flags) >= 0;
  1745. }
  1746. int repo_has_object_file(struct repository *r,
  1747. const struct object_id *oid)
  1748. {
  1749. return repo_has_object_file_with_flags(r, oid, 0);
  1750. }
  1751. static void check_tree(const void *buf, size_t size)
  1752. {
  1753. struct tree_desc desc;
  1754. struct name_entry entry;
  1755. init_tree_desc(&desc, buf, size);
  1756. while (tree_entry(&desc, &entry))
  1757. /* do nothing
  1758. * tree_entry() will die() on malformed entries */
  1759. ;
  1760. }
  1761. static void check_commit(const void *buf, size_t size)
  1762. {
  1763. struct commit c;
  1764. memset(&c, 0, sizeof(c));
  1765. if (parse_commit_buffer(the_repository, &c, buf, size, 0))
  1766. die(_("corrupt commit"));
  1767. }
  1768. static void check_tag(const void *buf, size_t size)
  1769. {
  1770. struct tag t;
  1771. memset(&t, 0, sizeof(t));
  1772. if (parse_tag_buffer(the_repository, &t, buf, size))
  1773. die(_("corrupt tag"));
  1774. }
  1775. static int index_mem(struct index_state *istate,
  1776. struct object_id *oid, void *buf, size_t size,
  1777. enum object_type type,
  1778. const char *path, unsigned flags)
  1779. {
  1780. int ret, re_allocated = 0;
  1781. int write_object = flags & HASH_WRITE_OBJECT;
  1782. if (!type)
  1783. type = OBJ_BLOB;
  1784. /*
  1785. * Convert blobs to git internal format
  1786. */
  1787. if ((type == OBJ_BLOB) && path) {
  1788. struct strbuf nbuf = STRBUF_INIT;
  1789. if (convert_to_git(istate, path, buf, size, &nbuf,
  1790. get_conv_flags(flags))) {
  1791. buf = strbuf_detach(&nbuf, &size);
  1792. re_allocated = 1;
  1793. }
  1794. }
  1795. if (flags & HASH_FORMAT_CHECK) {
  1796. if (type == OBJ_TREE)
  1797. check_tree(buf, size);
  1798. if (type == OBJ_COMMIT)
  1799. check_commit(buf, size);
  1800. if (type == OBJ_TAG)
  1801. check_tag(buf, size);
  1802. }
  1803. if (write_object)
  1804. ret = write_object_file(buf, size, type_name(type), oid);
  1805. else
  1806. ret = hash_object_file(the_hash_algo, buf, size,
  1807. type_name(type), oid);
  1808. if (re_allocated)
  1809. free(buf);
  1810. return ret;
  1811. }
  1812. static int index_stream_convert_blob(struct index_state *istate,
  1813. struct object_id *oid,
  1814. int fd,
  1815. const char *path,
  1816. unsigned flags)
  1817. {
  1818. int ret;
  1819. const int write_object = flags & HASH_WRITE_OBJECT;
  1820. struct strbuf sbuf = STRBUF_INIT;
  1821. assert(path);
  1822. assert(would_convert_to_git_filter_fd(istate, path));
  1823. convert_to_git_filter_fd(istate, path, fd, &sbuf,
  1824. get_conv_flags(flags));
  1825. if (write_object)
  1826. ret = write_object_file(sbuf.buf, sbuf.len, type_name(OBJ_BLOB),
  1827. oid);
  1828. else
  1829. ret = hash_object_file(the_hash_algo, sbuf.buf, sbuf.len,
  1830. type_name(OBJ_BLOB), oid);
  1831. strbuf_release(&sbuf);
  1832. return ret;
  1833. }
  1834. static int index_pipe(struct index_state *istate, struct object_id *oid,
  1835. int fd, enum object_type type,
  1836. const char *path, unsigned flags)
  1837. {
  1838. struct strbuf sbuf = STRBUF_INIT;
  1839. int ret;
  1840. if (strbuf_read(&sbuf, fd, 4096) >= 0)
  1841. ret = index_mem(istate, oid, sbuf.buf, sbuf.len, type, path, flags);
  1842. else
  1843. ret = -1;
  1844. strbuf_release(&sbuf);
  1845. return ret;
  1846. }
  1847. #define SMALL_FILE_SIZE (32*1024)
  1848. static int index_core(struct index_state *istate,
  1849. struct object_id *oid, int fd, size_t size,
  1850. enum object_type type, const char *path,
  1851. unsigned flags)
  1852. {
  1853. int ret;
  1854. if (!size) {
  1855. ret = index_mem(istate, oid, "", size, type, path, flags);
  1856. } else if (size <= SMALL_FILE_SIZE) {
  1857. char *buf = xmalloc(size);
  1858. ssize_t read_result = read_in_full(fd, buf, size);
  1859. if (read_result < 0)
  1860. ret = error_errno(_("read error while indexing %s"),
  1861. path ? path : "<unknown>");
  1862. else if (read_result != size)
  1863. ret = error(_("short read while indexing %s"),
  1864. path ? path : "<unknown>");
  1865. else
  1866. ret = index_mem(istate, oid, buf, size, type, path, flags);
  1867. free(buf);
  1868. } else {
  1869. void *buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
  1870. ret = index_mem(istate, oid, buf, size, type, path, flags);
  1871. munmap(buf, size);
  1872. }
  1873. return ret;
  1874. }
  1875. /*
  1876. * This creates one packfile per large blob unless bulk-checkin
  1877. * machinery is "plugged".
  1878. *
  1879. * This also bypasses the usual "convert-to-git" dance, and that is on
  1880. * purpose. We could write a streaming version of the converting
  1881. * functions and insert that before feeding the data to fast-import
  1882. * (or equivalent in-core API described above). However, that is
  1883. * somewhat complicated, as we do not know the size of the filter
  1884. * result, which we need to know beforehand when writing a git object.
  1885. * Since the primary motivation for trying to stream from the working
  1886. * tree file and to avoid mmaping it in core is to deal with large
  1887. * binary blobs, they generally do not want to get any conversion, and
  1888. * callers should avoid this code path when filters are requested.
  1889. */
  1890. static int index_stream(struct object_id *oid, int fd, size_t size,
  1891. enum object_type type, const char *path,
  1892. unsigned flags)
  1893. {
  1894. return index_bulk_checkin(oid, fd, size, type, path, flags);
  1895. }
  1896. int index_fd(struct index_state *istate, struct object_id *oid,
  1897. int fd, struct stat *st,
  1898. enum object_type type, const char *path, unsigned flags)
  1899. {
  1900. int ret;
  1901. /*
  1902. * Call xsize_t() only when needed to avoid potentially unnecessary
  1903. * die() for large files.
  1904. */
  1905. if (type == OBJ_BLOB && path && would_convert_to_git_filter_fd(istate, path))
  1906. ret = index_stream_convert_blob(istate, oid, fd, path, flags);
  1907. else if (!S_ISREG(st->st_mode))
  1908. ret = index_pipe(istate, oid, fd, type, path, flags);
  1909. else if (st->st_size <= big_file_threshold || type != OBJ_BLOB ||
  1910. (path && would_convert_to_git(istate, path)))
  1911. ret = index_core(istate, oid, fd, xsize_t(st->st_size),
  1912. type, path, flags);
  1913. else
  1914. ret = index_stream(oid, fd, xsize_t(st->st_size), type, path,
  1915. flags);
  1916. close(fd);
  1917. return ret;
  1918. }
  1919. int index_path(struct index_state *istate, struct object_id *oid,
  1920. const char *path, struct stat *st, unsigned flags)
  1921. {
  1922. int fd;
  1923. struct strbuf sb = STRBUF_INIT;
  1924. int rc = 0;
  1925. switch (st->st_mode & S_IFMT) {
  1926. case S_IFREG:
  1927. fd = open(path, O_RDONLY);
  1928. if (fd < 0)
  1929. return error_errno("open(\"%s\")", path);
  1930. if (index_fd(istate, oid, fd, st, OBJ_BLOB, path, flags) < 0)
  1931. return error(_("%s: failed to insert into database"),
  1932. path);
  1933. break;
  1934. case S_IFLNK:
  1935. if (strbuf_readlink(&sb, path, st->st_size))
  1936. return error_errno("readlink(\"%s\")", path);
  1937. if (!(flags & HASH_WRITE_OBJECT))
  1938. hash_object_file(the_hash_algo, sb.buf, sb.len,
  1939. blob_type, oid);
  1940. else if (write_object_file(sb.buf, sb.len, blob_type, oid))
  1941. rc = error(_("%s: failed to insert into database"), path);
  1942. strbuf_release(&sb);
  1943. break;
  1944. case S_IFDIR:
  1945. return resolve_gitlink_ref(path, "HEAD", oid);
  1946. default:
  1947. return error(_("%s: unsupported file type"), path);
  1948. }
  1949. return rc;
  1950. }
  1951. int read_pack_header(int fd, struct pack_header *header)
  1952. {
  1953. if (read_in_full(fd, header, sizeof(*header)) != sizeof(*header))
  1954. /* "eof before pack header was fully read" */
  1955. return PH_ERROR_EOF;
  1956. if (header->hdr_signature != htonl(PACK_SIGNATURE))
  1957. /* "protocol error (pack signature mismatch detected)" */
  1958. return PH_ERROR_PACK_SIGNATURE;
  1959. if (!pack_version_ok(header->hdr_version))
  1960. /* "protocol error (pack version unsupported)" */
  1961. return PH_ERROR_PROTOCOL;
  1962. return 0;
  1963. }
  1964. void assert_oid_type(const struct object_id *oid, enum object_type expect)
  1965. {
  1966. enum object_type type = oid_object_info(the_repository, oid, NULL);
  1967. if (type < 0)
  1968. die(_("%s is not a valid object"), oid_to_hex(oid));
  1969. if (type != expect)
  1970. die(_("%s is not a valid '%s' object"), oid_to_hex(oid),
  1971. type_name(expect));
  1972. }
  1973. int for_each_file_in_obj_subdir(unsigned int subdir_nr,
  1974. struct strbuf *path,
  1975. each_loose_object_fn obj_cb,
  1976. each_loose_cruft_fn cruft_cb,
  1977. each_loose_subdir_fn subdir_cb,
  1978. void *data)
  1979. {
  1980. size_t origlen, baselen;
  1981. DIR *dir;
  1982. struct dirent *de;
  1983. int r = 0;
  1984. struct object_id oid;
  1985. if (subdir_nr > 0xff)
  1986. BUG("invalid loose object subdirectory: %x", subdir_nr);
  1987. origlen = path->len;
  1988. strbuf_complete(path, '/');
  1989. strbuf_addf(path, "%02x", subdir_nr);
  1990. dir = opendir(path->buf);
  1991. if (!dir) {
  1992. if (errno != ENOENT)
  1993. r = error_errno(_("unable to open %s"), path->buf);
  1994. strbuf_setlen(path, origlen);
  1995. return r;
  1996. }
  1997. oid.hash[0] = subdir_nr;
  1998. strbuf_addch(path, '/');
  1999. baselen = path->len;
  2000. while ((de = readdir(dir))) {
  2001. size_t namelen;
  2002. if (is_dot_or_dotdot(de->d_name))
  2003. continue;
  2004. namelen = strlen(de->d_name);
  2005. strbuf_setlen(path, baselen);
  2006. strbuf_add(path, de->d_name, namelen);
  2007. if (namelen == the_hash_algo->hexsz - 2 &&
  2008. !hex_to_bytes(oid.hash + 1, de->d_name,
  2009. the_hash_algo->rawsz - 1)) {
  2010. if (obj_cb) {
  2011. r = obj_cb(&oid, path->buf, data);
  2012. if (r)
  2013. break;
  2014. }
  2015. continue;
  2016. }
  2017. if (cruft_cb) {
  2018. r = cruft_cb(de->d_name, path->buf, data);
  2019. if (r)
  2020. break;
  2021. }
  2022. }
  2023. closedir(dir);
  2024. strbuf_setlen(path, baselen - 1);
  2025. if (!r && subdir_cb)
  2026. r = subdir_cb(subdir_nr, path->buf, data);
  2027. strbuf_setlen(path, origlen);
  2028. return r;
  2029. }
  2030. int for_each_loose_file_in_objdir_buf(struct strbuf *path,
  2031. each_loose_object_fn obj_cb,
  2032. each_loose_cruft_fn cruft_cb,
  2033. each_loose_subdir_fn subdir_cb,
  2034. void *data)
  2035. {
  2036. int r = 0;
  2037. int i;
  2038. for (i = 0; i < 256; i++) {
  2039. r = for_each_file_in_obj_subdir(i, path, obj_cb, cruft_cb,
  2040. subdir_cb, data);
  2041. if (r)
  2042. break;
  2043. }
  2044. return r;
  2045. }
  2046. int for_each_loose_file_in_objdir(const char *path,
  2047. each_loose_object_fn obj_cb,
  2048. each_loose_cruft_fn cruft_cb,
  2049. each_loose_subdir_fn subdir_cb,
  2050. void *data)
  2051. {
  2052. struct strbuf buf = STRBUF_INIT;
  2053. int r;
  2054. strbuf_addstr(&buf, path);
  2055. r = for_each_loose_file_in_objdir_buf(&buf, obj_cb, cruft_cb,
  2056. subdir_cb, data);
  2057. strbuf_release(&buf);
  2058. return r;
  2059. }
  2060. int for_each_loose_object(each_loose_object_fn cb, void *data,
  2061. enum for_each_object_flags flags)
  2062. {
  2063. struct object_directory *odb;
  2064. prepare_alt_odb(the_repository);
  2065. for (odb = the_repository->objects->odb; odb; odb = odb->next) {
  2066. int r = for_each_loose_file_in_objdir(odb->path, cb, NULL,
  2067. NULL, data);
  2068. if (r)
  2069. return r;
  2070. if (flags & FOR_EACH_OBJECT_LOCAL_ONLY)
  2071. break;
  2072. }
  2073. return 0;
  2074. }
  2075. static int append_loose_object(const struct object_id *oid, const char *path,
  2076. void *data)
  2077. {
  2078. oid_array_append(data, oid);
  2079. return 0;
  2080. }
  2081. struct oid_array *odb_loose_cache(struct object_directory *odb,
  2082. const struct object_id *oid)
  2083. {
  2084. int subdir_nr = oid->hash[0];
  2085. struct strbuf buf = STRBUF_INIT;
  2086. if (subdir_nr < 0 ||
  2087. subdir_nr >= ARRAY_SIZE(odb->loose_objects_subdir_seen))
  2088. BUG("subdir_nr out of range");
  2089. if (odb->loose_objects_subdir_seen[subdir_nr])
  2090. return &odb->loose_objects_cache[subdir_nr];
  2091. strbuf_addstr(&buf, odb->path);
  2092. for_each_file_in_obj_subdir(subdir_nr, &buf,
  2093. append_loose_object,
  2094. NULL, NULL,
  2095. &odb->loose_objects_cache[subdir_nr]);
  2096. odb->loose_objects_subdir_seen[subdir_nr] = 1;
  2097. strbuf_release(&buf);
  2098. return &odb->loose_objects_cache[subdir_nr];
  2099. }
  2100. void odb_clear_loose_cache(struct object_directory *odb)
  2101. {
  2102. int i;
  2103. for (i = 0; i < ARRAY_SIZE(odb->loose_objects_cache); i++)
  2104. oid_array_clear(&odb->loose_objects_cache[i]);
  2105. memset(&odb->loose_objects_subdir_seen, 0,
  2106. sizeof(odb->loose_objects_subdir_seen));
  2107. }
  2108. static int check_stream_oid(git_zstream *stream,
  2109. const char *hdr,
  2110. unsigned long size,
  2111. const char *path,
  2112. const struct object_id *expected_oid)
  2113. {
  2114. git_hash_ctx c;
  2115. struct object_id real_oid;
  2116. unsigned char buf[4096];
  2117. unsigned long total_read;
  2118. int status = Z_OK;
  2119. the_hash_algo->init_fn(&c);
  2120. the_hash_algo->update_fn(&c, hdr, stream->total_out);
  2121. /*
  2122. * We already read some bytes into hdr, but the ones up to the NUL
  2123. * do not count against the object's content size.
  2124. */
  2125. total_read = stream->total_out - strlen(hdr) - 1;
  2126. /*
  2127. * This size comparison must be "<=" to read the final zlib packets;
  2128. * see the comment in unpack_loose_rest for details.
  2129. */
  2130. while (total_read <= size &&
  2131. (status == Z_OK ||
  2132. (status == Z_BUF_ERROR && !stream->avail_out))) {
  2133. stream->next_out = buf;
  2134. stream->avail_out = sizeof(buf);
  2135. if (size - total_read < stream->avail_out)
  2136. stream->avail_out = size - total_read;
  2137. status = git_inflate(stream, Z_FINISH);
  2138. the_hash_algo->update_fn(&c, buf, stream->next_out - buf);
  2139. total_read += stream->next_out - buf;
  2140. }
  2141. git_inflate_end(stream);
  2142. if (status != Z_STREAM_END) {
  2143. error(_("corrupt loose object '%s'"), oid_to_hex(expected_oid));
  2144. return -1;
  2145. }
  2146. if (stream->avail_in) {
  2147. error(_("garbage at end of loose object '%s'"),
  2148. oid_to_hex(expected_oid));
  2149. return -1;
  2150. }
  2151. the_hash_algo->final_fn(real_oid.hash, &c);
  2152. if (!oideq(expected_oid, &real_oid)) {
  2153. error(_("hash mismatch for %s (expected %s)"), path,
  2154. oid_to_hex(expected_oid));
  2155. return -1;
  2156. }
  2157. return 0;
  2158. }
  2159. int read_loose_object(const char *path,
  2160. const struct object_id *expected_oid,
  2161. enum object_type *type,
  2162. unsigned long *size,
  2163. void **contents)
  2164. {
  2165. int ret = -1;
  2166. void *map = NULL;
  2167. unsigned long mapsize;
  2168. git_zstream stream;
  2169. char hdr[MAX_HEADER_LEN];
  2170. *contents = NULL;
  2171. map = map_loose_object_1(the_repository, path, NULL, &mapsize);
  2172. if (!map) {
  2173. error_errno(_("unable to mmap %s"), path);
  2174. goto out;
  2175. }
  2176. if (unpack_loose_header(&stream, map, mapsize, hdr, sizeof(hdr)) < 0) {
  2177. error(_("unable to unpack header of %s"), path);
  2178. goto out;
  2179. }
  2180. *type = parse_loose_header(hdr, size);
  2181. if (*type < 0) {
  2182. error(_("unable to parse header of %s"), path);
  2183. git_inflate_end(&stream);
  2184. goto out;
  2185. }
  2186. if (*type == OBJ_BLOB && *size > big_file_threshold) {
  2187. if (check_stream_oid(&stream, hdr, *size, path, expected_oid) < 0)
  2188. goto out;
  2189. } else {
  2190. *contents = unpack_loose_rest(&stream, hdr, *size, expected_oid);
  2191. if (!*contents) {
  2192. error(_("unable to unpack contents of %s"), path);
  2193. git_inflate_end(&stream);
  2194. goto out;
  2195. }
  2196. if (check_object_signature(the_repository, expected_oid,
  2197. *contents, *size,
  2198. type_name(*type))) {
  2199. error(_("hash mismatch for %s (expected %s)"), path,
  2200. oid_to_hex(expected_oid));
  2201. free(*contents);
  2202. goto out;
  2203. }
  2204. }
  2205. ret = 0; /* everything checks out */
  2206. out:
  2207. if (map)
  2208. munmap(map, mapsize);
  2209. return ret;
  2210. }