~ubuntu-branches/ubuntu/lucid/pytagsfs/lucid

« back to all changes in this revision

Viewing changes to modules/pytagsfs/file.py

  • Committer: Bazaar Package Importer
  • Author(s): Y Giridhar Appaji Nag
  • Date: 2009-01-28 22:11:56 UTC
  • mfrom: (1.1.3 upstream) (3.1.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090128221156-tbhq0bau1tke233i
Tags: 0.9.0~rc1-1
* New upstream release
  + Needs python-fuse to build add to Build-Depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
 
 
3
from threading import RLock
 
4
 
 
5
from pytagsfs.exceptions import (
 
6
  PathNotFound,
 
7
  InvalidArgument,
 
8
)
 
9
from pytagsfs.util import (
 
10
  nonatomic,
 
11
  attr_named,
 
12
)
 
13
 
 
14
 
 
15
class File(object):
 
16
    _lock = None
 
17
    filesystem = None
 
18
    fake_path = None
 
19
    real_path = None
 
20
    flags = None
 
21
    truncate_to = None
 
22
 
 
23
    def __init__(
 
24
      self,
 
25
      filesystem,
 
26
      fake_path,
 
27
      flags,
 
28
      truncate_to = None,
 
29
    ):
 
30
        self._lock = RLock()
 
31
        self.filesystem = filesystem
 
32
        self.fake_path = fake_path
 
33
        self.real_path = filesystem.source_tree_rep.get_real_path(fake_path)
 
34
        self.flags = flags
 
35
        self.truncate_to = truncate_to
 
36
 
 
37
    def fgetattr(self):
 
38
        raise NotImplementedError
 
39
 
 
40
    def flush(self):
 
41
        raise NotImplementedError
 
42
 
 
43
    def fsync(self, datasync):
 
44
        raise NotImplementedError
 
45
 
 
46
    def ftruncate(self, len):
 
47
        raise NotImplementedError
 
48
 
 
49
    def read(self, length, offset):
 
50
        raise NotImplementedError
 
51
 
 
52
    def release(self, flags):
 
53
        pass
 
54
 
 
55
    def write(self, buf, offset):
 
56
        raise NotImplementedError
 
57
 
 
58
 
 
59
class ReadOnlyFile(File):
 
60
    fd = None
 
61
    file = None
 
62
 
 
63
    def __init__(self, *args, **kwargs):
 
64
        super(ReadOnlyFile, self).__init__(*args, **kwargs)
 
65
        self.open_file()
 
66
 
 
67
    @nonatomic(attr_named('_lock'))
 
68
    def open_file(self):
 
69
        real_path = self.filesystem.encode_real_path(self.real_path)
 
70
        self.file = os.fdopen(os.open(real_path, self.flags), 'r')
 
71
 
 
72
################################################################################
 
73
 
 
74
    @nonatomic(attr_named('_lock'))
 
75
    def fgetattr(self):
 
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((
 
81
          stat_result.st_mode,
 
82
          stat_result.st_ino,
 
83
          stat_result.st_dev,
 
84
          stat_result.st_nlink,
 
85
          stat_result.st_uid,
 
86
          stat_result.st_gid,
 
87
          st_size,
 
88
          stat_result.st_atime,
 
89
          stat_result.st_mtime,
 
90
          stat_result.st_ctime,
 
91
        ))
 
92
 
 
93
    def ftruncate(self, len):
 
94
        raise InvalidArgument
 
95
 
 
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
 
100
            if length < 0:
 
101
                length = 0
 
102
        self.file.seek(offset)
 
103
        return self.file.read(length)
 
104
 
 
105
    @nonatomic(attr_named('_lock'))
 
106
    def release(self, flags):
 
107
        self.file.close()
 
108
 
 
109
    def write(self, buf, offset):
 
110
        raise InvalidArgument
 
111
 
 
112
 
 
113
class ReadWriteFile(File):
 
114
    fd = None
 
115
 
 
116
    def __init__(self, *args, **kwargs):
 
117
        super(ReadWriteFile, self).__init__(*args, **kwargs)
 
118
        self.open_file()
 
119
 
 
120
    @nonatomic(attr_named('_lock'))
 
121
    def open_file(self):
 
122
        real_path = self.filesystem.encode_real_path(self.real_path)
 
123
        self.fd = os.open(real_path, self.flags)
 
124
 
 
125
        if self.truncate_to is not None:
 
126
            os.ftruncate(self.fd, self.truncate_to)
 
127
 
 
128
################################################################################
 
129
 
 
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)
 
134
 
 
135
    @nonatomic(attr_named('_lock'))
 
136
    def release(self, flags):
 
137
        return os.close(self.fd)
 
138
 
 
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)
 
143
 
 
144
    @nonatomic(attr_named('_lock'))
 
145
    def fgetattr(self):
 
146
        return os.fstat(self.fd)
 
147
 
 
148
    @nonatomic(attr_named('_lock'))
 
149
    def flush(self):
 
150
        return os.close(os.dup(self.fd))
 
151
 
 
152
    @nonatomic(attr_named('_lock'))
 
153
    def fsync(self, datasync):
 
154
        if datasync and hasattr(os, 'fdatasync'):
 
155
            return os.fdatasync(self.fd)
 
156
        else:
 
157
            return os.fsync(self.fd)
 
158
 
 
159
    @nonatomic(attr_named('_lock'))
 
160
    def ftruncate(self, len):
 
161
        return os.ftruncate(self.fd, len)