Blame
Date:
Thu May 5 10:18:05 2022 UTC
Message:
garbage collect `dequeue_first_download'
001
2021-11-05
op
/*
002
2021-11-05
op
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
003
2021-11-05
op
*
004
2021-11-05
op
* Permission to use, copy, modify, and distribute this software for any
005
2021-11-05
op
* purpose with or without fee is hereby granted, provided that the above
006
2021-11-05
op
* copyright notice and this permission notice appear in all copies.
007
2021-11-05
op
*
008
2021-11-05
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2021-11-05
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2021-11-05
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2021-11-05
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2021-11-05
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2021-11-05
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2021-11-05
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
015
2021-11-05
op
*/
016
2021-11-05
op
017
2021-11-05
op
#include "compat.h"
018
2021-11-05
op
019
2021-11-05
op
#include <stdlib.h>
020
2021-11-05
op
#include <string.h>
021
2021-11-05
op
022
2021-11-05
op
#include "telescope.h"
023
2021-11-05
op
#include "ui.h"
024
2021-11-05
op
025
2021-11-05
op
struct downloads downloads = STAILQ_HEAD_INITIALIZER(downloads);
026
2021-11-05
op
027
2021-11-05
op
static void
028
2021-11-29
op
no_downloads(void)
029
2021-11-05
op
{
030
2021-11-05
op
struct line *l;
031
2021-11-05
op
032
2021-11-05
op
if ((l = calloc(1, sizeof(*l))) == NULL)
033
2021-11-05
op
abort();
034
2021-11-05
op
035
2021-11-05
op
l->type = LINE_DOWNLOAD_INFO;
036
2021-11-05
op
l->line = strdup("No downloads");
037
2021-11-05
op
038
2021-11-05
op
TAILQ_INSERT_TAIL(&downloadwin.page.head, l, lines);
039
2021-11-05
op
}
040
2021-11-05
op
041
2021-11-05
op
void
042
2021-11-05
op
recompute_downloads(void)
043
2021-11-05
op
{
044
2021-11-05
op
struct download *d;
045
2021-11-05
op
struct line *l;
046
2021-11-05
op
char buf[FMT_SCALED_STRSIZE];
047
2021-11-05
op
048
2021-11-05
op
downloadwin.page.name = "*Downloads*";
049
2021-11-05
op
erase_buffer(&downloadwin);
050
2021-11-05
op
051
2021-11-05
op
if (STAILQ_EMPTY(&downloads)) {
052
2021-11-05
op
no_downloads();
053
2021-11-05
op
goto end;
054
2021-11-05
op
}
055
2021-11-05
op
056
2021-11-05
op
STAILQ_FOREACH(d, &downloads, entries) {
057
2021-11-05
op
if ((l = calloc(1, sizeof(*l))) == NULL)
058
2021-11-05
op
abort();
059
2021-11-05
op
060
2021-11-05
op
fmt_scaled(d->bytes, buf);
061
2021-11-05
op
062
2021-11-05
op
l->type = LINE_DOWNLOAD;
063
2021-11-05
op
if (d->fd == -1)
064
2021-11-05
op
l->type = LINE_DOWNLOAD_DONE;
065
2021-11-05
op
066
2021-11-05
op
l->line = strdup(buf);
067
2021-11-05
op
l->alt = strdup(d->path);
068
2021-11-05
op
069
2021-11-05
op
TAILQ_INSERT_TAIL(&downloadwin.page.head, l, lines);
070
2021-11-05
op
}
071
2021-11-05
op
072
2021-11-05
op
end:
073
2021-11-27
op
/*
074
2021-11-27
op
* The exact value doesn't matter, as wrap_page only considers
075
2021-11-27
op
* l->line, which is the human representation of the byte
076
2021-11-27
op
* counter, and we know for sure is < FMT_SCALED_STRSIZE so it
077
2021-11-27
op
* fits.
078
2021-11-27
op
*/
079
2021-11-27
op
wrap_page(&downloadwin, download_cols);
080
2021-11-05
op
}
081
2021-11-05
op
082
2022-04-24
op
struct download *
083
2022-05-05
op
enqueue_download(uint32_t id, const char *path)
084
2021-11-05
op
{
085
2021-11-05
op
struct download *d;
086
2021-11-05
op
087
2021-11-05
op
if ((d = calloc(1, sizeof(*d))) == NULL)
088
2021-11-05
op
abort();
089
2021-11-05
op
090
2021-11-05
op
d->id = id;
091
2021-11-05
op
d->fd = -1;
092
2021-11-05
op
d->path = strdup(path);
093
2021-11-05
op
094
2022-04-13
op
STAILQ_INSERT_HEAD(&downloads, d, entries);
095
2022-04-24
op
096
2022-04-24
op
return d;
097
2021-11-05
op
}
098
2021-11-05
op
099
2021-11-05
op
struct download *
100
2021-11-05
op
download_by_id(uint32_t id)
101
2021-11-05
op
{
102
2021-11-05
op
struct download *d;
103
2021-11-05
op
104
2021-11-05
op
STAILQ_FOREACH(d, &downloads, entries) {
105
2021-11-05
op
if (d->id == id)
106
2021-11-05
op
return d;
107
2021-11-05
op
}
108
2021-11-05
op
109
2021-11-05
op
return NULL;
110
2021-11-05
op
}
Omar Polo