fs_api.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001--2012 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file fs/fs_api.c
  18. * @brief main FS functions (master initialization, serialization, deserialization, shared code)
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_fs_service.h"
  24. #include "fs_api.h"
  25. #include "fs_tree.h"
  26. /**
  27. * How many block requests can we have outstanding in parallel at a time by default?
  28. */
  29. #define DEFAULT_MAX_PARALLEL_REQUESTS (1024 * 10)
  30. /**
  31. * How many downloads can we have outstanding in parallel at a time by default?
  32. */
  33. #define DEFAULT_MAX_PARALLEL_DOWNLOADS 16
  34. /**
  35. * Start the given job (send signal, remove from pending queue, update
  36. * counters and state).
  37. *
  38. * @param qe job to start
  39. */
  40. static void
  41. start_job (struct GNUNET_FS_QueueEntry *qe)
  42. {
  43. qe->active = GNUNET_YES;
  44. qe->start (qe->cls);
  45. qe->start_times++;
  46. qe->h->active_blocks += qe->blocks;
  47. qe->h->active_downloads++;
  48. qe->start_time = GNUNET_TIME_absolute_get ();
  49. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  50. "Starting job %p (%u active)\n",
  51. qe,
  52. qe->h->active_downloads);
  53. GNUNET_CONTAINER_DLL_remove (qe->h->pending_head, qe->h->pending_tail, qe);
  54. GNUNET_CONTAINER_DLL_insert_after (qe->h->running_head,
  55. qe->h->running_tail,
  56. qe->h->running_tail,
  57. qe);
  58. }
  59. /**
  60. * Stop the given job (send signal, remove from active queue, update
  61. * counters and state).
  62. *
  63. * @param qe job to stop
  64. */
  65. static void
  66. stop_job (struct GNUNET_FS_QueueEntry *qe)
  67. {
  68. qe->active = GNUNET_NO;
  69. qe->stop (qe->cls);
  70. GNUNET_assert (0 < qe->h->active_downloads);
  71. qe->h->active_downloads--;
  72. qe->h->active_blocks -= qe->blocks;
  73. qe->run_time = GNUNET_TIME_relative_add (qe->run_time,
  74. GNUNET_TIME_absolute_get_duration (
  75. qe->start_time));
  76. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  77. "Stopping job %p (%u active)\n",
  78. qe,
  79. qe->h->active_downloads);
  80. GNUNET_CONTAINER_DLL_remove (qe->h->running_head, qe->h->running_tail, qe);
  81. GNUNET_CONTAINER_DLL_insert_after (qe->h->pending_head,
  82. qe->h->pending_tail,
  83. qe->h->pending_tail,
  84. qe);
  85. }
  86. /**
  87. * Process the jobs in the job queue, possibly starting some
  88. * and stopping others.
  89. *
  90. * @param cls the `struct GNUNET_FS_Handle *`
  91. */
  92. static void
  93. process_job_queue (void *cls)
  94. {
  95. struct GNUNET_FS_Handle *h = cls;
  96. struct GNUNET_FS_QueueEntry *qe;
  97. struct GNUNET_FS_QueueEntry *next;
  98. struct GNUNET_TIME_Relative run_time;
  99. struct GNUNET_TIME_Relative restart_at;
  100. struct GNUNET_TIME_Relative rst;
  101. struct GNUNET_TIME_Absolute end_time;
  102. unsigned int num_downloads_waiting;
  103. unsigned int num_downloads_active;
  104. unsigned int num_downloads_expired;
  105. unsigned int num_probes_active;
  106. unsigned int num_probes_waiting;
  107. unsigned int num_probes_expired;
  108. int num_probes_change;
  109. int num_downloads_change;
  110. int block_limit_hit;
  111. h->queue_job = NULL;
  112. /* restart_at will be set to the time when it makes sense to
  113. re-evaluate the job queue (unless, of course, jobs complete
  114. or are added, then we'll be triggered immediately */
  115. restart_at = GNUNET_TIME_UNIT_FOREVER_REL;
  116. /* first, calculate some basic statistics on pending jobs */
  117. num_probes_waiting = 0;
  118. num_downloads_waiting = 0;
  119. for (qe = h->pending_head; NULL != qe; qe = qe->next)
  120. {
  121. switch (qe->priority)
  122. {
  123. case GNUNET_FS_QUEUE_PRIORITY_PROBE:
  124. num_probes_waiting++;
  125. break;
  126. case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
  127. num_downloads_waiting++;
  128. break;
  129. default:
  130. GNUNET_break (0);
  131. break;
  132. }
  133. }
  134. /* now, calculate some basic statistics on running jobs */
  135. num_probes_active = 0;
  136. num_probes_expired = 0;
  137. num_downloads_active = 0;
  138. num_downloads_expired = 0;
  139. next = h->running_head;
  140. while (NULL != (qe = next))
  141. {
  142. next = qe->next;
  143. switch (qe->priority)
  144. {
  145. case GNUNET_FS_QUEUE_PRIORITY_PROBE:
  146. run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
  147. end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
  148. rst = GNUNET_TIME_absolute_get_remaining (end_time);
  149. if (0 == rst.rel_value_us)
  150. {
  151. num_probes_expired++;
  152. stop_job (qe);
  153. }
  154. else
  155. {
  156. num_probes_active++;
  157. restart_at = GNUNET_TIME_relative_min (rst, restart_at);
  158. }
  159. break;
  160. case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
  161. run_time =
  162. GNUNET_TIME_relative_saturating_multiply (h->avg_block_latency,
  163. qe->blocks * qe->start_times);
  164. end_time = GNUNET_TIME_absolute_add (qe->start_time, run_time);
  165. rst = GNUNET_TIME_absolute_get_remaining (end_time);
  166. if (0 == rst.rel_value_us)
  167. {
  168. num_downloads_expired++;
  169. stop_job (qe);
  170. }
  171. else
  172. {
  173. num_downloads_active++;
  174. restart_at = GNUNET_TIME_relative_min (rst, restart_at);
  175. }
  176. break;
  177. default:
  178. GNUNET_break (0);
  179. break;
  180. }
  181. }
  182. GNUNET_break (h->active_downloads ==
  183. num_downloads_active + num_probes_active);
  184. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  185. "PA: %u, PE: %u, PW: %u; DA: %u, DE: %u, DW: %u\n",
  186. num_probes_active,
  187. num_probes_expired,
  188. num_probes_waiting,
  189. num_downloads_active,
  190. num_downloads_expired,
  191. num_downloads_waiting);
  192. GNUNET_break (h->active_downloads + num_probes_active <=
  193. h->max_parallel_downloads);
  194. /* calculate start/stop decisions */
  195. if (h->active_downloads + num_downloads_waiting > h->max_parallel_downloads)
  196. {
  197. /* stop as many probes as there are downloads and probes */
  198. num_probes_change = -GNUNET_MIN (num_probes_active, num_downloads_waiting);
  199. /* start as many downloads as there are free slots, including those
  200. we just opened up */
  201. num_downloads_change =
  202. h->max_parallel_downloads - h->active_downloads - num_probes_change;
  203. }
  204. else
  205. {
  206. /* start all downloads (we can) */
  207. num_downloads_change = num_downloads_waiting;
  208. /* also start probes if there is room, but use a lower cap of (mpd/4) + 1 */
  209. if (1 + h->max_parallel_downloads / 4 >=
  210. (h->active_downloads + num_downloads_change))
  211. num_probes_change =
  212. GNUNET_MIN (num_probes_waiting,
  213. (1 + h->max_parallel_downloads / 4)
  214. - (h->active_downloads + num_downloads_change));
  215. else
  216. num_probes_change = 0;
  217. }
  218. GNUNET_break (num_downloads_change <= num_downloads_waiting);
  219. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  220. "Changing %d probes and %d/%u/%u downloads\n",
  221. num_probes_change,
  222. num_downloads_change,
  223. (unsigned int) h->active_downloads,
  224. (unsigned int) h->max_parallel_downloads);
  225. /* actually stop probes */
  226. next = h->running_head;
  227. while (NULL != (qe = next))
  228. {
  229. next = qe->next;
  230. if (GNUNET_FS_QUEUE_PRIORITY_PROBE != qe->priority)
  231. continue;
  232. if (num_probes_change < 0)
  233. {
  234. stop_job (qe);
  235. num_probes_change++;
  236. if (0 == num_probes_change)
  237. break;
  238. }
  239. }
  240. GNUNET_break (0 <= num_probes_change);
  241. /* start some more tasks if we now have empty slots */
  242. block_limit_hit = GNUNET_NO;
  243. next = h->pending_head;
  244. while ((NULL != (qe = next)) &&
  245. ((num_probes_change > 0) || (num_downloads_change > 0)))
  246. {
  247. next = qe->next;
  248. switch (qe->priority)
  249. {
  250. case GNUNET_FS_QUEUE_PRIORITY_PROBE:
  251. if (num_probes_change > 0)
  252. {
  253. start_job (qe);
  254. num_probes_change--;
  255. run_time = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2);
  256. restart_at = GNUNET_TIME_relative_min (run_time, restart_at);
  257. }
  258. break;
  259. case GNUNET_FS_QUEUE_PRIORITY_NORMAL:
  260. if ((num_downloads_change > 0) &&
  261. ((qe->blocks + h->active_blocks <= h->max_parallel_requests) ||
  262. ((qe->blocks > h->max_parallel_requests) &&
  263. (0 == h->active_downloads))))
  264. {
  265. start_job (qe);
  266. num_downloads_change--;
  267. }
  268. else if (num_downloads_change > 0)
  269. block_limit_hit = GNUNET_YES;
  270. break;
  271. default:
  272. GNUNET_break (0);
  273. break;
  274. }
  275. }
  276. GNUNET_break ((0 == num_downloads_change) || (GNUNET_YES == block_limit_hit));
  277. GNUNET_break (0 == num_probes_change);
  278. GNUNET_log (
  279. GNUNET_ERROR_TYPE_DEBUG,
  280. "AD: %u, MP: %u; %d probes and %d downloads to start, will run again in %s\n",
  281. h->active_downloads,
  282. h->max_parallel_requests,
  283. num_probes_change,
  284. num_downloads_change,
  285. GNUNET_STRINGS_relative_time_to_string (restart_at, GNUNET_YES));
  286. /* make sure we run again, callbacks might have
  287. already re-scheduled the job, so cancel such
  288. an operation (if it exists) */
  289. if (NULL != h->queue_job)
  290. GNUNET_SCHEDULER_cancel (h->queue_job);
  291. h->queue_job =
  292. GNUNET_SCHEDULER_add_delayed (restart_at, &process_job_queue, h);
  293. }
  294. /**
  295. * Add a job to the queue.
  296. *
  297. * @param h handle to the overall FS state
  298. * @param start function to call to begin the job
  299. * @param stop function to call to pause the job, or on dequeue (if the job was running)
  300. * @param cls closure for start and stop
  301. * @param blocks number of blocks this jobs uses
  302. * @param priority how important is this download
  303. * @return queue handle
  304. */
  305. struct GNUNET_FS_QueueEntry *
  306. GNUNET_FS_queue_ (struct GNUNET_FS_Handle *h,
  307. GNUNET_SCHEDULER_TaskCallback start,
  308. GNUNET_SCHEDULER_TaskCallback stop,
  309. void *cls,
  310. unsigned int blocks,
  311. enum GNUNET_FS_QueuePriority priority)
  312. {
  313. struct GNUNET_FS_QueueEntry *qe;
  314. qe = GNUNET_new (struct GNUNET_FS_QueueEntry);
  315. qe->h = h;
  316. qe->start = start;
  317. qe->stop = stop;
  318. qe->cls = cls;
  319. qe->queue_time = GNUNET_TIME_absolute_get ();
  320. qe->blocks = blocks;
  321. qe->priority = priority;
  322. GNUNET_CONTAINER_DLL_insert_after (h->pending_head,
  323. h->pending_tail,
  324. h->pending_tail,
  325. qe);
  326. if (NULL != h->queue_job)
  327. GNUNET_SCHEDULER_cancel (h->queue_job);
  328. h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
  329. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Queueing job %p\n", qe);
  330. return qe;
  331. }
  332. /**
  333. * Dequeue a job from the queue.
  334. *
  335. * @param qe handle for the job
  336. */
  337. void
  338. GNUNET_FS_dequeue_ (struct GNUNET_FS_QueueEntry *qe)
  339. {
  340. struct GNUNET_FS_Handle *h;
  341. h = qe->h;
  342. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Dequeueing job %p\n", qe);
  343. if (GNUNET_YES == qe->active)
  344. stop_job (qe);
  345. GNUNET_CONTAINER_DLL_remove (h->pending_head, h->pending_tail, qe);
  346. GNUNET_free (qe);
  347. if (NULL != h->queue_job)
  348. GNUNET_SCHEDULER_cancel (h->queue_job);
  349. h->queue_job = GNUNET_SCHEDULER_add_now (&process_job_queue, h);
  350. }
  351. /**
  352. * Create a top-level activity entry.
  353. *
  354. * @param h global fs handle
  355. * @param ssf suspend signal function to use
  356. * @param ssf_cls closure for @a ssf
  357. * @return fresh top-level activity handle
  358. */
  359. struct TopLevelActivity *
  360. GNUNET_FS_make_top (struct GNUNET_FS_Handle *h,
  361. SuspendSignalFunction ssf,
  362. void *ssf_cls)
  363. {
  364. struct TopLevelActivity *ret;
  365. ret = GNUNET_new (struct TopLevelActivity);
  366. ret->ssf = ssf;
  367. ret->ssf_cls = ssf_cls;
  368. GNUNET_CONTAINER_DLL_insert (h->top_head, h->top_tail, ret);
  369. return ret;
  370. }
  371. /**
  372. * Destroy a top-level activity entry.
  373. *
  374. * @param h global fs handle
  375. * @param top top level activity entry
  376. */
  377. void
  378. GNUNET_FS_end_top (struct GNUNET_FS_Handle *h, struct TopLevelActivity *top)
  379. {
  380. GNUNET_CONTAINER_DLL_remove (h->top_head, h->top_tail, top);
  381. GNUNET_free (top);
  382. }
  383. /**
  384. * Closure for #GNUNET_FS_data_reader_file_().
  385. */
  386. struct FileInfo
  387. {
  388. /**
  389. * Name of the file to read.
  390. */
  391. char *filename;
  392. /**
  393. * File descriptor, NULL if it has not yet been opened.
  394. */
  395. struct GNUNET_DISK_FileHandle *fd;
  396. };
  397. /**
  398. * Function that provides data by reading from a file.
  399. *
  400. * @param cls closure with the `struct FileInfo *`
  401. * @param offset offset to read from; it is possible
  402. * that the caller might need to go backwards
  403. * a bit at times; set to `UINT64_MAX` to tell
  404. * the reader that we won't be reading for a while
  405. * (used to close the file descriptor but NOT fully
  406. * clean up the reader's state); in this case,
  407. * a value of '0' for @a max should be ignored
  408. * @param max maximum number of bytes that should be
  409. * copied to @a buf; readers are not allowed
  410. * to provide less data unless there is an error;
  411. * a value of "0" will be used at the end to allow
  412. * the reader to clean up its internal state
  413. * @param buf where the reader should write the data
  414. * @param emsg location for the reader to store an error message
  415. * @return number of bytes written, usually @a max, 0 on error
  416. */
  417. size_t
  418. GNUNET_FS_data_reader_file_ (void *cls,
  419. uint64_t offset,
  420. size_t max,
  421. void *buf,
  422. char **emsg)
  423. {
  424. struct FileInfo *fi = cls;
  425. ssize_t ret;
  426. if (UINT64_MAX == offset)
  427. {
  428. if (NULL != fi->fd)
  429. {
  430. GNUNET_DISK_file_close (fi->fd);
  431. fi->fd = NULL;
  432. }
  433. return 0;
  434. }
  435. if (0 == max)
  436. {
  437. if (NULL != fi->fd)
  438. GNUNET_DISK_file_close (fi->fd);
  439. GNUNET_free (fi->filename);
  440. GNUNET_free (fi);
  441. return 0;
  442. }
  443. if (NULL == fi->fd)
  444. {
  445. fi->fd = GNUNET_DISK_file_open (fi->filename,
  446. GNUNET_DISK_OPEN_READ,
  447. GNUNET_DISK_PERM_NONE);
  448. if (NULL == fi->fd)
  449. {
  450. GNUNET_asprintf (emsg,
  451. _ ("Could not open file `%s': %s"),
  452. fi->filename,
  453. strerror (errno));
  454. return 0;
  455. }
  456. }
  457. if ((GNUNET_SYSERR ==
  458. GNUNET_DISK_file_seek (fi->fd, offset, GNUNET_DISK_SEEK_SET)) ||
  459. (-1 == (ret = GNUNET_DISK_file_read (fi->fd, buf, max))))
  460. {
  461. GNUNET_asprintf (emsg,
  462. _ ("Could not read file `%s': %s"),
  463. fi->filename,
  464. strerror (errno));
  465. return 0;
  466. }
  467. if (ret != max)
  468. {
  469. GNUNET_asprintf (emsg,
  470. _ ("Short read reading from file `%s'!"),
  471. fi->filename);
  472. return 0;
  473. }
  474. return max;
  475. }
  476. /**
  477. * Create the closure for the #GNUNET_FS_data_reader_file_() callback.
  478. *
  479. * @param filename file to read
  480. * @return closure to use, NULL on error
  481. */
  482. void *
  483. GNUNET_FS_make_file_reader_context_ (const char *filename)
  484. {
  485. struct FileInfo *fi;
  486. fi = GNUNET_new (struct FileInfo);
  487. fi->filename = GNUNET_STRINGS_filename_expand (filename);
  488. if (NULL == fi->filename)
  489. {
  490. GNUNET_free (fi);
  491. return NULL;
  492. }
  493. return fi;
  494. }
  495. /**
  496. * Function that provides data by copying from a buffer.
  497. *
  498. * @param cls closure (points to the buffer)
  499. * @param offset offset to read from; it is possible
  500. * that the caller might need to go backwards
  501. * a bit at times; set to `UINT64_MAX` to tell
  502. * the reader that we won't be reading for a while
  503. * (used to close the file descriptor but NOT fully
  504. * clean up the reader's state); in this case,
  505. * a value of '0' for @a max should be ignored
  506. * @param max maximum number of bytes that should be
  507. * copied to @a buf; readers are not allowed
  508. * to provide less data unless there is an error;
  509. * a value of "0" will be used at the end to allow
  510. * the reader to clean up its internal state
  511. * @param buf where the reader should write the data
  512. * @param emsg location for the reader to store an error message
  513. * @return number of bytes written, usually @a max, 0 on error
  514. */
  515. size_t
  516. GNUNET_FS_data_reader_copy_ (void *cls,
  517. uint64_t offset,
  518. size_t max,
  519. void *buf,
  520. char **emsg)
  521. {
  522. char *data = cls;
  523. if (UINT64_MAX == offset)
  524. return 0;
  525. if (0 == max)
  526. {
  527. GNUNET_free (data);
  528. return 0;
  529. }
  530. GNUNET_memcpy (buf, &data[offset], max);
  531. return max;
  532. }
  533. /**
  534. * Return the full filename where we would store state information
  535. * (for serialization/deserialization).
  536. *
  537. * @param h master context
  538. * @param ext component of the path
  539. * @param ent entity identifier (or emtpy string for the directory)
  540. * @return NULL on error
  541. */
  542. static char *
  543. get_serialization_file_name (struct GNUNET_FS_Handle *h,
  544. const char *ext,
  545. const char *ent)
  546. {
  547. char *basename;
  548. char *ret;
  549. if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
  550. return NULL; /* persistence not requested */
  551. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (h->cfg,
  552. "fs",
  553. "STATE_DIR",
  554. &basename))
  555. return NULL;
  556. GNUNET_asprintf (&ret,
  557. "%s%s%s%s%s%s%s",
  558. basename,
  559. DIR_SEPARATOR_STR,
  560. h->client_name,
  561. DIR_SEPARATOR_STR,
  562. ext,
  563. DIR_SEPARATOR_STR,
  564. ent);
  565. GNUNET_free (basename);
  566. return ret;
  567. }
  568. /**
  569. * Return the full filename where we would store state information
  570. * (for serialization/deserialization) that is associated with a
  571. * parent operation.
  572. *
  573. * @param h master context
  574. * @param ext component of the path
  575. * @param uni name of the parent operation
  576. * @param ent entity identifier (or emtpy string for the directory)
  577. * @return NULL on error
  578. */
  579. static char *
  580. get_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
  581. const char *ext,
  582. const char *uni,
  583. const char *ent)
  584. {
  585. char *basename;
  586. char *ret;
  587. if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
  588. return NULL; /* persistence not requested */
  589. if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (h->cfg,
  590. "fs",
  591. "STATE_DIR",
  592. &basename))
  593. return NULL;
  594. GNUNET_asprintf (&ret,
  595. "%s%s%s%s%s%s%s.dir%s%s",
  596. basename,
  597. DIR_SEPARATOR_STR,
  598. h->client_name,
  599. DIR_SEPARATOR_STR,
  600. ext,
  601. DIR_SEPARATOR_STR,
  602. uni,
  603. DIR_SEPARATOR_STR,
  604. ent);
  605. GNUNET_free (basename);
  606. return ret;
  607. }
  608. /**
  609. * Return a read handle for deserialization.
  610. *
  611. * @param h master context
  612. * @param ext component of the path
  613. * @param ent entity identifier (or emtpy string for the directory)
  614. * @return NULL on error
  615. */
  616. static struct GNUNET_BIO_ReadHandle *
  617. get_read_handle (struct GNUNET_FS_Handle *h, const char *ext, const char *ent)
  618. {
  619. char *fn;
  620. struct GNUNET_BIO_ReadHandle *ret;
  621. fn = get_serialization_file_name (h, ext, ent);
  622. if (NULL == fn)
  623. return NULL;
  624. ret = GNUNET_BIO_read_open_file (fn);
  625. GNUNET_free (fn);
  626. return ret;
  627. }
  628. /**
  629. * Return a write handle for serialization.
  630. *
  631. * @param h master context
  632. * @param ext component of the path
  633. * @param ent entity identifier (or emtpy string for the directory)
  634. * @return NULL on error
  635. */
  636. static struct GNUNET_BIO_WriteHandle *
  637. get_write_handle (struct GNUNET_FS_Handle *h, const char *ext, const char *ent)
  638. {
  639. char *fn;
  640. struct GNUNET_BIO_WriteHandle *ret;
  641. fn = get_serialization_file_name (h, ext, ent);
  642. if (NULL == fn)
  643. return NULL;
  644. ret = GNUNET_BIO_write_open_file (fn);
  645. GNUNET_break (NULL != ret);
  646. GNUNET_free (fn);
  647. return ret;
  648. }
  649. /**
  650. * Return a write handle for serialization.
  651. *
  652. * @param h master context
  653. * @param ext component of the path
  654. * @param uni name of parent
  655. * @param ent entity identifier (or emtpy string for the directory)
  656. * @return NULL on error
  657. */
  658. static struct GNUNET_BIO_WriteHandle *
  659. get_write_handle_in_dir (struct GNUNET_FS_Handle *h,
  660. const char *ext,
  661. const char *uni,
  662. const char *ent)
  663. {
  664. char *fn;
  665. struct GNUNET_BIO_WriteHandle *ret;
  666. fn = get_serialization_file_name_in_dir (h, ext, uni, ent);
  667. if (NULL == fn)
  668. return NULL;
  669. ret = GNUNET_BIO_write_open_file (fn);
  670. GNUNET_free (fn);
  671. return ret;
  672. }
  673. /**
  674. * Remove serialization/deserialization file from disk.
  675. *
  676. * @param h master context
  677. * @param ext component of the path
  678. * @param ent entity identifier
  679. */
  680. void
  681. GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
  682. const char *ext,
  683. const char *ent)
  684. {
  685. char *filename;
  686. if ((NULL == ent) || (0 == strlen (ent)))
  687. {
  688. GNUNET_break (0);
  689. return;
  690. }
  691. filename = get_serialization_file_name (h, ext, ent);
  692. if (NULL != filename)
  693. {
  694. if ((0 != unlink (filename)) && (ENOENT != errno))
  695. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
  696. GNUNET_free (filename);
  697. }
  698. }
  699. /**
  700. * Remove serialization/deserialization file from disk.
  701. *
  702. * @param h master context
  703. * @param ext component of the path
  704. * @param uni parent name
  705. * @param ent entity identifier
  706. */
  707. static void
  708. remove_sync_file_in_dir (struct GNUNET_FS_Handle *h,
  709. const char *ext,
  710. const char *uni,
  711. const char *ent)
  712. {
  713. char *filename;
  714. if ((NULL == ent) || (0 == strlen (ent)))
  715. {
  716. GNUNET_break (0);
  717. return;
  718. }
  719. filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
  720. if (NULL == filename)
  721. return;
  722. if (0 != unlink (filename))
  723. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
  724. GNUNET_free (filename);
  725. }
  726. /**
  727. * Remove serialization/deserialization directory from disk.
  728. *
  729. * @param h master context
  730. * @param ext component of the path
  731. * @param uni unique name of parent
  732. */
  733. void
  734. GNUNET_FS_remove_sync_dir_ (struct GNUNET_FS_Handle *h,
  735. const char *ext,
  736. const char *uni)
  737. {
  738. char *dn;
  739. if (NULL == uni)
  740. return;
  741. dn = get_serialization_file_name_in_dir (h, ext, uni, "");
  742. if (NULL == dn)
  743. return;
  744. if ((GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES)) &&
  745. (GNUNET_OK != GNUNET_DISK_directory_remove (dn)))
  746. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", dn);
  747. GNUNET_free (dn);
  748. }
  749. /**
  750. * Serialize a start-time. Since we use start-times to
  751. * calculate the duration of some operation, we actually
  752. * do not serialize the absolute time but the (relative)
  753. * duration since the start time. When we then
  754. * deserialize the start time, we take the current time and
  755. * subtract that duration so that we get again an absolute
  756. * time stamp that will result in correct performance
  757. * calculations.
  758. *
  759. * @param wh handle for writing
  760. * @param timestamp time to serialize
  761. * @return #GNUNET_OK on success
  762. */
  763. static int
  764. write_start_time (struct GNUNET_BIO_WriteHandle *wh,
  765. struct GNUNET_TIME_Absolute timestamp)
  766. {
  767. struct GNUNET_TIME_Relative dur;
  768. dur = GNUNET_TIME_absolute_get_duration (timestamp);
  769. return GNUNET_BIO_write_int64 (wh, "start time", dur.rel_value_us);
  770. }
  771. /**
  772. * Deserialize a start-time. Since we use start-times to
  773. * calculate the duration of some operation, we actually
  774. * do not serialize the absolute time but the (relative)
  775. * duration since the start time. Thus, when we then
  776. * deserialize the start time, we take the current time and
  777. * subtract that duration so that we get again an absolute
  778. * time stamp that will result in correct performance
  779. * calculations.
  780. *
  781. * @param rh handle for reading
  782. * @param timestamp where to write the deserialized timestamp
  783. * @return #GNUNET_OK on success
  784. */
  785. static int
  786. read_start_time (struct GNUNET_BIO_ReadHandle *rh,
  787. struct GNUNET_TIME_Absolute *timestamp)
  788. {
  789. struct GNUNET_TIME_Relative dur;
  790. if (GNUNET_OK != GNUNET_BIO_read_int64 (rh, "start time",
  791. (int64_t *) &dur.rel_value_us))
  792. return GNUNET_SYSERR;
  793. *timestamp = GNUNET_TIME_absolute_subtract (GNUNET_TIME_absolute_get (), dur);
  794. return GNUNET_OK;
  795. }
  796. /**
  797. * Using the given serialization filename, try to deserialize
  798. * the file-information tree associated with it.
  799. *
  800. * @param h master context
  801. * @param filename name of the file (without directory) with
  802. * the infromation
  803. * @return NULL on error
  804. */
  805. static struct GNUNET_FS_FileInformation *
  806. deserialize_file_information (struct GNUNET_FS_Handle *h, const char *filename);
  807. /**
  808. * Using the given serialization filename, try to deserialize
  809. * the file-information tree associated with it.
  810. *
  811. * @param h master context
  812. * @param fn name of the file (without directory) with
  813. * the infromation
  814. * @param rh handle for reading
  815. * @return NULL on error
  816. */
  817. static struct GNUNET_FS_FileInformation *
  818. deserialize_fi_node (struct GNUNET_FS_Handle *h,
  819. const char *fn,
  820. struct GNUNET_BIO_ReadHandle *rh)
  821. {
  822. struct GNUNET_FS_FileInformation *ret;
  823. struct GNUNET_FS_FileInformation *nxt;
  824. char b;
  825. char *ksks;
  826. char *chks;
  827. char *skss;
  828. char *filename;
  829. uint32_t dsize;
  830. if (GNUNET_OK != GNUNET_BIO_read (rh, "status flag", &b, sizeof(b)))
  831. {
  832. GNUNET_break (0);
  833. return NULL;
  834. }
  835. ret = GNUNET_new (struct GNUNET_FS_FileInformation);
  836. ret->h = h;
  837. ksks = NULL;
  838. chks = NULL;
  839. skss = NULL;
  840. filename = NULL;
  841. if ((GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "metadata", &ret->meta)) ||
  842. (GNUNET_OK != GNUNET_BIO_read_string (rh, "ksk-uri", &ksks, 32 * 1024)) ||
  843. ((NULL != ksks) &&
  844. ((NULL == (ret->keywords = GNUNET_FS_uri_parse (ksks, NULL))) ||
  845. (GNUNET_YES != GNUNET_FS_uri_test_ksk (ret->keywords)))) ||
  846. (GNUNET_OK != GNUNET_BIO_read_string (rh, "chk-uri", &chks, 1024)) ||
  847. ((NULL != chks) &&
  848. ((NULL == (ret->chk_uri = GNUNET_FS_uri_parse (chks, NULL))) ||
  849. (GNUNET_YES != GNUNET_FS_uri_test_chk (ret->chk_uri)))) ||
  850. (GNUNET_OK != GNUNET_BIO_read_string (rh, "sks-uri", &skss, 1024)) ||
  851. ((NULL != skss) &&
  852. ((NULL == (ret->sks_uri = GNUNET_FS_uri_parse (skss, NULL))) ||
  853. (GNUNET_YES != GNUNET_FS_uri_test_sks (ret->sks_uri)))) ||
  854. (GNUNET_OK != read_start_time (rh, &ret->start_time)) ||
  855. (GNUNET_OK !=
  856. GNUNET_BIO_read_string (rh, "emsg", &ret->emsg, 16 * 1024)) ||
  857. (GNUNET_OK !=
  858. GNUNET_BIO_read_string (rh, "fn", &ret->filename, 16 * 1024)) ||
  859. (GNUNET_OK !=
  860. GNUNET_BIO_read_int64 (
  861. rh,
  862. "expiration time",
  863. (int64_t *) &ret->bo.expiration_time.abs_value_us)) ||
  864. (GNUNET_OK != GNUNET_BIO_read_int32 (
  865. rh,
  866. "anonymity level",
  867. (int32_t *) &ret->bo.anonymity_level)) ||
  868. (GNUNET_OK != GNUNET_BIO_read_int32 (
  869. rh,
  870. "content priority",
  871. (int32_t *) &ret->bo.content_priority)) ||
  872. (GNUNET_OK != GNUNET_BIO_read_int32 (
  873. rh,
  874. "replication level",
  875. (int32_t *) &ret->bo.replication_level)))
  876. {
  877. GNUNET_break (0);
  878. goto cleanup;
  879. }
  880. switch (b)
  881. {
  882. case 0: /* file-insert */
  883. if (GNUNET_OK != GNUNET_BIO_read_int64 (
  884. rh,
  885. "file size",
  886. (int64_t *) &ret->data.file.file_size))
  887. {
  888. GNUNET_break (0);
  889. goto cleanup;
  890. }
  891. ret->is_directory = GNUNET_NO;
  892. ret->data.file.do_index = GNUNET_NO;
  893. ret->data.file.have_hash = GNUNET_NO;
  894. ret->data.file.index_start_confirmed = GNUNET_NO;
  895. if (GNUNET_NO == ret->is_published)
  896. {
  897. if (NULL == ret->filename)
  898. {
  899. ret->data.file.reader = &GNUNET_FS_data_reader_copy_;
  900. ret->data.file.reader_cls =
  901. GNUNET_malloc_large (ret->data.file.file_size);
  902. if (ret->data.file.reader_cls == NULL)
  903. goto cleanup;
  904. if (GNUNET_OK != GNUNET_BIO_read (rh,
  905. "file-data",
  906. ret->data.file.reader_cls,
  907. ret->data.file.file_size))
  908. {
  909. GNUNET_break (0);
  910. goto cleanup;
  911. }
  912. }
  913. else
  914. {
  915. ret->data.file.reader = &GNUNET_FS_data_reader_file_;
  916. ret->data.file.reader_cls =
  917. GNUNET_FS_make_file_reader_context_ (ret->filename);
  918. }
  919. }
  920. break;
  921. case 1: /* file-index, no hash */
  922. if (NULL == ret->filename)
  923. {
  924. GNUNET_break (0);
  925. goto cleanup;
  926. }
  927. if (GNUNET_OK != GNUNET_BIO_read_int64 (
  928. rh,
  929. "file size",
  930. (int64_t *) &ret->data.file.file_size))
  931. {
  932. GNUNET_break (0);
  933. goto cleanup;
  934. }
  935. ret->is_directory = GNUNET_NO;
  936. ret->data.file.do_index = GNUNET_YES;
  937. ret->data.file.have_hash = GNUNET_NO;
  938. ret->data.file.index_start_confirmed = GNUNET_NO;
  939. ret->data.file.reader = &GNUNET_FS_data_reader_file_;
  940. ret->data.file.reader_cls =
  941. GNUNET_FS_make_file_reader_context_ (ret->filename);
  942. break;
  943. case 2: /* file-index-with-hash */
  944. if (NULL == ret->filename)
  945. {
  946. GNUNET_break (0);
  947. goto cleanup;
  948. }
  949. if ((GNUNET_OK != GNUNET_BIO_read_int64 (
  950. rh,
  951. "file size",
  952. (int64_t *) &ret->data.file.file_size)) ||
  953. (GNUNET_OK != GNUNET_BIO_read (rh,
  954. "fileid",
  955. &ret->data.file.file_id,
  956. sizeof(struct GNUNET_HashCode))))
  957. {
  958. GNUNET_break (0);
  959. goto cleanup;
  960. }
  961. ret->is_directory = GNUNET_NO;
  962. ret->data.file.do_index = GNUNET_YES;
  963. ret->data.file.have_hash = GNUNET_YES;
  964. ret->data.file.index_start_confirmed = GNUNET_NO;
  965. ret->data.file.reader = &GNUNET_FS_data_reader_file_;
  966. ret->data.file.reader_cls =
  967. GNUNET_FS_make_file_reader_context_ (ret->filename);
  968. break;
  969. case 3: /* file-index-with-hash-confirmed */
  970. if (NULL == ret->filename)
  971. {
  972. GNUNET_break (0);
  973. goto cleanup;
  974. }
  975. if ((GNUNET_OK != GNUNET_BIO_read_int64 (
  976. rh,
  977. "file size",
  978. (int64_t *) &ret->data.file.file_size)) ||
  979. (GNUNET_OK != GNUNET_BIO_read (rh,
  980. "fileid",
  981. &ret->data.file.file_id,
  982. sizeof(struct GNUNET_HashCode))))
  983. {
  984. GNUNET_break (0);
  985. goto cleanup;
  986. }
  987. ret->is_directory = GNUNET_NO;
  988. ret->data.file.do_index = GNUNET_YES;
  989. ret->data.file.have_hash = GNUNET_YES;
  990. ret->data.file.index_start_confirmed = GNUNET_YES;
  991. ret->data.file.reader = &GNUNET_FS_data_reader_file_;
  992. ret->data.file.reader_cls =
  993. GNUNET_FS_make_file_reader_context_ (ret->filename);
  994. break;
  995. case 4: /* directory */
  996. ret->is_directory = GNUNET_YES;
  997. if ((GNUNET_OK != GNUNET_BIO_read_int32 (rh, "dsize",
  998. (int32_t *) &dsize)) ||
  999. (GNUNET_OK !=
  1000. GNUNET_BIO_read_int64 (
  1001. rh,
  1002. "contents completed",
  1003. (int64_t *) &ret->data.dir.contents_completed)) ||
  1004. (GNUNET_OK !=
  1005. GNUNET_BIO_read_int64 (
  1006. rh,
  1007. "contents size",
  1008. (int64_t *) &ret->data.dir.contents_size)) ||
  1009. (NULL == (ret->data.dir.dir_data = GNUNET_malloc_large (dsize))) ||
  1010. (GNUNET_OK !=
  1011. GNUNET_BIO_read (rh, "dir-data", ret->data.dir.dir_data, dsize)) ||
  1012. (GNUNET_OK !=
  1013. GNUNET_BIO_read_string (rh, "ent-filename", &filename, 16 * 1024)))
  1014. {
  1015. GNUNET_break (0);
  1016. goto cleanup;
  1017. }
  1018. ret->data.dir.dir_size = (uint32_t) dsize;
  1019. if (NULL != filename)
  1020. {
  1021. ret->data.dir.entries = deserialize_file_information (h, filename);
  1022. GNUNET_free (filename);
  1023. filename = NULL;
  1024. nxt = ret->data.dir.entries;
  1025. while (NULL != nxt)
  1026. {
  1027. nxt->dir = ret;
  1028. nxt = nxt->next;
  1029. }
  1030. }
  1031. break;
  1032. default:
  1033. GNUNET_break (0);
  1034. goto cleanup;
  1035. }
  1036. ret->serialization = GNUNET_strdup (fn);
  1037. if (GNUNET_OK !=
  1038. GNUNET_BIO_read_string (rh, "nxt-filename", &filename, 16 * 1024))
  1039. {
  1040. GNUNET_break (0);
  1041. goto cleanup;
  1042. }
  1043. if (NULL != filename)
  1044. {
  1045. ret->next = deserialize_file_information (h, filename);
  1046. GNUNET_free (filename);
  1047. filename = NULL;
  1048. }
  1049. GNUNET_free (ksks);
  1050. GNUNET_free (skss);
  1051. GNUNET_free (chks);
  1052. return ret;
  1053. cleanup:
  1054. GNUNET_free (ksks);
  1055. GNUNET_free (chks);
  1056. GNUNET_free (skss);
  1057. GNUNET_free (filename);
  1058. GNUNET_FS_file_information_destroy (ret, NULL, NULL);
  1059. return NULL;
  1060. }
  1061. /**
  1062. * Using the given serialization filename, try to deserialize
  1063. * the file-information tree associated with it.
  1064. *
  1065. * @param h master context
  1066. * @param filename name of the file (without directory) with
  1067. * the infromation
  1068. * @return NULL on error
  1069. */
  1070. static struct GNUNET_FS_FileInformation *
  1071. deserialize_file_information (struct GNUNET_FS_Handle *h, const char *filename)
  1072. {
  1073. struct GNUNET_FS_FileInformation *ret;
  1074. struct GNUNET_BIO_ReadHandle *rh;
  1075. char *emsg;
  1076. char *fn;
  1077. rh = get_read_handle (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
  1078. if (NULL == rh)
  1079. return NULL;
  1080. ret = deserialize_fi_node (h, filename, rh);
  1081. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  1082. {
  1083. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1084. _ ("Failed to resume publishing information `%s': %s\n"),
  1085. filename,
  1086. emsg);
  1087. GNUNET_free (emsg);
  1088. }
  1089. if (NULL == ret)
  1090. {
  1091. fn =
  1092. get_serialization_file_name (h, GNUNET_FS_SYNC_PATH_FILE_INFO, filename);
  1093. if (NULL != fn)
  1094. {
  1095. if (0 != unlink (fn))
  1096. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
  1097. GNUNET_free (fn);
  1098. }
  1099. }
  1100. return ret;
  1101. }
  1102. /**
  1103. * Given a serialization name (full absolute path), return the
  1104. * basename of the file (without the path), which must only
  1105. * consist of the 6 random characters.
  1106. *
  1107. * @param fullname name to extract the basename from
  1108. * @return copy of the basename, NULL on error
  1109. */
  1110. static char *
  1111. get_serialization_short_name (const char *fullname)
  1112. {
  1113. const char *end;
  1114. const char *nxt;
  1115. end = NULL;
  1116. nxt = fullname;
  1117. /* FIXME: we could do this faster since we know
  1118. * the length of 'end'... */
  1119. while ('\0' != *nxt)
  1120. {
  1121. if (DIR_SEPARATOR == *nxt)
  1122. end = nxt + 1;
  1123. nxt++;
  1124. }
  1125. if ((NULL == end) || (0 == strlen (end)))
  1126. {
  1127. GNUNET_break (0);
  1128. return NULL;
  1129. }
  1130. GNUNET_break (6 == strlen (end));
  1131. return GNUNET_strdup (end);
  1132. }
  1133. /**
  1134. * Create a new random name for serialization. Also checks if persistence
  1135. * is enabled and returns NULL if not.
  1136. *
  1137. * @param h master context
  1138. * @param ext component of the path
  1139. * @return NULL on errror
  1140. */
  1141. static char *
  1142. make_serialization_file_name (struct GNUNET_FS_Handle *h, const char *ext)
  1143. {
  1144. char *fn;
  1145. char *dn;
  1146. char *ret;
  1147. if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
  1148. return NULL; /* persistence not requested */
  1149. dn = get_serialization_file_name (h, ext, "");
  1150. if (NULL == dn)
  1151. return NULL;
  1152. if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
  1153. {
  1154. GNUNET_free (dn);
  1155. return NULL;
  1156. }
  1157. fn = GNUNET_DISK_mktemp (dn);
  1158. GNUNET_free (dn);
  1159. if (NULL == fn)
  1160. return NULL; /* epic fail */
  1161. ret = get_serialization_short_name (fn);
  1162. GNUNET_free (fn);
  1163. return ret;
  1164. }
  1165. /**
  1166. * Create a new random name for serialization. Also checks if persistence
  1167. * is enabled and returns NULL if not.
  1168. *
  1169. * @param h master context
  1170. * @param ext component of the path
  1171. * @param uni name of parent
  1172. * @return NULL on errror
  1173. */
  1174. static char *
  1175. make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
  1176. const char *ext,
  1177. const char *uni)
  1178. {
  1179. char *fn;
  1180. char *dn;
  1181. char *ret;
  1182. if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
  1183. return NULL; /* persistence not requested */
  1184. dn = get_serialization_file_name_in_dir (h, ext, uni, "");
  1185. if (NULL == dn)
  1186. return NULL;
  1187. if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dn))
  1188. {
  1189. GNUNET_free (dn);
  1190. return NULL;
  1191. }
  1192. fn = GNUNET_DISK_mktemp (dn);
  1193. GNUNET_free (dn);
  1194. if (NULL == fn)
  1195. return NULL; /* epic fail */
  1196. ret = get_serialization_short_name (fn);
  1197. GNUNET_free (fn);
  1198. return ret;
  1199. }
  1200. /**
  1201. * Copy all of the data from the reader to the write handle.
  1202. *
  1203. * @param wh write handle
  1204. * @param fi file with reader
  1205. * @return #GNUNET_OK on success
  1206. */
  1207. static int
  1208. copy_from_reader (struct GNUNET_BIO_WriteHandle *wh,
  1209. struct GNUNET_FS_FileInformation *fi)
  1210. {
  1211. char buf[32 * 1024];
  1212. uint64_t off;
  1213. size_t ret;
  1214. size_t left;
  1215. char *emsg;
  1216. emsg = NULL;
  1217. off = 0;
  1218. while (off < fi->data.file.file_size)
  1219. {
  1220. left = GNUNET_MIN (sizeof(buf), fi->data.file.file_size - off);
  1221. ret =
  1222. fi->data.file.reader (fi->data.file.reader_cls, off, left, buf, &emsg);
  1223. if (0 == ret)
  1224. {
  1225. GNUNET_free (emsg);
  1226. return GNUNET_SYSERR;
  1227. }
  1228. if (GNUNET_OK != GNUNET_BIO_write (wh, "copied from reader", buf, ret))
  1229. return GNUNET_SYSERR;
  1230. off += ret;
  1231. }
  1232. return GNUNET_OK;
  1233. }
  1234. /**
  1235. * Create a temporary file on disk to store the current
  1236. * state of @a fi in.
  1237. *
  1238. * @param fi file information to sync with disk
  1239. */
  1240. void
  1241. GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation *fi)
  1242. {
  1243. char *fn;
  1244. struct GNUNET_BIO_WriteHandle *wh;
  1245. char b;
  1246. char *ksks;
  1247. char *chks;
  1248. char *skss;
  1249. if (NULL == fi->serialization)
  1250. fi->serialization =
  1251. make_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO);
  1252. if (NULL == fi->serialization)
  1253. return;
  1254. wh =
  1255. get_write_handle (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO, fi->serialization);
  1256. if (NULL == wh)
  1257. {
  1258. GNUNET_free (fi->serialization);
  1259. fi->serialization = NULL;
  1260. return;
  1261. }
  1262. if (GNUNET_YES == fi->is_directory)
  1263. b = 4;
  1264. else if (GNUNET_YES == fi->data.file.index_start_confirmed)
  1265. b = 3;
  1266. else if (GNUNET_YES == fi->data.file.have_hash)
  1267. b = 2;
  1268. else if (GNUNET_YES == fi->data.file.do_index)
  1269. b = 1;
  1270. else
  1271. b = 0;
  1272. if (NULL != fi->keywords)
  1273. ksks = GNUNET_FS_uri_to_string (fi->keywords);
  1274. else
  1275. ksks = NULL;
  1276. if (NULL != fi->chk_uri)
  1277. chks = GNUNET_FS_uri_to_string (fi->chk_uri);
  1278. else
  1279. chks = NULL;
  1280. if (NULL != fi->sks_uri)
  1281. skss = GNUNET_FS_uri_to_string (fi->sks_uri);
  1282. else
  1283. skss = NULL;
  1284. struct GNUNET_BIO_WriteSpec ws1[] = {
  1285. GNUNET_BIO_write_spec_object ("b", &b, sizeof (b)),
  1286. GNUNET_BIO_write_spec_meta_data ("meta", fi->meta),
  1287. GNUNET_BIO_write_spec_string ("ksks", ksks),
  1288. GNUNET_BIO_write_spec_string ("chks", chks),
  1289. GNUNET_BIO_write_spec_string ("skss", skss),
  1290. GNUNET_BIO_write_spec_end (),
  1291. };
  1292. struct GNUNET_BIO_WriteSpec ws2[] = {
  1293. GNUNET_BIO_write_spec_string ("emsg", fi->emsg),
  1294. GNUNET_BIO_write_spec_string ("filename", fi->filename),
  1295. GNUNET_BIO_write_spec_int64 (
  1296. "expiration time",
  1297. (int64_t *) &fi->bo.expiration_time.abs_value_us),
  1298. GNUNET_BIO_write_spec_int32 (
  1299. "anonymity level",
  1300. (int32_t *) &fi->bo.anonymity_level),
  1301. GNUNET_BIO_write_spec_int32 (
  1302. "content priority",
  1303. (int32_t *) &fi->bo.content_priority),
  1304. GNUNET_BIO_write_spec_int32 (
  1305. "replication level",
  1306. (int32_t *) &fi->bo.replication_level),
  1307. GNUNET_BIO_write_spec_end (),
  1308. };
  1309. if ((GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws1)) ||
  1310. (GNUNET_OK != write_start_time (wh, fi->start_time)) ||
  1311. (GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws2)))
  1312. {
  1313. GNUNET_break (0);
  1314. goto cleanup;
  1315. }
  1316. GNUNET_free (chks);
  1317. chks = NULL;
  1318. GNUNET_free (ksks);
  1319. ksks = NULL;
  1320. GNUNET_free (skss);
  1321. skss = NULL;
  1322. switch (b)
  1323. {
  1324. case 0: /* file-insert */
  1325. if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, "file size",
  1326. fi->data.file.file_size))
  1327. {
  1328. GNUNET_break (0);
  1329. goto cleanup;
  1330. }
  1331. if ((GNUNET_NO == fi->is_published) && (NULL == fi->filename))
  1332. if (GNUNET_OK != copy_from_reader (wh, fi))
  1333. {
  1334. GNUNET_break (0);
  1335. goto cleanup;
  1336. }
  1337. break;
  1338. case 1: /* file-index, no hash */
  1339. if (NULL == fi->filename)
  1340. {
  1341. GNUNET_break (0);
  1342. goto cleanup;
  1343. }
  1344. if (GNUNET_OK != GNUNET_BIO_write_int64 (wh, "file size",
  1345. fi->data.file.file_size))
  1346. {
  1347. GNUNET_break (0);
  1348. goto cleanup;
  1349. }
  1350. break;
  1351. case 2: /* file-index-with-hash */
  1352. case 3: /* file-index-with-hash-confirmed */
  1353. if (NULL == fi->filename)
  1354. {
  1355. GNUNET_break (0);
  1356. goto cleanup;
  1357. }
  1358. if ((GNUNET_OK != GNUNET_BIO_write_int64 (wh, "file size",
  1359. fi->data.file.file_size)) ||
  1360. (GNUNET_OK != GNUNET_BIO_write (wh,
  1361. "file id",
  1362. &fi->data.file.file_id,
  1363. sizeof(struct GNUNET_HashCode))))
  1364. {
  1365. GNUNET_break (0);
  1366. goto cleanup;
  1367. }
  1368. break;
  1369. case 4: /* directory */
  1370. if ((NULL != fi->data.dir.entries) &&
  1371. (NULL == fi->data.dir.entries->serialization))
  1372. GNUNET_FS_file_information_sync_ (fi->data.dir.entries);
  1373. struct GNUNET_BIO_WriteSpec ws[] = {
  1374. GNUNET_BIO_write_spec_int32 ("dir size",
  1375. (int32_t *) &fi->data.dir.dir_size),
  1376. GNUNET_BIO_write_spec_int64 (
  1377. "contents completed",
  1378. (int64_t *) &fi->data.dir.contents_completed),
  1379. GNUNET_BIO_write_spec_int64 ("contents size",
  1380. (int64_t *) &fi->data.dir.contents_size),
  1381. GNUNET_BIO_write_spec_object ("dir data",
  1382. fi->data.dir.dir_data,
  1383. (uint32_t) fi->data.dir.dir_size),
  1384. GNUNET_BIO_write_spec_string ("dir entries",
  1385. (fi->data.dir.entries == NULL)
  1386. ? NULL
  1387. : fi->data.dir.entries->serialization),
  1388. GNUNET_BIO_write_spec_end (),
  1389. };
  1390. if ((GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws)))
  1391. {
  1392. GNUNET_break (0);
  1393. goto cleanup;
  1394. }
  1395. break;
  1396. default:
  1397. GNUNET_assert (0);
  1398. goto cleanup;
  1399. }
  1400. if ((NULL != fi->next) && (NULL == fi->next->serialization))
  1401. GNUNET_FS_file_information_sync_ (fi->next);
  1402. if (GNUNET_OK != GNUNET_BIO_write_string (wh,
  1403. "serialization",
  1404. (fi->next != NULL)
  1405. ? fi->next->serialization
  1406. : NULL))
  1407. {
  1408. GNUNET_break (0);
  1409. goto cleanup;
  1410. }
  1411. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  1412. {
  1413. wh = NULL;
  1414. GNUNET_break (0);
  1415. goto cleanup;
  1416. }
  1417. return; /* done! */
  1418. cleanup:
  1419. if (NULL != wh)
  1420. (void) GNUNET_BIO_write_close (wh, NULL);
  1421. GNUNET_free (chks);
  1422. GNUNET_free (ksks);
  1423. GNUNET_free (skss);
  1424. fn = get_serialization_file_name (fi->h,
  1425. GNUNET_FS_SYNC_PATH_FILE_INFO,
  1426. fi->serialization);
  1427. if (NULL != fn)
  1428. {
  1429. if (0 != unlink (fn))
  1430. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
  1431. GNUNET_free (fn);
  1432. }
  1433. GNUNET_free (fi->serialization);
  1434. fi->serialization = NULL;
  1435. }
  1436. /**
  1437. * Find the entry in the file information struct where the
  1438. * serialization filename matches the given name.
  1439. *
  1440. * @param pos file information to search
  1441. * @param srch filename to search for
  1442. * @return NULL if srch was not found in this subtree
  1443. */
  1444. static struct GNUNET_FS_FileInformation *
  1445. find_file_position (struct GNUNET_FS_FileInformation *pos, const char *srch)
  1446. {
  1447. struct GNUNET_FS_FileInformation *r;
  1448. while (NULL != pos)
  1449. {
  1450. if (0 == strcmp (srch, pos->serialization))
  1451. return pos;
  1452. if ((GNUNET_YES == pos->is_directory) &&
  1453. (NULL != (r = find_file_position (pos->data.dir.entries, srch))))
  1454. return r;
  1455. pos = pos->next;
  1456. }
  1457. return NULL;
  1458. }
  1459. /**
  1460. * Signal the FS's progress function that we are resuming
  1461. * an upload.
  1462. *
  1463. * @param cls closure (of type `struct GNUNET_FS_PublishContext *`, for the parent (!))
  1464. * @param fi the entry in the publish-structure
  1465. * @param length length of the file or directory
  1466. * @param meta metadata for the file or directory (can be modified)
  1467. * @param uri pointer to the keywords that will be used for this entry (can be modified)
  1468. * @param bo block options (can be modified)
  1469. * @param do_index should we index?
  1470. * @param client_info pointer to client context set upon creation (can be modified)
  1471. * @return #GNUNET_OK to continue (always)
  1472. */
  1473. static int
  1474. fip_signal_resume (void *cls,
  1475. struct GNUNET_FS_FileInformation *fi,
  1476. uint64_t length,
  1477. struct GNUNET_CONTAINER_MetaData *meta,
  1478. struct GNUNET_FS_Uri **uri,
  1479. struct GNUNET_FS_BlockOptions *bo,
  1480. int *do_index,
  1481. void **client_info)
  1482. {
  1483. struct GNUNET_FS_PublishContext *pc = cls;
  1484. struct GNUNET_FS_ProgressInfo pi;
  1485. if (GNUNET_YES == pc->skip_next_fi_callback)
  1486. {
  1487. pc->skip_next_fi_callback = GNUNET_NO;
  1488. return GNUNET_OK;
  1489. }
  1490. pi.status = GNUNET_FS_STATUS_PUBLISH_RESUME;
  1491. pi.value.publish.specifics.resume.message = fi->emsg;
  1492. pi.value.publish.specifics.resume.chk_uri = fi->chk_uri;
  1493. *client_info = GNUNET_FS_publish_make_status_ (&pi, pc, fi, 0);
  1494. if (GNUNET_YES == GNUNET_FS_meta_data_test_for_directory (meta))
  1495. {
  1496. /* process entries in directory */
  1497. pc->skip_next_fi_callback = GNUNET_YES;
  1498. GNUNET_FS_file_information_inspect (fi, &fip_signal_resume, pc);
  1499. }
  1500. return GNUNET_OK;
  1501. }
  1502. /**
  1503. * Function called with a filename of serialized publishing operation
  1504. * to deserialize.
  1505. *
  1506. * @param cls the `struct GNUNET_FS_Handle *`
  1507. * @param filename complete filename (absolute path)
  1508. * @return #GNUNET_OK (continue to iterate)
  1509. */
  1510. static int
  1511. deserialize_publish_file (void *cls, const char *filename)
  1512. {
  1513. struct GNUNET_FS_Handle *h = cls;
  1514. struct GNUNET_BIO_ReadHandle *rh;
  1515. struct GNUNET_FS_PublishContext *pc;
  1516. int32_t options;
  1517. int32_t all_done;
  1518. int32_t have_ns;
  1519. char *fi_root;
  1520. struct GNUNET_CRYPTO_EcdsaPrivateKey ns;
  1521. char *fi_pos;
  1522. char *emsg;
  1523. pc = GNUNET_new (struct GNUNET_FS_PublishContext);
  1524. pc->h = h;
  1525. pc->serialization = get_serialization_short_name (filename);
  1526. fi_root = NULL;
  1527. fi_pos = NULL;
  1528. rh = GNUNET_BIO_read_open_file (filename);
  1529. if (NULL == rh)
  1530. {
  1531. GNUNET_break (0);
  1532. goto cleanup;
  1533. }
  1534. struct GNUNET_BIO_ReadSpec rs[] = {
  1535. GNUNET_BIO_read_spec_string ("publish-nid", &pc->nid, 1024),
  1536. GNUNET_BIO_read_spec_string ("publish-nuid", &pc->nuid, 1024),
  1537. GNUNET_BIO_read_spec_int32 ("options", &options),
  1538. GNUNET_BIO_read_spec_int32 ("all done", &all_done),
  1539. GNUNET_BIO_read_spec_int32 ("have ns", &have_ns),
  1540. GNUNET_BIO_read_spec_string ("publish-firoot", &fi_root, 128),
  1541. GNUNET_BIO_read_spec_string ("publish-fipos", &fi_pos, 128),
  1542. GNUNET_BIO_read_spec_end (),
  1543. };
  1544. if ((GNUNET_OK != GNUNET_BIO_read_spec_commit (rh, rs)) ||
  1545. ((GNUNET_YES == have_ns) &&
  1546. (GNUNET_OK != GNUNET_BIO_read (rh, "publish-ns", &ns, sizeof(ns)))))
  1547. {
  1548. GNUNET_break (0);
  1549. goto cleanup;
  1550. }
  1551. pc->options = options;
  1552. pc->all_done = all_done;
  1553. if (NULL == fi_root)
  1554. {
  1555. GNUNET_break (0);
  1556. goto cleanup;
  1557. }
  1558. pc->fi = deserialize_file_information (h, fi_root);
  1559. if (NULL == pc->fi)
  1560. {
  1561. GNUNET_break (0);
  1562. goto cleanup;
  1563. }
  1564. if (GNUNET_YES == have_ns)
  1565. {
  1566. pc->ns = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
  1567. *pc->ns = ns;
  1568. }
  1569. if ((0 == (pc->options & GNUNET_FS_PUBLISH_OPTION_SIMULATE_ONLY)) &&
  1570. (GNUNET_YES != pc->all_done))
  1571. {
  1572. pc->dsh = GNUNET_DATASTORE_connect (h->cfg);
  1573. if (NULL == pc->dsh)
  1574. goto cleanup;
  1575. }
  1576. if (NULL != fi_pos)
  1577. {
  1578. pc->fi_pos = find_file_position (pc->fi, fi_pos);
  1579. GNUNET_free (fi_pos);
  1580. fi_pos = NULL;
  1581. if (NULL == pc->fi_pos)
  1582. {
  1583. /* failed to find position for resuming, outch! Will start from root! */
  1584. GNUNET_break (0);
  1585. if (GNUNET_YES != pc->all_done)
  1586. pc->fi_pos = pc->fi;
  1587. }
  1588. }
  1589. GNUNET_free (fi_root);
  1590. fi_root = NULL;
  1591. /* generate RESUME event(s) */
  1592. GNUNET_FS_file_information_inspect (pc->fi, &fip_signal_resume, pc);
  1593. /* re-start publishing (if needed)... */
  1594. if (GNUNET_YES != pc->all_done)
  1595. {
  1596. GNUNET_assert (NULL == pc->upload_task);
  1597. pc->upload_task =
  1598. GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_BACKGROUND,
  1599. &GNUNET_FS_publish_main_,
  1600. pc);
  1601. }
  1602. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  1603. {
  1604. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1605. _ ("Failure while resuming publishing operation `%s': %s\n"),
  1606. filename,
  1607. emsg);
  1608. GNUNET_free (emsg);
  1609. }
  1610. pc->top = GNUNET_FS_make_top (h, &GNUNET_FS_publish_signal_suspend_, pc);
  1611. return GNUNET_OK;
  1612. cleanup:
  1613. GNUNET_free (pc->nid);
  1614. GNUNET_free (pc->nuid);
  1615. GNUNET_free (fi_root);
  1616. GNUNET_free (fi_pos);
  1617. if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
  1618. {
  1619. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1620. _ ("Failed to resume publishing operation `%s': %s\n"),
  1621. filename,
  1622. emsg);
  1623. GNUNET_free (emsg);
  1624. }
  1625. if (NULL != pc->fi)
  1626. GNUNET_FS_file_information_destroy (pc->fi, NULL, NULL);
  1627. if (0 != unlink (filename))
  1628. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
  1629. GNUNET_free (pc->serialization);
  1630. GNUNET_free (pc);
  1631. return GNUNET_OK;
  1632. }
  1633. /**
  1634. * Synchronize this publishing struct with its mirror
  1635. * on disk. Note that all internal FS-operations that change
  1636. * publishing structs should already call "sync" internally,
  1637. * so this function is likely not useful for clients.
  1638. *
  1639. * @param pc the struct to sync
  1640. */
  1641. void
  1642. GNUNET_FS_publish_sync_ (struct GNUNET_FS_PublishContext *pc)
  1643. {
  1644. struct GNUNET_BIO_WriteHandle *wh;
  1645. int32_t have_ns;
  1646. if (NULL == pc->serialization)
  1647. pc->serialization =
  1648. make_serialization_file_name (pc->h, GNUNET_FS_SYNC_PATH_MASTER_PUBLISH);
  1649. if (NULL == pc->serialization)
  1650. return;
  1651. if (NULL == pc->fi)
  1652. return;
  1653. if (NULL == pc->fi->serialization)
  1654. {
  1655. GNUNET_break (0);
  1656. return;
  1657. }
  1658. wh = get_write_handle (pc->h,
  1659. GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
  1660. pc->serialization);
  1661. if (NULL == wh)
  1662. {
  1663. GNUNET_break (0);
  1664. goto cleanup;
  1665. }
  1666. have_ns = (NULL != pc->ns) ? GNUNET_YES : GNUNET_NO;
  1667. struct GNUNET_BIO_WriteSpec ws[] = {
  1668. GNUNET_BIO_write_spec_string ("nid", pc->nid),
  1669. GNUNET_BIO_write_spec_string ("nuid", pc->nuid),
  1670. GNUNET_BIO_write_spec_int32 ("options", (int32_t *) &pc->options),
  1671. GNUNET_BIO_write_spec_int32 ("all done", &pc->all_done),
  1672. GNUNET_BIO_write_spec_int32 ("have ns", &have_ns),
  1673. GNUNET_BIO_write_spec_string ("serialization", pc->fi->serialization),
  1674. GNUNET_BIO_write_spec_string ("pos serialization", (NULL == pc->fi_pos)
  1675. ? NULL
  1676. : pc->fi_pos->serialization),
  1677. GNUNET_BIO_read_spec_end ()
  1678. };
  1679. if ((GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws)) ||
  1680. ((NULL != pc->ns) &&
  1681. (GNUNET_OK !=
  1682. GNUNET_BIO_write (wh,
  1683. "ns",
  1684. pc->ns,
  1685. sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)))))
  1686. {
  1687. GNUNET_break (0);
  1688. goto cleanup;
  1689. }
  1690. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  1691. {
  1692. wh = NULL;
  1693. GNUNET_break (0);
  1694. goto cleanup;
  1695. }
  1696. return;
  1697. cleanup:
  1698. if (NULL != wh)
  1699. (void) GNUNET_BIO_write_close (wh, NULL);
  1700. GNUNET_FS_remove_sync_file_ (pc->h,
  1701. GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
  1702. pc->serialization);
  1703. GNUNET_free (pc->serialization);
  1704. pc->serialization = NULL;
  1705. }
  1706. /**
  1707. * Synchronize this unindex struct with its mirror
  1708. * on disk. Note that all internal FS-operations that change
  1709. * publishing structs should already call "sync" internally,
  1710. * so this function is likely not useful for clients.
  1711. *
  1712. * @param uc the struct to sync
  1713. */
  1714. void
  1715. GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
  1716. {
  1717. struct GNUNET_BIO_WriteHandle *wh;
  1718. char *uris;
  1719. if (NULL == uc->serialization)
  1720. uc->serialization =
  1721. make_serialization_file_name (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
  1722. if (NULL == uc->serialization)
  1723. return;
  1724. wh = get_write_handle (uc->h,
  1725. GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
  1726. uc->serialization);
  1727. if (NULL == wh)
  1728. {
  1729. GNUNET_break (0);
  1730. goto cleanup;
  1731. }
  1732. if (NULL != uc->ksk_uri)
  1733. uris = GNUNET_FS_uri_to_string (uc->ksk_uri);
  1734. else
  1735. uris = NULL;
  1736. struct GNUNET_BIO_WriteSpec ws1[] = {
  1737. GNUNET_BIO_write_spec_string ("filename", uc->filename),
  1738. GNUNET_BIO_write_spec_int64 ("file size", (int64_t *) &uc->file_size),
  1739. GNUNET_BIO_write_spec_end (),
  1740. };
  1741. struct GNUNET_BIO_WriteSpec ws2[] = {
  1742. GNUNET_BIO_write_spec_int32 ("state", (int32_t *) &uc->state),
  1743. GNUNET_BIO_write_spec_object ("hashkey", &uc->chk,
  1744. sizeof (struct ContentHashKey)),
  1745. GNUNET_BIO_write_spec_string ("uris", uris),
  1746. GNUNET_BIO_write_spec_int32 ("ksk offset", (int32_t *) &uc->ksk_offset),
  1747. GNUNET_BIO_write_spec_end (),
  1748. };
  1749. if ((GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws1)) ||
  1750. (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
  1751. (GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws2)) ||
  1752. ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
  1753. (GNUNET_OK != GNUNET_BIO_write (wh,
  1754. "file id",
  1755. &uc->file_id,
  1756. sizeof(struct GNUNET_HashCode)))) ||
  1757. ((uc->state == UNINDEX_STATE_ERROR) &&
  1758. (GNUNET_OK != GNUNET_BIO_write_string (wh, "emsg", uc->emsg))))
  1759. {
  1760. GNUNET_break (0);
  1761. goto cleanup;
  1762. }
  1763. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  1764. {
  1765. wh = NULL;
  1766. GNUNET_break (0);
  1767. goto cleanup;
  1768. }
  1769. return;
  1770. cleanup:
  1771. if (NULL != wh)
  1772. (void) GNUNET_BIO_write_close (wh, NULL);
  1773. GNUNET_FS_remove_sync_file_ (uc->h,
  1774. GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
  1775. uc->serialization);
  1776. GNUNET_free (uc->serialization);
  1777. uc->serialization = NULL;
  1778. }
  1779. /**
  1780. * Serialize a download request.
  1781. *
  1782. * @param wh handle for writing the download request to disk
  1783. * @param dr the the request to write to disk
  1784. * @return #GNUNET_YES on success, #GNUNET_NO on error
  1785. */
  1786. static int
  1787. write_download_request (struct GNUNET_BIO_WriteHandle *wh,
  1788. struct DownloadRequest *dr)
  1789. {
  1790. unsigned int i;
  1791. struct GNUNET_BIO_WriteSpec ws[] = {
  1792. GNUNET_BIO_write_spec_int32 ("state", (int32_t *) &dr->state),
  1793. GNUNET_BIO_write_spec_int64 ("offset", (int64_t *) &dr->offset),
  1794. GNUNET_BIO_write_spec_int32 ("num children", (int32_t *) &dr->num_children),
  1795. GNUNET_BIO_write_spec_int32 ("depth", (int32_t *) &dr->depth),
  1796. GNUNET_BIO_write_spec_end (),
  1797. };
  1798. if ((GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws)))
  1799. return GNUNET_NO;
  1800. if ((BRS_CHK_SET == dr->state) &&
  1801. (GNUNET_OK !=
  1802. GNUNET_BIO_write (wh, "hashkey",
  1803. &dr->chk, sizeof(struct ContentHashKey))))
  1804. return GNUNET_NO;
  1805. for (i = 0; i < dr->num_children; i++)
  1806. if (GNUNET_NO == write_download_request (wh, dr->children[i]))
  1807. return GNUNET_NO;
  1808. return GNUNET_YES;
  1809. }
  1810. /**
  1811. * Read a download request tree.
  1812. *
  1813. * @param rh cadet to read from
  1814. * @return value the download request read from disk, NULL on error
  1815. */
  1816. static struct DownloadRequest *
  1817. read_download_request (struct GNUNET_BIO_ReadHandle *rh)
  1818. {
  1819. struct DownloadRequest *dr;
  1820. unsigned int i;
  1821. dr = GNUNET_new (struct DownloadRequest);
  1822. struct GNUNET_BIO_ReadSpec rs[] = {
  1823. GNUNET_BIO_read_spec_int32 ("state", (int32_t *) &dr->state),
  1824. GNUNET_BIO_read_spec_int64 ("offset", (int64_t *) &dr->offset),
  1825. GNUNET_BIO_read_spec_int32 ("num children", (int32_t *) &dr->num_children),
  1826. GNUNET_BIO_read_spec_end (),
  1827. };
  1828. if ((GNUNET_OK != GNUNET_BIO_read_spec_commit (rh, rs)) ||
  1829. (dr->num_children > CHK_PER_INODE) ||
  1830. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "depth",
  1831. (int32_t *) &dr->depth)) ||
  1832. ((0 == dr->depth) && (dr->num_children > 0)) ||
  1833. ((dr->depth > 0) && (0 == dr->num_children)))
  1834. {
  1835. GNUNET_break (0);
  1836. dr->num_children = 0;
  1837. goto cleanup;
  1838. }
  1839. if (dr->num_children > 0)
  1840. dr->children =
  1841. GNUNET_malloc (dr->num_children * sizeof(struct DownloadRequest *));
  1842. switch (dr->state)
  1843. {
  1844. case BRS_INIT:
  1845. case BRS_RECONSTRUCT_DOWN:
  1846. case BRS_RECONSTRUCT_META_UP:
  1847. case BRS_RECONSTRUCT_UP:
  1848. break;
  1849. case BRS_CHK_SET:
  1850. if (GNUNET_OK !=
  1851. GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof(struct ContentHashKey)))
  1852. goto cleanup;
  1853. break;
  1854. case BRS_DOWNLOAD_DOWN:
  1855. case BRS_DOWNLOAD_UP:
  1856. case BRS_ERROR:
  1857. break;
  1858. default:
  1859. GNUNET_break (0);
  1860. goto cleanup;
  1861. }
  1862. for (i = 0; i < dr->num_children; i++)
  1863. {
  1864. if (NULL == (dr->children[i] = read_download_request (rh)))
  1865. goto cleanup;
  1866. dr->children[i]->parent = dr;
  1867. }
  1868. return dr;
  1869. cleanup:
  1870. GNUNET_FS_free_download_request_ (dr);
  1871. return NULL;
  1872. }
  1873. /**
  1874. * Compute the name of the sync file (or directory) for the given download
  1875. * context.
  1876. *
  1877. * @param dc download context to compute for
  1878. * @param uni unique filename to use, use "" for the directory name
  1879. * @param ext extension to use, use ".dir" for our own subdirectory
  1880. * @return the expanded file name, NULL for none
  1881. */
  1882. static char *
  1883. get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
  1884. const char *uni,
  1885. const char *ext)
  1886. {
  1887. char *par;
  1888. char *epar;
  1889. if (dc->parent == NULL)
  1890. return get_serialization_file_name (dc->h,
  1891. (dc->search != NULL)
  1892. ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
  1893. : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
  1894. uni);
  1895. if (NULL == dc->parent->serialization)
  1896. return NULL;
  1897. par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
  1898. if (NULL == par)
  1899. return NULL;
  1900. GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
  1901. GNUNET_free (par);
  1902. return epar;
  1903. }
  1904. /**
  1905. * Synchronize this download struct with its mirror
  1906. * on disk. Note that all internal FS-operations that change
  1907. * publishing structs should already call "sync" internally,
  1908. * so this function is likely not useful for clients.
  1909. *
  1910. * @param dc the struct to sync
  1911. */
  1912. void
  1913. GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
  1914. {
  1915. struct GNUNET_BIO_WriteHandle *wh;
  1916. char *uris;
  1917. char *fn;
  1918. char *dir;
  1919. if (0 != (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
  1920. return; /* we don't sync probes */
  1921. if (NULL == dc->serialization)
  1922. {
  1923. dir = get_download_sync_filename (dc, "", "");
  1924. if (NULL == dir)
  1925. return;
  1926. if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
  1927. {
  1928. GNUNET_free (dir);
  1929. return;
  1930. }
  1931. fn = GNUNET_DISK_mktemp (dir);
  1932. GNUNET_free (dir);
  1933. if (NULL == fn)
  1934. return;
  1935. dc->serialization = get_serialization_short_name (fn);
  1936. }
  1937. else
  1938. {
  1939. fn = get_download_sync_filename (dc, dc->serialization, "");
  1940. if (NULL == fn)
  1941. {
  1942. GNUNET_free (dc->serialization);
  1943. dc->serialization = NULL;
  1944. GNUNET_free (fn);
  1945. return;
  1946. }
  1947. }
  1948. wh = GNUNET_BIO_write_open_file (fn);
  1949. if (NULL == wh)
  1950. {
  1951. GNUNET_free (dc->serialization);
  1952. dc->serialization = NULL;
  1953. GNUNET_free (fn);
  1954. return;
  1955. }
  1956. GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
  1957. (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
  1958. uris = GNUNET_FS_uri_to_string (dc->uri);
  1959. struct GNUNET_BIO_WriteSpec ws1[] = {
  1960. GNUNET_BIO_write_spec_string ("uris", uris),
  1961. GNUNET_BIO_write_spec_meta_data ("metadata", dc->meta),
  1962. GNUNET_BIO_write_spec_string ("emsg", dc->emsg),
  1963. GNUNET_BIO_write_spec_string ("filename", dc->filename),
  1964. GNUNET_BIO_write_spec_string ("temp filename", dc->temp_filename),
  1965. GNUNET_BIO_write_spec_int64 ("old file size",
  1966. (int64_t *) &dc->old_file_size),
  1967. GNUNET_BIO_write_spec_int64 ("offset", (int64_t *) &dc->offset),
  1968. GNUNET_BIO_write_spec_int64 ("length", (int64_t *) &dc->length),
  1969. GNUNET_BIO_write_spec_int64 ("completed", (int64_t *) &dc->completed),
  1970. GNUNET_BIO_write_spec_end (),
  1971. };
  1972. struct GNUNET_BIO_WriteSpec ws2[] = {
  1973. GNUNET_BIO_write_spec_int32 ("anonymity", (int32_t *) &dc->anonymity),
  1974. GNUNET_BIO_write_spec_int32 ("options", (int32_t *) &dc->options),
  1975. GNUNET_BIO_write_spec_int32 ("has finished", (int32_t *) &dc->has_finished),
  1976. GNUNET_BIO_write_spec_end (),
  1977. };
  1978. if ((GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws1)) ||
  1979. (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
  1980. (GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws2)))
  1981. {
  1982. GNUNET_break (0);
  1983. goto cleanup;
  1984. }
  1985. if (NULL == dc->emsg)
  1986. {
  1987. GNUNET_assert (dc->top_request != NULL);
  1988. if (GNUNET_YES != write_download_request (wh, dc->top_request))
  1989. {
  1990. GNUNET_break (0);
  1991. goto cleanup;
  1992. }
  1993. }
  1994. GNUNET_free (uris);
  1995. uris = NULL;
  1996. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  1997. {
  1998. wh = NULL;
  1999. GNUNET_break (0);
  2000. goto cleanup;
  2001. }
  2002. GNUNET_free (fn);
  2003. return;
  2004. cleanup:
  2005. if (NULL != wh)
  2006. (void) GNUNET_BIO_write_close (wh, NULL);
  2007. GNUNET_free (uris);
  2008. if (0 != unlink (fn))
  2009. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
  2010. GNUNET_free (fn);
  2011. GNUNET_free (dc->serialization);
  2012. dc->serialization = NULL;
  2013. }
  2014. /**
  2015. * Synchronize this search result with its mirror
  2016. * on disk. Note that all internal FS-operations that change
  2017. * publishing structs should already call "sync" internally,
  2018. * so this function is likely not useful for clients.
  2019. *
  2020. * @param sr the struct to sync
  2021. */
  2022. void
  2023. GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
  2024. {
  2025. struct GNUNET_BIO_WriteHandle *wh;
  2026. char *uris;
  2027. if (NULL == sr->sc)
  2028. return;
  2029. uris = NULL;
  2030. if (NULL == sr->serialization)
  2031. sr->serialization =
  2032. make_serialization_file_name_in_dir (sr->h,
  2033. (sr->sc->psearch_result == NULL)
  2034. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2035. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2036. sr->sc->serialization);
  2037. if (NULL == sr->serialization)
  2038. return;
  2039. wh = get_write_handle_in_dir (sr->h,
  2040. (sr->sc->psearch_result == NULL)
  2041. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2042. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2043. sr->sc->serialization,
  2044. sr->serialization);
  2045. if (NULL == wh)
  2046. {
  2047. GNUNET_break (0);
  2048. goto cleanup;
  2049. }
  2050. uris = GNUNET_FS_uri_to_string (sr->uri);
  2051. struct GNUNET_BIO_WriteSpec ws[] = {
  2052. GNUNET_BIO_write_spec_string ("uris", uris),
  2053. GNUNET_BIO_write_spec_string ("download serialization",
  2054. (sr->download != NULL)
  2055. ? sr->download->serialization
  2056. : NULL),
  2057. GNUNET_BIO_write_spec_string ("update search serialization",
  2058. (sr->update_search != NULL)
  2059. ? sr->update_search->serialization
  2060. : NULL),
  2061. GNUNET_BIO_write_spec_meta_data ("metadata", sr->meta),
  2062. GNUNET_BIO_write_spec_object ("key", &sr->key,
  2063. sizeof(struct GNUNET_HashCode)),
  2064. GNUNET_BIO_write_spec_int32 ("mandatory missing",
  2065. (int32_t *) &sr->mandatory_missing),
  2066. GNUNET_BIO_write_spec_int32 ("optional support",
  2067. (int32_t *) &sr->optional_support),
  2068. GNUNET_BIO_write_spec_int32 ("availability success",
  2069. (int32_t *) &sr->availability_success),
  2070. GNUNET_BIO_write_spec_int32 ("availability trials",
  2071. (int32_t *) &sr->availability_trials),
  2072. GNUNET_BIO_write_spec_end (),
  2073. };
  2074. if ((GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws)))
  2075. {
  2076. GNUNET_break (0);
  2077. goto cleanup;
  2078. }
  2079. if ((NULL != sr->uri) && (GNUNET_FS_URI_KSK == sr->sc->uri->type) &&
  2080. (GNUNET_OK !=
  2081. GNUNET_BIO_write (wh,
  2082. "keyword bitmap",
  2083. sr->keyword_bitmap,
  2084. (sr->sc->uri->data.ksk.keywordCount + 7) / 8)))
  2085. {
  2086. GNUNET_break (0);
  2087. goto cleanup;
  2088. }
  2089. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  2090. {
  2091. wh = NULL;
  2092. GNUNET_break (0);
  2093. goto cleanup;
  2094. }
  2095. GNUNET_free (uris);
  2096. return;
  2097. cleanup:
  2098. GNUNET_free (uris);
  2099. if (NULL != wh)
  2100. (void) GNUNET_BIO_write_close (wh, NULL);
  2101. remove_sync_file_in_dir (sr->h,
  2102. (NULL == sr->sc->psearch_result)
  2103. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2104. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2105. sr->sc->serialization,
  2106. sr->serialization);
  2107. GNUNET_free (sr->serialization);
  2108. sr->serialization = NULL;
  2109. }
  2110. /**
  2111. * Synchronize this search struct with its mirror
  2112. * on disk. Note that all internal FS-operations that change
  2113. * publishing structs should already call "sync" internally,
  2114. * so this function is likely not useful for clients.
  2115. *
  2116. * @param sc the struct to sync
  2117. */
  2118. void
  2119. GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
  2120. {
  2121. struct GNUNET_BIO_WriteHandle *wh;
  2122. char *uris;
  2123. char in_pause;
  2124. const char *category;
  2125. category = (NULL == sc->psearch_result) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2126. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
  2127. if (NULL == sc->serialization)
  2128. sc->serialization = make_serialization_file_name (sc->h, category);
  2129. if (NULL == sc->serialization)
  2130. return;
  2131. uris = NULL;
  2132. wh = get_write_handle (sc->h, category, sc->serialization);
  2133. if (NULL == wh)
  2134. {
  2135. GNUNET_break (0);
  2136. goto cleanup;
  2137. }
  2138. GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
  2139. (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
  2140. uris = GNUNET_FS_uri_to_string (sc->uri);
  2141. in_pause = (sc->task != NULL) ? 'r' : '\0';
  2142. if ((GNUNET_OK != GNUNET_BIO_write_string (wh, "uris", uris)) ||
  2143. (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
  2144. (GNUNET_OK != GNUNET_BIO_write_string (wh, "emsg", sc->emsg)) ||
  2145. (GNUNET_OK != GNUNET_BIO_write_int32 (wh, "options",
  2146. (uint32_t) sc->options)) ||
  2147. (GNUNET_OK != GNUNET_BIO_write (wh, "in pause",
  2148. &in_pause, sizeof(in_pause))) ||
  2149. (GNUNET_OK != GNUNET_BIO_write_int32 (wh, "anonymity", sc->anonymity)))
  2150. {
  2151. GNUNET_break (0);
  2152. goto cleanup;
  2153. }
  2154. GNUNET_free (uris);
  2155. uris = NULL;
  2156. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  2157. {
  2158. wh = NULL;
  2159. GNUNET_break (0);
  2160. goto cleanup;
  2161. }
  2162. return;
  2163. cleanup:
  2164. if (NULL != wh)
  2165. (void) GNUNET_BIO_write_close (wh, NULL);
  2166. GNUNET_free (uris);
  2167. GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
  2168. GNUNET_free (sc->serialization);
  2169. sc->serialization = NULL;
  2170. }
  2171. /**
  2172. * Function called with a filename of serialized unindexing operation
  2173. * to deserialize.
  2174. *
  2175. * @param cls the `struct GNUNET_FS_Handle *`
  2176. * @param filename complete filename (absolute path)
  2177. * @return #GNUNET_OK (continue to iterate)
  2178. */
  2179. static int
  2180. deserialize_unindex_file (void *cls, const char *filename)
  2181. {
  2182. struct GNUNET_FS_Handle *h = cls;
  2183. struct GNUNET_BIO_ReadHandle *rh;
  2184. struct GNUNET_FS_UnindexContext *uc;
  2185. struct GNUNET_FS_ProgressInfo pi;
  2186. char *emsg;
  2187. char *uris;
  2188. uint32_t state;
  2189. uc = GNUNET_new (struct GNUNET_FS_UnindexContext);
  2190. uc->h = h;
  2191. uc->serialization = get_serialization_short_name (filename);
  2192. rh = GNUNET_BIO_read_open_file (filename);
  2193. if (NULL == rh)
  2194. {
  2195. GNUNET_break (0);
  2196. goto cleanup;
  2197. }
  2198. uris = NULL;
  2199. if ((GNUNET_OK !=
  2200. GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
  2201. (GNUNET_OK != GNUNET_BIO_read_int64 (rh, "file size",
  2202. (int64_t *) &uc->file_size)) ||
  2203. (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
  2204. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "state",
  2205. (int32_t *) &state)) ||
  2206. (GNUNET_OK !=
  2207. GNUNET_BIO_read (rh, "uri", &uc->chk, sizeof(struct ContentHashKey))) ||
  2208. (GNUNET_OK !=
  2209. GNUNET_BIO_read_string (rh, "unindex-kskuri", &uris, 10 * 1024)) ||
  2210. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "ksk offset",
  2211. (int32_t *) &uc->ksk_offset)))
  2212. {
  2213. GNUNET_free (uris);
  2214. GNUNET_break (0);
  2215. goto cleanup;
  2216. }
  2217. if (NULL != uris)
  2218. {
  2219. uc->ksk_uri = GNUNET_FS_uri_parse (uris, &emsg);
  2220. GNUNET_free (uris);
  2221. if (NULL == uc->ksk_uri)
  2222. {
  2223. GNUNET_break (0);
  2224. GNUNET_free (emsg);
  2225. goto cleanup;
  2226. }
  2227. }
  2228. if ((uc->ksk_offset > 0) &&
  2229. ((NULL == uc->ksk_uri) ||
  2230. (uc->ksk_offset > uc->ksk_uri->data.ksk.keywordCount)))
  2231. {
  2232. GNUNET_break (0);
  2233. goto cleanup;
  2234. }
  2235. uc->state = (enum UnindexState) state;
  2236. switch (state)
  2237. {
  2238. case UNINDEX_STATE_HASHING:
  2239. break;
  2240. case UNINDEX_STATE_FS_NOTIFY:
  2241. if (GNUNET_OK != GNUNET_BIO_read (rh,
  2242. "unindex-hash",
  2243. &uc->file_id,
  2244. sizeof(struct GNUNET_HashCode)))
  2245. {
  2246. GNUNET_break (0);
  2247. goto cleanup;
  2248. }
  2249. break;
  2250. case UNINDEX_STATE_DS_REMOVE:
  2251. case UNINDEX_STATE_EXTRACT_KEYWORDS:
  2252. case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
  2253. break;
  2254. case UNINDEX_STATE_COMPLETE:
  2255. break;
  2256. case UNINDEX_STATE_ERROR:
  2257. if (GNUNET_OK !=
  2258. GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
  2259. {
  2260. GNUNET_break (0);
  2261. goto cleanup;
  2262. }
  2263. break;
  2264. default:
  2265. GNUNET_break (0);
  2266. goto cleanup;
  2267. }
  2268. uc->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, uc);
  2269. pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
  2270. pi.value.unindex.specifics.resume.message = uc->emsg;
  2271. GNUNET_FS_unindex_make_status_ (&pi,
  2272. uc,
  2273. (uc->state == UNINDEX_STATE_COMPLETE)
  2274. ? uc->file_size
  2275. : 0);
  2276. switch (uc->state)
  2277. {
  2278. case UNINDEX_STATE_HASHING:
  2279. uc->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
  2280. uc->filename,
  2281. HASHING_BLOCKSIZE,
  2282. &GNUNET_FS_unindex_process_hash_,
  2283. uc);
  2284. break;
  2285. case UNINDEX_STATE_FS_NOTIFY:
  2286. uc->state = UNINDEX_STATE_HASHING;
  2287. GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
  2288. break;
  2289. case UNINDEX_STATE_DS_REMOVE:
  2290. GNUNET_FS_unindex_do_remove_ (uc);
  2291. break;
  2292. case UNINDEX_STATE_EXTRACT_KEYWORDS:
  2293. GNUNET_FS_unindex_do_extract_keywords_ (uc);
  2294. break;
  2295. case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
  2296. GNUNET_FS_unindex_do_remove_kblocks_ (uc);
  2297. break;
  2298. case UNINDEX_STATE_COMPLETE:
  2299. case UNINDEX_STATE_ERROR:
  2300. /* no need to resume any operation, we were done */
  2301. break;
  2302. default:
  2303. break;
  2304. }
  2305. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  2306. {
  2307. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2308. _ ("Failure while resuming unindexing operation `%s': %s\n"),
  2309. filename,
  2310. emsg);
  2311. GNUNET_free (emsg);
  2312. }
  2313. return GNUNET_OK;
  2314. cleanup:
  2315. GNUNET_free (uc->filename);
  2316. if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
  2317. {
  2318. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2319. _ ("Failed to resume unindexing operation `%s': %s\n"),
  2320. filename,
  2321. emsg);
  2322. GNUNET_free (emsg);
  2323. }
  2324. if (NULL != uc->serialization)
  2325. GNUNET_FS_remove_sync_file_ (h,
  2326. GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
  2327. uc->serialization);
  2328. GNUNET_free (uc->serialization);
  2329. GNUNET_free (uc);
  2330. return GNUNET_OK;
  2331. }
  2332. /**
  2333. * Deserialize a download.
  2334. *
  2335. * @param h overall context
  2336. * @param rh file to deserialize from
  2337. * @param parent parent download
  2338. * @param search associated search
  2339. * @param serialization name under which the search was serialized
  2340. */
  2341. static void
  2342. deserialize_download (struct GNUNET_FS_Handle *h,
  2343. struct GNUNET_BIO_ReadHandle *rh,
  2344. struct GNUNET_FS_DownloadContext *parent,
  2345. struct GNUNET_FS_SearchResult *search,
  2346. const char *serialization);
  2347. /**
  2348. * Deserialize a search.
  2349. *
  2350. * @param h overall context
  2351. * @param rh file to deserialize from
  2352. * @param psearch_result parent search result
  2353. * @param serialization name under which the search was serialized
  2354. */
  2355. static struct GNUNET_FS_SearchContext *
  2356. deserialize_search (struct GNUNET_FS_Handle *h,
  2357. struct GNUNET_BIO_ReadHandle *rh,
  2358. struct GNUNET_FS_SearchResult *psearch_result,
  2359. const char *serialization);
  2360. /**
  2361. * Function called with a filename of serialized search result
  2362. * to deserialize.
  2363. *
  2364. * @param cls the `struct GNUNET_FS_SearchContext *`
  2365. * @param filename complete filename (absolute path)
  2366. * @return #GNUNET_OK (continue to iterate)
  2367. */
  2368. static int
  2369. deserialize_search_result (void *cls, const char *filename)
  2370. {
  2371. struct GNUNET_FS_SearchContext *sc = cls;
  2372. char *ser;
  2373. char *uris;
  2374. char *emsg;
  2375. char *download;
  2376. char *update_srch;
  2377. struct GNUNET_BIO_ReadHandle *rh;
  2378. struct GNUNET_BIO_ReadHandle *drh;
  2379. struct GNUNET_FS_SearchResult *sr;
  2380. ser = get_serialization_short_name (filename);
  2381. rh = GNUNET_BIO_read_open_file (filename);
  2382. if (NULL == rh)
  2383. {
  2384. if (NULL != ser)
  2385. {
  2386. remove_sync_file_in_dir (sc->h,
  2387. (NULL == sc->psearch_result)
  2388. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2389. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2390. sc->serialization,
  2391. ser);
  2392. GNUNET_free (ser);
  2393. }
  2394. return GNUNET_OK;
  2395. }
  2396. emsg = NULL;
  2397. uris = NULL;
  2398. download = NULL;
  2399. update_srch = NULL;
  2400. sr = GNUNET_new (struct GNUNET_FS_SearchResult);
  2401. sr->h = sc->h;
  2402. sr->sc = sc;
  2403. sr->serialization = ser;
  2404. if ((GNUNET_OK !=
  2405. GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024)) ||
  2406. (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
  2407. (GNUNET_OK !=
  2408. GNUNET_BIO_read_string (rh, "download-lnk", &download, 16)) ||
  2409. (GNUNET_OK !=
  2410. GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
  2411. (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
  2412. (GNUNET_OK != GNUNET_BIO_read (rh,
  2413. "result-key",
  2414. &sr->key,
  2415. sizeof(struct GNUNET_HashCode))) ||
  2416. (GNUNET_OK != GNUNET_BIO_read_int32 (
  2417. rh,
  2418. "mandatory missing",
  2419. (int32_t *) &sr->mandatory_missing)) ||
  2420. (GNUNET_OK != GNUNET_BIO_read_int32 (
  2421. rh,
  2422. "optional support",
  2423. (int32_t *) &sr->optional_support)) ||
  2424. (GNUNET_OK != GNUNET_BIO_read_int32 (
  2425. rh,
  2426. "availability success",
  2427. (int32_t *) &sr->availability_success)) ||
  2428. (GNUNET_OK != GNUNET_BIO_read_int32 (
  2429. rh,
  2430. "availability trials",
  2431. (int32_t *) &sr->availability_trials)))
  2432. {
  2433. GNUNET_break (0);
  2434. goto cleanup;
  2435. }
  2436. if (GNUNET_FS_URI_KSK == sr->sc->uri->type)
  2437. {
  2438. sr->keyword_bitmap = GNUNET_malloc (
  2439. (sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
  2440. if (GNUNET_OK !=
  2441. GNUNET_BIO_read (rh,
  2442. "keyword-bitmap",
  2443. sr->keyword_bitmap,
  2444. (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
  2445. {
  2446. GNUNET_break (0);
  2447. goto cleanup;
  2448. }
  2449. }
  2450. GNUNET_free (uris);
  2451. if (NULL != download)
  2452. {
  2453. drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
  2454. if (NULL != drh)
  2455. {
  2456. deserialize_download (sc->h, drh, NULL, sr, download);
  2457. if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
  2458. {
  2459. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2460. _ ("Failed to resume sub-download `%s': %s\n"),
  2461. download,
  2462. emsg);
  2463. GNUNET_free (emsg);
  2464. }
  2465. }
  2466. GNUNET_free (download);
  2467. }
  2468. if (NULL != update_srch)
  2469. {
  2470. drh =
  2471. get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
  2472. if (NULL != drh)
  2473. {
  2474. deserialize_search (sc->h, drh, sr, update_srch);
  2475. if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
  2476. {
  2477. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2478. _ ("Failed to resume sub-search `%s': %s\n"),
  2479. update_srch,
  2480. emsg);
  2481. GNUNET_free (emsg);
  2482. }
  2483. }
  2484. GNUNET_free (update_srch);
  2485. }
  2486. GNUNET_break (GNUNET_YES == GNUNET_CONTAINER_multihashmap_put (
  2487. sc->master_result_map,
  2488. &sr->key,
  2489. sr,
  2490. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
  2491. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  2492. {
  2493. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2494. _ ("Failure while resuming search operation `%s': %s\n"),
  2495. filename,
  2496. emsg);
  2497. GNUNET_free (emsg);
  2498. }
  2499. return GNUNET_OK;
  2500. cleanup:
  2501. GNUNET_free (download);
  2502. GNUNET_free (emsg);
  2503. GNUNET_free (uris);
  2504. GNUNET_free (update_srch);
  2505. if (NULL != sr->uri)
  2506. GNUNET_FS_uri_destroy (sr->uri);
  2507. if (NULL != sr->meta)
  2508. GNUNET_CONTAINER_meta_data_destroy (sr->meta);
  2509. GNUNET_free (sr->serialization);
  2510. GNUNET_free (sr);
  2511. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  2512. {
  2513. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2514. _ ("Failure while resuming search operation `%s': %s\n"),
  2515. filename,
  2516. emsg);
  2517. GNUNET_free (emsg);
  2518. }
  2519. return GNUNET_OK;
  2520. }
  2521. /**
  2522. * Send the 'resume' signal to the callback; also actually
  2523. * resume the download (put it in the queue). Does this
  2524. * recursively for the top-level download and all child
  2525. * downloads.
  2526. *
  2527. * @param dc download to resume
  2528. */
  2529. static void
  2530. signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
  2531. {
  2532. struct GNUNET_FS_DownloadContext *dcc;
  2533. struct GNUNET_FS_ProgressInfo pi;
  2534. pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
  2535. pi.value.download.specifics.resume.meta = dc->meta;
  2536. pi.value.download.specifics.resume.message = dc->emsg;
  2537. GNUNET_FS_download_make_status_ (&pi, dc);
  2538. dcc = dc->child_head;
  2539. while (NULL != dcc)
  2540. {
  2541. signal_download_resume (dcc);
  2542. dcc = dcc->next;
  2543. }
  2544. }
  2545. /**
  2546. * Signal resuming of a search to our clients (for the
  2547. * top level search and all sub-searches).
  2548. *
  2549. * @param sc search being resumed
  2550. */
  2551. static void
  2552. signal_search_resume (struct GNUNET_FS_SearchContext *sc);
  2553. /**
  2554. * Iterator over search results signaling resume to the client for
  2555. * each result.
  2556. *
  2557. * @param cls closure, the `struct GNUNET_FS_SearchContext *`
  2558. * @param key current key code
  2559. * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
  2560. * @return #GNUNET_YES (we should continue to iterate)
  2561. */
  2562. static int
  2563. signal_result_resume (void *cls, const struct GNUNET_HashCode *key, void *value)
  2564. {
  2565. struct GNUNET_FS_SearchContext *sc = cls;
  2566. struct GNUNET_FS_ProgressInfo pi;
  2567. struct GNUNET_FS_SearchResult *sr = value;
  2568. if (0 == sr->mandatory_missing)
  2569. {
  2570. pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
  2571. pi.value.search.specifics.resume_result.meta = sr->meta;
  2572. pi.value.search.specifics.resume_result.uri = sr->uri;
  2573. pi.value.search.specifics.resume_result.result = sr;
  2574. pi.value.search.specifics.resume_result.availability_rank =
  2575. 2 * sr->availability_success - sr->availability_trials;
  2576. pi.value.search.specifics.resume_result.availability_certainty =
  2577. sr->availability_trials;
  2578. pi.value.search.specifics.resume_result.applicability_rank =
  2579. sr->optional_support;
  2580. sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
  2581. }
  2582. if (NULL != sr->download)
  2583. {
  2584. signal_download_resume (sr->download);
  2585. }
  2586. else
  2587. {
  2588. GNUNET_FS_search_start_probe_ (sr);
  2589. }
  2590. if (NULL != sr->update_search)
  2591. signal_search_resume (sr->update_search);
  2592. return GNUNET_YES;
  2593. }
  2594. /**
  2595. * Free memory allocated by the search context and its children
  2596. *
  2597. * @param sc search context to free
  2598. */
  2599. static void
  2600. free_search_context (struct GNUNET_FS_SearchContext *sc);
  2601. /**
  2602. * Iterator over search results freeing each.
  2603. *
  2604. * @param cls closure, the `struct GNUNET_FS_SearchContext *`
  2605. * @param key current key code
  2606. * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
  2607. * @return #GNUNET_YES (we should continue to iterate)
  2608. */
  2609. static int
  2610. free_result (void *cls, const struct GNUNET_HashCode *key, void *value)
  2611. {
  2612. struct GNUNET_FS_SearchResult *sr = value;
  2613. if (NULL != sr->update_search)
  2614. {
  2615. free_search_context (sr->update_search);
  2616. GNUNET_assert (NULL == sr->update_search);
  2617. }
  2618. GNUNET_CONTAINER_meta_data_destroy (sr->meta);
  2619. GNUNET_FS_uri_destroy (sr->uri);
  2620. GNUNET_free (sr);
  2621. return GNUNET_YES;
  2622. }
  2623. /**
  2624. * Free memory allocated by the search context and its children
  2625. *
  2626. * @param sc search context to free
  2627. */
  2628. static void
  2629. free_search_context (struct GNUNET_FS_SearchContext *sc)
  2630. {
  2631. if (NULL != sc->serialization)
  2632. {
  2633. GNUNET_FS_remove_sync_file_ (sc->h,
  2634. (sc->psearch_result == NULL)
  2635. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2636. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2637. sc->serialization);
  2638. GNUNET_FS_remove_sync_dir_ (sc->h,
  2639. (sc->psearch_result == NULL)
  2640. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2641. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2642. sc->serialization);
  2643. }
  2644. GNUNET_free (sc->serialization);
  2645. GNUNET_free (sc->emsg);
  2646. if (NULL != sc->uri)
  2647. GNUNET_FS_uri_destroy (sc->uri);
  2648. if (NULL != sc->master_result_map)
  2649. {
  2650. GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
  2651. &free_result,
  2652. sc);
  2653. GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
  2654. }
  2655. GNUNET_free (sc);
  2656. }
  2657. /**
  2658. * Function called with a filename of serialized sub-download
  2659. * to deserialize.
  2660. *
  2661. * @param cls the `struct GNUNET_FS_DownloadContext *` (parent)
  2662. * @param filename complete filename (absolute path)
  2663. * @return #GNUNET_OK (continue to iterate)
  2664. */
  2665. static int
  2666. deserialize_subdownload (void *cls, const char *filename)
  2667. {
  2668. struct GNUNET_FS_DownloadContext *parent = cls;
  2669. char *ser;
  2670. char *emsg;
  2671. struct GNUNET_BIO_ReadHandle *rh;
  2672. ser = get_serialization_short_name (filename);
  2673. rh = GNUNET_BIO_read_open_file (filename);
  2674. if (NULL == rh)
  2675. {
  2676. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2677. _ (
  2678. "Failed to resume sub-download `%s': could not open file `%s'\n"),
  2679. ser,
  2680. filename);
  2681. GNUNET_free (ser);
  2682. return GNUNET_OK;
  2683. }
  2684. deserialize_download (parent->h, rh, parent, NULL, ser);
  2685. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  2686. {
  2687. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2688. _ ("Failed to resume sub-download `%s': %s\n"),
  2689. ser,
  2690. emsg);
  2691. GNUNET_free (emsg);
  2692. }
  2693. GNUNET_free (ser);
  2694. return GNUNET_OK;
  2695. }
  2696. /**
  2697. * Free this download context and all of its descendants.
  2698. * (only works during deserialization since not all possible
  2699. * state it taken care of).
  2700. *
  2701. * @param dc context to free
  2702. */
  2703. static void
  2704. free_download_context (struct GNUNET_FS_DownloadContext *dc)
  2705. {
  2706. struct GNUNET_FS_DownloadContext *dcc;
  2707. if (NULL != dc->meta)
  2708. GNUNET_CONTAINER_meta_data_destroy (dc->meta);
  2709. if (NULL != dc->uri)
  2710. GNUNET_FS_uri_destroy (dc->uri);
  2711. GNUNET_free (dc->temp_filename);
  2712. GNUNET_free (dc->emsg);
  2713. GNUNET_free (dc->filename);
  2714. GNUNET_free (dc->serialization);
  2715. while (NULL != (dcc = dc->child_head))
  2716. {
  2717. GNUNET_CONTAINER_DLL_remove (dc->child_head, dc->child_tail, dcc);
  2718. free_download_context (dcc);
  2719. }
  2720. GNUNET_FS_free_download_request_ (dc->top_request);
  2721. if (NULL != dc->active)
  2722. GNUNET_CONTAINER_multihashmap_destroy (dc->active);
  2723. GNUNET_free (dc);
  2724. }
  2725. /**
  2726. * Deserialize a download.
  2727. *
  2728. * @param h overall context
  2729. * @param rh file to deserialize from
  2730. * @param parent parent download
  2731. * @param search associated search
  2732. * @param serialization name under which the search was serialized
  2733. */
  2734. static void
  2735. deserialize_download (struct GNUNET_FS_Handle *h,
  2736. struct GNUNET_BIO_ReadHandle *rh,
  2737. struct GNUNET_FS_DownloadContext *parent,
  2738. struct GNUNET_FS_SearchResult *search,
  2739. const char *serialization)
  2740. {
  2741. struct GNUNET_FS_DownloadContext *dc;
  2742. char *emsg;
  2743. char *uris;
  2744. char *dn;
  2745. uint32_t options;
  2746. uint32_t status;
  2747. uris = NULL;
  2748. emsg = NULL;
  2749. dc = GNUNET_new (struct GNUNET_FS_DownloadContext);
  2750. dc->parent = parent;
  2751. dc->h = h;
  2752. dc->serialization = GNUNET_strdup (serialization);
  2753. struct GNUNET_BIO_ReadSpec rs[] = {
  2754. GNUNET_BIO_read_spec_meta_data ("download-meta", &dc->meta),
  2755. GNUNET_BIO_read_spec_string ("download-emsg", &dc->emsg, 10 * 1024),
  2756. GNUNET_BIO_read_spec_string ("download-fn", &dc->filename, 10 * 1024),
  2757. GNUNET_BIO_read_spec_string ("download-tfn",
  2758. &dc->temp_filename, 10 * 1024),
  2759. GNUNET_BIO_read_spec_int64 ("old file size",
  2760. (int64_t *) &dc->old_file_size),
  2761. GNUNET_BIO_read_spec_int64 ("offset",
  2762. (int64_t *) &dc->offset),
  2763. GNUNET_BIO_read_spec_int64 ("length",
  2764. (int64_t *) &dc->length),
  2765. GNUNET_BIO_read_spec_int64 ("completed",
  2766. (int64_t *) &dc->completed),
  2767. GNUNET_BIO_read_spec_end (),
  2768. };
  2769. if ((GNUNET_OK !=
  2770. GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
  2771. (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
  2772. ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
  2773. (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
  2774. (GNUNET_OK != GNUNET_BIO_read_spec_commit (rh, rs)) ||
  2775. (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
  2776. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "anonymity",
  2777. (int32_t *) &dc->anonymity)) ||
  2778. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "options",
  2779. (int32_t *) &options)) ||
  2780. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "status",
  2781. (int32_t *) &status)))
  2782. {
  2783. GNUNET_break (0);
  2784. goto cleanup;
  2785. }
  2786. dc->options = (enum GNUNET_FS_DownloadOptions) options;
  2787. dc->active =
  2788. GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE),
  2789. GNUNET_NO);
  2790. dc->has_finished = (int) status;
  2791. dc->treedepth =
  2792. GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
  2793. if (GNUNET_FS_uri_test_loc (dc->uri))
  2794. GNUNET_assert (GNUNET_OK ==
  2795. GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
  2796. if (NULL == dc->emsg)
  2797. {
  2798. dc->top_request = read_download_request (rh);
  2799. if (NULL == dc->top_request)
  2800. {
  2801. GNUNET_break (0);
  2802. goto cleanup;
  2803. }
  2804. }
  2805. dn = get_download_sync_filename (dc, dc->serialization, ".dir");
  2806. if (NULL != dn)
  2807. {
  2808. if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
  2809. GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
  2810. GNUNET_free (dn);
  2811. }
  2812. if (NULL != parent)
  2813. {
  2814. GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
  2815. }
  2816. if (NULL != search)
  2817. {
  2818. dc->search = search;
  2819. search->download = dc;
  2820. }
  2821. if ((NULL == parent) && (NULL == search))
  2822. {
  2823. dc->top =
  2824. GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
  2825. signal_download_resume (dc);
  2826. }
  2827. GNUNET_free (uris);
  2828. GNUNET_assert (NULL == dc->job_queue);
  2829. dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
  2830. return;
  2831. cleanup:
  2832. GNUNET_free (uris);
  2833. GNUNET_free (emsg);
  2834. free_download_context (dc);
  2835. }
  2836. /**
  2837. * Signal resuming of a search to our clients (for the
  2838. * top level search and all sub-searches).
  2839. *
  2840. * @param sc search being resumed
  2841. */
  2842. static void
  2843. signal_search_resume (struct GNUNET_FS_SearchContext *sc)
  2844. {
  2845. struct GNUNET_FS_ProgressInfo pi;
  2846. pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
  2847. pi.value.search.specifics.resume.message = sc->emsg;
  2848. pi.value.search.specifics.resume.is_paused =
  2849. (NULL == sc->mq) ? GNUNET_YES : GNUNET_NO;
  2850. sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
  2851. GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
  2852. &signal_result_resume,
  2853. sc);
  2854. }
  2855. /**
  2856. * Deserialize a search.
  2857. *
  2858. * @param h overall context
  2859. * @param rh file to deserialize from
  2860. * @param psearch_result parent search result
  2861. * @param serialization name under which the search was serialized
  2862. */
  2863. static struct GNUNET_FS_SearchContext *
  2864. deserialize_search (struct GNUNET_FS_Handle *h,
  2865. struct GNUNET_BIO_ReadHandle *rh,
  2866. struct GNUNET_FS_SearchResult *psearch_result,
  2867. const char *serialization)
  2868. {
  2869. struct GNUNET_FS_SearchContext *sc;
  2870. char *emsg;
  2871. char *uris;
  2872. char *dn;
  2873. uint32_t options;
  2874. char in_pause;
  2875. if ((NULL != psearch_result) && (NULL != psearch_result->update_search))
  2876. {
  2877. GNUNET_break (0);
  2878. return NULL;
  2879. }
  2880. uris = NULL;
  2881. emsg = NULL;
  2882. sc = GNUNET_new (struct GNUNET_FS_SearchContext);
  2883. if (NULL != psearch_result)
  2884. {
  2885. sc->psearch_result = psearch_result;
  2886. psearch_result->update_search = sc;
  2887. }
  2888. sc->h = h;
  2889. sc->serialization = GNUNET_strdup (serialization);
  2890. if ((GNUNET_OK !=
  2891. GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024)) ||
  2892. (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
  2893. ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
  2894. (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
  2895. (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
  2896. (GNUNET_OK !=
  2897. GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
  2898. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "options",
  2899. (int32_t *) &options)) ||
  2900. (GNUNET_OK !=
  2901. GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof(in_pause))) ||
  2902. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "anonymity",
  2903. (int32_t *) &sc->anonymity)))
  2904. {
  2905. GNUNET_break (0);
  2906. goto cleanup;
  2907. }
  2908. sc->options = (enum GNUNET_FS_SearchOptions) options;
  2909. sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
  2910. dn = get_serialization_file_name_in_dir (h,
  2911. (NULL == sc->psearch_result)
  2912. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2913. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2914. sc->serialization,
  2915. "");
  2916. if (NULL != dn)
  2917. {
  2918. if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
  2919. GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
  2920. GNUNET_free (dn);
  2921. }
  2922. if (('\0' == in_pause) &&
  2923. (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
  2924. {
  2925. GNUNET_log (
  2926. GNUNET_ERROR_TYPE_WARNING,
  2927. _ ("Could not resume running search, will resume as paused search\n"));
  2928. }
  2929. signal_search_resume (sc);
  2930. GNUNET_free (uris);
  2931. return sc;
  2932. cleanup:
  2933. GNUNET_free (emsg);
  2934. free_search_context (sc);
  2935. GNUNET_free (uris);
  2936. return NULL;
  2937. }
  2938. /**
  2939. * Function called with a filename of serialized search operation
  2940. * to deserialize.
  2941. *
  2942. * @param cls the `struct GNUNET_FS_Handle *`
  2943. * @param filename complete filename (absolute path)
  2944. * @return #GNUNET_OK (continue to iterate)
  2945. */
  2946. static int
  2947. deserialize_search_file (void *cls, const char *filename)
  2948. {
  2949. struct GNUNET_FS_Handle *h = cls;
  2950. char *ser;
  2951. char *emsg;
  2952. struct GNUNET_BIO_ReadHandle *rh;
  2953. struct GNUNET_FS_SearchContext *sc;
  2954. struct stat buf;
  2955. if (0 != stat (filename, &buf))
  2956. {
  2957. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
  2958. return GNUNET_OK;
  2959. }
  2960. if (S_ISDIR (buf.st_mode))
  2961. return GNUNET_OK; /* skip directories */
  2962. ser = get_serialization_short_name (filename);
  2963. rh = GNUNET_BIO_read_open_file (filename);
  2964. if (NULL == rh)
  2965. {
  2966. if (NULL != ser)
  2967. {
  2968. GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
  2969. GNUNET_free (ser);
  2970. }
  2971. return GNUNET_OK;
  2972. }
  2973. sc = deserialize_search (h, rh, NULL, ser);
  2974. if (NULL != sc)
  2975. sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
  2976. GNUNET_free (ser);
  2977. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  2978. {
  2979. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2980. _ ("Failure while resuming search operation `%s': %s\n"),
  2981. filename,
  2982. emsg);
  2983. GNUNET_free (emsg);
  2984. }
  2985. return GNUNET_OK;
  2986. }
  2987. /**
  2988. * Function called with a filename of serialized download operation
  2989. * to deserialize.
  2990. *
  2991. * @param cls the `struct GNUNET_FS_Handle *`
  2992. * @param filename complete filename (absolute path)
  2993. * @return #GNUNET_OK (continue to iterate)
  2994. */
  2995. static int
  2996. deserialize_download_file (void *cls, const char *filename)
  2997. {
  2998. struct GNUNET_FS_Handle *h = cls;
  2999. char *ser;
  3000. char *emsg;
  3001. struct GNUNET_BIO_ReadHandle *rh;
  3002. ser = get_serialization_short_name (filename);
  3003. rh = GNUNET_BIO_read_open_file (filename);
  3004. if (NULL == rh)
  3005. {
  3006. if (0 != unlink (filename))
  3007. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
  3008. GNUNET_free (ser);
  3009. return GNUNET_OK;
  3010. }
  3011. deserialize_download (h, rh, NULL, NULL, ser);
  3012. GNUNET_free (ser);
  3013. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  3014. {
  3015. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  3016. _ ("Failure while resuming download operation `%s': %s\n"),
  3017. filename,
  3018. emsg);
  3019. GNUNET_free (emsg);
  3020. }
  3021. return GNUNET_OK;
  3022. }
  3023. /**
  3024. * Deserialize informatin about pending operations.
  3025. *
  3026. * @param master_path which master directory should be scanned
  3027. * @param proc function to call for each entry (will get @a h for 'cls')
  3028. * @param h the `struct GNUNET_FS_Handle *`
  3029. */
  3030. static void
  3031. deserialization_master (const char *master_path,
  3032. GNUNET_FileNameCallback proc,
  3033. struct GNUNET_FS_Handle *h)
  3034. {
  3035. char *dn;
  3036. dn = get_serialization_file_name (h, master_path, "");
  3037. if (NULL == dn)
  3038. return;
  3039. if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
  3040. GNUNET_DISK_directory_scan (dn, proc, h);
  3041. GNUNET_free (dn);
  3042. }
  3043. /**
  3044. * Setup a connection to the file-sharing service.
  3045. *
  3046. * @param cfg configuration to use
  3047. * @param client_name unique identifier for this client
  3048. * @param upcb function to call to notify about FS actions
  3049. * @param upcb_cls closure for @a upcb
  3050. * @param flags specific attributes for fs-operations
  3051. * @param ... list of optional options, terminated with #GNUNET_FS_OPTIONS_END
  3052. * @return NULL on error
  3053. */
  3054. struct GNUNET_FS_Handle *
  3055. GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
  3056. const char *client_name,
  3057. GNUNET_FS_ProgressCallback upcb,
  3058. void *upcb_cls,
  3059. enum GNUNET_FS_Flags flags,
  3060. ...)
  3061. {
  3062. struct GNUNET_FS_Handle *ret;
  3063. enum GNUNET_FS_OPTIONS opt;
  3064. va_list ap;
  3065. ret = GNUNET_new (struct GNUNET_FS_Handle);
  3066. ret->cfg = cfg;
  3067. ret->client_name = GNUNET_strdup (client_name);
  3068. ret->upcb = upcb;
  3069. ret->upcb_cls = upcb_cls;
  3070. ret->flags = flags;
  3071. ret->max_parallel_downloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
  3072. ret->max_parallel_requests = DEFAULT_MAX_PARALLEL_REQUESTS;
  3073. ret->avg_block_latency =
  3074. GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
  3075. va_start (ap, flags);
  3076. while (GNUNET_FS_OPTIONS_END !=
  3077. (opt = GNUNET_VA_ARG_ENUM (ap, GNUNET_FS_OPTIONS)))
  3078. {
  3079. switch (opt)
  3080. {
  3081. case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
  3082. ret->max_parallel_downloads = va_arg (ap, unsigned int);
  3083. break;
  3084. case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
  3085. ret->max_parallel_requests = va_arg (ap, unsigned int);
  3086. break;
  3087. default:
  3088. GNUNET_break (0);
  3089. GNUNET_free (ret->client_name);
  3090. GNUNET_free (ret);
  3091. va_end (ap);
  3092. return NULL;
  3093. }
  3094. }
  3095. va_end (ap);
  3096. if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
  3097. {
  3098. deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
  3099. &deserialize_publish_file,
  3100. ret);
  3101. deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
  3102. &deserialize_search_file,
  3103. ret);
  3104. deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
  3105. &deserialize_download_file,
  3106. ret);
  3107. deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
  3108. &deserialize_unindex_file,
  3109. ret);
  3110. }
  3111. return ret;
  3112. }
  3113. /**
  3114. * Close our connection with the file-sharing service.
  3115. * The callback given to #GNUNET_FS_start() will no longer be
  3116. * called after this function returns.
  3117. * This function MUST NOT be called from within the
  3118. * callback itself.
  3119. *
  3120. * @param h handle that was returned from #GNUNET_FS_start()
  3121. */
  3122. void
  3123. GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
  3124. {
  3125. while (NULL != h->top_head)
  3126. h->top_head->ssf (h->top_head->ssf_cls);
  3127. if (NULL != h->queue_job)
  3128. GNUNET_SCHEDULER_cancel (h->queue_job);
  3129. GNUNET_free (h->client_name);
  3130. GNUNET_free (h);
  3131. }
  3132. /* end of fs_api.c */