📄 deserialize.c
字号:
uri = read_uri (ectx, rb);
if (uri == NULL)
break; /* error */
ret = GNUNET_malloc (sizeof (struct SearchRecordList));
ret->key = key;
ret->uri = uri;
ret->search = NULL;
ret->is_required = is_required;
ret->next = NULL;
if (head == NULL)
head = ret;
if (tail != NULL)
tail->next = ret;
tail = ret;
}
return head;
}
/**
* Read all of the results received so far
* for this search.
*
* @param search_count length of search_list
* @param search_list list of ECRS search requests
*/
struct SearchResultList *
read_result_list (struct GNUNET_GE_Context *ectx,
ReadBuffer * rb,
unsigned int search_count,
struct SearchRecordList **search_list)
{
unsigned int matching;
unsigned int remaining;
unsigned int probeSucc;
unsigned int probeFail;
struct SearchResultList *ret;
struct SearchResultList *head;
struct SearchResultList *tail;
unsigned int i;
unsigned int idx;
ret = NULL;
head = NULL;
tail = NULL;
while (1)
{
if (GNUNET_OK != read_uint (rb, &matching))
break;
if (matching == -1)
break; /* end of list marker */
if ((GNUNET_OK != read_uint (rb, &remaining)) ||
(GNUNET_OK != read_uint (rb, &probeSucc)) ||
(GNUNET_OK != read_uint (rb, &probeFail)))
break;
ret = GNUNET_malloc (sizeof (struct SearchResultList));
if (GNUNET_OK != readFileInfo (ectx, rb, &ret->fi))
{
GNUNET_free (ret);
break;
}
ret->matchingSearchCount = matching;
ret->mandatoryMatchesRemaining = remaining;
ret->probeSuccess = probeSucc;
ret->probeFailure = probeFail;
if ((ret->probeSuccess + ret->probeFailure > GNUNET_FSUI_MAX_PROBES) ||
(ret->probeSuccess > GNUNET_FSUI_MAX_PROBES) ||
(ret->probeFailure > GNUNET_FSUI_MAX_PROBES))
{
GNUNET_GE_BREAK (NULL, 0);
/* try to recover */
ret->probeSuccess = 0;
ret->probeFailure = 0;
}
ret->test_download = NULL;
ret->next = NULL;
ret->matchingSearches = NULL;
i = 0;
GNUNET_array_grow (ret->matchingSearches, i, ret->matchingSearchCount);
while (i-- > 0)
{
if ((GNUNET_OK != read_uint (rb, &idx)) || (idx > search_count))
{
GNUNET_GE_BREAK (NULL, 0);
GNUNET_array_grow (ret->matchingSearches,
ret->matchingSearchCount, 0);
GNUNET_free (ret);
return head;
}
if (idx == 0)
{
GNUNET_GE_BREAK (NULL, 0);
ret->matchingSearches[i] = NULL;
}
else
{
GNUNET_GE_BREAK (NULL, search_list[idx - 1] != NULL);
ret->matchingSearches[i] = search_list[idx - 1];
}
}
if (head == NULL)
head = ret;
if (tail != NULL)
tail->next = ret;
tail = ret;
}
return head;
}
/**
* Read in all of the FSUI-searches that we are
* performing.
*/
static int
readSearches (ReadBuffer * rb, struct GNUNET_FSUI_Context *ctx)
{
int big;
GNUNET_FSUI_SearchList *list;
GNUNET_FSUI_SearchList *last;
struct SearchResultList *srp;
struct SearchRecordList *srl;
struct SearchRecordList **srla;
char *buf;
GNUNET_CronTime stime;
unsigned int total_searches;
unsigned int i;
while (1)
{
READINT (big);
if (big == 0)
return GNUNET_OK;
list = GNUNET_malloc (sizeof (GNUNET_FSUI_SearchList));
memset (list, 0, sizeof (GNUNET_FSUI_SearchList));
list->ctx = ctx;
if ((GNUNET_OK != read_int (rb, (int *) &list->state)) ||
(GNUNET_OK != read_long (rb, (long long *) &list->start_time)) ||
(GNUNET_OK != read_long (rb, (long long *) &stime)) ||
(GNUNET_OK != read_uint (rb, &list->anonymityLevel)) ||
(GNUNET_OK != read_uint (rb, &list->mandatory_keyword_count)))
{
GNUNET_GE_BREAK (NULL, 0);
break;
}
fixState (&list->state);
if (stime > GNUNET_get_time ())
stime = GNUNET_get_time ();
list->start_time += GNUNET_get_time () - stime;
buf = read_string (rb, 1024 * 1024);
if (buf == NULL)
{
GNUNET_GE_BREAK (NULL, 0);
break;
}
list->uri = GNUNET_ECRS_string_to_uri (NULL, buf);
GNUNET_free (buf);
if (list->uri == NULL)
{
GNUNET_GE_BREAK (NULL, 0);
break;
}
if (!
(GNUNET_ECRS_uri_test_ksk (list->uri)
|| GNUNET_ECRS_uri_test_sks (list->uri)))
{
GNUNET_GE_BREAK (NULL, 0);
break;
}
list->searches = read_search_record_list (ctx->ectx, rb);
if (list->searches == NULL)
goto ERR; /* can never be empty in practice */
srl = list->searches;
total_searches = 0;
while (srl != NULL)
{
total_searches++;
srl = srl->next;
}
srla =
GNUNET_malloc (total_searches * sizeof (struct SearchRecordList *));
srl = list->searches;
i = total_searches;
while (srl != NULL)
{
srla[--i] = srl;
srl = srl->next;
}
list->resultsReceived = read_result_list (ctx->ectx, rb,
total_searches, srla);
GNUNET_free (srla);
list->next = NULL;
/* finally: append (!) to list */
if (ctx->activeSearches == NULL)
{
ctx->activeSearches = list;
}
else
{
last = ctx->activeSearches;
while (last->next != NULL)
last = last->next;
last->next = list;
}
} /* end OUTER: 'while(1)' */
ERR:
/* error - deallocate 'list' */
while (list->resultsReceived != NULL)
{
srp = list->resultsReceived;
list->resultsReceived = srp->next;
GNUNET_free (srp);
}
while (list->searches != NULL)
{
srl = list->searches;
list->searches = srl->next;
if (srl->uri != NULL)
GNUNET_ECRS_uri_destroy (srl->uri);
GNUNET_free (srl);
}
if (list->uri != NULL)
GNUNET_ECRS_uri_destroy (list->uri);
GNUNET_free (list);
return GNUNET_SYSERR;
}
static int
readDownloads (ReadBuffer * rb, struct GNUNET_FSUI_Context *ctx)
{
memset (&ctx->activeDownloads, 0, sizeof (GNUNET_FSUI_DownloadList));
ctx->activeDownloads.child
= readDownloadList (ctx->ectx, rb, ctx, &ctx->activeDownloads);
return GNUNET_OK;
}
static int
readUploadList (struct GNUNET_FSUI_Context *ctx,
struct GNUNET_FSUI_UploadList *parent,
ReadBuffer * rb, struct GNUNET_FSUI_UploadShared *shared,
int top)
{
struct GNUNET_FSUI_UploadList *list;
struct GNUNET_FSUI_UploadList l;
unsigned long long stime;
int big;
int bag;
struct GNUNET_GE_Context *ectx;
ectx = ctx->ectx;
GNUNET_GE_ASSERT (ectx, shared != NULL);
while (1)
{
READINT (big);
if (big == 0)
return GNUNET_OK;
if ((big < 1) || (big > 15))
{
GNUNET_GE_BREAK (NULL, 0);
return GNUNET_SYSERR;
}
READINT (bag);
if (bag != 0x34D1F023)
{
GNUNET_GE_BREAK (NULL, 0);
return GNUNET_SYSERR;
}
memset (&l, 0, sizeof (GNUNET_FSUI_UploadList));
READINT (l.state);
fixState (&l.state);
if (l.state == GNUNET_FSUI_PENDING)
l.state = GNUNET_FSUI_ACTIVE;
READLONG (l.completed);
READLONG (l.total);
READLONG (stime);
if (stime < GNUNET_get_time ())
stime = GNUNET_get_time ();
READLONG (l.start_time);
if (l.start_time != 0)
l.start_time = (GNUNET_get_time () - stime) + l.start_time;
l.uri = NULL;
if ((big & 2) == 2)
READURI (l.uri);
if ((big & 4) == 4)
{
l.keywords = read_uri (ctx->ectx, rb);
if (l.keywords == NULL)
{
if (l.uri != NULL)
GNUNET_ECRS_uri_destroy (l.uri);
GNUNET_GE_BREAK (NULL, 0);
break;
}
}
if ((big & 8) == 8)
{
l.meta = read_meta (ctx->ectx, rb);
if (l.meta == NULL)
{
if (l.uri != NULL)
GNUNET_ECRS_uri_destroy (l.uri);
if (l.keywords != NULL)
GNUNET_ECRS_uri_destroy (l.keywords);
GNUNET_GE_BREAK (NULL, 0);
break;
}
}
l.filename = read_string (rb, 1024 * 1024);
if (l.filename == NULL)
{
if (l.uri != NULL)
GNUNET_ECRS_uri_destroy (l.uri);
if (l.meta != NULL)
GNUNET_meta_data_destroy (l.meta);
if (l.keywords != NULL)
GNUNET_ECRS_uri_destroy (l.keywords);
GNUNET_GE_BREAK (NULL, 0);
break;
}
list = GNUNET_malloc (sizeof (struct GNUNET_FSUI_UploadList));
memcpy (list, &l, sizeof (struct GNUNET_FSUI_UploadList));
list->shared = shared;
list->parent = parent;
if (GNUNET_OK != readUploadList (ctx, list, rb, shared, GNUNET_NO))
{
if (l.uri != NULL)
GNUNET_ECRS_uri_destroy (l.uri);
GNUNET_free (l.filename);
GNUNET_free (list);
GNUNET_GE_BREAK (NULL, 0);
break;
}
list->next = parent->child;
parent->child = list;
if (top == GNUNET_YES)
return GNUNET_OK;
}
return GNUNET_SYSERR;
}
static int
readUploads (ReadBuffer * rb, struct GNUNET_FSUI_Context *ctx)
{
int big;
int bag;
struct GNUNET_FSUI_UploadShared *shared;
struct GNUNET_FSUI_UploadShared sshared;
memset (&ctx->activeUploads, 0, sizeof (GNUNET_FSUI_UploadList));
while (1)
{
READINT (big);
if (big == 0)
return GNUNET_OK;
if ((big < 1) && (big > 7))
{
GNUNET_GE_BREAK (NULL, 0);
break;
}
READINT (bag);
if (bag != 0x44D1F024)
{
GNUNET_GE_BREAK (NULL, 0);
return GNUNET_SYSERR;
}
memset (&sshared, 0, sizeof (GNUNET_FSUI_UploadShared));
READINT (sshared.doIndex);
READINT (sshared.anonymityLevel);
READINT (sshared.priority);
READINT (sshared.individualKeywords);
READLONG (sshared.expiration);
if ((big & 2) == 2)
READSTRING (sshared.extractor_config, 1024 * 1024);
READSTRING (sshared.top_filename, 1024 * 1024);
if ((big & 4) == 4)
{
sshared.global_keywords = read_uri (ctx->ectx, rb);
if (sshared.global_keywords == NULL)
{
GNUNET_free_non_null (sshared.extractor_config);
GNUNET_GE_BREAK (NULL, 0);
return GNUNET_SYSERR;
}
}
shared = GNUNET_malloc (sizeof (GNUNET_FSUI_UploadShared));
memcpy (shared, &sshared, sizeof (GNUNET_FSUI_UploadShared));
shared->ctx = ctx;
if (GNUNET_OK !=
readUploadList (ctx, &ctx->activeUploads, rb, shared, GNUNET_YES))
{
GNUNET_GE_BREAK (NULL, 0);
#if 0
/* cannot do this, readUploadList
may have added *some* uploads that
still reference shared -- need to
find and cleanup those first,
or at least detect their presence
and not free */
GNUNET_free (shared->extractor_config);
GNUNET_free (shared);
#endif
break;
}
}
return GNUNET_SYSERR;
}
static int
readUnindex (ReadBuffer * rb, struct GNUNET_FSUI_Context *ctx)
{
int big;
char *name;
struct GNUNET_FSUI_UnindexList *ul;
while (1)
{
READINT (big);
if (big != 1)
return GNUNET_OK;
READINT (big); /* state */
READSTRING (name, 1024 * 1024);
ul = GNUNET_malloc (sizeof (struct GNUNET_FSUI_UnindexList));
ul->state = big;
ul->filename = name;
ul->next = ctx->unindexOperations;
ul->ctx = ctx;
ctx->unindexOperations = ul;
}
return GNUNET_SYSERR;
}
void
GNUNET_FSUI_deserialize (struct GNUNET_FSUI_Context *ctx)
{
ReadBuffer rb;
rb.fd = -1;
if (0 != ACCESS (ctx->name, R_OK))
return;
rb.fd = GNUNET_disk_file_open (ctx->ectx, ctx->name, O_RDONLY);
if (rb.fd == -1)
return;
rb.pos = 0;
rb.size = 64 * 1024;
rb.have = 0;
rb.buffer = GNUNET_malloc (rb.size);
if ((GNUNET_OK != checkMagic (&rb)) ||
(GNUNET_OK != readCollection (&rb, ctx)) ||
(GNUNET_OK != readSearches (&rb, ctx)) ||
(GNUNET_OK != readDownloads (&rb, ctx)) ||
(GNUNET_OK != readUnindex (&rb, ctx))
|| (GNUNET_OK != readUploads (&rb, ctx)))
{
GNUNET_GE_BREAK (ctx->ectx, 0);
GNUNET_GE_LOG (ctx->ectx,
GNUNET_GE_WARNING | GNUNET_GE_BULK | GNUNET_GE_USER,
_
("FSUI state file `%s' had syntax error at offset %u.\n"),
ctx->name, LSEEK (rb.fd, 0, SEEK_CUR));
}
CLOSE (rb.fd);
UNLINK (ctx->name);
GNUNET_free (rb.buffer);
}
/* end of deserialize.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -