aboutsummaryrefslogtreecommitdiff
path: root/src/vtab.c
blob: adb87f8a166c3bfc763178d87e03a372c76f93c6 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* wydawca - automatic release submission daemon
   Copyright (C) 2009-2013, 2017, 2019-2022 Sergey Poznyakoff

   Wydawca is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   Wydawca is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with wydawca. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"

struct wy_url {
    char *printable;
    char *scheme;
    char *path;
    int local:1;
};

wy_url_t
wy_url_create(const char *str)
{
    wy_url_t url;

    url = grecs_zalloc(sizeof(url[0]));
    url->printable = grecs_strdup(str);
    if (*str == '/') {
	url->scheme = grecs_strdup("file");
	url->path = grecs_strdup(str);
	url->local = 1;
    } else {
	size_t len = strcspn(str, ":");
	if (!str[len]) {
	    wy_url_free(url);
	    errno = EINVAL;
	    return NULL;
	}
	url->scheme = grecs_malloc(len + 1);
	memcpy(url->scheme, str, len);
	url->scheme[len] = 0;

	if (str[len + 1] && strncmp(str + len + 1, "//", 2) == 0)
	    len += 2;
	if (str[len + 1])
	    url->path = grecs_strdup(str + len + 1);
	else
	    url->path = NULL;
	url->local = 0;
    }
    return url;
}

void
wy_url_free(wy_url_t url)
{
    free(url->printable);
    free(url->scheme);
    free(url->path);
    free(url);
}

const char *
wy_url_path(wy_url_t url)
{
    return url->path;
}

const char *
wy_url_scheme(wy_url_t url)
{
    return url->scheme;
}

const char *
wy_url_printable(wy_url_t url)
{
    return url->printable;
}

int
wy_url_is_local(wy_url_t url)
{
    return url && url->local;
}

struct virt_tab_reg {
    char *scheme;
    struct virt_tab vtab;
};

static struct virt_tab_reg reg[] = {
    { "file",
      { dir_test_url, dir_move_file, dir_archive_file,
	dir_symlink_file, dir_rmsymlink_file } },
    { "dir",
      { dir_test_url, dir_move_file, dir_archive_file,
	dir_symlink_file, dir_rmsymlink_file } },
    { "null",
      { NULL, null_move_file, null_archive_file, null_symlink_file,
	null_rmsymlink_file } },
    { NULL }
};

int
url_to_vtab(wy_url_t url, struct virt_tab *vtab)
{
    const char *scheme = wy_url_scheme(url);
    int i;

    if (!scheme)
	return 1;
    for (i = 0; reg[i].scheme; i++)
	if (strcmp(reg[i].scheme, scheme) == 0) {
	    *vtab = reg[i].vtab;
	    return 0;
	}
    return 1;
}

int
move_file(struct wy_triplet *trp, enum file_type file_id)
{
    int rc = trp->spool->vtab.move_file(trp, file_id);
    triplet_report_add(trp, "Move %s to %s: %s", trp->file[file_id].name,
		       trp->relative_dir, rc == 0 ? "OK" : "FAILED");
    return rc;
}

int
archive_file(struct wy_triplet *trp, const char *file_name)
{
    int rc = trp->spool->vtab.archive_file(trp, file_name);
    triplet_report_add(trp, "Archive and remove %s/%s: %s",
		       trp->relative_dir, file_name,
		       rc == 0 ? "OK" : "FAILED");
    return rc;
}

int
symlink_file(struct wy_triplet *trp,
	     const char *wanted_src, const char *wanted_dst)
{
    int rc = trp->spool->vtab.symlink_file(trp, wanted_src, wanted_dst);
    triplet_report_add(trp, "Symlink %s to %s in %s/: %s",
		       wanted_src, wanted_dst,
		       trp->relative_dir, rc == 0 ? "OK" : "FAILED");
    return rc;
}

int
rmsymlink_file(struct wy_triplet *trp, const char *file_name)
{
    int rc = trp->spool->vtab.rmsymlink_file(trp, file_name);
    triplet_report_add(trp, "Remove symlink %s/%s: %s",
		       trp->relative_dir, file_name,
		       rc == 0 ? "OK" : "FAILED");
    return rc;
}

Return to:

Send suggestions and report system problems to the System administrator.