3
from threading import RLock
5
from pytagsfs.exceptions import (
9
from pytagsfs.util import (
31
self.filesystem = filesystem
32
self.fake_path = fake_path
33
self.real_path = filesystem.source_tree_rep.get_real_path(fake_path)
35
self.truncate_to = truncate_to
38
raise NotImplementedError
41
raise NotImplementedError
43
def fsync(self, datasync):
44
raise NotImplementedError
46
def ftruncate(self, len):
47
raise NotImplementedError
49
def read(self, length, offset):
50
raise NotImplementedError
52
def release(self, flags):
55
def write(self, buf, offset):
56
raise NotImplementedError
59
class ReadOnlyFile(File):
63
def __init__(self, *args, **kwargs):
64
super(ReadOnlyFile, self).__init__(*args, **kwargs)
67
@nonatomic(attr_named('_lock'))
69
real_path = self.filesystem.encode_real_path(self.real_path)
70
self.file = os.fdopen(os.open(real_path, self.flags), 'r')
72
################################################################################
74
@nonatomic(attr_named('_lock'))
76
stat_result = os.fstat(self.file.fileno())
77
st_size = stat_result.st_size
78
if (self.truncate_to is not None) and (st_size > self.truncate_to):
79
st_size = self.truncate_to
80
return os.stat_result((
93
def ftruncate(self, len):
96
@nonatomic(attr_named('_lock'))
97
def read(self, length, offset):
98
if self.truncate_to is not None:
99
length = self.truncate_to - offset
102
self.file.seek(offset)
103
return self.file.read(length)
105
@nonatomic(attr_named('_lock'))
106
def release(self, flags):
109
def write(self, buf, offset):
110
raise InvalidArgument
113
class ReadWriteFile(File):
116
def __init__(self, *args, **kwargs):
117
super(ReadWriteFile, self).__init__(*args, **kwargs)
120
@nonatomic(attr_named('_lock'))
122
real_path = self.filesystem.encode_real_path(self.real_path)
123
self.fd = os.open(real_path, self.flags)
125
if self.truncate_to is not None:
126
os.ftruncate(self.fd, self.truncate_to)
128
################################################################################
130
@nonatomic(attr_named('_lock'))
131
def read(self, length, offset):
132
os.lseek(self.fd, offset, 0)
133
return os.read(self.fd, length)
135
@nonatomic(attr_named('_lock'))
136
def release(self, flags):
137
return os.close(self.fd)
139
@nonatomic(attr_named('_lock'))
140
def write(self, buf, offset):
141
os.lseek(self.fd, offset, 0)
142
return os.write(self.fd, buf)
144
@nonatomic(attr_named('_lock'))
146
return os.fstat(self.fd)
148
@nonatomic(attr_named('_lock'))
150
return os.close(os.dup(self.fd))
152
@nonatomic(attr_named('_lock'))
153
def fsync(self, datasync):
154
if datasync and hasattr(os, 'fdatasync'):
155
return os.fdatasync(self.fd)
157
return os.fsync(self.fd)
159
@nonatomic(attr_named('_lock'))
160
def ftruncate(self, len):
161
return os.ftruncate(self.fd, len)