forked from cjgiridhar/seFS
/
seFS.py
170 lines (151 loc) · 4.42 KB
/
seFS.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
#############################################################################
# seFS.py - Database interface for seFS Database.
#
# seFS Version 1.0
# Copyright 2012
# Chetan Giridhar cjgiridhar@gmail.com
# Vishal Kanaujia vishalkanaujia@gmail.com
# Created: 04/03/2012 Last Modified: 06/03/2012
#
# This file is part of seFS.
#
# seFS is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License v3 as published by
# the Free Software Foundation;
#
# seFS is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#############################################################################
from db.SQLiteHandler import SQLiteHandler
from security.Crypto import Crypto
from archive.Compression import Compression
import time,bz2, random,sys
class seFS:
"""
Class representing seFS
"""
def __init__(self):
"""
Connects to seFS database
"""
self.sql = SQLiteHandler('seFS')
self.sql.connect()
def ls(self):
"""
Implements ls operation
"""
rows = self.sql.execute(''' select * from file ''')
list = []
for row in rows:
list.append(row[1])
return list
def getutime(self, path):
"""
Getter for utime
"""
rows = self.sql.execute(''' select mtime, ctime, atime from file where abspath='%s' ''' %( path))
for row in rows:
tup = (row[0], row[1], row[2])
return tup
def utime(self, path, times):
"""
Setter for utime
"""
print times
mtime, ctime, atime = times
print mtime, ctime, atime
self.sql.execute(''' update file set mtime='%s', ctime='%s', atime='%s' where abspath='%s' ''' %(mtime, ctime, atime, path))
self.sql.commit()
return times
def setinode(self,path,inode):
"""
Setter for inode
"""
print path,inode
self.sql.execute(''' update file set inode = '%d' where abspath='%s' ''' %(inode, path))
self.sql.commit()
return inode
def getinode(self,path):
"""
Getter for inode
"""
rows = self.sql.execute(''' select inode from file where abspath='%s' ''' %( path))
for row in rows:
return row[0]
def getlength(self,path):
"""
Calculates length of data
"""
rows = self.sql.execute(''' select length from file where abspath='%s' ''' %( path))
for row in rows:
return row[0]
def open(self, path):
"""
Implements file open() call
"""
self.sql.execute(''' insert into file (abspath) select '%s' where not exists (SELECT 1 FROM file WHERE abspath='%s') ''' %(path,path))
self.sql.commit()
return path
def write(self, fh, data):
"""
Implements write() call
"""
length = len(data)
shaObj = Crypto(string = data)
sha1 = shaObj.sha1()
comp = Compression(data)
compressed = comp.compress()
rows = self.sql.execute(''' select id from fileinfo where sha='%s' ''' %(sha1) )
id = None
for row in rows:
id = row[0]
if id is None:
self.sql.execute(''' insert into fileinfo (length, sha, compressed, data) values ('%s','%s','%s', '%s') ''' %(length, sha1, compressed, data))
self.sql.commit()
row = self.sql.execute(''' select id,length from fileinfo where sha='%s' ''' %(sha1))
for rows in row:
id = rows[0]
print id,length
self.sql.execute(''' update file set id='%d', length='%d' where abspath='%s' ''' %(id,length,fh))
self.sql.commit()
return
else:
self.sql.execute(''' update file set id='%d', length='%d' where abspath='%s' ''' %(id,length,fh))
self.sql.commit()
return
def remove(self,abspath):
"""
Implements unlink() call
"""
self.sql.execute(''' delete from file where abspath='%s' ''' %(abspath))
self.sql.commit()
return
def search(self, abspath):
"""
Search for database record
"""
rows = self.sql.execute(''' select * from file where abspath='%s' ''' %(abspath))
if rows.fetchall():
return True
return False
def read(self, fh):
"""
Implements read() call
"""
id = None
rows = self.sql.execute(''' select id from file where abspath='%s' ''' %(fh))
for row in rows:
id = row[0]
if id is None:
return None
rows = self.sql.execute(''' select data from fileinfo where id='%d' ''' %id)
for row in rows:
return row[0]
def __del__(self):
"""
Closes the databse connection
"""
self.sql.close()