import-util.c revision 85dbc41dc67ff49fd8a843dbac5b8b5cb0b61155
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2015 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "util.h"
#include "strv.h"
#include "copy.h"
#include "btrfs-util.h"
#include "import-util.h"
#define FILENAME_ESCAPE "/.#\"\'"
bool http_etag_is_valid(const char *etag) {
return false;
return false;
return true;
}
int import_find_old_etags(const char *url, const char *image_root, int dt, const char *prefix, const char *suffix, char ***etags) {
_cleanup_strv_free_ char **l = NULL;
int r;
if (!image_root)
image_root = "/var/lib/machines";
if (!escaped_url)
return -ENOMEM;
d = opendir(image_root);
if (!d) {
return 0;
}
return -errno;
}
const char *a, *b;
char *u;
continue;
if (prefix) {
if (!a)
continue;
} else
a = startswith(a, escaped_url);
if (!a)
continue;
a = startswith(a, ".");
if (!a)
continue;
if (suffix) {
if (!b)
continue;
} else
if (a >= b)
continue;
u = cunescape_length(a, b - a);
if (!u)
return -ENOMEM;
if (!http_etag_is_valid(u)) {
free(u);
continue;
}
r = strv_consume(&l, u);
if (r < 0)
return r;
}
*etags = l;
l = NULL;
return 0;
}
int import_make_local_copy(const char *final, const char *image_root, const char *local, bool force_local) {
const char *p;
int r;
if (!image_root)
image_root = "/var/lib/machines";
if (force_local) {
(void) btrfs_subvol_remove(p);
(void) rm_rf_dangerous(p, false, true, false);
}
r = btrfs_subvol_snapshot(final, p, false, false);
if (r == -ENOTTY) {
if (r < 0)
return log_error_errno(r, "Failed to copy image: %m");
} else if (r < 0)
return log_error_errno(r, "Failed to create local image: %m");
return 0;
}
int import_make_read_only_fd(int fd) {
int r;
/* First, let's make this a read-only subvolume if it refers
* to a subvolume */
r = btrfs_subvol_set_read_only_fd(fd, true);
/* This doesn't refer to a subvolume, or the file
* system isn't even btrfs. In that, case fall back to
* chmod()ing */
if (r < 0)
/* Drop "w" flag */
return 0;
} else if (r < 0)
return log_error_errno(r, "Failed to make subvolume read-only: %m");
return 0;
}
int import_make_read_only(const char *path) {
if (fd < 0)
return import_make_read_only_fd(fd);
}
int import_make_path(const char *url, const char *etag, const char *image_root, const char *prefix, const char *suffix, char **ret) {
char *path;
if (!image_root)
image_root = "/var/lib/machines";
if (!escaped_url)
return -ENOMEM;
if (etag) {
if (!escaped_etag)
return -ENOMEM;
path = strjoin(image_root, "/", strempty(prefix), escaped_url, ".", escaped_etag, strempty(suffix), NULL);
} else
if (!path)
return -ENOMEM;
return 0;
}
const char *e, *p;
char *s;
e--;
p = e;
p--;
if (e <= p)
return -EINVAL;
s = strndup(p, e - p);
if (!s)
return -ENOMEM;
*ret = s;
return 0;
}
const char *e;
char *s;
e--;
e--;
if (e <= url)
return -EINVAL;
if (!s)
return -ENOMEM;
*ret = s;
return 0;
}