aboutsummaryrefslogtreecommitdiff
path: root/src/cli/updater.c
blob: fd6df1b370d160d374da74b4875a10ab074bb2aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "fs.h"
#include "toast.h"
#include "pool.h"

#include "updater.h"

#include <assert.h>

struct thread_object_info {
    char* of_dir;
    char* remote;
    struct revision_t* rev;
    size_t index;
};

static void* thread_download(void* pinfo)
{
    struct thread_object_info* info = pinfo;
    if (info)
    {
        char* of_dir = info->of_dir;
        char* remote = info->remote;
        struct revision_t* rev = info->rev;
        size_t i = info->index;

        struct file_info* file = &rev->files[i];
        if (file->type == TYPE_WRITE)
        {
            fprintf(stderr, "\rChecking    %zu/%zu (%s)", i+1, rev->file_count, file->object);

            if (verifyFileHash(of_dir, file))
            {
                fprintf(stderr, "\rDownloading %zu/%zu (%s)", i+1, rev->file_count, file->object);
                downloadObject(of_dir, remote, file);
            }
        }
    }
    return NULL;
}


void update_setup(char* of_dir, char* remote, int local_rev, int remote_rev)
{
    struct revision_t* rev = fastFowardRevisions(remote, local_rev, remote_rev);

    if (rev)
    {
        fprintf(stderr, "Validating revisions");
        for (size_t i = 0; i < rev->file_count; ++i)
        {
            struct file_info* file = &rev->files[i];

            if (leavesRelativePath(file->path))
            {
                fprintf(stderr, "Revision contains invalid path '%s'\n", file->path);
                freeRevision(rev);
                return;
            }
        }

        struct thread_object_info* thread_info = malloc(sizeof(struct thread_object_info) * rev->file_count);
        struct pool_t* pool = pool_init();

        for (size_t i = 0; i < rev->file_count; ++i)
        {
            struct thread_object_info* info = &thread_info[i];

            info->of_dir = of_dir;
            info->remote = remote;
            info->rev = rev;
            info->index = i;

            pool_submit(pool, thread_download, info);
        }

        pool_complete(pool);
        pool_free(pool);
        free(thread_info);

        puts("");
        for (size_t i = 0; i < rev->file_count; ++i)
        {
            struct file_info* file = &rev->files[i];

            if (file->type != TYPE_DELETE) continue;
            fprintf(stderr, "\rDeleting  %zu/%zu", i+1, rev->file_count);
            size_t len = strlen(of_dir) + strlen(OS_PATH_SEP) + strlen(file->path) + 1;
            char* buf = malloc(len);
            snprintf(buf, len, "%s%s%s", of_dir, OS_PATH_SEP, file->path);
            if (isFile(buf) && remove(buf))
            {
                // deletion failure is fine and can simple be ignored.
                fprintf(stderr, "\rFailed to delete %s\n", file->path);
            }
            free(buf);
        }

        for (size_t i = 0; i < rev->file_count; ++i)
        {
            struct file_info* file = &rev->files[i];

            if (file->type != TYPE_MKDIR) continue;
            size_t len = strlen(of_dir) + strlen(OS_PATH_SEP) + strlen(file->path) + 1;
            char* buf = malloc(len);
            fprintf(stderr, "\rCreating %zu/%zu", i+1, rev->file_count);
            snprintf(buf, len, "%s%s%s", of_dir, OS_PATH_SEP, file->path);
            if (!isDir(buf) && makeDir(buf))
            {
                fprintf(stderr, "\nFailed to create %s\n", file->path);
            }
            free(buf);
        }


        char write_failure = 0;

        for (size_t i = 0; i < rev->file_count; ++i)
        {
            struct file_info* file = &rev->files[i];

            if (file->type != TYPE_WRITE) continue;
            fprintf(stderr, "\rInstalling  %zu/%zu (%s)", i+1, rev->file_count, file->object);
            if (applyObject(of_dir, file))
            {
                // failing to write an object usually means something is wrong with the path or object
                fprintf(stderr, "\nFailed to write %s\n", file->path);
                write_failure = 1;
            }
        }

        setLocalRemote(of_dir, remote);
        if (!write_failure)
        {
            removeObjects(of_dir);
            setLocalRevision(of_dir, remote_rev);
            fprintf(stderr, "\nUpdated OpenFortress\n");
        }
        else
            fprintf(stderr, "\nSomething went wrong during the upgrade\nThe Revisionw as installed as is, you can try updating again to fix it.\n");

        freeRevision(rev);
    }
}