1 |
root |
1.1 |
// -*- Mode: C++ -*- |
2 |
|
|
|
3 |
|
|
// GiSTfile.cpp |
4 |
|
|
// |
5 |
|
|
// Copyright (c) 1996, Regents of the University of California |
6 |
|
|
// $Header: /usr/local/devel/GiST/libGiST/libGiST/GiSTfile.cpp,v 1.1.1.1 1996/08/06 23:47:21 jmh Exp $ |
7 |
|
|
|
8 |
|
|
#include <stdio.h> |
9 |
|
|
#include <sys/types.h> |
10 |
|
|
#include <sys/stat.h> |
11 |
|
|
#include <fcntl.h> |
12 |
|
|
#include <string.h> |
13 |
|
|
#ifdef UNIX |
14 |
|
|
#include <unistd.h> |
15 |
|
|
#else |
16 |
|
|
#include <io.h> |
17 |
|
|
#endif |
18 |
|
|
|
19 |
|
|
#ifdef UNIX |
20 |
|
|
#define O_BINARY 0 |
21 |
|
|
#endif |
22 |
|
|
|
23 |
|
|
#include "GiSTfile.h" |
24 |
|
|
|
25 |
|
|
// The first page in the file has these "magic words" |
26 |
|
|
// and the head of the deleted page list. |
27 |
|
|
static char magic[]="GiST data file"; |
28 |
|
|
|
29 |
|
|
void |
30 |
|
|
GiSTfile::Create(const char *filename) |
31 |
|
|
{ |
32 |
|
|
if(IsOpen()) return; |
33 |
|
|
fileHandle=open(filename, O_RDWR|O_BINARY); |
34 |
|
|
if(fileHandle>=0) { |
35 |
|
|
close(fileHandle); |
36 |
|
|
return; |
37 |
|
|
} |
38 |
|
|
fileHandle=open(filename, O_BINARY|O_RDWR|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE); |
39 |
|
|
if(fileHandle<0) return; |
40 |
|
|
SetOpen(1); |
41 |
|
|
|
42 |
|
|
/* Reserve page 0 */ |
43 |
|
|
char *page=new char[PageSize()]; |
44 |
|
|
|
45 |
|
|
memset(page, 0, PageSize()); |
46 |
|
|
memcpy(page, magic, sizeof magic); |
47 |
|
|
write(fileHandle, page, PageSize()); |
48 |
|
|
delete page; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
void |
52 |
|
|
GiSTfile::Open(const char *filename) |
53 |
|
|
{ |
54 |
|
|
char *page; |
55 |
|
|
|
56 |
|
|
if(IsOpen()) return; |
57 |
|
|
fileHandle=open(filename, O_RDWR|O_BINARY); |
58 |
|
|
if(fileHandle<0) return; |
59 |
|
|
// Verify that the magic words are there |
60 |
|
|
page=new char[PageSize()]; |
61 |
|
|
read(fileHandle, page, PageSize()); |
62 |
|
|
if(memcmp(page, magic, sizeof(magic))) { |
63 |
|
|
close(fileHandle); |
64 |
|
|
delete page; |
65 |
|
|
return; |
66 |
|
|
} |
67 |
|
|
delete page; |
68 |
|
|
SetOpen(1); |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
void |
72 |
|
|
GiSTfile::Close() |
73 |
|
|
{ |
74 |
|
|
if(!IsOpen()) return; |
75 |
|
|
close(fileHandle); |
76 |
|
|
SetOpen(0); |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
void |
80 |
|
|
GiSTfile::Read(GiSTpage page, char *buf) |
81 |
|
|
{ |
82 |
|
|
if(IsOpen()) { |
83 |
|
|
lseek(fileHandle, page*PageSize(), SEEK_SET); |
84 |
|
|
read(fileHandle, buf, PageSize()); |
85 |
|
|
} |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
void |
89 |
|
|
GiSTfile::Write(GiSTpage page, const char *buf) |
90 |
|
|
{ |
91 |
|
|
if(IsOpen()) { |
92 |
|
|
lseek(fileHandle, page*PageSize(), SEEK_SET); |
93 |
|
|
write(fileHandle, buf, PageSize()); |
94 |
|
|
} |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
GiSTpage |
98 |
|
|
GiSTfile::Allocate() |
99 |
|
|
{ |
100 |
|
|
GiSTpage page; |
101 |
|
|
char *buf; |
102 |
|
|
|
103 |
|
|
if(!IsOpen()) return (0); |
104 |
|
|
// See if there's a deleted page |
105 |
|
|
buf=new char[PageSize()]; |
106 |
|
|
Read(0, buf); |
107 |
|
|
memcpy(&page, buf+sizeof(magic), sizeof(GiSTpage)); |
108 |
|
|
if(page) { |
109 |
|
|
// Reclaim this page |
110 |
|
|
Read(page, buf); |
111 |
|
|
Write(0, buf); |
112 |
|
|
} |
113 |
|
|
else { |
114 |
|
|
page=lseek(fileHandle, 0, SEEK_END)/PageSize(); |
115 |
|
|
memset(buf, 0, PageSize()); |
116 |
|
|
write(fileHandle, buf, PageSize()); |
117 |
|
|
} |
118 |
|
|
delete buf; |
119 |
|
|
return page; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
void |
123 |
|
|
GiSTfile::Deallocate(GiSTpage page) |
124 |
|
|
{ |
125 |
|
|
char *buf; |
126 |
|
|
GiSTpage temp; |
127 |
|
|
|
128 |
|
|
if(!IsOpen()) return; |
129 |
|
|
// Get the old head of the list |
130 |
|
|
buf=new char[PageSize()]; |
131 |
|
|
Read(0, buf); |
132 |
|
|
memcpy(&temp, buf+sizeof(magic), sizeof(GiSTpage)); |
133 |
|
|
// Write the new head of the list |
134 |
|
|
memcpy(buf+sizeof(magic), &page, sizeof(GiSTpage)); |
135 |
|
|
Write(0, buf); |
136 |
|
|
// In our new head, put link to old head |
137 |
|
|
memcpy(buf+sizeof(magic), &temp, sizeof(GiSTpage)); |
138 |
|
|
Write(page, buf); |
139 |
|
|
delete buf; |
140 |
|
|
} |