Allow creating keyfiles with no file attached.
authormdw <mdw>
Sat, 3 Feb 2001 11:57:38 +0000 (11:57 +0000)
committermdw <mdw>
Sat, 3 Feb 2001 11:57:38 +0000 (11:57 +0000)
key-file.c
key-io.c
key.h

index ab620e7..2e556e9 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: key-file.c,v 1.1 1999/12/22 15:47:48 mdw Exp $
+ * $Id: key-file.c,v 1.2 2001/02/03 11:57:38 mdw Exp $
  *
  * System-dependent key filing operations
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: key-file.c,v $
+ * Revision 1.2  2001/02/03 11:57:38  mdw
+ * Allow creating keyfiles with no file attached.
+ *
  * Revision 1.1  1999/12/22 15:47:48  mdw
  * Major key-management revision.
  *
@@ -108,6 +111,8 @@ int key_save(key_file *f)
 
   if (!(f->f & KF_MODIFIED))
     return (KWRITE_OK);
+  if (!f->fp)
+    return (KWRITE_FAIL);
 
   /* --- Write a new key file out --- *
    *
@@ -271,7 +276,7 @@ fail_write:
  *
  * Arguments:  @key_file *f@ = pointer to file structure to initialize
  *             @const char *file@ = pointer to the file name
- *             @int how@ = opening options (@KOPEN_*@).
+ *             @unsigned how@ = opening options (@KOPEN_*@).
  *
  * Returns:    Zero if it worked, nonzero otherwise.
  *
@@ -286,15 +291,22 @@ fail_write:
  *             for the private use of @key_open@.
  */
 
