##// END OF EJS Templates
py3: make sure return value of posix.groupname() is bytes...
Pulkit Goyal -
r41993:faa04f45 default
parent child Browse files
Show More
@@ -1,706 +1,706
1 1 # posix.py - Posix utility function implementations for Mercurial
2 2 #
3 3 # Copyright 2005-2009 Matt Mackall <mpm@selenic.com> and others
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from __future__ import absolute_import
9 9
10 10 import errno
11 11 import fcntl
12 12 import getpass
13 13 import grp
14 14 import os
15 15 import pwd
16 16 import re
17 17 import select
18 18 import stat
19 19 import sys
20 20 import tempfile
21 21 import unicodedata
22 22
23 23 from .i18n import _
24 24 from . import (
25 25 encoding,
26 26 error,
27 27 policy,
28 28 pycompat,
29 29 )
30 30
31 31 osutil = policy.importmod(r'osutil')
32 32
33 33 posixfile = open
34 34 normpath = os.path.normpath
35 35 samestat = os.path.samestat
36 36 try:
37 37 oslink = os.link
38 38 except AttributeError:
39 39 # Some platforms build Python without os.link on systems that are
40 40 # vaguely unix-like but don't have hardlink support. For those
41 41 # poor souls, just say we tried and that it failed so we fall back
42 42 # to copies.
43 43 def oslink(src, dst):
44 44 raise OSError(errno.EINVAL,
45 45 'hardlinks not supported: %s to %s' % (src, dst))
46 46 readlink = os.readlink
47 47 unlink = os.unlink
48 48 rename = os.rename
49 49 removedirs = os.removedirs
50 50 expandglobs = False
51 51
52 52 umask = os.umask(0)
53 53 os.umask(umask)
54 54
55 55 def split(p):
56 56 '''Same as posixpath.split, but faster
57 57
58 58 >>> import posixpath
59 59 >>> for f in [b'/absolute/path/to/file',
60 60 ... b'relative/path/to/file',
61 61 ... b'file_alone',
62 62 ... b'path/to/directory/',
63 63 ... b'/multiple/path//separators',
64 64 ... b'/file_at_root',
65 65 ... b'///multiple_leading_separators_at_root',
66 66 ... b'']:
67 67 ... assert split(f) == posixpath.split(f), f
68 68 '''
69 69 ht = p.rsplit('/', 1)
70 70 if len(ht) == 1:
71 71 return '', p
72 72 nh = ht[0].rstrip('/')
73 73 if nh:
74 74 return nh, ht[1]
75 75 return ht[0] + '/', ht[1]
76 76
77 77 def openhardlinks():
78 78 '''return true if it is safe to hold open file handles to hardlinks'''
79 79 return True
80 80
81 81 def nlinks(name):
82 82 '''return number of hardlinks for the given file'''
83 83 return os.lstat(name).st_nlink
84 84
85 85 def parsepatchoutput(output_line):
86 86 """parses the output produced by patch and returns the filename"""
87 87 pf = output_line[14:]
88 88 if pycompat.sysplatform == 'OpenVMS':
89 89 if pf[0] == '`':
90 90 pf = pf[1:-1] # Remove the quotes
91 91 else:
92 92 if pf.startswith("'") and pf.endswith("'") and " " in pf:
93 93 pf = pf[1:-1] # Remove the quotes
94 94 return pf
95 95
96 96 def sshargs(sshcmd, host, user, port):
97 97 '''Build argument list for ssh'''
98 98 args = user and ("%s@%s" % (user, host)) or host
99 99 if '-' in args[:1]:
100 100 raise error.Abort(
101 101 _('illegal ssh hostname or username starting with -: %s') % args)
102 102 args = shellquote(args)
103 103 if port:
104 104 args = '-p %s %s' % (shellquote(port), args)
105 105 return args
106 106
107 107 def isexec(f):
108 108 """check whether a file is executable"""
109 109 return (os.lstat(f).st_mode & 0o100 != 0)
110 110
111 111 def setflags(f, l, x):
112 112 st = os.lstat(f)
113 113 s = st.st_mode
114 114 if l:
115 115 if not stat.S_ISLNK(s):
116 116 # switch file to link
117 117 fp = open(f, 'rb')
118 118 data = fp.read()
119 119 fp.close()
120 120 unlink(f)
121 121 try:
122 122 os.symlink(data, f)
123 123 except OSError:
124 124 # failed to make a link, rewrite file
125 125 fp = open(f, "wb")
126 126 fp.write(data)
127 127 fp.close()
128 128 # no chmod needed at this point
129 129 return
130 130 if stat.S_ISLNK(s):
131 131 # switch link to file
132 132 data = os.readlink(f)
133 133 unlink(f)
134 134 fp = open(f, "wb")
135 135 fp.write(data)
136 136 fp.close()
137 137 s = 0o666 & ~umask # avoid restatting for chmod
138 138
139 139 sx = s & 0o100
140 140 if st.st_nlink > 1 and bool(x) != bool(sx):
141 141 # the file is a hardlink, break it
142 142 with open(f, "rb") as fp:
143 143 data = fp.read()
144 144 unlink(f)
145 145 with open(f, "wb") as fp:
146 146 fp.write(data)
147 147
148 148 if x and not sx:
149 149 # Turn on +x for every +r bit when making a file executable
150 150 # and obey umask.
151 151 os.chmod(f, s | (s & 0o444) >> 2 & ~umask)
152 152 elif not x and sx:
153 153 # Turn off all +x bits
154 154 os.chmod(f, s & 0o666)
155 155
156 156 def copymode(src, dst, mode=None, enforcewritable=False):
157 157 '''Copy the file mode from the file at path src to dst.
158 158 If src doesn't exist, we're using mode instead. If mode is None, we're
159 159 using umask.'''
160 160 try:
161 161 st_mode = os.lstat(src).st_mode & 0o777
162 162 except OSError as inst:
163 163 if inst.errno != errno.ENOENT:
164 164 raise
165 165 st_mode = mode
166 166 if st_mode is None:
167 167 st_mode = ~umask
168 168 st_mode &= 0o666
169 169
170 170 new_mode = st_mode
171 171
172 172 if enforcewritable:
173 173 new_mode |= stat.S_IWUSR
174 174
175 175 os.chmod(dst, new_mode)
176 176
177 177 def checkexec(path):
178 178 """
179 179 Check whether the given path is on a filesystem with UNIX-like exec flags
180 180
181 181 Requires a directory (like /foo/.hg)
182 182 """
183 183
184 184 # VFAT on some Linux versions can flip mode but it doesn't persist
185 185 # a FS remount. Frequently we can detect it if files are created
186 186 # with exec bit on.
187 187
188 188 try:
189 189 EXECFLAGS = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
190 190 basedir = os.path.join(path, '.hg')
191 191 cachedir = os.path.join(basedir, 'wcache')
192 192 storedir = os.path.join(basedir, 'store')
193 193 if not os.path.exists(cachedir):
194 194 try:
195 195 # we want to create the 'cache' directory, not the '.hg' one.
196 196 # Automatically creating '.hg' directory could silently spawn
197 197 # invalid Mercurial repositories. That seems like a bad idea.
198 198 os.mkdir(cachedir)
199 199 if os.path.exists(storedir):
200 200 copymode(storedir, cachedir)
201 201 else:
202 202 copymode(basedir, cachedir)
203 203 except (IOError, OSError):
204 204 # we other fallback logic triggers
205 205 pass
206 206 if os.path.isdir(cachedir):
207 207 checkisexec = os.path.join(cachedir, 'checkisexec')
208 208 checknoexec = os.path.join(cachedir, 'checknoexec')
209 209
210 210 try:
211 211 m = os.stat(checkisexec).st_mode
212 212 except OSError as e:
213 213 if e.errno != errno.ENOENT:
214 214 raise
215 215 # checkisexec does not exist - fall through ...
216 216 else:
217 217 # checkisexec exists, check if it actually is exec
218 218 if m & EXECFLAGS != 0:
219 219 # ensure checkisexec exists, check it isn't exec
220 220 try:
221 221 m = os.stat(checknoexec).st_mode
222 222 except OSError as e:
223 223 if e.errno != errno.ENOENT:
224 224 raise
225 225 open(checknoexec, 'w').close() # might fail
226 226 m = os.stat(checknoexec).st_mode
227 227 if m & EXECFLAGS == 0:
228 228 # check-exec is exec and check-no-exec is not exec
229 229 return True
230 230 # checknoexec exists but is exec - delete it
231 231 unlink(checknoexec)
232 232 # checkisexec exists but is not exec - delete it
233 233 unlink(checkisexec)
234 234
235 235 # check using one file, leave it as checkisexec
236 236 checkdir = cachedir
237 237 else:
238 238 # check directly in path and don't leave checkisexec behind
239 239 checkdir = path
240 240 checkisexec = None
241 241 fh, fn = pycompat.mkstemp(dir=checkdir, prefix='hg-checkexec-')
242 242 try:
243 243 os.close(fh)
244 244 m = os.stat(fn).st_mode
245 245 if m & EXECFLAGS == 0:
246 246 os.chmod(fn, m & 0o777 | EXECFLAGS)
247 247 if os.stat(fn).st_mode & EXECFLAGS != 0:
248 248 if checkisexec is not None:
249 249 os.rename(fn, checkisexec)
250 250 fn = None
251 251 return True
252 252 finally:
253 253 if fn is not None:
254 254 unlink(fn)
255 255 except (IOError, OSError):
256 256 # we don't care, the user probably won't be able to commit anyway
257 257 return False
258 258
259 259 def checklink(path):
260 260 """check whether the given path is on a symlink-capable filesystem"""
261 261 # mktemp is not racy because symlink creation will fail if the
262 262 # file already exists
263 263 while True:
264 264 cachedir = os.path.join(path, '.hg', 'wcache')
265 265 checklink = os.path.join(cachedir, 'checklink')
266 266 # try fast path, read only
267 267 if os.path.islink(checklink):
268 268 return True
269 269 if os.path.isdir(cachedir):
270 270 checkdir = cachedir
271 271 else:
272 272 checkdir = path
273 273 cachedir = None
274 274 name = tempfile.mktemp(dir=pycompat.fsdecode(checkdir),
275 275 prefix=r'checklink-')
276 276 name = pycompat.fsencode(name)
277 277 try:
278 278 fd = None
279 279 if cachedir is None:
280 280 fd = pycompat.namedtempfile(dir=checkdir,
281 281 prefix='hg-checklink-')
282 282 target = os.path.basename(fd.name)
283 283 else:
284 284 # create a fixed file to link to; doesn't matter if it
285 285 # already exists.
286 286 target = 'checklink-target'
287 287 try:
288 288 fullpath = os.path.join(cachedir, target)
289 289 open(fullpath, 'w').close()
290 290 except IOError as inst:
291 291 if inst[0] == errno.EACCES:
292 292 # If we can't write to cachedir, just pretend
293 293 # that the fs is readonly and by association
294 294 # that the fs won't support symlinks. This
295 295 # seems like the least dangerous way to avoid
296 296 # data loss.
297 297 return False
298 298 raise
299 299 try:
300 300 os.symlink(target, name)
301 301 if cachedir is None:
302 302 unlink(name)
303 303 else:
304 304 try:
305 305 os.rename(name, checklink)
306 306 except OSError:
307 307 unlink(name)
308 308 return True
309 309 except OSError as inst:
310 310 # link creation might race, try again
311 311 if inst.errno == errno.EEXIST:
312 312 continue
313 313 raise
314 314 finally:
315 315 if fd is not None:
316 316 fd.close()
317 317 except AttributeError:
318 318 return False
319 319 except OSError as inst:
320 320 # sshfs might report failure while successfully creating the link
321 321 if inst.errno == errno.EIO and os.path.exists(name):
322 322 unlink(name)
323 323 return False
324 324
325 325 def checkosfilename(path):
326 326 '''Check that the base-relative path is a valid filename on this platform.
327 327 Returns None if the path is ok, or a UI string describing the problem.'''
328 328 return None # on posix platforms, every path is ok
329 329
330 330 def getfsmountpoint(dirpath):
331 331 '''Get the filesystem mount point from a directory (best-effort)
332 332
333 333 Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc.
334 334 '''
335 335 return getattr(osutil, 'getfsmountpoint', lambda x: None)(dirpath)
336 336
337 337 def getfstype(dirpath):
338 338 '''Get the filesystem type name from a directory (best-effort)
339 339
340 340 Returns None if we are unsure. Raises OSError on ENOENT, EPERM, etc.
341 341 '''
342 342 return getattr(osutil, 'getfstype', lambda x: None)(dirpath)
343 343
344 344 def setbinary(fd):
345 345 pass
346 346
347 347 def pconvert(path):
348 348 return path
349 349
350 350 def localpath(path):
351 351 return path
352 352
353 353 def samefile(fpath1, fpath2):
354 354 """Returns whether path1 and path2 refer to the same file. This is only
355 355 guaranteed to work for files, not directories."""
356 356 return os.path.samefile(fpath1, fpath2)
357 357
358 358 def samedevice(fpath1, fpath2):
359 359 """Returns whether fpath1 and fpath2 are on the same device. This is only
360 360 guaranteed to work for files, not directories."""
361 361 st1 = os.lstat(fpath1)
362 362 st2 = os.lstat(fpath2)
363 363 return st1.st_dev == st2.st_dev
364 364
365 365 # os.path.normcase is a no-op, which doesn't help us on non-native filesystems
366 366 def normcase(path):
367 367 return path.lower()
368 368
369 369 # what normcase does to ASCII strings
370 370 normcasespec = encoding.normcasespecs.lower
371 371 # fallback normcase function for non-ASCII strings
372 372 normcasefallback = normcase
373 373
374 374 if pycompat.isdarwin:
375 375
376 376 def normcase(path):
377 377 '''
378 378 Normalize a filename for OS X-compatible comparison:
379 379 - escape-encode invalid characters
380 380 - decompose to NFD
381 381 - lowercase
382 382 - omit ignored characters [200c-200f, 202a-202e, 206a-206f,feff]
383 383
384 384 >>> normcase(b'UPPER')
385 385 'upper'
386 386 >>> normcase(b'Caf\\xc3\\xa9')
387 387 'cafe\\xcc\\x81'
388 388 >>> normcase(b'\\xc3\\x89')
389 389 'e\\xcc\\x81'
390 390 >>> normcase(b'\\xb8\\xca\\xc3\\xca\\xbe\\xc8.JPG') # issue3918
391 391 '%b8%ca%c3\\xca\\xbe%c8.jpg'
392 392 '''
393 393
394 394 try:
395 395 return encoding.asciilower(path) # exception for non-ASCII
396 396 except UnicodeDecodeError:
397 397 return normcasefallback(path)
398 398
399 399 normcasespec = encoding.normcasespecs.lower
400 400
401 401 def normcasefallback(path):
402 402 try:
403 403 u = path.decode('utf-8')
404 404 except UnicodeDecodeError:
405 405 # OS X percent-encodes any bytes that aren't valid utf-8
406 406 s = ''
407 407 pos = 0
408 408 l = len(path)
409 409 while pos < l:
410 410 try:
411 411 c = encoding.getutf8char(path, pos)
412 412 pos += len(c)
413 413 except ValueError:
414 414 c = '%%%02X' % ord(path[pos:pos + 1])
415 415 pos += 1
416 416 s += c
417 417
418 418 u = s.decode('utf-8')
419 419
420 420 # Decompose then lowercase (HFS+ technote specifies lower)
421 421 enc = unicodedata.normalize(r'NFD', u).lower().encode('utf-8')
422 422 # drop HFS+ ignored characters
423 423 return encoding.hfsignoreclean(enc)
424 424
425 425 if pycompat.sysplatform == 'cygwin':
426 426 # workaround for cygwin, in which mount point part of path is
427 427 # treated as case sensitive, even though underlying NTFS is case
428 428 # insensitive.
429 429
430 430 # default mount points
431 431 cygwinmountpoints = sorted([
432 432 "/usr/bin",
433 433 "/usr/lib",
434 434 "/cygdrive",
435 435 ], reverse=True)
436 436
437 437 # use upper-ing as normcase as same as NTFS workaround
438 438 def normcase(path):
439 439 pathlen = len(path)
440 440 if (pathlen == 0) or (path[0] != pycompat.ossep):
441 441 # treat as relative
442 442 return encoding.upper(path)
443 443
444 444 # to preserve case of mountpoint part
445 445 for mp in cygwinmountpoints:
446 446 if not path.startswith(mp):
447 447 continue
448 448
449 449 mplen = len(mp)
450 450 if mplen == pathlen: # mount point itself
451 451 return mp
452 452 if path[mplen] == pycompat.ossep:
453 453 return mp + encoding.upper(path[mplen:])
454 454
455 455 return encoding.upper(path)
456 456
457 457 normcasespec = encoding.normcasespecs.other
458 458 normcasefallback = normcase
459 459
460 460 # Cygwin translates native ACLs to POSIX permissions,
461 461 # but these translations are not supported by native
462 462 # tools, so the exec bit tends to be set erroneously.
463 463 # Therefore, disable executable bit access on Cygwin.
464 464 def checkexec(path):
465 465 return False
466 466
467 467 # Similarly, Cygwin's symlink emulation is likely to create
468 468 # problems when Mercurial is used from both Cygwin and native
469 469 # Windows, with other native tools, or on shared volumes
470 470 def checklink(path):
471 471 return False
472 472
473 473 _needsshellquote = None
474 474 def shellquote(s):
475 475 if pycompat.sysplatform == 'OpenVMS':
476 476 return '"%s"' % s
477 477 global _needsshellquote
478 478 if _needsshellquote is None:
479 479 _needsshellquote = re.compile(br'[^a-zA-Z0-9._/+-]').search
480 480 if s and not _needsshellquote(s):
481 481 # "s" shouldn't have to be quoted
482 482 return s
483 483 else:
484 484 return "'%s'" % s.replace("'", "'\\''")
485 485
486 486 def shellsplit(s):
487 487 """Parse a command string in POSIX shell way (best-effort)"""
488 488 return pycompat.shlexsplit(s, posix=True)
489 489
490 490 def quotecommand(cmd):
491 491 return cmd
492 492
493 493 def testpid(pid):
494 494 '''return False if pid dead, True if running or not sure'''
495 495 if pycompat.sysplatform == 'OpenVMS':
496 496 return True
497 497 try:
498 498 os.kill(pid, 0)
499 499 return True
500 500 except OSError as inst:
501 501 return inst.errno != errno.ESRCH
502 502
503 503 def isowner(st):
504 504 """Return True if the stat object st is from the current user."""
505 505 return st.st_uid == os.getuid()
506 506
507 507 def findexe(command):
508 508 '''Find executable for command searching like which does.
509 509 If command is a basename then PATH is searched for command.
510 510 PATH isn't searched if command is an absolute or relative path.
511 511 If command isn't found None is returned.'''
512 512 if pycompat.sysplatform == 'OpenVMS':
513 513 return command
514 514
515 515 def findexisting(executable):
516 516 'Will return executable if existing file'
517 517 if os.path.isfile(executable) and os.access(executable, os.X_OK):
518 518 return executable
519 519 return None
520 520
521 521 if pycompat.ossep in command:
522 522 return findexisting(command)
523 523
524 524 if pycompat.sysplatform == 'plan9':
525 525 return findexisting(os.path.join('/bin', command))
526 526
527 527 for path in encoding.environ.get('PATH', '').split(pycompat.ospathsep):
528 528 executable = findexisting(os.path.join(path, command))
529 529 if executable is not None:
530 530 return executable
531 531 return None
532 532
533 533 def setsignalhandler():
534 534 pass
535 535
536 536 _wantedkinds = {stat.S_IFREG, stat.S_IFLNK}
537 537
538 538 def statfiles(files):
539 539 '''Stat each file in files. Yield each stat, or None if a file does not
540 540 exist or has a type we don't care about.'''
541 541 lstat = os.lstat
542 542 getkind = stat.S_IFMT
543 543 for nf in files:
544 544 try:
545 545 st = lstat(nf)
546 546 if getkind(st.st_mode) not in _wantedkinds:
547 547 st = None
548 548 except OSError as err:
549 549 if err.errno not in (errno.ENOENT, errno.ENOTDIR):
550 550 raise
551 551 st = None
552 552 yield st
553 553
554 554 def getuser():
555 555 '''return name of current user'''
556 556 return pycompat.fsencode(getpass.getuser())
557 557
558 558 def username(uid=None):
559 559 """Return the name of the user with the given uid.
560 560
561 561 If uid is None, return the name of the current user."""
562 562
563 563 if uid is None:
564 564 uid = os.getuid()
565 565 try:
566 566 return pycompat.fsencode(pwd.getpwuid(uid)[0])
567 567 except KeyError:
568 568 return b'%d' % uid
569 569
570 570 def groupname(gid=None):
571 571 """Return the name of the group with the given gid.
572 572
573 573 If gid is None, return the name of the current group."""
574 574
575 575 if gid is None:
576 576 gid = os.getgid()
577 577 try:
578 return grp.getgrgid(gid)[0]
578 return pycompat.fsencode(grp.getgrgid(gid)[0])
579 579 except KeyError:
580 return str(gid)
580 return pycompat.bytestr(gid)
581 581
582 582 def groupmembers(name):
583 583 """Return the list of members of the group with the given
584 584 name, KeyError if the group does not exist.
585 585 """
586 586 name = pycompat.fsdecode(name)
587 587 return pycompat.rapply(pycompat.fsencode, list(grp.getgrnam(name).gr_mem))
588 588
589 589 def spawndetached(args):
590 590 return os.spawnvp(os.P_NOWAIT | getattr(os, 'P_DETACH', 0),
591 591 args[0], args)
592 592
593 593 def gethgcmd():
594 594 return sys.argv[:1]
595 595
596 596 def makedir(path, notindexed):
597 597 os.mkdir(path)
598 598
599 599 def lookupreg(key, name=None, scope=None):
600 600 return None
601 601
602 602 def hidewindow():
603 603 """Hide current shell window.
604 604
605 605 Used to hide the window opened when starting asynchronous
606 606 child process under Windows, unneeded on other systems.
607 607 """
608 608 pass
609 609
610 610 class cachestat(object):
611 611 def __init__(self, path):
612 612 self.stat = os.stat(path)
613 613
614 614 def cacheable(self):
615 615 return bool(self.stat.st_ino)
616 616
617 617 __hash__ = object.__hash__
618 618
619 619 def __eq__(self, other):
620 620 try:
621 621 # Only dev, ino, size, mtime and atime are likely to change. Out
622 622 # of these, we shouldn't compare atime but should compare the
623 623 # rest. However, one of the other fields changing indicates
624 624 # something fishy going on, so return False if anything but atime
625 625 # changes.
626 626 return (self.stat.st_mode == other.stat.st_mode and
627 627 self.stat.st_ino == other.stat.st_ino and
628 628 self.stat.st_dev == other.stat.st_dev and
629 629 self.stat.st_nlink == other.stat.st_nlink and
630 630 self.stat.st_uid == other.stat.st_uid and
631 631 self.stat.st_gid == other.stat.st_gid and
632 632 self.stat.st_size == other.stat.st_size and
633 633 self.stat[stat.ST_MTIME] == other.stat[stat.ST_MTIME] and
634 634 self.stat[stat.ST_CTIME] == other.stat[stat.ST_CTIME])
635 635 except AttributeError:
636 636 return False
637 637
638 638 def __ne__(self, other):
639 639 return not self == other
640 640
641 641 def statislink(st):
642 642 '''check whether a stat result is a symlink'''
643 643 return st and stat.S_ISLNK(st.st_mode)
644 644
645 645 def statisexec(st):
646 646 '''check whether a stat result is an executable file'''
647 647 return st and (st.st_mode & 0o100 != 0)
648 648
649 649 def poll(fds):
650 650 """block until something happens on any file descriptor
651 651
652 652 This is a generic helper that will check for any activity
653 653 (read, write. exception) and return the list of touched files.
654 654
655 655 In unsupported cases, it will raise a NotImplementedError"""
656 656 try:
657 657 while True:
658 658 try:
659 659 res = select.select(fds, fds, fds)
660 660 break
661 661 except select.error as inst:
662 662 if inst.args[0] == errno.EINTR:
663 663 continue
664 664 raise
665 665 except ValueError: # out of range file descriptor
666 666 raise NotImplementedError()
667 667 return sorted(list(set(sum(res, []))))
668 668
669 669 def readpipe(pipe):
670 670 """Read all available data from a pipe."""
671 671 # We can't fstat() a pipe because Linux will always report 0.
672 672 # So, we set the pipe to non-blocking mode and read everything
673 673 # that's available.
674 674 flags = fcntl.fcntl(pipe, fcntl.F_GETFL)
675 675 flags |= os.O_NONBLOCK
676 676 oldflags = fcntl.fcntl(pipe, fcntl.F_SETFL, flags)
677 677
678 678 try:
679 679 chunks = []
680 680 while True:
681 681 try:
682 682 s = pipe.read()
683 683 if not s:
684 684 break
685 685 chunks.append(s)
686 686 except IOError:
687 687 break
688 688
689 689 return ''.join(chunks)
690 690 finally:
691 691 fcntl.fcntl(pipe, fcntl.F_SETFL, oldflags)
692 692
693 693 def bindunixsocket(sock, path):
694 694 """Bind the UNIX domain socket to the specified path"""
695 695 # use relative path instead of full path at bind() if possible, since
696 696 # AF_UNIX path has very small length limit (107 chars) on common
697 697 # platforms (see sys/un.h)
698 698 dirname, basename = os.path.split(path)
699 699 bakwdfd = None
700 700 if dirname:
701 701 bakwdfd = os.open('.', os.O_DIRECTORY)
702 702 os.chdir(dirname)
703 703 sock.bind(basename)
704 704 if bakwdfd:
705 705 os.fchdir(bakwdfd)
706 706 os.close(bakwdfd)
General Comments 0
You need to be logged in to leave comments. Login now