a52aa70057a87b02d5ab8f9b0c8c39d575cc9f76
[idzebra-moved-to-github.git] / bfile / mfile.h
1 /* $Id: mfile.h,v 1.13 2007-12-18 13:39:39 adam Exp $
2    Copyright (C) 1995-2007
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 #include <idzebra/util.h>
30
31 #ifdef WIN32
32
33 /* 64-bit access .. */
34 typedef __int64 mfile_off_t;
35 #define mfile_seek _lseeki64
36
37 #else
38 #include <sys/types.h>
39 typedef off_t mfile_off_t;
40 #define mfile_seek lseek
41 #endif
42
43 #ifndef FILENAME_MAX
44 #include <sys/param.h>
45 #define FILENAME_MAX MAXPATHLEN
46 #endif
47
48 #include <zebra-lock.h>
49
50 YAZ_BEGIN_CDECL
51
52 #define MF_MIN_BLOCKS_CREAT 1          /* minimum free blocks in new dir */
53 #define MF_MAX_PARTS 28                 /* max # of part-files per metafile */
54
55 #define mf_blocksize(mf) ((mf)->blocksize)
56
57
58 typedef struct mf_dir
59 {
60     char name[FILENAME_MAX+1];
61     mfile_off_t max_bytes;      /* allocated bytes in this dir. */
62     mfile_off_t avail_bytes;    /* bytes left */
63     struct mf_dir *next;
64 } mf_dir;
65
66 typedef struct part_file
67 {
68     zint number;
69     zint top;
70     zint blocks;
71     mfile_off_t bytes;
72     mf_dir *dir;
73     char *path;
74     int fd;
75 } part_file;
76
77 struct MFile_area_struct;
78 typedef struct MFile_area_struct *MFile_area;
79
80 typedef struct meta_file
81 {
82     char name[FILENAME_MAX+1];
83     part_file files[MF_MAX_PARTS];
84     int no_files;
85     int cur_file;
86     int open;                          /* is this file open? */
87     int blocksize;
88     mfile_off_t min_bytes_creat;  /* minimum bytes required to enter directory */
89     MFile_area ma;
90     int wr;
91     Zebra_mutex mutex;
92
93     struct meta_file *next;
94 } *MFile, meta_file;
95
96 struct MFile_area_struct
97 {
98     char name[FILENAME_MAX+1];
99     mf_dir *dirs;
100     struct meta_file *mfiles;
101     struct MFile_area_struct *next;  /* global list of active areas */
102     Zebra_mutex mutex;
103 };
104
105 /** \brief creates a metafile area
106     \param name of area (does not show up on disk - purely for notation)
107     \param spec area specification (e.g. "/a:1G dir /b:2000M"
108     \param base base directory (NULL for no base)
109     \param only_shadow_files only consider shadow files in area
110     \returns metafile area handle or NULL if error occurs
111 */
112 MFile_area mf_init(const char *name, const char *spec, const char *base,
113                    int only_shadow_files)
114     ZEBRA_GCC_ATTR((warn_unused_result));
115     
116 /** \brief destroys metafile area handle
117     \param ma metafile area handle
118 */
119 void mf_destroy(MFile_area ma);
120
121 /** \brief opens metafile
122     \param ma metafile area handle
123     \param name pseudo filename (name*.mf)
124     \param block_size block size for this file
125     \param wflag write flag, 0=read, 1=write&read
126     \returns metafile handle, or NULL for error (could not be opened)
127  */
128 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag)
129     ZEBRA_GCC_ATTR((warn_unused_result));
130     
131 /** \brief closes metafile
132     \param mf metafile handle
133     \retval 0 OK
134 */
135 int mf_close(MFile mf);
136
137 /** \brief reads block from metafile
138     \param mf metafile handle
139     \param no block position
140     \param offset offset within block
141     \param nbytes no of bytes to read (0 for whole block)
142     \param buf content (filled with data if OK)
143     \retval 0 block partially read
144     \retval 1 block fully read
145     \retval -1 block could not be read due to error
146  */
147 int mf_read(MFile mf, zint no, int offset, int nbytes, void *buf)
148     ZEBRA_GCC_ATTR((warn_unused_result));
149     
150 /** \brief writes block to metafile
151     \param mf metafile handle
152     \param no block position
153     \param offset offset within block
154     \param nbytes no of bytes to write (0 for whole block)
155     \param buf content to be written
156     \retval 0 block written
157     \retval -1 error (block not written)
158 */
159 int mf_write(MFile mf, zint no, int offset, int nbytes, const void *buf)
160     ZEBRA_GCC_ATTR((warn_unused_result));    
161     
162 /** \brief reset all files in a metafile area (optionally delete them as well)
163     \param ma metafile area
164     \param unlink_flag if unlink_flag=1 all files are removed from FS
165 */
166 void mf_reset(MFile_area ma, int unlink_flag);
167
168 /* \brief gets statistics about directory in metafile area
169    \param ma the area
170    \param no directory number (0=first, 1=second,...)
171    \param directory holds directory name (if found)
172    \param used_bytes used file bytes in directory (if found)
173    \param max_bytes max usage of bytes (if found)
174    \retval 1 no is within range and directory, used, max are set.
175    \retval 0 no is out of range and directory, used, max are unset
176
177    We are using double, because off_t may have a different size
178    on same platform depending on whether 64-bit is enabled or not.
179    Note that if an area has unlimited size, that is represented
180    as max_bytes = -1.
181 */ 
182 int mf_area_directory_stat(MFile_area ma, int no, const char **directory,
183                            double *bytes_used, double *bytes_max);
184     
185 YAZ_END_CDECL
186
187 #endif
188 /*
189  * Local variables:
190  * c-basic-offset: 4
191  * indent-tabs-mode: nil
192  * End:
193  * vim: shiftwidth=4 tabstop=8 expandtab
194  */
195