Towards GPL
[idzebra-moved-to-github.git] / bfile / mfile.c
index 10977b9..002a3f9 100644 (file)
@@ -1,10 +1,26 @@
-/*
- * Copyright (C) 1994-2002, Index Data
- * All rights reserved.
- * Sebastian Hammer, Adam Dickmeiss
- *
- * $Id: mfile.c,v 1.44 2002-04-11 20:09:08 adam Exp $
- */
+/* $Id: mfile.c,v 1.49 2002-08-02 19:26:55 adam Exp $
+   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
+   Index Data Aps
+
+This file is part of the Zebra server.
+
+Zebra is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+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.
+*/
+
+
 
 
  /*
@@ -133,13 +149,18 @@ static int file_position(MFile mf, int pos, int offset)
     }
     else
        off = c ? (mf->files[c-1].top + 1) : 0;
-    if (mf->files[c].fd < 0 && (mf->files[c].fd = open(mf->files[c].path,
-       mf->wr ? (O_BINARY|O_RDWR|O_CREAT) : (O_BINARY|O_RDONLY), 0666)) < 0)
+    if (mf->files[c].fd < 0)
     {
-        if (!mf->wr && errno == ENOENT && off == 0)
-            return -2;
-       logf (LOG_WARN|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
-       return -1;
+        if ((mf->files[c].fd = open(mf->files[c].path,
+                                   mf->wr ?
+                                        (O_BINARY|O_RDWR|O_CREAT) :
+                                        (O_BINARY|O_RDONLY), 0666)) < 0)
+        {
+            if (!mf->wr && errno == ENOENT && off == 0)
+                return -2;
+           logf (LOG_WARN|LOG_ERRNO, "Failed to open %s", mf->files[c].path);
+            return -1;
+        }
     }
     if (lseek(mf->files[c].fd, (ps = pos - off) * mf->blocksize + offset,
        SEEK_SET) < 0)
@@ -243,6 +264,9 @@ MFile_area mf_init(const char *name, const char *spec, const char *base)
                       dent->d_name);
                return 0;
            }
+#ifndef WIN32
+           fsync(fd);
+#endif
            close(fd);
            if (dirp->max_bytes >= 0)
                dirp->avail_bytes -= part_f->bytes;
@@ -405,6 +429,9 @@ int mf_close(MFile mf)
     for (i = 0; i < mf->no_files; i++)
        if (mf->files[i].fd >= 0)
        {
+#ifndef WIN32
+           fsync(mf->files[i].fd);
+#endif
            close(mf->files[i].fd);
            mf->files[i].fd = -1;
        }
@@ -428,7 +455,10 @@ int mf_read(MFile mf, int no, int offset, int nbytes, void *buf)
             return 0;
        }
         else
+        {
+            yaz_log (LOG_FATAL, "mf_read %s internal error", mf->name);
             exit(1);
+        }
     }
     toread = nbytes ? nbytes : mf->blocksize;
     if ((rd = read(mf->files[mf->cur_file].fd, buf, toread)) < 0)
@@ -456,14 +486,18 @@ int mf_write(MFile mf, int no, int offset, int nbytes, const void *buf)
 
     zebra_mutex_lock (&mf->mutex);
     if ((ps = file_position(mf, no, offset)) < 0)
+    {
+        yaz_log (LOG_FATAL, "mf_write %s internal error (1)", mf->name);
        exit(1);
+    }
     /* file needs to grow */
     while (ps >= mf->files[mf->cur_file].blocks)
     {
+        off_t needed = (ps - mf->files[mf->cur_file].blocks + 1) *
+                       mf->blocksize;
        /* file overflow - allocate new file */
        if (mf->files[mf->cur_file].dir->max_bytes >= 0 &&
-           (ps - mf->files[mf->cur_file].blocks + 1) * mf->blocksize >
-           mf->files[mf->cur_file].dir->avail_bytes)
+           needed > mf->files[mf->cur_file].dir->avail_bytes)
        {
            /* cap off file? */
            if ((nblocks = mf->files[mf->cur_file].dir->avail_bytes /
@@ -474,11 +508,16 @@ int mf_write(MFile mf, int no, int offset, int nbytes, const void *buf)
                if ((ps = file_position(mf,
                    (mf->cur_file ? mf->files[mf->cur_file-1].top : 0) +
                    mf->files[mf->cur_file].blocks + nblocks - 1, 0)) < 0)
-                       exit(1);
+                {
+                    yaz_log (LOG_FATAL, "mf_write %s internal error (2)",
+                                mf->name);
+                   exit(1);
+                }
                logf (LOG_DEBUG, "ps = %d", ps);
                if (write(mf->files[mf->cur_file].fd, &dummych, 1) < 1)
                {
-                   logf (LOG_ERRNO|LOG_FATAL, "write dummy");
+                   logf (LOG_ERRNO|LOG_FATAL, "mf_write %s internal error (3)",
+                                     mf->name);
                    exit(1);
                }
                mf->files[mf->cur_file].blocks += nblocks;
@@ -489,7 +528,7 @@ int mf_write(MFile mf, int no, int offset, int nbytes, const void *buf)
            /* get other bit */
            logf (LOG_DEBUG, "Creating new file.");
            for (dp = mf->ma->dirs; dp && dp->max_bytes >= 0 &&
-               dp->avail_bytes < mf->min_bytes_creat; dp = dp->next);
+               dp->avail_bytes < needed; dp = dp->next);
            if (!dp)
            {
                logf (LOG_FATAL, "Cannot allocate more space for %s",
@@ -512,7 +551,11 @@ int mf_write(MFile mf, int no, int offset, int nbytes, const void *buf)
            mf->no_files++;
            /* open new file and position at beginning */
            if ((ps = file_position(mf, no, offset)) < 0)
+            {
+                yaz_log (LOG_FATAL, "mf_write %s internal error (4)",
+                                mf->name);
                exit(1);
+            }  
        }
        else
        {