Welcome to little lamb

Code » ssp » commit 2ffe7c8

open_db: Check we can read the file first

author Olivier Brunel
2023-07-05 18:57:00 UTC
committer Olivier Brunel
2023-08-20 14:52:37 UTC
parent d90765387cdea1660066d88168ce1b2db32b4075

open_db: Check we can read the file first

Otherwise it could e.g. ask for the password, only to then error out
because the file doesn't exist. Now it'll error out right away, no need
to ask for a password we're not gonna use.

src/include/ssp.h +1 -1
src/ssp/add.c +1 -1
src/ssp/database.c +11 -6
src/ssp/edit.c +1 -3
src/ssp/get.c +1 -3
src/ssp/import.c +1 -1
src/ssp/list.c +1 -3
src/ssp/remove.c +1 -3
src/ssp/rename.c +1 -4
src/ssp/show.c +1 -3
src/ssp/ssp.c +2 -0

diff --git a/src/include/ssp.h b/src/include/ssp.h
index 75e9ec2..5b96226 100644
--- a/src/include/ssp.h
+++ b/src/include/ssp.h
@@ -67,7 +67,7 @@ int rebuild_cdb(cdbmaker_sa *mkr, const char *oldkey, const char *newkey,
                 const struct otp *otp, struct ssp *ctx);
 int write_db(char *data, size_t dlen, struct ssp *ctx);
 void rebuild_db(const char *oldkey, const char *newkey, const struct otp *otp, struct ssp *ctx);
-int open_db(struct ssp *ctx);
+int open_db(struct ssp *ctx, int is_needed);
 void close_db(struct ssp *ctx);
 
 #define db_file(ctx)        (ctx)->sa.s
diff --git a/src/ssp/add.c b/src/ssp/add.c
index b2ee8fc..14bd7dc 100644
--- a/src/ssp/add.c
+++ b/src/ssp/add.c
@@ -194,7 +194,7 @@ add_main(int argc, const char *argv[], const char *env[], const char usage[], vo
     else
         p->data[l] = 0;
 
-    if (open_db(ssp) < 0)
+    if (open_db(ssp, 0) < 0)
         diefu(exitcode_from_errno(errno), "open database");
 
     const char *entry = ctx.sa.s + ctx.entoff;
diff --git a/src/ssp/database.c b/src/ssp/database.c
index a3ecbdd..04ce283 100644
--- a/src/ssp/database.c
+++ b/src/ssp/database.c
@@ -33,7 +33,7 @@ get_pwd(const char *prompt, struct ssp *ctx)
 }
 
 int
-open_db(struct ssp *ctx)
+open_db(struct ssp *ctx, int is_needed)
 {
     dbg("opening database");
 
@@ -43,6 +43,16 @@ open_db(struct ssp *ctx)
         return -1;
     }
 
+    if (access(db_file(ctx), R_OK) < 0) {
+        if (errno != ENOENT || is_needed) {
+            warnusys("open ", ESC, db_file(ctx), ESC);
+            return -1;
+        }
+        ctx->cdb = cdb_zero;
+        ctx->cdboff = ctx->sa.len;
+        return 0;
+    }
+
     size_t off  = ctx->sa.len;
     ssize_t plen;
 
