-
Notifications
You must be signed in to change notification settings - Fork 3
/
BCache.py
201 lines (174 loc) · 6.33 KB
/
BCache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python
import Config
import struct
import mmap
import os
import fcntl
import Board
from Util import Util
from Log import Log
from sysv_ipc import *
from cstruct import *
from errors import *
@init_fields
class BoardHeader(object):
'''struct boardheader'''
_fields = [
['filename', Str(Config.STRLEN)],
['BM', Str(Config.BM_LEN)],
['title', Str(Config.STRLEN)],
['level', U32()],
['nowid', U32()],
['clubnum', U32()],
['flag', U32()],
['adv_club', U32()],
['createtime', U32()],
['toptitle', I32()],
['ann_path', Str(128)],
['group', I32()],
['title_level', I8()],
['des', Str(195)]
]
def read(self, pos, len):
return BCache.bcache[self.idx * self.size + pos:self.idx * self.size + pos + len]
def write(self, pos, data):
BCache.bcache[self.idx * self.size + pos : self.idx * self.size + pos + len(data)] = data
def __init__(self, idx):
self.idx = idx - 1 # we start from 0 here...
@init_fields
class BoardStatus(object):
'''struct boardheader'''
_fields = [
['total', I32()],
['lastpost', I32()],
['updatemark', I32()],
['updatetitle', I32()],
['updateorigin', I32()],
['currentusers', I32()]
]
def read(self, pos, len):
return BCache.brdshm.read(len, 4 + self.size * self.idx + pos)
def write(self, pos, data):
BCache.brdshm.write(data, 4 + self.size * self.idx + pos)
def __init__(self, idx):
self.idx = idx - 1 # we start from 0 here...
class BCache:
bcache = None
brdshm = None
BRDSHM_SIZE = 4 + Config.MAXBOARD * BoardStatus.size
@staticmethod
def GetBoardCount():
if (BCache.brdshm == None):
BCache.Init()
return struct.unpack('=i', BCache.brdshm.read(4, 0))[0]
@staticmethod
def SetBoardCount(count):
if (BCache.brdshm == None):
BCache.Init()
BCache.brdshm.write(struct.pack('=i', count), 0)
@staticmethod
def GetBoardHeader(name):
for i in range(0, BCache.GetBoardCount()):
bh = BoardHeader(i + 1)
if (bh.filename == name):
return bh
return None
@staticmethod
def GetBoardNum(name):
for i in range(0, BCache.GetBoardCount()):
bh = BoardHeader(i + 1)
if (bh.filename == name):
return i + 1
return 0
@staticmethod
def Init():
Log.info("Initializing BCache")
if (BCache.bcache == None):
boardf = open(Config.Config.GetBoardsFile(), 'r+b')
if (boardf == None):
Log.error("Cannot open boards file")
raise ServerError("fail to open boards file")
try:
BCache.bcache = mmap.mmap(boardf.fileno(), Config.MAXBOARD * BoardHeader.size, flags = mmap.MAP_SHARED, prot = mmap.PROT_READ)
if (BCache.bcache == None):
Log.error("Cannot mmap boards file")
raise ServerError("fail to mmap boards file")
finally:
boardf.close()
Log.info("Got boards list")
if (BCache.brdshm == None):
try:
Log.info("Attaching to BCache shared memory")
BCache.brdshm = SharedMemory(Config.Config.GetInt("BCACHE_SHMKEY", 3693), size = BCache.BRDSHM_SIZE)
# print "Got SHM"
# for i in range(0, Config.MAXBOARD):
# bh = BoardHeader(i)
# if (bh.filename != ''):
# bs = BoardStatus(i)
# print "Board: ", bh.filename, " lastpost: ", bs.lastpost, " total: ", bs.total, " curruser: ", bs.currentusers
# print BoardStatus.size
except:
# do initialization
Log.info("Creating BCache shared memory")
BCache.brdshm = SharedMemory(Config.Config.GetInt("BCACHE_SHMKEY", 3693), size = BCache.BRDSHM_SIZE, flags = IPC_CREAT, mode = 0660)
fd = BCache.Lock()
try:
maxbrd = -1
for i in range(0, Config.MAXBOARD):
bh = BoardHeader(i)
if (bh.filename != ''):
bs = BoardStatus(i)
board = Board(bh, bs, i)
board.UpdateLastPost()
maxbrd = i
if (maxbrd != -1):
BCache.SetBoardCount(maxbrd + 1)
finally:
BCache.Unlock(fd)
Log.info("BCache initialized")
return
@staticmethod
def SetReadOnly(readonly):
if (BCache.bcache != None):
BCache.bcache.close()
boardf = open(Config.Config.GetBoardsFile(), 'r+b')
if (boardf == None):
print "Cannot open boards file"
return False
if (readonly):
BCache.bcache = mmap.mmap(boardf.fileno(), Config.MAXBOARD * BoardHeader.size, flags=mmap.MAP_SHARED, prot=mmap.PROT_READ)
else:
BCache.bcache = mmap.mmap(boardf.fileno(), Config.MAXBOARD * BoardHeader.size, flags=mmap.MAP_SHARED, prot=mmap.PROT_READ | mmap.PROT_WRITE)
boardf.close()
return True
@staticmethod
def Lock():
lockfd = os.open(Config.BBS_ROOT + "bcache.lock", os.O_CREAT | os.O_TRUNC | os.O_WRONLY, 0600)
if (lockfd == None):
Log.warn("fail to create bcache.lock")
return None
BCache.SetReadOnly(False)
fcntl.flock(lockfd, fcntl.LOCK_EX)
return lockfd
@staticmethod
def Unlock(lockfd):
fcntl.flock(lockfd, fcntl.LOCK_UN)
BCache.SetReadOnly(True)
os.close(lockfd)
@staticmethod
def GetNextID(boardname):
ret = -1
lockfd = BCache.Lock()
if (lockfd < 0):
Log.warn("BCache lock fail")
return -1
try:
bh = BCache.GetBoardHeader(boardname)
if (bh != None):
bh.nowid += 1
ret = bh.nowid
else:
Log.warn("GetNextID() fail for board %s" % boardname)
finally:
BCache.Unlock(lockfd)
return ret