# Win32 File.size slow?

**URL:** <https://www.ruby-forum.com/t/win32-file-size-slow/132289>\
**Category:** Ruby\
**Created:** [March 18, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/win32-file-size-slow/132289 "2008-03-18T19:58:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![NationS\_Carey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/87869e/32.png) [@NationS\_Carey](https://www.ruby-forum.com/u/NationS_Carey)\
**Post date:** [March 18, 2008, 7:58pm UTC](https://www.ruby-forum.com/t/win32-file-size-slow/132289/1 "2008-03-18T19:58:08Z")

</div>

Last week I posted a question about incorrect file sizes coming back  
incorrectly from File.size? for really large files. I changed my script  
to use the win32 version, which returns the correct sizes, and now the  
script is unacceptably slow. The bottleneck is the File.size call. My  
scripts startup went from about a minute to, well I don’t know because  
it’s never finished. I’m walking a file path that is somewhat indirect,  
but the other file operations perform well. I’m reading files, using  
UNC paths, through samba on linux boxes that mount a giant file system  
through fiber.

So anyway, does anyone have any alternatives?

* * *

Carey Nation [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Lead Software Engineer

CNN BEST

Broadcast Production Systems

Video Solutions Group

(404)827-2935 (wk)

(404)824-0033 (cell)

---

<div class="post-metadata">

**Author:** ![NationS\_Carey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/87869e/32.png) [@NationS\_Carey](https://www.ruby-forum.com/u/NationS_Carey)\
**Post date:** [March 18, 2008, 11:48pm UTC](https://www.ruby-forum.com/t/win32-file-size-slow/132289/2 "2008-03-18T23:48:49Z")

</div>

On Mar 18, 12:56 pm, “Nation, Carey” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Last week I posted a question about incorrect file sizes coming back  
> incorrectly from File.size? for really large files. I changed my script  
> to use the win32 version, which returns the correct sizes, and now the  
> script is unacceptably slow. The bottleneck is the File.size call. My  
> scripts startup went from about a minute to, well I don’t know because  
> it’s never finished. I’m walking a file path that is somewhat indirect,  
> but the other file operations perform well. I’m reading files, using  
> UNC paths, through samba on linux boxes that mount a giant file system  
> through fiber.

That’s interesting. The File.size method is really a wrapper for  
File::Stat#size, which uses stat64() to stat the file in question. I  
wonder if it’s choking on the Samba/UNC path. Wouldn’t be the first  
app to do so.

What happens if you replace File.size in lib/win32/file.rb with this  
version:

# Untested

class \<\< self  
def size(file)  
handle = CreateFile(  
file,  
GENERIC\_READ,  
FILE\_SHARE\_READ,  
nil,  
OPEN\_EXISTING,  
FILE\_FLAG\_OVERLAPPED | FILE\_FLAG\_NO\_BUFFERING,  
nil  
)

```
  if handle == INVALID_HANDLE_VALUE
     raise Error, get_last_error
  end

  file_size = [0].pack('Q')
  GetFileSizeEx(handle, file_size)
  file_size = file_size.unpack('Q')[0]

  unless CloseHandle(handle)
     raise Error, get_last_error
  end

  file_size

```

end  
end

---

<div class="post-metadata">

**Author:** ![NationS\_Carey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/87869e/32.png) [@NationS\_Carey](https://www.ruby-forum.com/u/NationS_Carey)\
**Post date:** [March 19, 2008, 3:42pm UTC](https://www.ruby-forum.com/t/win32-file-size-slow/132289/3 "2008-03-19T15:42:36Z")

</div>

I’ll try this. I don’t think that it’s the path because it _does_ work.  
It’s just not in any hurry. I’ll let you know in a bit about the call  
below…

---

<div class="post-metadata">

**Author:** ![NationS\_Carey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/87869e/32.png) [@NationS\_Carey](https://www.ruby-forum.com/u/NationS_Carey)\
**Post date:** [March 19, 2008, 7:24pm UTC](https://www.ruby-forum.com/t/win32-file-size-slow/132289/4 "2008-03-19T19:24:36Z")

</div>

That works and the performance is good. Two caveats. First, in the  
CreateFile, it wants 0 rather than nil. Second, CloseHandle isn’t  
implemented in windows/file.rb, so I added it to my local copy. This is  
actually a really big oversight since leaving handles of any kind open  
in win32 is a big leak problem.

Thanks!

---

<div class="post-metadata">

**Author:** ![NationS\_Carey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/87869e/32.png) [@NationS\_Carey](https://www.ruby-forum.com/u/NationS_Carey)\
**Post date:** [March 19, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/win32-file-size-slow/132289/5 "2008-03-19T22:32:33Z")

</div>

On Mar 19, 12:23 pm, “Nation, Carey” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That works and the performance is good. Two caveats. First, in the CreateFile, it wants 0 rather than nil.

It should work. I’ll look into that.

> Second, CloseHandle isn’t implemented in windows/file.rb, so I added it to my local copy. This is actually a really big oversight since leaving handles of any kind open in win32 is a big leak problem.

Ah, ok. Not to panic, since win32-file uses stat64(), so there are no  
open handles to begin with.

Regards,

Dan
