2005-08-15

mkstemp: diabolical bastard

Old and busted:

  t = tempfile.mkstemp('.tmp','.','.')
  tmpfn = t[1]
  tmpfh = open(tmpfn,'w')

  loop = int(size)
  while (0 < loop):
    data = stream.read(1024)
    loop -= os.write(t[0],data)

  tmpfh.flush()
  tmpfh.close()
  os.rename(tmpfn,fn)

New hotness:

  t = tempfile.mkstemp('.tmp','.','.')

  loop = int(size)
  while (0 < loop):
    data = stream.read(1024)
    loop -= os.write(t[0],data)

  os.fsync(t[0])
  os.close(t[0])
  os.rename(t[1],fn)

And for your information, yes, I am the type of person who continues to say "old and busted" and "new hotness", years after it ceased to be the catchphrase du jour. Yeah, baby!

No comments: