fs_api.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348
  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_non_null (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_non_null (ksks);
  1050. GNUNET_free_non_null (skss);
  1051. GNUNET_free_non_null (chks);
  1052. return ret;
  1053. cleanup:
  1054. GNUNET_free_non_null (ksks);
  1055. GNUNET_free_non_null (chks);
  1056. GNUNET_free_non_null (skss);
  1057. GNUNET_free_non_null (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_non_null (chks);
  1317. chks = NULL;
  1318. GNUNET_free_non_null (ksks);
  1319. ksks = NULL;
  1320. GNUNET_free_non_null (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_non_null (chks);
  1422. GNUNET_free_non_null (ksks);
  1423. GNUNET_free_non_null (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_non_null (pc->nid);
  1614. GNUNET_free_non_null (pc->nuid);
  1615. GNUNET_free_non_null (fi_root);
  1616. GNUNET_free_non_null (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. };
  1678. if ((GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws)) ||
  1679. ((NULL != pc->ns) &&
  1680. (GNUNET_OK !=
  1681. GNUNET_BIO_write (wh,
  1682. "ns",
  1683. pc->ns,
  1684. sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)))))
  1685. {
  1686. GNUNET_break (0);
  1687. goto cleanup;
  1688. }
  1689. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  1690. {
  1691. wh = NULL;
  1692. GNUNET_break (0);
  1693. goto cleanup;
  1694. }
  1695. return;
  1696. cleanup:
  1697. if (NULL != wh)
  1698. (void) GNUNET_BIO_write_close (wh, NULL);
  1699. GNUNET_FS_remove_sync_file_ (pc->h,
  1700. GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
  1701. pc->serialization);
  1702. GNUNET_free (pc->serialization);
  1703. pc->serialization = NULL;
  1704. }
  1705. /**
  1706. * Synchronize this unindex struct with its mirror
  1707. * on disk. Note that all internal FS-operations that change
  1708. * publishing structs should already call "sync" internally,
  1709. * so this function is likely not useful for clients.
  1710. *
  1711. * @param uc the struct to sync
  1712. */
  1713. void
  1714. GNUNET_FS_unindex_sync_ (struct GNUNET_FS_UnindexContext *uc)
  1715. {
  1716. struct GNUNET_BIO_WriteHandle *wh;
  1717. char *uris;
  1718. if (NULL == uc->serialization)
  1719. uc->serialization =
  1720. make_serialization_file_name (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX);
  1721. if (NULL == uc->serialization)
  1722. return;
  1723. wh = get_write_handle (uc->h,
  1724. GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
  1725. uc->serialization);
  1726. if (NULL == wh)
  1727. {
  1728. GNUNET_break (0);
  1729. goto cleanup;
  1730. }
  1731. if (NULL != uc->ksk_uri)
  1732. uris = GNUNET_FS_uri_to_string (uc->ksk_uri);
  1733. else
  1734. uris = NULL;
  1735. struct GNUNET_BIO_WriteSpec ws1[] = {
  1736. GNUNET_BIO_write_spec_string ("filename", uc->filename),
  1737. GNUNET_BIO_write_spec_int64 ("file size", (int64_t *) &uc->file_size),
  1738. GNUNET_BIO_write_spec_end (),
  1739. };
  1740. struct GNUNET_BIO_WriteSpec ws2[] = {
  1741. GNUNET_BIO_write_spec_int32 ("state", (int32_t *) &uc->state),
  1742. GNUNET_BIO_write_spec_object ("hashkey", &uc->chk,
  1743. sizeof (struct ContentHashKey)),
  1744. GNUNET_BIO_write_spec_string ("uris", uris),
  1745. GNUNET_BIO_write_spec_int32 ("ksk offset", (int32_t *) &uc->ksk_offset),
  1746. GNUNET_BIO_write_spec_end (),
  1747. };
  1748. if ((GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws1)) ||
  1749. (GNUNET_OK != write_start_time (wh, uc->start_time)) ||
  1750. (GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws2)) ||
  1751. ((uc->state == UNINDEX_STATE_FS_NOTIFY) &&
  1752. (GNUNET_OK != GNUNET_BIO_write (wh,
  1753. "file id",
  1754. &uc->file_id,
  1755. sizeof(struct GNUNET_HashCode)))) ||
  1756. ((uc->state == UNINDEX_STATE_ERROR) &&
  1757. (GNUNET_OK != GNUNET_BIO_write_string (wh, "emsg", uc->emsg))))
  1758. {
  1759. GNUNET_break (0);
  1760. goto cleanup;
  1761. }
  1762. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  1763. {
  1764. wh = NULL;
  1765. GNUNET_break (0);
  1766. goto cleanup;
  1767. }
  1768. return;
  1769. cleanup:
  1770. if (NULL != wh)
  1771. (void) GNUNET_BIO_write_close (wh, NULL);
  1772. GNUNET_FS_remove_sync_file_ (uc->h,
  1773. GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
  1774. uc->serialization);
  1775. GNUNET_free (uc->serialization);
  1776. uc->serialization = NULL;
  1777. }
  1778. /**
  1779. * Serialize a download request.
  1780. *
  1781. * @param wh handle for writing the download request to disk
  1782. * @param dr the the request to write to disk
  1783. * @return #GNUNET_YES on success, #GNUNET_NO on error
  1784. */
  1785. static int
  1786. write_download_request (struct GNUNET_BIO_WriteHandle *wh,
  1787. struct DownloadRequest *dr)
  1788. {
  1789. unsigned int i;
  1790. struct GNUNET_BIO_WriteSpec ws[] = {
  1791. GNUNET_BIO_write_spec_int32 ("state", (int32_t *) &dr->state),
  1792. GNUNET_BIO_write_spec_int64 ("offset", (int64_t *) &dr->offset),
  1793. GNUNET_BIO_write_spec_int32 ("num children", (int32_t *) &dr->num_children),
  1794. GNUNET_BIO_write_spec_int32 ("depth", (int32_t *) &dr->depth),
  1795. GNUNET_BIO_write_spec_end (),
  1796. };
  1797. if ((GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws)))
  1798. return GNUNET_NO;
  1799. if ((BRS_CHK_SET == dr->state) &&
  1800. (GNUNET_OK !=
  1801. GNUNET_BIO_write (wh, "hashkey",
  1802. &dr->chk, sizeof(struct ContentHashKey))))
  1803. return GNUNET_NO;
  1804. for (i = 0; i < dr->num_children; i++)
  1805. if (GNUNET_NO == write_download_request (wh, dr->children[i]))
  1806. return GNUNET_NO;
  1807. return GNUNET_YES;
  1808. }
  1809. /**
  1810. * Read a download request tree.
  1811. *
  1812. * @param rh cadet to read from
  1813. * @return value the download request read from disk, NULL on error
  1814. */
  1815. static struct DownloadRequest *
  1816. read_download_request (struct GNUNET_BIO_ReadHandle *rh)
  1817. {
  1818. struct DownloadRequest *dr;
  1819. unsigned int i;
  1820. dr = GNUNET_new (struct DownloadRequest);
  1821. struct GNUNET_BIO_ReadSpec rs[] = {
  1822. GNUNET_BIO_read_spec_int32 ("state", (int32_t *) &dr->state),
  1823. GNUNET_BIO_read_spec_int64 ("offset", (int64_t *) &dr->offset),
  1824. GNUNET_BIO_read_spec_int32 ("num children", (int32_t *) &dr->num_children),
  1825. GNUNET_BIO_read_spec_end (),
  1826. };
  1827. if ((GNUNET_OK != GNUNET_BIO_read_spec_commit (rh, rs)) ||
  1828. (dr->num_children > CHK_PER_INODE) ||
  1829. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "depth",
  1830. (int32_t *) &dr->depth)) ||
  1831. ((0 == dr->depth) && (dr->num_children > 0)) ||
  1832. ((dr->depth > 0) && (0 == dr->num_children)))
  1833. {
  1834. GNUNET_break (0);
  1835. dr->num_children = 0;
  1836. goto cleanup;
  1837. }
  1838. if (dr->num_children > 0)
  1839. dr->children =
  1840. GNUNET_malloc (dr->num_children * sizeof(struct DownloadRequest *));
  1841. switch (dr->state)
  1842. {
  1843. case BRS_INIT:
  1844. case BRS_RECONSTRUCT_DOWN:
  1845. case BRS_RECONSTRUCT_META_UP:
  1846. case BRS_RECONSTRUCT_UP:
  1847. break;
  1848. case BRS_CHK_SET:
  1849. if (GNUNET_OK !=
  1850. GNUNET_BIO_read (rh, "chk", &dr->chk, sizeof(struct ContentHashKey)))
  1851. goto cleanup;
  1852. break;
  1853. case BRS_DOWNLOAD_DOWN:
  1854. case BRS_DOWNLOAD_UP:
  1855. case BRS_ERROR:
  1856. break;
  1857. default:
  1858. GNUNET_break (0);
  1859. goto cleanup;
  1860. }
  1861. for (i = 0; i < dr->num_children; i++)
  1862. {
  1863. if (NULL == (dr->children[i] = read_download_request (rh)))
  1864. goto cleanup;
  1865. dr->children[i]->parent = dr;
  1866. }
  1867. return dr;
  1868. cleanup:
  1869. GNUNET_FS_free_download_request_ (dr);
  1870. return NULL;
  1871. }
  1872. /**
  1873. * Compute the name of the sync file (or directory) for the given download
  1874. * context.
  1875. *
  1876. * @param dc download context to compute for
  1877. * @param uni unique filename to use, use "" for the directory name
  1878. * @param ext extension to use, use ".dir" for our own subdirectory
  1879. * @return the expanded file name, NULL for none
  1880. */
  1881. static char *
  1882. get_download_sync_filename (struct GNUNET_FS_DownloadContext *dc,
  1883. const char *uni,
  1884. const char *ext)
  1885. {
  1886. char *par;
  1887. char *epar;
  1888. if (dc->parent == NULL)
  1889. return get_serialization_file_name (dc->h,
  1890. (dc->search != NULL)
  1891. ? GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD
  1892. : GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
  1893. uni);
  1894. if (NULL == dc->parent->serialization)
  1895. return NULL;
  1896. par = get_download_sync_filename (dc->parent, dc->parent->serialization, "");
  1897. if (NULL == par)
  1898. return NULL;
  1899. GNUNET_asprintf (&epar, "%s.dir%s%s%s", par, DIR_SEPARATOR_STR, uni, ext);
  1900. GNUNET_free (par);
  1901. return epar;
  1902. }
  1903. /**
  1904. * Synchronize this download struct with its mirror
  1905. * on disk. Note that all internal FS-operations that change
  1906. * publishing structs should already call "sync" internally,
  1907. * so this function is likely not useful for clients.
  1908. *
  1909. * @param dc the struct to sync
  1910. */
  1911. void
  1912. GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
  1913. {
  1914. struct GNUNET_BIO_WriteHandle *wh;
  1915. char *uris;
  1916. char *fn;
  1917. char *dir;
  1918. if (0 != (dc->options & GNUNET_FS_DOWNLOAD_IS_PROBE))
  1919. return; /* we don't sync probes */
  1920. if (NULL == dc->serialization)
  1921. {
  1922. dir = get_download_sync_filename (dc, "", "");
  1923. if (NULL == dir)
  1924. return;
  1925. if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (dir))
  1926. {
  1927. GNUNET_free (dir);
  1928. return;
  1929. }
  1930. fn = GNUNET_DISK_mktemp (dir);
  1931. GNUNET_free (dir);
  1932. if (NULL == fn)
  1933. return;
  1934. dc->serialization = get_serialization_short_name (fn);
  1935. }
  1936. else
  1937. {
  1938. fn = get_download_sync_filename (dc, dc->serialization, "");
  1939. if (NULL == fn)
  1940. {
  1941. GNUNET_free (dc->serialization);
  1942. dc->serialization = NULL;
  1943. GNUNET_free (fn);
  1944. return;
  1945. }
  1946. }
  1947. wh = GNUNET_BIO_write_open_file (fn);
  1948. if (NULL == wh)
  1949. {
  1950. GNUNET_free (dc->serialization);
  1951. dc->serialization = NULL;
  1952. GNUNET_free (fn);
  1953. return;
  1954. }
  1955. GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_chk (dc->uri)) ||
  1956. (GNUNET_YES == GNUNET_FS_uri_test_loc (dc->uri)));
  1957. uris = GNUNET_FS_uri_to_string (dc->uri);
  1958. struct GNUNET_BIO_WriteSpec ws1[] = {
  1959. GNUNET_BIO_write_spec_string ("uris", uris),
  1960. GNUNET_BIO_write_spec_meta_data ("metadata", dc->meta),
  1961. GNUNET_BIO_write_spec_string ("emsg", dc->emsg),
  1962. GNUNET_BIO_write_spec_string ("filename", dc->filename),
  1963. GNUNET_BIO_write_spec_string ("temp filename", dc->temp_filename),
  1964. GNUNET_BIO_write_spec_int64 ("old file size",
  1965. (int64_t *) &dc->old_file_size),
  1966. GNUNET_BIO_write_spec_int64 ("offset", (int64_t *) &dc->offset),
  1967. GNUNET_BIO_write_spec_int64 ("length", (int64_t *) &dc->length),
  1968. GNUNET_BIO_write_spec_int64 ("completed", (int64_t *) &dc->completed),
  1969. GNUNET_BIO_write_spec_end (),
  1970. };
  1971. struct GNUNET_BIO_WriteSpec ws2[] = {
  1972. GNUNET_BIO_write_spec_int32 ("anonymity", (int32_t *) &dc->anonymity),
  1973. GNUNET_BIO_write_spec_int32 ("options", (int32_t *) &dc->options),
  1974. GNUNET_BIO_write_spec_int32 ("has finished", (int32_t *) &dc->has_finished),
  1975. GNUNET_BIO_write_spec_end (),
  1976. };
  1977. if ((GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws1)) ||
  1978. (GNUNET_OK != write_start_time (wh, dc->start_time)) ||
  1979. (GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws2)))
  1980. {
  1981. GNUNET_break (0);
  1982. goto cleanup;
  1983. }
  1984. if (NULL == dc->emsg)
  1985. {
  1986. GNUNET_assert (dc->top_request != NULL);
  1987. if (GNUNET_YES != write_download_request (wh, dc->top_request))
  1988. {
  1989. GNUNET_break (0);
  1990. goto cleanup;
  1991. }
  1992. }
  1993. GNUNET_free_non_null (uris);
  1994. uris = NULL;
  1995. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  1996. {
  1997. wh = NULL;
  1998. GNUNET_break (0);
  1999. goto cleanup;
  2000. }
  2001. GNUNET_free (fn);
  2002. return;
  2003. cleanup:
  2004. if (NULL != wh)
  2005. (void) GNUNET_BIO_write_close (wh, NULL);
  2006. GNUNET_free_non_null (uris);
  2007. if (0 != unlink (fn))
  2008. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
  2009. GNUNET_free (fn);
  2010. GNUNET_free (dc->serialization);
  2011. dc->serialization = NULL;
  2012. }
  2013. /**
  2014. * Synchronize this search result with its mirror
  2015. * on disk. Note that all internal FS-operations that change
  2016. * publishing structs should already call "sync" internally,
  2017. * so this function is likely not useful for clients.
  2018. *
  2019. * @param sr the struct to sync
  2020. */
  2021. void
  2022. GNUNET_FS_search_result_sync_ (struct GNUNET_FS_SearchResult *sr)
  2023. {
  2024. struct GNUNET_BIO_WriteHandle *wh;
  2025. char *uris;
  2026. if (NULL == sr->sc)
  2027. return;
  2028. uris = NULL;
  2029. if (NULL == sr->serialization)
  2030. sr->serialization =
  2031. make_serialization_file_name_in_dir (sr->h,
  2032. (sr->sc->psearch_result == NULL)
  2033. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2034. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2035. sr->sc->serialization);
  2036. if (NULL == sr->serialization)
  2037. return;
  2038. wh = get_write_handle_in_dir (sr->h,
  2039. (sr->sc->psearch_result == NULL)
  2040. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2041. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2042. sr->sc->serialization,
  2043. sr->serialization);
  2044. if (NULL == wh)
  2045. {
  2046. GNUNET_break (0);
  2047. goto cleanup;
  2048. }
  2049. uris = GNUNET_FS_uri_to_string (sr->uri);
  2050. struct GNUNET_BIO_WriteSpec ws[] = {
  2051. GNUNET_BIO_write_spec_string ("uris", uris),
  2052. GNUNET_BIO_write_spec_string ("download serialization",
  2053. (sr->download != NULL)
  2054. ? sr->download->serialization
  2055. : NULL),
  2056. GNUNET_BIO_write_spec_string ("update search serialization",
  2057. (sr->update_search != NULL)
  2058. ? sr->update_search->serialization
  2059. : NULL),
  2060. GNUNET_BIO_write_spec_meta_data ("metadata", sr->meta),
  2061. GNUNET_BIO_write_spec_object ("key", &sr->key,
  2062. sizeof(struct GNUNET_HashCode)),
  2063. GNUNET_BIO_write_spec_int32 ("mandatory missing",
  2064. (int32_t *) &sr->mandatory_missing),
  2065. GNUNET_BIO_write_spec_int32 ("optional support",
  2066. (int32_t *) &sr->optional_support),
  2067. GNUNET_BIO_write_spec_int32 ("availability success",
  2068. (int32_t *) &sr->availability_success),
  2069. GNUNET_BIO_write_spec_int32 ("availability trials",
  2070. (int32_t *) &sr->availability_trials),
  2071. GNUNET_BIO_write_spec_end (),
  2072. };
  2073. if ((GNUNET_OK != GNUNET_BIO_write_spec_commit (wh, ws)))
  2074. {
  2075. GNUNET_break (0);
  2076. goto cleanup;
  2077. }
  2078. if ((NULL != sr->uri) && (GNUNET_FS_URI_KSK == sr->sc->uri->type) &&
  2079. (GNUNET_OK !=
  2080. GNUNET_BIO_write (wh,
  2081. "keyword bitmap",
  2082. sr->keyword_bitmap,
  2083. (sr->sc->uri->data.ksk.keywordCount + 7) / 8)))
  2084. {
  2085. GNUNET_break (0);
  2086. goto cleanup;
  2087. }
  2088. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  2089. {
  2090. wh = NULL;
  2091. GNUNET_break (0);
  2092. goto cleanup;
  2093. }
  2094. GNUNET_free_non_null (uris);
  2095. return;
  2096. cleanup:
  2097. GNUNET_free_non_null (uris);
  2098. if (NULL != wh)
  2099. (void) GNUNET_BIO_write_close (wh, NULL);
  2100. remove_sync_file_in_dir (sr->h,
  2101. (NULL == sr->sc->psearch_result)
  2102. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2103. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2104. sr->sc->serialization,
  2105. sr->serialization);
  2106. GNUNET_free (sr->serialization);
  2107. sr->serialization = NULL;
  2108. }
  2109. /**
  2110. * Synchronize this search struct with its mirror
  2111. * on disk. Note that all internal FS-operations that change
  2112. * publishing structs should already call "sync" internally,
  2113. * so this function is likely not useful for clients.
  2114. *
  2115. * @param sc the struct to sync
  2116. */
  2117. void
  2118. GNUNET_FS_search_sync_ (struct GNUNET_FS_SearchContext *sc)
  2119. {
  2120. struct GNUNET_BIO_WriteHandle *wh;
  2121. char *uris;
  2122. char in_pause;
  2123. const char *category;
  2124. category = (NULL == sc->psearch_result) ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2125. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH;
  2126. if (NULL == sc->serialization)
  2127. sc->serialization = make_serialization_file_name (sc->h, category);
  2128. if (NULL == sc->serialization)
  2129. return;
  2130. uris = NULL;
  2131. wh = get_write_handle (sc->h, category, sc->serialization);
  2132. if (NULL == wh)
  2133. {
  2134. GNUNET_break (0);
  2135. goto cleanup;
  2136. }
  2137. GNUNET_assert ((GNUNET_YES == GNUNET_FS_uri_test_ksk (sc->uri)) ||
  2138. (GNUNET_YES == GNUNET_FS_uri_test_sks (sc->uri)));
  2139. uris = GNUNET_FS_uri_to_string (sc->uri);
  2140. in_pause = (sc->task != NULL) ? 'r' : '\0';
  2141. if ((GNUNET_OK != GNUNET_BIO_write_string (wh, "uris", uris)) ||
  2142. (GNUNET_OK != write_start_time (wh, sc->start_time)) ||
  2143. (GNUNET_OK != GNUNET_BIO_write_string (wh, "emsg", sc->emsg)) ||
  2144. (GNUNET_OK != GNUNET_BIO_write_int32 (wh, "options",
  2145. (uint32_t) sc->options)) ||
  2146. (GNUNET_OK != GNUNET_BIO_write (wh, "in pause",
  2147. &in_pause, sizeof(in_pause))) ||
  2148. (GNUNET_OK != GNUNET_BIO_write_int32 (wh, "anonymity", sc->anonymity)))
  2149. {
  2150. GNUNET_break (0);
  2151. goto cleanup;
  2152. }
  2153. GNUNET_free (uris);
  2154. uris = NULL;
  2155. if (GNUNET_OK != GNUNET_BIO_write_close (wh, NULL))
  2156. {
  2157. wh = NULL;
  2158. GNUNET_break (0);
  2159. goto cleanup;
  2160. }
  2161. return;
  2162. cleanup:
  2163. if (NULL != wh)
  2164. (void) GNUNET_BIO_write_close (wh, NULL);
  2165. GNUNET_free_non_null (uris);
  2166. GNUNET_FS_remove_sync_file_ (sc->h, category, sc->serialization);
  2167. GNUNET_free (sc->serialization);
  2168. sc->serialization = NULL;
  2169. }
  2170. /**
  2171. * Function called with a filename of serialized unindexing operation
  2172. * to deserialize.
  2173. *
  2174. * @param cls the `struct GNUNET_FS_Handle *`
  2175. * @param filename complete filename (absolute path)
  2176. * @return #GNUNET_OK (continue to iterate)
  2177. */
  2178. static int
  2179. deserialize_unindex_file (void *cls, const char *filename)
  2180. {
  2181. struct GNUNET_FS_Handle *h = cls;
  2182. struct GNUNET_BIO_ReadHandle *rh;
  2183. struct GNUNET_FS_UnindexContext *uc;
  2184. struct GNUNET_FS_ProgressInfo pi;
  2185. char *emsg;
  2186. char *uris;
  2187. uint32_t state;
  2188. uc = GNUNET_new (struct GNUNET_FS_UnindexContext);
  2189. uc->h = h;
  2190. uc->serialization = get_serialization_short_name (filename);
  2191. rh = GNUNET_BIO_read_open_file (filename);
  2192. if (NULL == rh)
  2193. {
  2194. GNUNET_break (0);
  2195. goto cleanup;
  2196. }
  2197. uris = NULL;
  2198. if ((GNUNET_OK !=
  2199. GNUNET_BIO_read_string (rh, "unindex-fn", &uc->filename, 10 * 1024)) ||
  2200. (GNUNET_OK != GNUNET_BIO_read_int64 (rh, "file size",
  2201. (int64_t *) &uc->file_size)) ||
  2202. (GNUNET_OK != read_start_time (rh, &uc->start_time)) ||
  2203. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "state",
  2204. (int32_t *) &state)) ||
  2205. (GNUNET_OK !=
  2206. GNUNET_BIO_read (rh, "uri", &uc->chk, sizeof(struct ContentHashKey))) ||
  2207. (GNUNET_OK !=
  2208. GNUNET_BIO_read_string (rh, "unindex-kskuri", &uris, 10 * 1024)) ||
  2209. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "ksk offset",
  2210. (int32_t *) &uc->ksk_offset)))
  2211. {
  2212. GNUNET_free_non_null (uris);
  2213. GNUNET_break (0);
  2214. goto cleanup;
  2215. }
  2216. if (NULL != uris)
  2217. {
  2218. uc->ksk_uri = GNUNET_FS_uri_parse (uris, &emsg);
  2219. GNUNET_free (uris);
  2220. if (NULL == uc->ksk_uri)
  2221. {
  2222. GNUNET_break (0);
  2223. GNUNET_free_non_null (emsg);
  2224. goto cleanup;
  2225. }
  2226. }
  2227. if ((uc->ksk_offset > 0) &&
  2228. ((NULL == uc->ksk_uri) ||
  2229. (uc->ksk_offset > uc->ksk_uri->data.ksk.keywordCount)))
  2230. {
  2231. GNUNET_break (0);
  2232. goto cleanup;
  2233. }
  2234. uc->state = (enum UnindexState) state;
  2235. switch (state)
  2236. {
  2237. case UNINDEX_STATE_HASHING:
  2238. break;
  2239. case UNINDEX_STATE_FS_NOTIFY:
  2240. if (GNUNET_OK != GNUNET_BIO_read (rh,
  2241. "unindex-hash",
  2242. &uc->file_id,
  2243. sizeof(struct GNUNET_HashCode)))
  2244. {
  2245. GNUNET_break (0);
  2246. goto cleanup;
  2247. }
  2248. break;
  2249. case UNINDEX_STATE_DS_REMOVE:
  2250. case UNINDEX_STATE_EXTRACT_KEYWORDS:
  2251. case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
  2252. break;
  2253. case UNINDEX_STATE_COMPLETE:
  2254. break;
  2255. case UNINDEX_STATE_ERROR:
  2256. if (GNUNET_OK !=
  2257. GNUNET_BIO_read_string (rh, "unindex-emsg", &uc->emsg, 10 * 1024))
  2258. {
  2259. GNUNET_break (0);
  2260. goto cleanup;
  2261. }
  2262. break;
  2263. default:
  2264. GNUNET_break (0);
  2265. goto cleanup;
  2266. }
  2267. uc->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, uc);
  2268. pi.status = GNUNET_FS_STATUS_UNINDEX_RESUME;
  2269. pi.value.unindex.specifics.resume.message = uc->emsg;
  2270. GNUNET_FS_unindex_make_status_ (&pi,
  2271. uc,
  2272. (uc->state == UNINDEX_STATE_COMPLETE)
  2273. ? uc->file_size
  2274. : 0);
  2275. switch (uc->state)
  2276. {
  2277. case UNINDEX_STATE_HASHING:
  2278. uc->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
  2279. uc->filename,
  2280. HASHING_BLOCKSIZE,
  2281. &GNUNET_FS_unindex_process_hash_,
  2282. uc);
  2283. break;
  2284. case UNINDEX_STATE_FS_NOTIFY:
  2285. uc->state = UNINDEX_STATE_HASHING;
  2286. GNUNET_FS_unindex_process_hash_ (uc, &uc->file_id);
  2287. break;
  2288. case UNINDEX_STATE_DS_REMOVE:
  2289. GNUNET_FS_unindex_do_remove_ (uc);
  2290. break;
  2291. case UNINDEX_STATE_EXTRACT_KEYWORDS:
  2292. GNUNET_FS_unindex_do_extract_keywords_ (uc);
  2293. break;
  2294. case UNINDEX_STATE_DS_REMOVE_KBLOCKS:
  2295. GNUNET_FS_unindex_do_remove_kblocks_ (uc);
  2296. break;
  2297. case UNINDEX_STATE_COMPLETE:
  2298. case UNINDEX_STATE_ERROR:
  2299. /* no need to resume any operation, we were done */
  2300. break;
  2301. default:
  2302. break;
  2303. }
  2304. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  2305. {
  2306. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2307. _ ("Failure while resuming unindexing operation `%s': %s\n"),
  2308. filename,
  2309. emsg);
  2310. GNUNET_free (emsg);
  2311. }
  2312. return GNUNET_OK;
  2313. cleanup:
  2314. GNUNET_free_non_null (uc->filename);
  2315. if ((NULL != rh) && (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg)))
  2316. {
  2317. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2318. _ ("Failed to resume unindexing operation `%s': %s\n"),
  2319. filename,
  2320. emsg);
  2321. GNUNET_free (emsg);
  2322. }
  2323. if (NULL != uc->serialization)
  2324. GNUNET_FS_remove_sync_file_ (h,
  2325. GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
  2326. uc->serialization);
  2327. GNUNET_free_non_null (uc->serialization);
  2328. GNUNET_free (uc);
  2329. return GNUNET_OK;
  2330. }
  2331. /**
  2332. * Deserialize a download.
  2333. *
  2334. * @param h overall context
  2335. * @param rh file to deserialize from
  2336. * @param parent parent download
  2337. * @param search associated search
  2338. * @param serialization name under which the search was serialized
  2339. */
  2340. static void
  2341. deserialize_download (struct GNUNET_FS_Handle *h,
  2342. struct GNUNET_BIO_ReadHandle *rh,
  2343. struct GNUNET_FS_DownloadContext *parent,
  2344. struct GNUNET_FS_SearchResult *search,
  2345. const char *serialization);
  2346. /**
  2347. * Deserialize a search.
  2348. *
  2349. * @param h overall context
  2350. * @param rh file to deserialize from
  2351. * @param psearch_result parent search result
  2352. * @param serialization name under which the search was serialized
  2353. */
  2354. static struct GNUNET_FS_SearchContext *
  2355. deserialize_search (struct GNUNET_FS_Handle *h,
  2356. struct GNUNET_BIO_ReadHandle *rh,
  2357. struct GNUNET_FS_SearchResult *psearch_result,
  2358. const char *serialization);
  2359. /**
  2360. * Function called with a filename of serialized search result
  2361. * to deserialize.
  2362. *
  2363. * @param cls the `struct GNUNET_FS_SearchContext *`
  2364. * @param filename complete filename (absolute path)
  2365. * @return #GNUNET_OK (continue to iterate)
  2366. */
  2367. static int
  2368. deserialize_search_result (void *cls, const char *filename)
  2369. {
  2370. struct GNUNET_FS_SearchContext *sc = cls;
  2371. char *ser;
  2372. char *uris;
  2373. char *emsg;
  2374. char *download;
  2375. char *update_srch;
  2376. struct GNUNET_BIO_ReadHandle *rh;
  2377. struct GNUNET_BIO_ReadHandle *drh;
  2378. struct GNUNET_FS_SearchResult *sr;
  2379. ser = get_serialization_short_name (filename);
  2380. rh = GNUNET_BIO_read_open_file (filename);
  2381. if (NULL == rh)
  2382. {
  2383. if (NULL != ser)
  2384. {
  2385. remove_sync_file_in_dir (sc->h,
  2386. (NULL == sc->psearch_result)
  2387. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2388. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2389. sc->serialization,
  2390. ser);
  2391. GNUNET_free (ser);
  2392. }
  2393. return GNUNET_OK;
  2394. }
  2395. emsg = NULL;
  2396. uris = NULL;
  2397. download = NULL;
  2398. update_srch = NULL;
  2399. sr = GNUNET_new (struct GNUNET_FS_SearchResult);
  2400. sr->h = sc->h;
  2401. sr->sc = sc;
  2402. sr->serialization = ser;
  2403. if ((GNUNET_OK !=
  2404. GNUNET_BIO_read_string (rh, "result-uri", &uris, 10 * 1024)) ||
  2405. (NULL == (sr->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
  2406. (GNUNET_OK !=
  2407. GNUNET_BIO_read_string (rh, "download-lnk", &download, 16)) ||
  2408. (GNUNET_OK !=
  2409. GNUNET_BIO_read_string (rh, "search-lnk", &update_srch, 16)) ||
  2410. (GNUNET_OK != GNUNET_BIO_read_meta_data (rh, "result-meta", &sr->meta)) ||
  2411. (GNUNET_OK != GNUNET_BIO_read (rh,
  2412. "result-key",
  2413. &sr->key,
  2414. sizeof(struct GNUNET_HashCode))) ||
  2415. (GNUNET_OK != GNUNET_BIO_read_int32 (
  2416. rh,
  2417. "mandatory missing",
  2418. (int32_t *) &sr->mandatory_missing)) ||
  2419. (GNUNET_OK != GNUNET_BIO_read_int32 (
  2420. rh,
  2421. "optional support",
  2422. (int32_t *) &sr->optional_support)) ||
  2423. (GNUNET_OK != GNUNET_BIO_read_int32 (
  2424. rh,
  2425. "availability success",
  2426. (int32_t *) &sr->availability_success)) ||
  2427. (GNUNET_OK != GNUNET_BIO_read_int32 (
  2428. rh,
  2429. "availability trials",
  2430. (int32_t *) &sr->availability_trials)))
  2431. {
  2432. GNUNET_break (0);
  2433. goto cleanup;
  2434. }
  2435. if (GNUNET_FS_URI_KSK == sr->sc->uri->type)
  2436. {
  2437. sr->keyword_bitmap = GNUNET_malloc (
  2438. (sr->sc->uri->data.ksk.keywordCount + 7) / 8); /* round up, count bits */
  2439. if (GNUNET_OK !=
  2440. GNUNET_BIO_read (rh,
  2441. "keyword-bitmap",
  2442. sr->keyword_bitmap,
  2443. (sr->sc->uri->data.ksk.keywordCount + 7) / 8))
  2444. {
  2445. GNUNET_break (0);
  2446. goto cleanup;
  2447. }
  2448. }
  2449. GNUNET_free (uris);
  2450. if (NULL != download)
  2451. {
  2452. drh = get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD, download);
  2453. if (NULL != drh)
  2454. {
  2455. deserialize_download (sc->h, drh, NULL, sr, download);
  2456. if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
  2457. {
  2458. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2459. _ ("Failed to resume sub-download `%s': %s\n"),
  2460. download,
  2461. emsg);
  2462. GNUNET_free (emsg);
  2463. }
  2464. }
  2465. GNUNET_free (download);
  2466. }
  2467. if (NULL != update_srch)
  2468. {
  2469. drh =
  2470. get_read_handle (sc->h, GNUNET_FS_SYNC_PATH_CHILD_SEARCH, update_srch);
  2471. if (NULL != drh)
  2472. {
  2473. deserialize_search (sc->h, drh, sr, update_srch);
  2474. if (GNUNET_OK != GNUNET_BIO_read_close (drh, &emsg))
  2475. {
  2476. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2477. _ ("Failed to resume sub-search `%s': %s\n"),
  2478. update_srch,
  2479. emsg);
  2480. GNUNET_free (emsg);
  2481. }
  2482. }
  2483. GNUNET_free (update_srch);
  2484. }
  2485. GNUNET_break (GNUNET_YES == GNUNET_CONTAINER_multihashmap_put (
  2486. sc->master_result_map,
  2487. &sr->key,
  2488. sr,
  2489. GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
  2490. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  2491. {
  2492. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2493. _ ("Failure while resuming search operation `%s': %s\n"),
  2494. filename,
  2495. emsg);
  2496. GNUNET_free (emsg);
  2497. }
  2498. return GNUNET_OK;
  2499. cleanup:
  2500. GNUNET_free_non_null (download);
  2501. GNUNET_free_non_null (emsg);
  2502. GNUNET_free_non_null (uris);
  2503. GNUNET_free_non_null (update_srch);
  2504. if (NULL != sr->uri)
  2505. GNUNET_FS_uri_destroy (sr->uri);
  2506. if (NULL != sr->meta)
  2507. GNUNET_CONTAINER_meta_data_destroy (sr->meta);
  2508. GNUNET_free (sr->serialization);
  2509. GNUNET_free (sr);
  2510. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  2511. {
  2512. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2513. _ ("Failure while resuming search operation `%s': %s\n"),
  2514. filename,
  2515. emsg);
  2516. GNUNET_free (emsg);
  2517. }
  2518. return GNUNET_OK;
  2519. }
  2520. /**
  2521. * Send the 'resume' signal to the callback; also actually
  2522. * resume the download (put it in the queue). Does this
  2523. * recursively for the top-level download and all child
  2524. * downloads.
  2525. *
  2526. * @param dc download to resume
  2527. */
  2528. static void
  2529. signal_download_resume (struct GNUNET_FS_DownloadContext *dc)
  2530. {
  2531. struct GNUNET_FS_DownloadContext *dcc;
  2532. struct GNUNET_FS_ProgressInfo pi;
  2533. pi.status = GNUNET_FS_STATUS_DOWNLOAD_RESUME;
  2534. pi.value.download.specifics.resume.meta = dc->meta;
  2535. pi.value.download.specifics.resume.message = dc->emsg;
  2536. GNUNET_FS_download_make_status_ (&pi, dc);
  2537. dcc = dc->child_head;
  2538. while (NULL != dcc)
  2539. {
  2540. signal_download_resume (dcc);
  2541. dcc = dcc->next;
  2542. }
  2543. }
  2544. /**
  2545. * Signal resuming of a search to our clients (for the
  2546. * top level search and all sub-searches).
  2547. *
  2548. * @param sc search being resumed
  2549. */
  2550. static void
  2551. signal_search_resume (struct GNUNET_FS_SearchContext *sc);
  2552. /**
  2553. * Iterator over search results signaling resume to the client for
  2554. * each result.
  2555. *
  2556. * @param cls closure, the `struct GNUNET_FS_SearchContext *`
  2557. * @param key current key code
  2558. * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
  2559. * @return #GNUNET_YES (we should continue to iterate)
  2560. */
  2561. static int
  2562. signal_result_resume (void *cls, const struct GNUNET_HashCode *key, void *value)
  2563. {
  2564. struct GNUNET_FS_SearchContext *sc = cls;
  2565. struct GNUNET_FS_ProgressInfo pi;
  2566. struct GNUNET_FS_SearchResult *sr = value;
  2567. if (0 == sr->mandatory_missing)
  2568. {
  2569. pi.status = GNUNET_FS_STATUS_SEARCH_RESUME_RESULT;
  2570. pi.value.search.specifics.resume_result.meta = sr->meta;
  2571. pi.value.search.specifics.resume_result.uri = sr->uri;
  2572. pi.value.search.specifics.resume_result.result = sr;
  2573. pi.value.search.specifics.resume_result.availability_rank =
  2574. 2 * sr->availability_success - sr->availability_trials;
  2575. pi.value.search.specifics.resume_result.availability_certainty =
  2576. sr->availability_trials;
  2577. pi.value.search.specifics.resume_result.applicability_rank =
  2578. sr->optional_support;
  2579. sr->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
  2580. }
  2581. if (NULL != sr->download)
  2582. {
  2583. signal_download_resume (sr->download);
  2584. }
  2585. else
  2586. {
  2587. GNUNET_FS_search_start_probe_ (sr);
  2588. }
  2589. if (NULL != sr->update_search)
  2590. signal_search_resume (sr->update_search);
  2591. return GNUNET_YES;
  2592. }
  2593. /**
  2594. * Free memory allocated by the search context and its children
  2595. *
  2596. * @param sc search context to free
  2597. */
  2598. static void
  2599. free_search_context (struct GNUNET_FS_SearchContext *sc);
  2600. /**
  2601. * Iterator over search results freeing each.
  2602. *
  2603. * @param cls closure, the `struct GNUNET_FS_SearchContext *`
  2604. * @param key current key code
  2605. * @param value value in the hash map, the `struct GNUNET_FS_SearchResult *`
  2606. * @return #GNUNET_YES (we should continue to iterate)
  2607. */
  2608. static int
  2609. free_result (void *cls, const struct GNUNET_HashCode *key, void *value)
  2610. {
  2611. struct GNUNET_FS_SearchResult *sr = value;
  2612. if (NULL != sr->update_search)
  2613. {
  2614. free_search_context (sr->update_search);
  2615. GNUNET_assert (NULL == sr->update_search);
  2616. }
  2617. GNUNET_CONTAINER_meta_data_destroy (sr->meta);
  2618. GNUNET_FS_uri_destroy (sr->uri);
  2619. GNUNET_free (sr);
  2620. return GNUNET_YES;
  2621. }
  2622. /**
  2623. * Free memory allocated by the search context and its children
  2624. *
  2625. * @param sc search context to free
  2626. */
  2627. static void
  2628. free_search_context (struct GNUNET_FS_SearchContext *sc)
  2629. {
  2630. if (NULL != sc->serialization)
  2631. {
  2632. GNUNET_FS_remove_sync_file_ (sc->h,
  2633. (sc->psearch_result == NULL)
  2634. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2635. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2636. sc->serialization);
  2637. GNUNET_FS_remove_sync_dir_ (sc->h,
  2638. (sc->psearch_result == NULL)
  2639. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2640. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2641. sc->serialization);
  2642. }
  2643. GNUNET_free_non_null (sc->serialization);
  2644. GNUNET_free_non_null (sc->emsg);
  2645. if (NULL != sc->uri)
  2646. GNUNET_FS_uri_destroy (sc->uri);
  2647. if (NULL != sc->master_result_map)
  2648. {
  2649. GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
  2650. &free_result,
  2651. sc);
  2652. GNUNET_CONTAINER_multihashmap_destroy (sc->master_result_map);
  2653. }
  2654. GNUNET_free (sc);
  2655. }
  2656. /**
  2657. * Function called with a filename of serialized sub-download
  2658. * to deserialize.
  2659. *
  2660. * @param cls the `struct GNUNET_FS_DownloadContext *` (parent)
  2661. * @param filename complete filename (absolute path)
  2662. * @return #GNUNET_OK (continue to iterate)
  2663. */
  2664. static int
  2665. deserialize_subdownload (void *cls, const char *filename)
  2666. {
  2667. struct GNUNET_FS_DownloadContext *parent = cls;
  2668. char *ser;
  2669. char *emsg;
  2670. struct GNUNET_BIO_ReadHandle *rh;
  2671. ser = get_serialization_short_name (filename);
  2672. rh = GNUNET_BIO_read_open_file (filename);
  2673. if (NULL == rh)
  2674. {
  2675. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2676. _ (
  2677. "Failed to resume sub-download `%s': could not open file `%s'\n"),
  2678. ser,
  2679. filename);
  2680. GNUNET_free (ser);
  2681. return GNUNET_OK;
  2682. }
  2683. deserialize_download (parent->h, rh, parent, NULL, ser);
  2684. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  2685. {
  2686. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2687. _ ("Failed to resume sub-download `%s': %s\n"),
  2688. ser,
  2689. emsg);
  2690. GNUNET_free (emsg);
  2691. }
  2692. GNUNET_free (ser);
  2693. return GNUNET_OK;
  2694. }
  2695. /**
  2696. * Free this download context and all of its descendants.
  2697. * (only works during deserialization since not all possible
  2698. * state it taken care of).
  2699. *
  2700. * @param dc context to free
  2701. */
  2702. static void
  2703. free_download_context (struct GNUNET_FS_DownloadContext *dc)
  2704. {
  2705. struct GNUNET_FS_DownloadContext *dcc;
  2706. if (NULL != dc->meta)
  2707. GNUNET_CONTAINER_meta_data_destroy (dc->meta);
  2708. if (NULL != dc->uri)
  2709. GNUNET_FS_uri_destroy (dc->uri);
  2710. GNUNET_free_non_null (dc->temp_filename);
  2711. GNUNET_free_non_null (dc->emsg);
  2712. GNUNET_free_non_null (dc->filename);
  2713. GNUNET_free_non_null (dc->serialization);
  2714. while (NULL != (dcc = dc->child_head))
  2715. {
  2716. GNUNET_CONTAINER_DLL_remove (dc->child_head, dc->child_tail, dcc);
  2717. free_download_context (dcc);
  2718. }
  2719. GNUNET_FS_free_download_request_ (dc->top_request);
  2720. if (NULL != dc->active)
  2721. GNUNET_CONTAINER_multihashmap_destroy (dc->active);
  2722. GNUNET_free (dc);
  2723. }
  2724. /**
  2725. * Deserialize a download.
  2726. *
  2727. * @param h overall context
  2728. * @param rh file to deserialize from
  2729. * @param parent parent download
  2730. * @param search associated search
  2731. * @param serialization name under which the search was serialized
  2732. */
  2733. static void
  2734. deserialize_download (struct GNUNET_FS_Handle *h,
  2735. struct GNUNET_BIO_ReadHandle *rh,
  2736. struct GNUNET_FS_DownloadContext *parent,
  2737. struct GNUNET_FS_SearchResult *search,
  2738. const char *serialization)
  2739. {
  2740. struct GNUNET_FS_DownloadContext *dc;
  2741. char *emsg;
  2742. char *uris;
  2743. char *dn;
  2744. uint32_t options;
  2745. uint32_t status;
  2746. uris = NULL;
  2747. emsg = NULL;
  2748. dc = GNUNET_new (struct GNUNET_FS_DownloadContext);
  2749. dc->parent = parent;
  2750. dc->h = h;
  2751. dc->serialization = GNUNET_strdup (serialization);
  2752. struct GNUNET_BIO_ReadSpec rs[] = {
  2753. GNUNET_BIO_read_spec_meta_data ("download-meta", &dc->meta),
  2754. GNUNET_BIO_read_spec_string ("download-emsg", &dc->emsg, 10 * 1024),
  2755. GNUNET_BIO_read_spec_string ("download-fn", &dc->filename, 10 * 1024),
  2756. GNUNET_BIO_read_spec_string ("download-tfn",
  2757. &dc->temp_filename, 10 * 1024),
  2758. GNUNET_BIO_read_spec_int64 ("old file size",
  2759. (int64_t *) &dc->old_file_size),
  2760. GNUNET_BIO_read_spec_int64 ("offset",
  2761. (int64_t *) &dc->offset),
  2762. GNUNET_BIO_read_spec_int64 ("length",
  2763. (int64_t *) &dc->length),
  2764. GNUNET_BIO_read_spec_int64 ("completed",
  2765. (int64_t *) &dc->completed),
  2766. GNUNET_BIO_read_spec_end (),
  2767. };
  2768. if ((GNUNET_OK !=
  2769. GNUNET_BIO_read_string (rh, "download-uri", &uris, 10 * 1024)) ||
  2770. (NULL == (dc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
  2771. ((GNUNET_YES != GNUNET_FS_uri_test_chk (dc->uri)) &&
  2772. (GNUNET_YES != GNUNET_FS_uri_test_loc (dc->uri))) ||
  2773. (GNUNET_OK != GNUNET_BIO_read_spec_commit (rh, rs)) ||
  2774. (GNUNET_OK != read_start_time (rh, &dc->start_time)) ||
  2775. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "anonymity",
  2776. (int32_t *) &dc->anonymity)) ||
  2777. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "options",
  2778. (int32_t *) &options)) ||
  2779. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "status",
  2780. (int32_t *) &status)))
  2781. {
  2782. GNUNET_break (0);
  2783. goto cleanup;
  2784. }
  2785. dc->options = (enum GNUNET_FS_DownloadOptions) options;
  2786. dc->active =
  2787. GNUNET_CONTAINER_multihashmap_create (1 + 2 * (dc->length / DBLOCK_SIZE),
  2788. GNUNET_NO);
  2789. dc->has_finished = (int) status;
  2790. dc->treedepth =
  2791. GNUNET_FS_compute_depth (GNUNET_FS_uri_chk_get_file_size (dc->uri));
  2792. if (GNUNET_FS_uri_test_loc (dc->uri))
  2793. GNUNET_assert (GNUNET_OK ==
  2794. GNUNET_FS_uri_loc_get_peer_identity (dc->uri, &dc->target));
  2795. if (NULL == dc->emsg)
  2796. {
  2797. dc->top_request = read_download_request (rh);
  2798. if (NULL == dc->top_request)
  2799. {
  2800. GNUNET_break (0);
  2801. goto cleanup;
  2802. }
  2803. }
  2804. dn = get_download_sync_filename (dc, dc->serialization, ".dir");
  2805. if (NULL != dn)
  2806. {
  2807. if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
  2808. GNUNET_DISK_directory_scan (dn, &deserialize_subdownload, dc);
  2809. GNUNET_free (dn);
  2810. }
  2811. if (NULL != parent)
  2812. {
  2813. GNUNET_CONTAINER_DLL_insert (parent->child_head, parent->child_tail, dc);
  2814. }
  2815. if (NULL != search)
  2816. {
  2817. dc->search = search;
  2818. search->download = dc;
  2819. }
  2820. if ((NULL == parent) && (NULL == search))
  2821. {
  2822. dc->top =
  2823. GNUNET_FS_make_top (dc->h, &GNUNET_FS_download_signal_suspend_, dc);
  2824. signal_download_resume (dc);
  2825. }
  2826. GNUNET_free (uris);
  2827. GNUNET_assert (NULL == dc->job_queue);
  2828. dc->task = GNUNET_SCHEDULER_add_now (&GNUNET_FS_download_start_task_, dc);
  2829. return;
  2830. cleanup:
  2831. GNUNET_free_non_null (uris);
  2832. GNUNET_free_non_null (emsg);
  2833. free_download_context (dc);
  2834. }
  2835. /**
  2836. * Signal resuming of a search to our clients (for the
  2837. * top level search and all sub-searches).
  2838. *
  2839. * @param sc search being resumed
  2840. */
  2841. static void
  2842. signal_search_resume (struct GNUNET_FS_SearchContext *sc)
  2843. {
  2844. struct GNUNET_FS_ProgressInfo pi;
  2845. pi.status = GNUNET_FS_STATUS_SEARCH_RESUME;
  2846. pi.value.search.specifics.resume.message = sc->emsg;
  2847. pi.value.search.specifics.resume.is_paused =
  2848. (NULL == sc->mq) ? GNUNET_YES : GNUNET_NO;
  2849. sc->client_info = GNUNET_FS_search_make_status_ (&pi, sc->h, sc);
  2850. GNUNET_CONTAINER_multihashmap_iterate (sc->master_result_map,
  2851. &signal_result_resume,
  2852. sc);
  2853. }
  2854. /**
  2855. * Deserialize a search.
  2856. *
  2857. * @param h overall context
  2858. * @param rh file to deserialize from
  2859. * @param psearch_result parent search result
  2860. * @param serialization name under which the search was serialized
  2861. */
  2862. static struct GNUNET_FS_SearchContext *
  2863. deserialize_search (struct GNUNET_FS_Handle *h,
  2864. struct GNUNET_BIO_ReadHandle *rh,
  2865. struct GNUNET_FS_SearchResult *psearch_result,
  2866. const char *serialization)
  2867. {
  2868. struct GNUNET_FS_SearchContext *sc;
  2869. char *emsg;
  2870. char *uris;
  2871. char *dn;
  2872. uint32_t options;
  2873. char in_pause;
  2874. if ((NULL != psearch_result) && (NULL != psearch_result->update_search))
  2875. {
  2876. GNUNET_break (0);
  2877. return NULL;
  2878. }
  2879. uris = NULL;
  2880. emsg = NULL;
  2881. sc = GNUNET_new (struct GNUNET_FS_SearchContext);
  2882. if (NULL != psearch_result)
  2883. {
  2884. sc->psearch_result = psearch_result;
  2885. psearch_result->update_search = sc;
  2886. }
  2887. sc->h = h;
  2888. sc->serialization = GNUNET_strdup (serialization);
  2889. if ((GNUNET_OK !=
  2890. GNUNET_BIO_read_string (rh, "search-uri", &uris, 10 * 1024)) ||
  2891. (NULL == (sc->uri = GNUNET_FS_uri_parse (uris, &emsg))) ||
  2892. ((GNUNET_YES != GNUNET_FS_uri_test_ksk (sc->uri)) &&
  2893. (GNUNET_YES != GNUNET_FS_uri_test_sks (sc->uri))) ||
  2894. (GNUNET_OK != read_start_time (rh, &sc->start_time)) ||
  2895. (GNUNET_OK !=
  2896. GNUNET_BIO_read_string (rh, "search-emsg", &sc->emsg, 10 * 1024)) ||
  2897. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "options",
  2898. (int32_t *) &options)) ||
  2899. (GNUNET_OK !=
  2900. GNUNET_BIO_read (rh, "search-pause", &in_pause, sizeof(in_pause))) ||
  2901. (GNUNET_OK != GNUNET_BIO_read_int32 (rh, "anonymity",
  2902. (int32_t *) &sc->anonymity)))
  2903. {
  2904. GNUNET_break (0);
  2905. goto cleanup;
  2906. }
  2907. sc->options = (enum GNUNET_FS_SearchOptions) options;
  2908. sc->master_result_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
  2909. dn = get_serialization_file_name_in_dir (h,
  2910. (NULL == sc->psearch_result)
  2911. ? GNUNET_FS_SYNC_PATH_MASTER_SEARCH
  2912. : GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
  2913. sc->serialization,
  2914. "");
  2915. if (NULL != dn)
  2916. {
  2917. if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
  2918. GNUNET_DISK_directory_scan (dn, &deserialize_search_result, sc);
  2919. GNUNET_free (dn);
  2920. }
  2921. if (('\0' == in_pause) &&
  2922. (GNUNET_OK != GNUNET_FS_search_start_searching_ (sc)))
  2923. {
  2924. GNUNET_log (
  2925. GNUNET_ERROR_TYPE_WARNING,
  2926. _ ("Could not resume running search, will resume as paused search\n"));
  2927. }
  2928. signal_search_resume (sc);
  2929. GNUNET_free (uris);
  2930. return sc;
  2931. cleanup:
  2932. GNUNET_free_non_null (emsg);
  2933. free_search_context (sc);
  2934. GNUNET_free_non_null (uris);
  2935. return NULL;
  2936. }
  2937. /**
  2938. * Function called with a filename of serialized search operation
  2939. * to deserialize.
  2940. *
  2941. * @param cls the `struct GNUNET_FS_Handle *`
  2942. * @param filename complete filename (absolute path)
  2943. * @return #GNUNET_OK (continue to iterate)
  2944. */
  2945. static int
  2946. deserialize_search_file (void *cls, const char *filename)
  2947. {
  2948. struct GNUNET_FS_Handle *h = cls;
  2949. char *ser;
  2950. char *emsg;
  2951. struct GNUNET_BIO_ReadHandle *rh;
  2952. struct GNUNET_FS_SearchContext *sc;
  2953. struct stat buf;
  2954. if (0 != stat (filename, &buf))
  2955. {
  2956. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
  2957. return GNUNET_OK;
  2958. }
  2959. if (S_ISDIR (buf.st_mode))
  2960. return GNUNET_OK; /* skip directories */
  2961. ser = get_serialization_short_name (filename);
  2962. rh = GNUNET_BIO_read_open_file (filename);
  2963. if (NULL == rh)
  2964. {
  2965. if (NULL != ser)
  2966. {
  2967. GNUNET_FS_remove_sync_file_ (h, GNUNET_FS_SYNC_PATH_MASTER_SEARCH, ser);
  2968. GNUNET_free (ser);
  2969. }
  2970. return GNUNET_OK;
  2971. }
  2972. sc = deserialize_search (h, rh, NULL, ser);
  2973. if (NULL != sc)
  2974. sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
  2975. GNUNET_free (ser);
  2976. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  2977. {
  2978. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2979. _ ("Failure while resuming search operation `%s': %s\n"),
  2980. filename,
  2981. emsg);
  2982. GNUNET_free (emsg);
  2983. }
  2984. return GNUNET_OK;
  2985. }
  2986. /**
  2987. * Function called with a filename of serialized download operation
  2988. * to deserialize.
  2989. *
  2990. * @param cls the `struct GNUNET_FS_Handle *`
  2991. * @param filename complete filename (absolute path)
  2992. * @return #GNUNET_OK (continue to iterate)
  2993. */
  2994. static int
  2995. deserialize_download_file (void *cls, const char *filename)
  2996. {
  2997. struct GNUNET_FS_Handle *h = cls;
  2998. char *ser;
  2999. char *emsg;
  3000. struct GNUNET_BIO_ReadHandle *rh;
  3001. ser = get_serialization_short_name (filename);
  3002. rh = GNUNET_BIO_read_open_file (filename);
  3003. if (NULL == rh)
  3004. {
  3005. if (0 != unlink (filename))
  3006. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
  3007. GNUNET_free (ser);
  3008. return GNUNET_OK;
  3009. }
  3010. deserialize_download (h, rh, NULL, NULL, ser);
  3011. GNUNET_free (ser);
  3012. if (GNUNET_OK != GNUNET_BIO_read_close (rh, &emsg))
  3013. {
  3014. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  3015. _ ("Failure while resuming download operation `%s': %s\n"),
  3016. filename,
  3017. emsg);
  3018. GNUNET_free (emsg);
  3019. }
  3020. return GNUNET_OK;
  3021. }
  3022. /**
  3023. * Deserialize informatin about pending operations.
  3024. *
  3025. * @param master_path which master directory should be scanned
  3026. * @param proc function to call for each entry (will get @a h for 'cls')
  3027. * @param h the `struct GNUNET_FS_Handle *`
  3028. */
  3029. static void
  3030. deserialization_master (const char *master_path,
  3031. GNUNET_FileNameCallback proc,
  3032. struct GNUNET_FS_Handle *h)
  3033. {
  3034. char *dn;
  3035. dn = get_serialization_file_name (h, master_path, "");
  3036. if (NULL == dn)
  3037. return;
  3038. if (GNUNET_YES == GNUNET_DISK_directory_test (dn, GNUNET_YES))
  3039. GNUNET_DISK_directory_scan (dn, proc, h);
  3040. GNUNET_free (dn);
  3041. }
  3042. /**
  3043. * Setup a connection to the file-sharing service.
  3044. *
  3045. * @param cfg configuration to use
  3046. * @param client_name unique identifier for this client
  3047. * @param upcb function to call to notify about FS actions
  3048. * @param upcb_cls closure for @a upcb
  3049. * @param flags specific attributes for fs-operations
  3050. * @param ... list of optional options, terminated with #GNUNET_FS_OPTIONS_END
  3051. * @return NULL on error
  3052. */
  3053. struct GNUNET_FS_Handle *
  3054. GNUNET_FS_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
  3055. const char *client_name,
  3056. GNUNET_FS_ProgressCallback upcb,
  3057. void *upcb_cls,
  3058. enum GNUNET_FS_Flags flags,
  3059. ...)
  3060. {
  3061. struct GNUNET_FS_Handle *ret;
  3062. enum GNUNET_FS_OPTIONS opt;
  3063. va_list ap;
  3064. ret = GNUNET_new (struct GNUNET_FS_Handle);
  3065. ret->cfg = cfg;
  3066. ret->client_name = GNUNET_strdup (client_name);
  3067. ret->upcb = upcb;
  3068. ret->upcb_cls = upcb_cls;
  3069. ret->flags = flags;
  3070. ret->max_parallel_downloads = DEFAULT_MAX_PARALLEL_DOWNLOADS;
  3071. ret->max_parallel_requests = DEFAULT_MAX_PARALLEL_REQUESTS;
  3072. ret->avg_block_latency =
  3073. GNUNET_TIME_UNIT_MINUTES; /* conservative starting point */
  3074. va_start (ap, flags);
  3075. while (GNUNET_FS_OPTIONS_END !=
  3076. (opt = GNUNET_VA_ARG_ENUM (ap, GNUNET_FS_OPTIONS)))
  3077. {
  3078. switch (opt)
  3079. {
  3080. case GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM:
  3081. ret->max_parallel_downloads = va_arg (ap, unsigned int);
  3082. break;
  3083. case GNUNET_FS_OPTIONS_REQUEST_PARALLELISM:
  3084. ret->max_parallel_requests = va_arg (ap, unsigned int);
  3085. break;
  3086. default:
  3087. GNUNET_break (0);
  3088. GNUNET_free (ret->client_name);
  3089. GNUNET_free (ret);
  3090. va_end (ap);
  3091. return NULL;
  3092. }
  3093. }
  3094. va_end (ap);
  3095. if (0 != (GNUNET_FS_FLAGS_PERSISTENCE & flags))
  3096. {
  3097. deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_PUBLISH,
  3098. &deserialize_publish_file,
  3099. ret);
  3100. deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_SEARCH,
  3101. &deserialize_search_file,
  3102. ret);
  3103. deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_DOWNLOAD,
  3104. &deserialize_download_file,
  3105. ret);
  3106. deserialization_master (GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
  3107. &deserialize_unindex_file,
  3108. ret);
  3109. }
  3110. return ret;
  3111. }
  3112. /**
  3113. * Close our connection with the file-sharing service.
  3114. * The callback given to #GNUNET_FS_start() will no longer be
  3115. * called after this function returns.
  3116. * This function MUST NOT be called from within the
  3117. * callback itself.
  3118. *
  3119. * @param h handle that was returned from #GNUNET_FS_start()
  3120. */
  3121. void
  3122. GNUNET_FS_stop (struct GNUNET_FS_Handle *h)
  3123. {
  3124. while (NULL != h->top_head)
  3125. h->top_head->ssf (h->top_head->ssf_cls);
  3126. if (NULL != h->queue_job)
  3127. GNUNET_SCHEDULER_cancel (h->queue_job);
  3128. GNUNET_free (h->client_name);
  3129. GNUNET_free (h);
  3130. }
  3131. /* end of fs_api.c */