Simplify and optimize commit clean operation
[idzebra-moved-to-github.git] / bfile / mfile.c
index 189f202..06d4781 100644 (file)
@@ -1,5 +1,5 @@
-/* $Id: mfile.c,v 1.58 2005-01-15 19:38:17 adam Exp $
-   Copyright (C) 1995-2005
+/* $Id: mfile.c,v 1.68 2006-10-09 22:10:00 adam Exp $
+   Copyright (C) 1995-2006
    Index Data ApS
 
 This file is part of the Zebra server.
@@ -15,9 +15,9 @@ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
-along with Zebra; see the file LICENSE.zebra.  If not, write to the
-Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
 */
 
 
@@ -32,7 +32,8 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <fcntl.h>
 #ifdef WIN32
 #include <io.h>
-#else
+#endif
+#if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 #include <direntz.h>
@@ -43,7 +44,8 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <errno.h>
 
 #include <zebra-lock.h>
-#include <zebrautl.h>
+#include <idzebra/util.h>
+#include <yaz/yaz-util.h>
 #include "mfile.h"
 
 static int scan_areadef(MFile_area ma, const char *ad, const char *base)
@@ -160,7 +162,7 @@ static zint file_position(MFile mf, zint pos, int offset)
             if (!mf->wr && errno == ENOENT && off == 0)
                 return -2;
            yaz_log (YLOG_WARN|YLOG_ERRNO, "Failed to open %s", mf->files[c].path);
-            return -1;
+           return -1;
         }
     }
     ps = pos - off;
@@ -250,7 +252,6 @@ MFile_area mf_init(const char *name, const char *spec, const char *base)
                meta_f->next = ma->mfiles;
                meta_f->open = 0;
                meta_f->cur_file = -1;
-                meta_f->unlink_flag = 0;
                ma->mfiles = meta_f;
                strcpy(meta_f->name, metaname);
                part_f = &meta_f->files[0];
@@ -307,24 +308,11 @@ void mf_destroy(MFile_area ma)
        dp = dp->next;
        xfree (d);
     }
-    meta_f = ma->mfiles;
-    while (meta_f)
-    {
-       int i;
-       meta_file *m = meta_f;
-       
-       for (i = 0; i<m->no_files; i++)
-       {
-           xfree (m->files[i].path);
-       }
-       zebra_mutex_destroy (&meta_f->mutex);
-       meta_f = meta_f->next;
-       xfree (m);
-    }
+    mf_reset(ma, 0);
     xfree (ma);
 }
 
-void mf_reset(MFile_area ma)
+void mf_reset(MFile_area ma, int unlink_flag)
 {
     meta_file *meta_f;
 
@@ -336,13 +324,16 @@ void mf_reset(MFile_area ma)
        int i;
        meta_file *m = meta_f;
 
+       meta_f = meta_f->next;
+
        assert (!m->open);
        for (i = 0; i<m->no_files; i++)
        {
-           unlink (m->files[i].path);
+            if (unlink_flag)
+                unlink (m->files[i].path);
            xfree (m->files[i].path);
        }
-       meta_f = meta_f->next;
+       zebra_mutex_destroy (&m->mutex);
        xfree (m);
     }
     ma->mfiles = 0;
@@ -350,7 +341,6 @@ void mf_reset(MFile_area ma)
 
 /*
  * Open a metafile.
- * If !ma, Use MF_DEFAULT_AREA.
  */
 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
 {
@@ -382,7 +372,6 @@ MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
        mnew->files[0].top = -1;
        mnew->files[0].number = 0;
        mnew->files[0].fd = -1;
-        mnew->unlink_flag = 0;
        mnew->min_bytes_creat = MF_MIN_BLOCKS_CREAT * block_size;
        for (dp = ma->dirs; dp && dp->max_bytes >= 0 && dp->avail_bytes <
            mnew->min_bytes_creat; dp = dp->next);
@@ -447,8 +436,6 @@ int mf_close(MFile mf)
            close(mf->files[i].fd);
            mf->files[i].fd = -1;
        }
-        if (mf->unlink_flag)
-            unlink(mf->files[i].path);
     }
     mf->open = 0;
     return 0;
@@ -596,27 +583,34 @@ int mf_write(MFile mf, zint no, int offset, int nbytes, const void *buf)
     return 0;
 }
 
-/*
- * Destroy a metafile, unlinking component files. File must be open.
- */
-int mf_unlink(MFile mf)
+int mf_area_directory_stat(MFile_area ma, int no, const char **directory,
+                          double *used_bytes, double *max_bytes)
 {
-    if (mf->open)
-        mf->unlink_flag = 1;
-    else
+    int i;
+    mf_dir *d = ma->dirs;
+    for (i = 0; d && i<no; i++, d = d->next)
+       ;
+    if (!d)
+       return 0;
+    if (directory)
+       *directory = d->name;
+    if (max_bytes)
     {
-        int i;
-        for (i = 0; i<mf->no_files; i++)
-            unlink(mf->files[i].path);
+        /* possible loss of data. But it's just statistics and lies */
+       *max_bytes = (double) d->max_bytes;
     }
-    return 0;
+    if (used_bytes)
+    {
+        /* possible loss of data. But it's just statistics and lies */
+       *used_bytes = (double) (d->max_bytes - d->avail_bytes);
+    }
+    return 1;
 }
-
 /*
- * Unlink the file by name, rather than MFile-handle. File should be closed.
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
  */
-int mf_unlink_name(MFile_area ma, const char *name)
-{
-    abort();
-    return 0;
-}
+