Towards GPL
[idzebra-moved-to-github.git] / include / mfile.h
1 /* $Id: mfile.h,v 1.18 2002-08-02 19:26:55 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24
25 #ifndef MFILE_H
26 #define MFILE_H
27
28 #include <stdio.h>
29 #include <yaz/yconfig.h>
30
31 #ifdef WIN32
32 typedef long off_t;
33 #else
34 #include <sys/types.h>
35 #endif
36
37 #ifndef FILENAME_MAX
38 #include <sys/param.h>
39 #define FILENAME_MAX MAXPATHLEN
40 #endif
41
42 #include <zebra-lock.h>
43
44 YAZ_BEGIN_CDECL
45
46 #define MF_MIN_BLOCKS_CREAT 1          /* minimum free blocks in new dir */
47 #define MF_MAX_PARTS 28                 /* max # of part-files per metafile */
48
49 #define mf_blocksize(mf) ((mf)->blocksize)
50
51
52 typedef struct mf_dir
53 {
54     char name[FILENAME_MAX+1];
55     off_t max_bytes;      /* allocated bytes in this dir. */
56     off_t avail_bytes;    /* bytes left */
57     struct mf_dir *next;
58 } mf_dir;
59
60 typedef struct part_file
61 {
62     int number;
63     int top;
64     int blocks;
65     off_t bytes;
66     mf_dir *dir;
67     char *path;
68     int fd;
69 } part_file;
70
71 struct MFile_area_struct;
72 typedef struct MFile_area_struct *MFile_area;
73
74 typedef struct meta_file
75 {
76     char name[FILENAME_MAX+1];
77     part_file files[MF_MAX_PARTS];
78     int no_files;
79     int cur_file;
80     int open;                          /* is this file open? */
81     off_t blocksize;
82     off_t min_bytes_creat;  /* minimum bytes required to enter directory */
83     MFile_area ma;
84     int wr;
85     Zebra_mutex mutex;
86
87     struct meta_file *next;
88 } *MFile, meta_file;
89
90 typedef struct MFile_area_struct
91 {
92     char name[FILENAME_MAX+1];
93     mf_dir *dirs;
94     struct meta_file *mfiles;
95     struct MFile_area_struct *next;  /* global list of active areas */
96     Zebra_mutex mutex;
97 } MFile_area_struct;
98
99 /*
100  * Open an area, cotaining metafiles in directories.
101  */
102 MFile_area mf_init(const char *name, const char *spec, const char *base); 
103
104 /*
105  * Release an area.
106  */
107 void mf_destroy(MFile_area ma);
108
109 /*
110  * Open a metafile.
111  */
112 MFile mf_open(MFile_area ma, const char *name, int block_size, int wflag);
113
114 /*
115  * Close a metafile.
116  */
117 int mf_close(MFile mf);
118
119 /*
120  * Read one block from a metafile. Interface mirrors bfile.
121  */
122 int mf_read(MFile mf, int no, int offset, int nbytes, void *buf);
123
124 /*
125  * Same.
126  */
127 int mf_write(MFile mf, int no, int offset, int nbytes, const void *buf);
128
129 /*
130  * Destroy a metafile, unlinking component files. File must be open.
131  */
132 int mf_unlink(MFile mf);
133
134
135 /*
136  * Destroy all metafiles. No files may be opened.
137  */
138 void mf_reset(MFile_area ma);
139
140 /*
141  * Unlink the file by name, rather than MFile-handle.
142  */
143 int mf_unlink_name(MFile_area, const char *name);
144
145 YAZ_END_CDECL
146
147 #endif