]> mj.ucw.cz Git - libucw.git/commitdiff
The is_temp_file variable was originally a good idea, but it made
authorMartin Mares <mj@ucw.cz>
Mon, 30 Sep 2002 15:10:43 +0000 (15:10 +0000)
committerMartin Mares <mj@ucw.cz>
Mon, 30 Sep 2002 15:10:43 +0000 (15:10 +0000)
different fb back-ends giving the same interface almost impossible.

I've replaced it with bconfig() which is a universal interface for
altering various fb settings. It's somewhat ioctl()-ish, but I hope
it won't hurt.

lib/fastbuf.c
lib/fastbuf.h
lib/fb-file.c
lib/fb-mmap.c
lib/fb-temp.c
lib/sorter.h

index d976730d2b7affb829a2b4322e0f7ca57b2abb54..115719422b8844875edefb015f61170297c0494a 100644 (file)
@@ -301,3 +301,9 @@ bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l)
       l -= n;
     }
 }
+
+int
+bconfig(struct fastbuf *f, uns item, int value)
+{
+  return f->config ? f->config(f, item, value) : -1;
+}
index 1f502ce5d733a7ac85d95e7abbd20be2446a1a02..5ee23d48c88c556e0e25bb7ab1c20d9a1880b48d 100644 (file)
@@ -62,28 +62,31 @@ struct fastbuf {
   void (*spout)(struct fastbuf *);     /* Write buffer data to the file */
   void (*seek)(struct fastbuf *, sh_off_t, int);  /* Slow path for bseek(), buffer already flushed */
   void (*close)(struct fastbuf *);     /* Close the stream */
+  int (*config)(struct fastbuf *, uns, int);   /* Configure the stream */
 };
 
 /* FastIO on standard files */
 
-struct fb_file {
-  struct fastbuf fb;
-  int fd;                              /* File descriptor, -1 if not a real file */
-  int is_temp_file;                    /* 0=normal file, 1=temporary file, delete on close, -1=shared FD */
-};
-#define FB_FILE(f) ((struct fb_file *)(f)->is_fastbuf)
-
 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
 struct fastbuf *bopen_tmp(uns buffer);
 struct fastbuf *bfdopen(int fd, uns buffer);
 struct fastbuf *bfdopen_shared(int fd, uns buffer);
-#define FB_IS_TEMP_FILE(f) FB_FILE(f)->is_temp_file
 
 /* FastIO on in-memory streams */
 
 struct fastbuf *fbmem_create(unsigned blocksize);      /* Create stream and return its writing fastbuf */
 struct fastbuf *fbmem_clone_read(struct fastbuf *);    /* Create reading fastbuf */
 
+/* FastIO on memory mapped files */
+
+struct fastbuf *bopen_mm(byte *name, uns mode);
+
+/* Configuring stream parameters */
+
+int bconfig(struct fastbuf *f, uns type, int data);
+
+#define BCONFIG_IS_TEMP_FILE 0
+
 /* Universal functions working on all fastbuf's */
 
 void bclose(struct fastbuf *f);
index b5e6b6466f21c1c5bc5ba8fcc24c002562fea63d..7d9de003abf6a7fb9f3cfc95b2253af71b308b67 100644 (file)
 #include <fcntl.h>
 #include <unistd.h>
 
+struct fb_file {
+  struct fastbuf fb;
+  int fd;                              /* File descriptor, -1 if not a real file */
+  int is_temp_file;                    /* 0=normal file, 1=temporary file, delete on close, -1=shared FD */
+};
+#define FB_FILE(f) ((struct fb_file *)(f)->is_fastbuf)
+
 static int
 bfd_refill(struct fastbuf *f)
 {
@@ -74,6 +81,19 @@ bfd_close(struct fastbuf *f)
   xfree(f);
 }
 
