On 01/15/2011 01:15 PM, Szymon Janc wrote: > On Saturday 15 January 2011 01:37:13 Seth Goldberg wrote: > >> Hi, >> >> Thanks for this. What's the goal of this patch? It seems that it marks >> the size and unknown until it gets to the metadata that indicates the >> file's size, then updates the size to the correct size, right? Where do >> you see this being useful? >> > The biggest problem now is that opening compressed files require seeking to the > end to get uncompressed size so on not seekable fs this causes > grub_file_open("foo") to read whole file. With this patchset this is no longer a > case. So in that use case > > file = grub_file_open ("foo"); > while (grub_file_read (file, buf, sizeof (buf)) > 0) { ... } > > foo is read only once. > > > >> It would seems that even with this patch, one >> would still need to read a gzipped file twice to be able to get its size, >> then allocate a contiguous buffer large enough to hold the entire thing >> (if that is desired (which, in some cases, it is :))). >> > Wouldn't sth like that work? > > It's O(N^2). You need to always at least double the allocation every time you exceed already allocated buffer in order to get O(N) > char * ptr = 0; > char buf[0x10000]; > unsigned size, size_total = 0; > > grub_file_t file = grub_file_open ("foo"); > while ( (size = grub_file_read (file, buf, sizeof (buf))) > 0) > { > ptr = grub_realloc (ptr, size_total + size); > grub_memcpy (ptr+size_total, buf, size); > size_total += size; > } > > -- Regards Vladimir 'φ-coder/phcoder' Serbinenko