This introduces a function remove_contents() which is implementing a basic
rm -r and uses it to clean the RRDP repository when downloading a
snapshot (especially after a delta failure). It also cleans out the temp
directory after a failure to fetch.

With the introduction of a validated cache this will become a bit simpler
since the temp dir will disapear. Still this is a simple step and reduces
the size of the validated cache patch.
-- 
:wq Claudio

Index: extern.h
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/extern.h,v
retrieving revision 1.101
diff -u -p -r1.101 extern.h
--- extern.h    11 Jan 2022 13:06:07 -0000      1.101
+++ extern.h    13 Jan 2022 10:20:53 -0000
@@ -309,10 +309,11 @@ enum rrdp_msg {
        RRDP_START,
        RRDP_SESSION,
        RRDP_FILE,
+       RRDP_CLEAR,
        RRDP_END,
        RRDP_HTTP_REQ,
        RRDP_HTTP_INI,
-       RRDP_HTTP_FIN
+       RRDP_HTTP_FIN,
 };
 
 /*
@@ -501,6 +502,7 @@ void                 proc_rrdp(int);
 
 /* Repository handling */
 int             filepath_add(struct filepath_tree *, char *);
+void            rrdp_clear(unsigned int);
 void            rrdp_save_state(unsigned int, struct rrdp_session *);
 int             rrdp_handle_file(unsigned int, enum publish_type, char *,
                    char *, size_t, char *, size_t);
Index: main.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/main.c,v
retrieving revision 1.173
diff -u -p -r1.173 main.c
--- main.c      11 Jan 2022 13:06:07 -0000      1.173
+++ main.c      13 Jan 2022 10:20:04 -0000
@@ -631,6 +631,9 @@ rrdp_process(struct ibuf *b)
                free(uri);
                free(data);
                break;
+       case RRDP_CLEAR:
+               rrdp_clear(id);
+               break;
        default:
                errx(1, "unexpected rrdp response");
        }
Index: repo.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/repo.c,v
retrieving revision 1.20
diff -u -p -r1.20 repo.c
--- repo.c      11 Jan 2022 13:06:07 -0000      1.20
+++ repo.c      13 Jan 2022 11:07:49 -0000
@@ -106,6 +106,7 @@ static SLIST_HEAD(, repo)   repos = SLIST_
 unsigned int           repoid;
 
 static struct rsyncrepo        *rsync_get(const char *, int);
+static void             remove_contents(char *);
 
 /*
  * Database of all file path accessed during a run.
@@ -788,6 +789,23 @@ fail:
 }
 
 /*
+ * Remove RRDP repo and start over.
+ */
+void
+rrdp_clear(unsigned int id)
+{
+       struct rrdprepo *rr;
+
+       rr = rrdp_find(id);
+       if (rr == NULL)
+               errx(1, "non-existant rrdp repo %u", id);
+
+       /* remove rrdp repository contents */
+       remove_contents(rr->basedir);
+       remove_contents(rr->temp);
+}
+
+/*
  * Write a file into the temporary RRDP dir but only after checking
  * its hash (if required). The function also makes sure that the file
  * tracking is properly adjusted.
@@ -932,24 +950,6 @@ fail:
        return 0;
 }
 
-static void
-rrdp_clean_temp(struct rrdprepo *rr)
-{
-       struct filepath *fp, *nfp;
-       char *fn;
-
-       filepath_free(&rr->deleted);
-
-       RB_FOREACH_SAFE(fp, filepath_tree, &rr->added, nfp) {
-               if ((fn = rrdp_filename(rr, fp->file, 1)) != NULL) {
-                       if (unlink(fn) == -1)
-                               warn("unlink %s", fn);
-                       free(fn);
-               }
-               filepath_put(&rr->added, fp);
-       }
-}
-
 /*
  * RSYNC sync finished, either with or without success.
  */
@@ -981,10 +981,10 @@ rsync_finish(unsigned int id, int ok)
        rr = rsync_find(id);
        if (rr == NULL)
                errx(1, "unknown rsync repo %u", id);
-
        /* repository changed state already, ignore request */
        if (rr->state != REPO_LOADING)
                return;
