Hi Enrico,

On Wed, 2007-04-25 at 19:03 +1200, Enrico de Klerk wrote:
> I think the race condition can be removed by doing something like this
> (It's similar to what mkstemp does):
> 
> while True:
>     try:
>         fd = os.open(os.path.join(settings.MEDIA_ROOT, filename),
> os.O_RDWR | os.O_CREAT | os.O_EXCL)

Not quite that easy, unfortunately: O_CREAT and O_EXCL don't exist on
Windows. EXCL also doesn't work on some network file systems (NFS, in
particular). There are workarounds for the latter problem using
hardlinks, but I'm not sure how that works on something like
Windows-based filesystems.

>         fp = os.fdopen(fd, 'wb')
>         fp.write(raw_contents)
>         fp.close()
>         break;
>     except OSError, e:
>         if e.errno != errno.EEXIST:
>             raise
>     try:
>         dot_index = filename.rindex('.')
>     except ValueError: # filename has no dot
>         filename += '_'
>     else:
>         filename = filename[:dot_index] + '_' + filename[dot_index:]

For portability and simplicity, you should use something like
os.splitext() and then os.join() to replace all the messing around
trying to modify the bit just before the extension yourself. I think
that reduces the last six lines to one or two lines, depening on how you
write it.

Otherwise, the code looks like the right approach. Solve the Windows
portability problems (ha!) and you're done.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to