+static int
+bfd_config(struct fastbuf *f, uns item, int value)
+{
+  switch (item)
+    {
+    case BCONFIG_IS_TEMP_FILE:
+      FB_FILE(f)->is_temp_file = value;
+      return 0;
+    default:
+      return -1;
+    }
+}
+
 static struct fastbuf *
 bfdopen_internal(int fd, uns buflen, byte *name)
 {
@@ -92,6 +112,7 @@ bfdopen_internal(int fd, uns buflen, byte *name)
   f->spout = bfd_spout;
   f->seek = bfd_seek;
   f->close = bfd_close;
+  f->config = bfd_config;
   return f;
 }
 
index c9d7b10cd7b0b0547bbea25a167bb1f56e825fa8..d7309321f9e50eb7c6582c941d3df803f5309fb7 100644 (file)
@@ -9,7 +9,6 @@
 
 /*
  *  FIXME:
- *  - problems with temp files
  *  - O_WRONLY ? (& generally processing of mode bits)
  */
 
@@ -35,7 +34,7 @@
 struct fb_mmap {
   struct fastbuf fb;
   int fd;
-  int dummy;                           /* FIXME: dirty hack for is_temp_file, remove */
+  int is_temp_file;
   sh_off_t file_size;
   sh_off_t file_extend;
   sh_off_t window_pos;
@@ -137,10 +136,30 @@ bfmm_close(struct fastbuf *f)
   if (F->file_extend > F->file_size &&
       sh_ftruncate(F->fd, F->file_size))
     die("ftruncate(%s): %m", f->name);
-  close(F->fd);
+  switch (F->is_temp_file)
+    {
+    case 1:
+      if (unlink(f->name) < 0)
+       log(L_ERROR, "unlink(%s): %m", f->name);
+    case 0:
+      close(F->fd);
+    }
   xfree(f);
 }
 
+static int
+bfmm_config(struct fastbuf *f, uns item, int value)
+{
+  switch (item)
+    {
+    case BCONFIG_IS_TEMP_FILE:
+      FB_MMAP(f)->is_temp_file = value;
+      return 0;
+    default:
+      return -1;
+    }
+}
+
 static struct fastbuf *
 bfmmopen_internal(int fd, byte *name, uns mode)
 {
@@ -163,6 +182,7 @@ bfmmopen_internal(int fd, byte *name, uns mode)
   f->spout = bfmm_spout;
   f->seek = bfmm_seek;
   f->close = bfmm_close;
+  f->config = bfmm_config;
   return f;
 }
 
index c6daec31bbbbb73ac8eba3654c090fc41d61ad4c..bdaf831d5db4b12d4f4ed81076b0502b089cbad5 100644 (file)
@@ -36,6 +36,6 @@ bopen_tmp(uns bufsize)
 
   sprintf(buf, temp_template, (int) getpid(), temp_counter++);
   f = bopen(buf, O_RDWR | O_CREAT | O_EXCL, bufsize);
-  FB_IS_TEMP_FILE(f) = 1;
+  bconfig(f, BCONFIG_IS_TEMP_FILE, 1);
   return f;
 }
index aae3cd1f4089cbb3658623c3049acd41ea16c069..574096bc526d1b4e26474e54513197ca661059a4 100644 (file)
@@ -385,7 +385,7 @@ struct fastbuf *fb1, struct fastbuf *fb2
 #endif
 
 #ifdef SORT_DELETE_INPUT
-  FB_IS_TEMP_FILE(fb1) = SORT_DELETE_INPUT;
+  bconfig(fb1, BCONFIG_IS_TEMP_FILE, SORT_DELETE_INPUT);
 #endif
   sorter_pass_counter = 1;
 #ifdef SORT_PRESORT
@@ -399,7 +399,7 @@ struct fastbuf *fb1, struct fastbuf *fb2
 #ifdef SORT_OUTPUT_FB
   return fb1;
 #else
-  FB_IS_TEMP_FILE(fb1) = 0;
+  bconfig(fb1, BCONFIG_IS_TEMP_FILE, 0);
   if (rename(fb1->name, outname) < 0)
     die("rename(%s,%s): %m", fb1->name, outname);
   bclose(fb1);