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