-int key_lockfile(key_file *f, const char *file, int how)
+int key_lockfile(key_file *f, const char *file, unsigned how)
 {
   int of, lf;
   const char *ff;
   int fd;
 
+  /* --- Handle the magic no-file option --- */
+
+  if (how & KOPEN_NOFILE) {
+    f->fp = 0;
+    return (0);
+  }
+
   /* --- Lots of things depend on whether we're writing --- */
 
-  switch (how) {
+  switch (how & KOPEN_MASK) {
     case KOPEN_READ:
       of = O_RDONLY;
       lf = LOCK_NONEXCL;
index 44bb803..dbe8bdf 100644 (file)
--- a/key-io.c
+++ b/key-io.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: key-io.c,v 1.3 2001/01/20 11:56:48 mdw Exp $
+ * $Id: key-io.c,v 1.4 2001/02/03 11:57:38 mdw Exp $
  *
  * Adding new keys to a key file
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: key-io.c,v $
+ * Revision 1.4  2001/02/03 11:57:38  mdw
+ * Allow creating keyfiles with no file attached.
+ *
  * Revision 1.3  2001/01/20 11:56:48  mdw
  * Use mLib exported tuning parameters for hashtable.
  *
@@ -420,7 +423,7 @@ int key_extract(key_file *f, key *k, FILE *fp, const key_filter *kf)
  *
  * Arguments:  @key_file *f@ = pointer to file structure to initialize
  *             @const char *file@ = pointer to the file name
- *             @int how@ = opening options (@KOPEN_*@).
+ *             @unsigned how@ = opening options (@KOPEN_*@).
  *             @key_reporter *rep@ = error reporting function
  *             @void *arg@ = argument for function
  *
@@ -434,33 +437,26 @@ int key_extract(key_file *f, key *k, FILE *fp, const key_filter *kf)
  *             owner only.
  */
 
-int key_open(key_file *f, const char *file, int how,
+int key_open(key_file *f, const char *file, unsigned how,
             key_reporter *rep, void *arg)
 {
   if (key_lockfile(f, file, how))
     return (-1);
 
-  /* --- Trivial bits of initialization --- */
-
   f->f = 0;
   f->name = xstrdup(file);
 
-  /* --- Read the file of keys into the table --- */
-
   hash_create(&f->byid, KEY_INITSZ);
   f->idload = SYM_LIMIT(KEY_INITSZ);
   sym_create(&f->bytype);
   sym_create(&f->bytag);
   f->f |= KF_WRITE;
-  key_merge(f, file, f->fp, rep, arg);
-  if (how == KOPEN_READ)
-    f->f &= ~(KF_WRITE | KF_MODIFIED);
-  else
-    f->f &= ~KF_MODIFIED;
-
-  /* --- Close the file if only needed for reading --- */
+  if (f->fp)
+    key_merge(f, file, f->fp, rep, arg);
+  f->f &= ~KF_MODIFIED;
 
-  if (how == KOPEN_READ) {
+  if ((how & KOPEN_MASK) == KOPEN_READ) {
+    f->f &= ~KF_WRITE;
     fclose(f->fp);
     f->fp = 0;
   }
@@ -484,7 +480,7 @@ int key_close(key_file *f)
   hash_base *b;
   hash_iter i;
 
-  if ((e = key_save(f)) != KWRITE_OK)
+  if (f->fp && (e = key_save(f)) != KWRITE_OK)
     return (e);
 
   /* --- Free all the individual keys --- */
diff --git a/key.h b/key.h
index 980c561..09bcad5 100644 (file)
--- a/key.h
+++ b/key.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: key.h,v 1.7 2000/12/06 20:33:27 mdw Exp $
+ * $Id: key.h,v 1.8 2001/02/03 11:57:38 mdw Exp $
  *
  * Simple key management
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: key.h,v $
+ * Revision 1.8  2001/02/03 11:57:38  mdw
+ * Allow creating keyfiles with no file attached.
+ *
  * Revision 1.7  2000/12/06 20:33:27  mdw
  * Make flags be macros rather than enumerations, to ensure that they're
  * unsigned.
@@ -168,10 +171,10 @@ typedef struct key_fetchdef {
 
 /* --- File opening options --- */
 
-enum {
-  KOPEN_READ,
-  KOPEN_WRITE
-};
+#define KOPEN_READ 0u
+#define KOPEN_WRITE 1u
+#define KOPEN_MASK 0xff
+#define KOPEN_NOFILE 0x100
 
 /* --- Various other magic numbers --- */
 
@@ -252,7 +255,7 @@ extern int key_extract(key_file */*f*/, key */*k*/, FILE */*fp*/,
  *
  * Arguments:  @key_file *f@ = pointer to file structure to initialize
  *             @const char *file@ = pointer to the file name
- *             @int how@ = opening options (@KOPEN_*@).
+ *             @unsigned how@ = opening options (@KOPEN_*@).
  *             @key_reporter *rep@ = error reporting function
  *             @void *arg@ = argument for function
  *
@@ -266,7 +269,7 @@ extern int key_extract(key_file */*f*/, key */*k*/, FILE */*fp*/,
  *             owner only.
  */
 
-extern int key_open(key_file */*f*/, const char */*file*/, int /*how*/,
+extern int key_open(key_file */*f*/, const char */*file*/, unsigned /*how*/,
                    key_reporter */*rep*/, void */*arg*/);
 
 /* --- @key_close@ --- *
@@ -303,7 +306,7 @@ extern int key_save(key_file */*f*/);
  *
  * Arguments:  @key_file *f@ = pointer to file structure to initialize
  *             @const char *file@ = pointer to the file name
- *             @int how@ = opening options (@KOPEN_*@).
+ *             @unsigned how@ = opening options (@KOPEN_*@).
  *
  * Returns:    Zero if it worked, nonzero otherwise.
  *
@@ -318,7 +321,8 @@ extern int key_save(key_file */*f*/);
  *             for the private use of @key_open@.
  */
 
-extern int key_lockfile(key_file */*f*/, const char */*file*/, int /*how*/);
+extern int key_lockfile(key_file */*f*/, const char */*file*/,
+                       unsigned /*how*/);
 
 /*----- Creating and manipulating keys ------------------------------------*/