+
        if (ok) {
                logx("%s: loaded from network", rr->basedir);
                stats.rsync_repos++;
@@ -1016,15 +1016,15 @@ rrdp_finish(unsigned int id, int ok)
 
        if (ok && rrdp_merge_repo(rr)) {
                logx("%s: loaded from network", rr->notifyuri);
-               rr->state = REPO_DONE;
                stats.rrdp_repos++;
+               rr->state = REPO_DONE;
                repo_done(rr, ok);
        } else {
-               rrdp_clean_temp(rr);
-               stats.rrdp_fails++;
-               rr->state = REPO_FAILED;
                logx("%s: load from network failed, fallback to rsync",
                    rr->notifyuri);
+               stats.rrdp_fails++;
+               rr->state = REPO_FAILED;
+               remove_contents(rr->temp);
                repo_done(rr, 0);
        }
 }
@@ -1414,4 +1414,49 @@ repo_free(void)
        ta_free();
        rrdp_free();
        rsync_free();
+}
+
+static void
+remove_contents(char *base)
+{
+       char *argv[2] = { base, NULL };
+       FTS *fts;
+       FTSENT *e;
+
+       if ((fts = fts_open(argv, FTS_PHYSICAL | FTS_NOSTAT, NULL)) == NULL)
+               err(1, "fts_open");
+       errno = 0;
+       while ((e = fts_read(fts)) != NULL) {
+               switch (e->fts_info) {
+               case FTS_NSOK:
+               case FTS_SL:
+               case FTS_SLNONE:
+                       if (unlink(e->fts_accpath) == -1)
+                               warn("unlink %s", e->fts_path);
+                       break;
+               case FTS_D:
+                       break;
+               case FTS_DP:
+                       /* keep root directory */
+                       if (e->fts_level == FTS_ROOTLEVEL)
+                               break;
+                       if (rmdir(e->fts_accpath) == -1)
+                               warn("rmdir %s", e->fts_path);
+                       break;
+               case FTS_NS:
+               case FTS_ERR:
+                       warnx("fts_read %s: %s", e->fts_path,
+                           strerror(e->fts_errno));
+                       break;
+               default:
+                       warnx("unhandled[%x] %s", e->fts_info,
+                           e->fts_path);
+                       break;
+               }
+               errno = 0;
+       }
+       if (errno)
+               err(1, "fts_read");
+       if (fts_close(fts) == -1)
+               err(1, "fts_close");
 }
Index: rrdp.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/rrdp.c,v
retrieving revision 1.19
diff -u -p -r1.19 rrdp.c
--- rrdp.c      22 Dec 2021 09:35:14 -0000      1.19
+++ rrdp.c      13 Jan 2022 11:04:01 -0000
@@ -142,6 +142,21 @@ rrdp_state_send(struct rrdp *s)
 }
 
 /*
+ * Inform parent to clear the RRDP repository before start of snapshot.
+ */
+static void
+rrdp_clear_repo(struct rrdp *s)
+{
+       enum rrdp_msg type = RRDP_CLEAR;
+       struct ibuf *b;
+
+       b = io_new_buffer();
+       io_simple_buffer(b, &type, sizeof(type));
+       io_simple_buffer(b, &s->id, sizeof(s->id));
+       io_close_buffer(&msgq, b);
+}
+
+/*
  * Send a blob of data to the main process to store it in the repository.
  */
 void
@@ -246,6 +261,7 @@ rrdp_failed(struct rrdp *s)
                /* fallback to a snapshot as per RFC8182 */
                free_delta_xml(s->dxml);
                s->dxml = NULL;
+               rrdp_clear_repo(s);
                s->sxml = new_snapshot_xml(s->parser, &s->current, s);
                s->task = SNAPSHOT;
                s->state = RRDP_STATE_REQ;
@@ -315,6 +331,7 @@ rrdp_finished(struct rrdp *s)
                                break;
                        case SNAPSHOT:
                                logx("%s: downloading snapshot", s->local);
+                               rrdp_clear_repo(s);
                                s->sxml = new_snapshot_xml(p, &s->current, s);
                                s->state = RRDP_STATE_REQ;
                                break;

Reply via email to