@@ -55,11 +65,6 @@ open_db(struct ssp *ctx)
     u32 magic = SSP_MAGIC;
     u64 ver;
     if (!shldata_read(&magic, &ver, &ctx->sa, AT_FDCWD, db_file(ctx), ctx->pwd, plen)) {
-        if (errno == ENOENT) {
-            ctx->cdb = cdb_zero;
-            ctx->cdboff = off;
-            return 0;
-        }
         if (errno == EINVAL && magic != SSP_MAGIC)
             warnu("open database ", ESC, db_file(ctx), ESC, ": ", "not an SSP database");
         else if (errno == EBADMSG)
diff --git a/src/ssp/edit.c b/src/ssp/edit.c
index 8da75a7..209650a 100644
--- a/src/ssp/edit.c
+++ b/src/ssp/edit.c
@@ -129,11 +129,9 @@ edit_main(int argc, const char *argv[], const char *env[], const char usage[], v
 
     const char *entry = ctx.sa.s + ctx.entoff;
 
-    int r = open_db(ssp);
+    int r = open_db(ssp, 1);
     if (r < 0)
         diefu(exitcode_from_errno(errno), "open database");
-    if (!r)
-        dief(EX_NOINPUT, "no database");
 
     r = db_find(entry, ssp);
     if (r < 0)
diff --git a/src/ssp/get.c b/src/ssp/get.c
index db1897c..454d9db 100644
--- a/src/ssp/get.c
+++ b/src/ssp/get.c
@@ -106,11 +106,9 @@ parse_cmdline(int argc, const char *argv[], const char usage[], struct get *ctx)
                     /* re-align cdb in case of a re-alloc */
                     ctx->ssp->cdb.map = ctx->ssp->sa.s + ctx->ssp->cdboff;
                 } else {
-                    r = open_db(ctx->ssp);
+                    r = open_db(ctx->ssp, 1);
                     if (r < 0)
                         diefu(exitcode_from_errno(errno), "open database");
-                    if (!r)
-                        dief(EX_NOINPUT, "no database");
                     /* re-align */
                     entry = ctx->ssp->sa.s + LO_OFF(&lo);
                 }
diff --git a/src/ssp/import.c b/src/ssp/import.c
index 9c31d8f..6a834fa 100644
--- a/src/ssp/import.c
+++ b/src/ssp/import.c
@@ -147,7 +147,7 @@ import_main(int argc, const char *argv[], const char *env[], const char usage[],
                 }
 
                 /* open db if we haven't already */
-                if (!ssp->cdb.map && open_db(ssp) < 0)
+                if (!ssp->cdb.map && open_db(ssp, 0) < 0)
                     diefu(exitcode_from_errno(errno), "open database");
 
                 char key[nlen + 1];
diff --git a/src/ssp/list.c b/src/ssp/list.c
index 7588ab3..047536c 100644
--- a/src/ssp/list.c
+++ b/src/ssp/list.c
@@ -90,11 +90,9 @@ list_main(int argc, const char *argv[], const char *env[], const char usage[], v
     (void) env;
     parse_cmdline(argc, argv, usage, &ctx);
 
-    int r = open_db(ssp);
+    int r = open_db(ssp, 1);
     if (r < 0)
         diefu(exitcode_from_errno(errno), "open database");
-    if (!r)
-        dief(EX_NOINPUT, "no database");
 
     const char *ptrn = NULL;
     if (ctx.ptrnoff != (size_t) -1)
diff --git a/src/ssp/remove.c b/src/ssp/remove.c
index 3d430df..0d8fc67 100644
--- a/src/ssp/remove.c
+++ b/src/ssp/remove.c
@@ -54,11 +54,9 @@ remove_main(int argc, const char *argv[], const char *env[], const char usage[],
     (void) env;
     parse_cmdline(argc, argv, usage, &ctx);
 
-    int r = open_db(ssp);
+    int r = open_db(ssp, 1);
     if (r < 0)
         diefu(exitcode_from_errno(errno), "open database");
-    if (!r)
-        dief(EX_NOINPUT, "no database");
 
     const char *entry = ssp->sa.s + ctx.entoff;
 
diff --git a/src/ssp/rename.c b/src/ssp/rename.c
index 19840d0..0bff5ac 100644
--- a/src/ssp/rename.c
+++ b/src/ssp/rename.c
@@ -67,12 +67,9 @@ rename_main(int argc, const char *argv[], const char *env[], const char usage[],
     if (!strcmp(entry, newname))
         diefu(EX_DATA_ERR, "rename entry ", ESC, entry, ESC, ": new name is the same");
 
-    int r = open_db(ssp);
+    int r = open_db(ssp, 1);
     if (r < 0)
         diefu(exitcode_from_errno(errno), "open database");
-    if (!r)
-        dief(EX_NOINPUT, "no database");
-
 
     r = db_find(entry, ssp);
     if (r < 0)
diff --git a/src/ssp/show.c b/src/ssp/show.c
index ab088e4..0ee9221 100644
--- a/src/ssp/show.c
+++ b/src/ssp/show.c
@@ -130,11 +130,9 @@ show_main(int argc, const char *argv[], const char *env[], const char usage[], v
     (void) env;
     parse_cmdline(argc, argv, usage, &ctx);
 
-    int r = open_db(ssp);
+    int r = open_db(ssp, 1);
     if (r < 0)
         diefu(exitcode_from_errno(errno), "open database");
-    if (!r)
-        dief(EX_NOINPUT, "no database");
 
     const char *entry = ssp->sa.s + ctx.entoff;
     r = db_find(entry, ssp);
diff --git a/src/ssp/ssp.c b/src/ssp/ssp.c
index c19575c..3047bba 100644
--- a/src/ssp/ssp.c
+++ b/src/ssp/ssp.c
@@ -30,6 +30,8 @@ exitcode_from_errno(int e)
         case ENOMSG:
         case EBADE:
                      return EX_DATA_ERR;
+        case ENOENT:
+                     return EX_NOINPUT;
         default:
                      return EX_IOERR;
     }