Revision c3ec3b3b

b/snf-pithos-backend/pithos/backends/lib/hashfiler/context_file.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
from os import SEEK_CUR, SEEK_SET
35
from errno import ENOENT, EROFS
36

  
34
from os import (
35
    SEEK_CUR,
36
    SEEK_SET,
37
    O_RDONLY,
38
    O_WRONLY,
39
    O_RDWR
40
)
37 41

  
38 42
_zeros = ''
39 43

  
......
131 135

  
132 136

  
133 137
class ContextFile(object):
134
    __slots__ = ("name", "fdesc", "create")
138
    __slots__ = ("name", "fdesc", "oflag")
135 139

  
136
    def __init__(self, name, create=0):
140
    def __init__(self, name, oflag):
137 141
        self.name = name
138 142
        self.fdesc = None
139
        self.create = create
143
        self.oflag = oflag
140 144
        #self.dirty = 0
141 145

  
142 146
    def __enter__(self):
143 147
        name = self.name
144
        try:
145
            fdesc = open(name, 'rb+')
146
        except IOError, e:
147
            if self.create and e.errno == ENOENT:
148
                fdesc = open(name, 'w+')
149
            elif not self.create and e.errno == EROFS:
150
                fdesc = open(name, 'rb')
151
            else:
152
                raise
148
        if self.oflag == O_RDONLY:
149
            fdesc = open(name, 'rb')
150
        elif self.oflag == O_WRONLY:
151
            fdesc = open(name, 'wb')
152
        elif self.oflag == O_RDWR:
153
            fdesc = open(name, 'wb+')
154
        else:
155
            raise Exception("Wrong file acccess mode.")
153 156

  
154 157
        self.fdesc = fdesc
155 158
        return self

Also available in: Unified diff