bin/cookies: Drop libsoup code, fix fd-relative issues, new style

Prep for `ostree_repo_new_at()`. These commands were directly accessing
`repo->repodir`, which it turns out was unnecessary since the the APIs they then
used were fd-relative. Except actually there were bugs there, so fix all of the
cookie util code to actually use the passed `dfd` and not just hardcode
`AT_FDCWD`.

Also, libsoup can't handle this (its APIs require fully qualifed paths), and
there's not a really good reason to have two implementations now; historically
it was useful to cross-check them, but I don't think we need that.

While I'm here, port to new style.

Closes: #1010
Approved by: jlebon
This commit is contained in:
Colin Walters 2017-07-17 18:05:25 -04:00 committed by Atomic Bot
parent e0346c1494
commit 9430b8ad75
4 changed files with 28 additions and 138 deletions

View File

@ -36,18 +36,8 @@ static GOptionEntry option_entries[] = {
gboolean
ot_remote_builtin_add_cookie (int argc, char **argv, GCancellable *cancellable, GError **error)
{
g_autoptr(GOptionContext) context = NULL;
g_autoptr(GOptionContext) context = g_option_context_new ("NAME DOMAIN PATH COOKIE_NAME VALUE - Add a cookie to remote");
g_autoptr(OstreeRepo) repo = NULL;
const char *remote_name;
const char *domain;
const char *path;
const char *cookie_name;
const char *value;
g_autofree char *jar_path = NULL;
g_autofree char *cookie_file = NULL;
context = g_option_context_new ("NAME DOMAIN PATH COOKIE_NAME VALUE - Add a cookie to remote");
if (!ostree_option_context_parse (context, option_entries, &argc, &argv,
OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error))
return FALSE;
@ -58,16 +48,13 @@ ot_remote_builtin_add_cookie (int argc, char **argv, GCancellable *cancellable,
return FALSE;
}
remote_name = argv[1];
domain = argv[2];
path = argv[3];
cookie_name = argv[4];
value = argv[5];
cookie_file = g_strdup_printf ("%s.cookies.txt", remote_name);
jar_path = g_build_filename (gs_file_get_path_cached (repo->repodir), cookie_file, NULL);
if (!ot_add_cookie_at (AT_FDCWD, jar_path, domain, path, cookie_name, value, error))
const char *remote_name = argv[1];
const char *domain = argv[2];
const char *path = argv[3];
const char *cookie_name = argv[4];
const char *value = argv[5];
g_autofree char *cookie_file = g_strdup_printf ("%s.cookies.txt", remote_name);
if (!ot_add_cookie_at (ostree_repo_get_dfd (repo), cookie_file, domain, path, cookie_name, value, error))
return FALSE;
return TRUE;

View File

@ -36,16 +36,8 @@ static GOptionEntry option_entries[] = {
gboolean
ot_remote_builtin_delete_cookie (int argc, char **argv, GCancellable *cancellable, GError **error)
{
g_autoptr(GOptionContext) context = NULL;
g_autoptr(OstreeRepo) repo = NULL;
const char *remote_name;
const char *domain;
const char *path;
const char *cookie_name;
g_autofree char *jar_path = NULL;
g_autofree char *cookie_file = NULL;
context = g_option_context_new ("NAME DOMAIN PATH COOKIE_NAME- Remote one cookie from remote");
g_autoptr(GOptionContext) context = g_option_context_new ("NAME DOMAIN PATH COOKIE_NAME- Remote one cookie from remote");
if (!ostree_option_context_parse (context, option_entries, &argc, &argv,
OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error))
@ -57,15 +49,12 @@ ot_remote_builtin_delete_cookie (int argc, char **argv, GCancellable *cancellabl
return FALSE;
}
remote_name = argv[1];
domain = argv[2];
path = argv[3];
cookie_name = argv[4];
cookie_file = g_strdup_printf ("%s.cookies.txt", remote_name);
jar_path = g_build_filename (gs_file_get_path_cached (repo->repodir), cookie_file, NULL);
if (!ot_delete_cookie_at (AT_FDCWD, jar_path, domain, path, cookie_name, error))
const char *remote_name = argv[1];
const char *domain = argv[2];
const char *path = argv[3];
const char *cookie_name = argv[4];
g_autofree char *cookie_file = g_strdup_printf ("%s.cookies.txt", remote_name);
if (!ot_delete_cookie_at (ostree_repo_get_dfd (repo), cookie_file, domain, path, cookie_name, error))
return FALSE;
return TRUE;

View File

@ -35,13 +35,8 @@ static GOptionEntry option_entries[] = {
gboolean
ot_remote_builtin_list_cookies (int argc, char **argv, GCancellable *cancellable, GError **error)
{
g_autoptr(GOptionContext) context = NULL;
g_autoptr(OstreeRepo) repo = NULL;
const char *remote_name;
g_autofree char *jar_path = NULL;
g_autofree char *cookie_file = NULL;
context = g_option_context_new ("NAME - Show remote repository cookies");
g_autoptr(GOptionContext) context = g_option_context_new ("NAME - Show remote repository cookies");
if (!ostree_option_context_parse (context, option_entries, &argc, &argv,
OSTREE_BUILTIN_FLAG_NONE, &repo, cancellable, error))
@ -53,12 +48,9 @@ ot_remote_builtin_list_cookies (int argc, char **argv, GCancellable *cancellable
return FALSE;
}
remote_name = argv[1];
cookie_file = g_strdup_printf ("%s.cookies.txt", remote_name);
jar_path = g_build_filename (g_file_get_path (repo->repodir), cookie_file, NULL);
if (!ot_list_cookies_at (AT_FDCWD, jar_path, error))
const char *remote_name = argv[1];
g_autofree char *cookie_file = g_strdup_printf ("%s.cookies.txt", remote_name);
if (!ot_list_cookies_at (ostree_repo_get_dfd (repo), cookie_file, error))
return FALSE;
return TRUE;

View File

@ -23,10 +23,6 @@
#include "ot-remote-cookie-util.h"
#ifndef HAVE_LIBCURL
#include <libsoup/soup.h>
#endif
#include "otutil.h"
#include "ot-main.h"
#include "ot-remote-builtins.h"
@ -148,23 +144,15 @@ ot_add_cookie_at (int dfd, const char *jar_path,
const char *name, const char *value,
GError **error)
{
#ifdef HAVE_LIBCURL
glnx_fd_close int fd = openat (AT_FDCWD, jar_path, O_WRONLY | O_APPEND | O_CREAT, 0644);
g_autofree char *buf = NULL;
g_autoptr(GDateTime) now = NULL;
g_autoptr(GDateTime) expires = NULL;
glnx_fd_close int fd = openat (dfd, jar_path, O_WRONLY | O_APPEND | O_CREAT, 0644);
if (fd < 0)
{
glnx_set_error_from_errno (error);
return FALSE;
}
return glnx_throw_errno_prefix (error, "open(%s)", jar_path);
now = g_date_time_new_now_utc ();
expires = g_date_time_add_years (now, 25);
g_autoptr(GDateTime) now = g_date_time_new_now_utc ();
g_autoptr(GDateTime) expires = g_date_time_add_years (now, 25);
/* Adapted from soup-cookie-jar-text.c:write_cookie() */
buf = g_strdup_printf ("%s\t%s\t%s\t%s\t%llu\t%s\t%s\n",
g_autofree char *buf = g_strdup_printf ("%s\t%s\t%s\t%s\t%llu\t%s\t%s\n",
domain,
*domain == '.' ? "TRUE" : "FALSE",
path,
@ -173,24 +161,7 @@ ot_add_cookie_at (int dfd, const char *jar_path,
name,
value);
if (glnx_loop_write (fd, buf, strlen (buf)) < 0)
{
glnx_set_error_from_errno (error);
return FALSE;
}
#else
glnx_unref_object SoupCookieJar *jar = NULL;
SoupCookie *cookie;
jar = soup_cookie_jar_text_new (jar_path, FALSE);
/* Pick a silly long expire time, we're just storing the cookies in the
* jar and on pull the jar is read-only so expiry has little actual value */
cookie = soup_cookie_new (name, value, domain, path,
SOUP_COOKIE_MAX_AGE_ONE_YEAR * 25);
/* jar takes ownership of cookie */
soup_cookie_jar_add_cookie (jar, cookie);
#endif
return glnx_throw_errno_prefix (error, "write");
return TRUE;
}
@ -201,18 +172,14 @@ ot_delete_cookie_at (int dfd, const char *jar_path,
GError **error)
{
gboolean found = FALSE;
#ifdef HAVE_LIBCURL
g_auto(GLnxTmpfile) tmpf = { 0, };
g_autofree char *dnbuf = NULL;
const char *dn = NULL;
g_autoptr(OtCookieParser) parser = NULL;
if (!ot_parse_cookies_at (dfd, jar_path, &parser, NULL, error))
return FALSE;
dnbuf = g_strdup (jar_path);
dn = dirname (dnbuf);
if (!glnx_open_tmpfile_linkable_at (AT_FDCWD, dn, O_WRONLY | O_CLOEXEC,
g_assert (!strchr (jar_path, '/'));
if (!glnx_open_tmpfile_linkable_at (dfd, ".", O_WRONLY | O_CLOEXEC,
&tmpf, error))
return FALSE;
@ -233,33 +200,9 @@ ot_delete_cookie_at (int dfd, const char *jar_path,
}
if (!glnx_link_tmpfile_at (&tmpf, GLNX_LINK_TMPFILE_REPLACE,
AT_FDCWD, jar_path,
dfd, jar_path,
error))
return FALSE;
#else
GSList *cookies;
glnx_unref_object SoupCookieJar *jar = NULL;
jar = soup_cookie_jar_text_new (jar_path, FALSE);
cookies = soup_cookie_jar_all_cookies (jar);
while (cookies != NULL)
{
SoupCookie *cookie = cookies->data;
if (!strcmp (domain, soup_cookie_get_domain (cookie)) &&
!strcmp (path, soup_cookie_get_path (cookie)) &&
!strcmp (name, soup_cookie_get_name (cookie)))
{
soup_cookie_jar_delete_cookie (jar, cookie);
found = TRUE;
}
soup_cookie_free (cookie);
cookies = g_slist_delete_link (cookies, cookies);
}
#endif
if (!found)
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Cookie not found in jar");
@ -271,7 +214,6 @@ ot_delete_cookie_at (int dfd, const char *jar_path,
gboolean
ot_list_cookies_at (int dfd, const char *jar_path, GError **error)
{
#ifdef HAVE_LIBCURL
g_autoptr(OtCookieParser) parser = NULL;
if (!ot_parse_cookies_at (AT_FDCWD, jar_path, &parser, NULL, error))
@ -294,26 +236,6 @@ ot_list_cookies_at (int dfd, const char *jar_path, GError **error)
g_print ("Expires: %s\n", expires_str);
g_print ("Value: %s\n", parser->value);
}
#else
glnx_unref_object SoupCookieJar *jar = soup_cookie_jar_text_new (jar_path, TRUE);
GSList *cookies = soup_cookie_jar_all_cookies (jar);
while (cookies != NULL)
{
SoupCookie *cookie = cookies->data;
SoupDate *expiry = soup_cookie_get_expires (cookie);
g_print ("--\n");
g_print ("Domain: %s\n", soup_cookie_get_domain (cookie));
g_print ("Path: %s\n", soup_cookie_get_path (cookie));
g_print ("Name: %s\n", soup_cookie_get_name (cookie));
g_print ("Secure: %s\n", soup_cookie_get_secure (cookie) ? "yes" : "no");
g_print ("Expires: %s\n", soup_date_to_string (expiry, SOUP_DATE_COOKIE));
g_print ("Value: %s\n", soup_cookie_get_value (cookie));
soup_cookie_free (cookie);
cookies = g_slist_delete_link (cookies, cookies);
}
#endif
return TRUE;
}