[Sugar-devel] [PATCH Browse] Cancel a download if space is very tight SL #394
Martin Langhoff
martin.langhoff at gmail.com
Mon Sep 10 17:49:19 EDT 2012
On Mon, Sep 10, 2012 at 5:33 PM, Manuel Kaufmann <humitos at gmail.com> wrote:
> If there are less than 50Mb free on the Hard Disk the downloading
> process is canceled and this is informed to the user via an Alert.
Perhaps it is a stupid question but... in many HTTP requests you get
the size of the transfer in the HTTP headers, in the form of the
Content-Length header.
This is specially true with downloads of large files (less true for
dynamic content like PHP-generated pages).
> +# http://stackoverflow.com/questions/787776/find-free-disk-space-in-python-on-os-x
> +def free_space(path):
Do you really need that comment? Doing s.f_bavail * s.f_frsize is just
common sense.
> + global k
> + s = os.statvfs(path)
> + return (s.f_bavail * s.f_frsize) / 1024 / 1024
Points for using f_bavail instead of bfree :-)
Don't divide it yet. Return bytes -- only convert to MB if you are
displaying to users. You are going to need bytes to compare with
content-length which is in bytes too.
> progress = self._download.get_progress()
> - self.dl_jobject.metadata['progress'] = str(int(progress * 100))
Here, get the content-length... and compare with the free bytes (plus
some padding, say, 15%). Maybe we still want to preserve a minimum
disk space. For example: don't let a download put you below 50MB or
10MB free -- in that case do the multiplication in a constant
definition.
MIN_DISKFREE_AFTER_DOWNLOAD = 50 * 1024 * 1024
If the request does not have content-length, it is sane to assume it
will be small (dynamically generated content) so that padding you
preserved is very likely to fit it.
And then, you need the most clear step: if the write fails, and the
error / exception tells you it's ENOSPC, you delete the object.
cheers,
m
--
martin.langhoff at gmail.com
martin at laptop.org -- Software Architect - OLPC
- ask interesting questions
- don't get distracted with shiny stuff - working code first
- http://wiki.laptop.org/go/User:Martinlanghoff
More information about the Sugar-devel
mailing list