blob: 2cd416873e539c00b460265a707288390756e664 [file] [log] [blame]
Guido van Rossum78f8dea1994-05-18 11:07:44 +00001#
2# Start of posixfile.py
3#
4
5#
6# Extended file operations
7#
Guido van Rossum28aa2291995-03-23 10:39:49 +00008# f = posixfile.open(filename, [mode, [bufsize]])
Guido van Rossum45e2fbc1998-03-26 21:13:24 +00009# will create a new posixfile object
Guido van Rossumc762bec1994-05-18 11:08:10 +000010#
11# f = posixfile.fileopen(fileobject)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000012# will create a posixfile object from a builtin file object
Guido van Rossum78f8dea1994-05-18 11:07:44 +000013#
14# f.file()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000015# will return the original builtin file object
Guido van Rossum78f8dea1994-05-18 11:07:44 +000016#
17# f.dup()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000018# will return a new file object based on a new filedescriptor
Guido van Rossum78f8dea1994-05-18 11:07:44 +000019#
20# f.dup2(fd)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000021# will return a new file object based on the given filedescriptor
Guido van Rossum78f8dea1994-05-18 11:07:44 +000022#
23# f.flags(mode)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000024# will turn on the associated flag (merge)
25# mode can contain the following characters:
Guido van Rossum78f8dea1994-05-18 11:07:44 +000026#
27# (character representing a flag)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000028# a append only flag
29# c close on exec flag
30# n no delay flag
31# s synchronization flag
Guido van Rossum78f8dea1994-05-18 11:07:44 +000032# (modifiers)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000033# ! turn flags 'off' instead of default 'on'
34# = copy flags 'as is' instead of default 'merge'
35# ? return a string in which the characters represent the flags
36# that are set
Guido van Rossum78f8dea1994-05-18 11:07:44 +000037#
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000038# note: - the '!' and '=' modifiers are mutually exclusive.
39# - the '?' modifier will return the status of the flags after they
40# have been changed by other characters in the mode string
Guido van Rossum78f8dea1994-05-18 11:07:44 +000041#
42# f.lock(mode [, len [, start [, whence]]])
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000043# will (un)lock a region
44# mode can contain the following characters:
Guido van Rossum78f8dea1994-05-18 11:07:44 +000045#
46# (character representing type of lock)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000047# u unlock
48# r read lock
49# w write lock
Guido van Rossum78f8dea1994-05-18 11:07:44 +000050# (modifiers)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000051# | wait until the lock can be granted
52# ? return the first lock conflicting with the requested lock
53# or 'None' if there is no conflict. The lock returned is in the
54# format (mode, len, start, whence, pid) where mode is a
55# character representing the type of lock ('r' or 'w')
Guido van Rossum78f8dea1994-05-18 11:07:44 +000056#
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000057# note: - the '?' modifier prevents a region from being locked; it is
58# query only
Guido van Rossum78f8dea1994-05-18 11:07:44 +000059#
60
61class _posixfile_:
Guido van Rossumc762bec1994-05-18 11:08:10 +000062 states = ['open', 'closed']
63
Guido van Rossum78f8dea1994-05-18 11:07:44 +000064 #
65 # Internal routines
66 #
67 def __repr__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000068 file = self._file_
69 return "<%s posixfile '%s', mode '%s' at %s>" % \
70 (self.states[file.closed], file.name, file.mode, \
71 hex(id(self))[2:])
Guido van Rossum78f8dea1994-05-18 11:07:44 +000072
73 def __del__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000074 self._file_.close()
Guido van Rossum78f8dea1994-05-18 11:07:44 +000075
76 #
77 # Initialization routines
78 #
Guido van Rossum13c503e1995-03-16 15:58:12 +000079 def open(self, name, mode='r', bufsize=-1):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000080 import __builtin__
81 return self.fileopen(__builtin__.open(name, mode, bufsize))
Guido van Rossum78f8dea1994-05-18 11:07:44 +000082
Guido van Rossumc762bec1994-05-18 11:08:10 +000083 def fileopen(self, file):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000084 if `type(file)` != "<type 'file'>":
85 raise TypeError, 'posixfile.fileopen() arg must be file object'
86 self._file_ = file
87 # Copy basic file methods
88 for method in file.__methods__:
89 setattr(self, method, getattr(file, method))
90 return self
Guido van Rossum78f8dea1994-05-18 11:07:44 +000091
92 #
93 # New methods
94 #
95 def file(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000096 return self._file_
Guido van Rossum78f8dea1994-05-18 11:07:44 +000097
98 def dup(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000099 import posix
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000100
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000101 try: ignore = posix.fdopen
102 except: raise AttributeError, 'dup() method unavailable'
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000103
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000104 return posix.fdopen(posix.dup(self._file_.fileno()), self._file_.mode)
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000105
106 def dup2(self, fd):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000107 import posix
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000108
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000109 try: ignore = posix.fdopen
110 except: raise AttributeError, 'dup() method unavailable'
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000111
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000112 posix.dup2(self._file_.fileno(), fd)
113 return posix.fdopen(fd, self._file_.mode)
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000114
Guido van Rossumc762bec1994-05-18 11:08:10 +0000115 def flags(self, *which):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000116 import fcntl, FCNTL
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000117
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000118 if which:
119 if len(which) > 1:
120 raise TypeError, 'Too many arguments'
121 which = which[0]
122 else: which = '?'
Guido van Rossumc762bec1994-05-18 11:08:10 +0000123
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000124 l_flags = 0
125 if 'n' in which: l_flags = l_flags | FCNTL.O_NDELAY
126 if 'a' in which: l_flags = l_flags | FCNTL.O_APPEND
127 if 's' in which: l_flags = l_flags | FCNTL.O_SYNC
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000128
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000129 file = self._file_
Guido van Rossumc762bec1994-05-18 11:08:10 +0000130
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000131 if '=' not in which:
132 cur_fl = fcntl.fcntl(file.fileno(), FCNTL.F_GETFL, 0)
133 if '!' in which: l_flags = cur_fl & ~ l_flags
134 else: l_flags = cur_fl | l_flags
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000135
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000136 l_flags = fcntl.fcntl(file.fileno(), FCNTL.F_SETFL, l_flags)
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000137
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000138 if 'c' in which:
139 arg = ('!' not in which) # 0 is don't, 1 is do close on exec
140 l_flags = fcntl.fcntl(file.fileno(), FCNTL.F_SETFD, arg)
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000141
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000142 if '?' in which:
143 which = '' # Return current flags
144 l_flags = fcntl.fcntl(file.fileno(), FCNTL.F_GETFL, 0)
145 if FCNTL.O_APPEND & l_flags: which = which + 'a'
146 if fcntl.fcntl(file.fileno(), FCNTL.F_GETFD, 0) & 1:
147 which = which + 'c'
148 if FCNTL.O_NDELAY & l_flags: which = which + 'n'
149 if FCNTL.O_SYNC & l_flags: which = which + 's'
150 return which
151
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000152 def lock(self, how, *args):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000153 import struct, fcntl, FCNTL
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000154
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000155 if 'w' in how: l_type = FCNTL.F_WRLCK
156 elif 'r' in how: l_type = FCNTL.F_RDLCK
157 elif 'u' in how: l_type = FCNTL.F_UNLCK
158 else: raise TypeError, 'no type of lock specified'
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000159
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000160 if '|' in how: cmd = FCNTL.F_SETLKW
161 elif '?' in how: cmd = FCNTL.F_GETLK
162 else: cmd = FCNTL.F_SETLK
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000163
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000164 l_whence = 0
165 l_start = 0
166 l_len = 0
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000167
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000168 if len(args) == 1:
169 l_len = args[0]
170 elif len(args) == 2:
171 l_len, l_start = args
172 elif len(args) == 3:
173 l_len, l_start, l_whence = args
174 elif len(args) > 3:
175 raise TypeError, 'too many arguments'
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000176
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000177 # Hack by davem@magnet.com to get locking to go on freebsd;
178 # additions for AIX by Vladimir.Marangozov@imag.fr
Guido van Rossum7698d121996-07-30 16:28:45 +0000179 import sys, os
Guido van Rossum027188a1999-02-23 04:14:32 +0000180 if sys.platform in ('netbsd1',
181 'freebsd2', 'freebsd3',
182 'bsdos2', 'bsdos3', 'bsdos4'):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000183 flock = struct.pack('lxxxxlxxxxlhh', \
184 l_start, l_len, os.getpid(), l_type, l_whence)
Guido van Rossum0f6a3bf1996-08-20 20:23:34 +0000185 elif sys.platform in ['aix3', 'aix4']:
186 flock = struct.pack('hhlllii', \
187 l_type, l_whence, l_start, l_len, 0, 0, 0)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000188 else:
189 flock = struct.pack('hhllhh', \
190 l_type, l_whence, l_start, l_len, 0, 0)
Guido van Rossum7698d121996-07-30 16:28:45 +0000191
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000192 flock = fcntl.fcntl(self._file_.fileno(), cmd, flock)
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000193
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000194 if '?' in how:
Guido van Rossum027188a1999-02-23 04:14:32 +0000195 if sys.platform in ('netbsd1',
196 'freebsd2', 'freebsd3',
197 'bsdos2', 'bsdos3', 'bsdos4'):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000198 l_start, l_len, l_pid, l_type, l_whence = \
199 struct.unpack('lxxxxlxxxxlhh', flock)
Guido van Rossum0f6a3bf1996-08-20 20:23:34 +0000200 elif sys.platform in ['aix3', 'aix4']:
201 l_type, l_whence, l_start, l_len, l_sysid, l_pid, l_vfs = \
202 struct.unpack('hhlllii', flock)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000203 elif sys.platform == "linux2":
204 l_type, l_whence, l_start, l_len, l_pid, l_sysid = \
205 struct.unpack('hhllhh', flock)
206 else:
207 l_type, l_whence, l_start, l_len, l_sysid, l_pid = \
208 struct.unpack('hhllhh', flock)
Guido van Rossum7698d121996-07-30 16:28:45 +0000209
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000210 if l_type != FCNTL.F_UNLCK:
211 if l_type == FCNTL.F_RDLCK:
212 return 'r', l_len, l_start, l_whence, l_pid
213 else:
214 return 'w', l_len, l_start, l_whence, l_pid
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000215
216#
217# Public routine to obtain a posixfile object
218#
Guido van Rossum28aa2291995-03-23 10:39:49 +0000219def open(name, mode='r', bufsize=-1):
220 return _posixfile_().open(name, mode, bufsize)
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000221
Guido van Rossumc762bec1994-05-18 11:08:10 +0000222def fileopen(file):
223 return _posixfile_().fileopen(file)
224
225#
226# Constants
227#
228SEEK_SET = 0
229SEEK_CUR = 1
230SEEK_END = 2
231
Guido van Rossum78f8dea1994-05-18 11:07:44 +0000232#
233# End of posixfile.py
234#