Work on bug #550: Avoid exit. In particular the mfile/cfile/bfile has
[idzebra-moved-to-github.git] / bfile / mfile.h
1 /* $Id: mfile.h,v 1.9 2006-11-14 08:12:06 adam Exp $
2    Copyright (C) 1995-2006
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #ifndef MFILE_H
24 #define MFILE_H
25
26 #include <stdio.h>
27 #include <yaz/yconfig.h>
28 #include <idzebra/version.h>
29
30 #ifdef WIN32
31
32 /* 64-bit access .. */
33 typedef __int64 mfile_off_t;
34 #define mfile_seek _lseeki64
35
36 #else
37 #include <sys/types.h>
38 typedef off_t mfile_off_t;
39 #define mfile_seek lseek
40 #endif
41
42 #ifndef FILENAME_MAX
43 #include <sys/param.h>
44 #define FILENAME_MAX MAXPATHLEN
45 #endif
46
47 #include <zebra-lock.h>
48
49 YAZ_BEGIN_CDECL
50
51 #define MF_MIN_BLOCKS_CREAT 1          /* minimum free blocks in new dir */
52 #define MF_MAX_PARTS 28                 /* max # of part-files per metafile */
53
54 #define mf_blocksize(mf) ((mf)->blocksize)
55
56
57 typedef struct mf_dir
58 {
59     char name[FILENAME_MAX+1];
60     mfile_off_t max_bytes;      /* allocated bytes in this dir. */
61     mfile_off_t avail_bytes;    /* bytes left */
62     struct mf_dir *next;
63 } mf_dir;
64
65 typedef struct part_file
66 {
67     zint number;
68     zint top;
69     zint blocks;
70     mfile_off_t bytes;
71     mf_dir *dir;
72     char *path;
73     int fd;
74 } part_file;
75
76 struct MFile_area_struct;
77 typedef struct MFile_area_struct *MFile_area;
78
79 typedef struct meta_file
80 {
81     char name[FILENAME_MAX+1];
82     part_file files[MF_MAX_PARTS];
83     int no_files;
84     int cur_file;
85     int open;                          /* is this file open? */
86     int blocksize;
87     mfile_off_t min_bytes_creat;  /* minimum bytes required to enter directory */
88     MFile_area ma;
89     int wr;
90     Zebra_mutex mutex;
91
92     struct meta_file *next;
93 } *MFile, meta_file;
94
95 typedef struct MFile_area_struct
96 {
97     char name[FILENAME_MAX+1];
98     mf_dir *dirs;
99     struct meta_file *mfiles;
100     struct MFile_area_struct *next;  /* global list of active areas */
101     Zebra_mutex mutex;
102 } MFile_area_struct;
103
104 /** \brief creates a metafile area
105     \param name of area (does not show up on disk - purely for notation)
106     \param spec area specification (e.g. "/a:1G dir /b:2000M"
107     \param base base directory (NULL for no base)
108     \returns metafile area handle or NULL if error occurs
109 */
110 MFile_area mf_init(const char *name, const char *spec, const char *base)
111     ZEBRA_GCC_ATTR((warn_unused_result));
112
113 /** \brief destroys metafile area handle
114     \param ma metafile area handle
115 */
116 void mf_destroy(MFile_area ma);
117
118 /** \brief opens metafile
119     \param ma metafile area handle
120     \param name pseudo filename (name*.mf)
121     \param block_size block size for this file
122     \param wflag write flag, 0=read, 1=write&read
123     \returns metafile handle, or NULL for error (could not be opened)
124  */
125 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
126     ZEBRA_GCC_ATTR((warn_unused_result));
127     
128 /** \brief closes metafile
129     \param mf metafile handle
130     \retval 0 OK
131 */
132 int mf_close(MFile mf);
133
134 /** \brief reads block from metafile
135     \param mf metafile handle
136     \param no block position
137     \param offset offset within block
138     \param nbytes no of bytes to read (0 for whole block)
139     \param buf content (filled with data if OK)
140     \retval 0 block partially read
141     \retval 1 block fully read
142     \retval -1 block could not be read due to error
143  */
144 int mf_read(MFile mf, zint no, int offset, int nbytes, void *buf)
145     ZEBRA_GCC_ATTR((warn_unused_result));
146     
147 /** \brief writes block to metafile
148     \param mf metafile handle
149     \param no block position
150     \param offset offset within block
151     \param nbytes no of bytes to write (0 for whole block)
152     \param buf content to be written
153     \retval 0 block written
154     \retval -1 error (block not written)
155 */
156 int mf_write(MFile mf, zint no, int offset, int nbytes, const void *buf)
157     ZEBRA_GCC_ATTR((warn_unused_result));    
158     
159 /** \brief reset all files in a metafile area (optionally delete them as well)
160     \param ma metafile area
161     \param unlink_flag if unlink_flag=1 all files are removed from FS
162 */
163 void mf_reset(MFile_area ma, int unlink_flag);
164
165 /* \brief gets statistics about directory in metafile area
166    \param ma the area
167    \param no directory number (0=first, 1=second,...)
168    \param directory holds directory name (if found)
169    \param used_bytes used file bytes in directory (if found)
170    \param max_bytes max usage of bytes (if found)
171    \retval 1 no is within range and directory, used, max are set.
172    \retval 0 no is out of range and directory, used, max are unset
173
174    We are using double, because off_t may have a different size
175    on same platform depending on whether 64-bit is enabled or not.
176    Note that if an area has unlimited size, that is represented
177    as max_bytes = -1.
178 */ 
179 int mf_area_directory_stat(MFile_area ma, int no, const char **directory,
180                            double *bytes_used, double *bytes_max);
181     
182 YAZ_END_CDECL
183
184 #endif
185 /*
186  * Local variables:
187  * c-basic-offset: 4
188  * indent-tabs-mode: nil
189  * End:
190  * vim: shiftwidth=4 tabstop=8 expandtab
191  */
192