Revision b4478d34 lib/utils.py

b/lib/utils.py
2446 2446
  """Utility class for file locks.
2447 2447

  
2448 2448
  """
2449
  def __init__(self, filename):
2449
  def __init__(self, fd, filename):
2450 2450
    """Constructor for FileLock.
2451 2451

  
2452
    This will open the file denoted by the I{filename} argument.
2453

  
2452
    @type fd: file
2453
    @param fd: File object
2454 2454
    @type filename: str
2455
    @param filename: path to the file to be locked
2455
    @param filename: Path of the file opened at I{fd}
2456 2456

  
2457 2457
    """
2458
    self.fd = fd
2458 2459
    self.filename = filename
2459
    self.fd = open(self.filename, "w")
2460

  
2461
  @classmethod
2462
  def Open(cls, filename):
2463
    """Creates and opens a file to be used as a file-based lock.
2464

  
2465
    @type filename: string
2466
    @param filename: path to the file to be locked
2467

  
2468
    """
2469
    # Using "os.open" is necessary to allow both opening existing file
2470
    # read/write and creating if not existing. Vanilla "open" will truncate an
2471
    # existing file -or- allow creating if not existing.
2472
    return cls(os.fdopen(os.open(filename, os.O_RDWR | os.O_CREAT), "w+"),
2473
               filename)
2460 2474

  
2461 2475
  def __del__(self):
2462 2476
    self.Close()

Also available in: Unified diff