{"action":"created","issue":{"url":"https://api.github.com/repos/WinRb/winrm-fs/issues/16","labels_url":"https://api.github.com/repos/WinRb/winrm-fs/issues/16/labels{/name}","comments_url":"https://api.github.com/repos/WinRb/winrm-fs/issues/16/comments","events_url":"https://api.github.com/repos/WinRb/winrm-fs/issues/16/events","html_url":"https://github.com/WinRb/winrm-fs/issues/16","id":96817397,"number":16,"title":"Error on Windows on create_temp_zip_file","user":{"login":"bheuvel","id":2699104,"avatar_url":"https://avatars.githubusercontent.com/u/2699104?v=3","gravatar_id":"","url":"https://api.github.com/users/bheuvel","html_url":"https://github.com/bheuvel","followers_url":"https://api.github.com/users/bheuvel/followers","following_url":"https://api.github.com/users/bheuvel/following{/other_user}","gists_url":"https://api.github.com/users/bheuvel/gists{/gist_id}","starred_url":"https://api.github.com/users/bheuvel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bheuvel/subscriptions","organizations_url":"https://api.github.com/users/bheuvel/orgs","repos_url":"https://api.github.com/users/bheuvel/repos","events_url":"https://api.github.com/users/bheuvel/events{/privacy}","received_events_url":"https://api.github.com/users/bheuvel/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-07-23T13:42:10Z","updated_at":"2015-07-23T15:21:05Z","closed_at":"2015-07-23T15:21:05Z","body":"## The problem:\r\nWhile using winrm-fs with vagrant (-cloudstack plugin), an error occurs when running from Windows and not from Linux;\r\n\r\nThe error is:\r\n```\r\nY:/Dev/winrm-fs-fllock/vendor/bundle/ruby/2.1.0/gems/rubyzip-1.1.7/lib/zip/file.rb:416:in `rename': Permission denied @ sys_fail2 - (C:/Users/WINUSER/AppData/Local/Temp/winrm_upload20150723-5064-ggm875.zip20150723-5064-12t705i, C:/Users/WINUSER/AppData/Local/Temp/winrm_upload20150723-5064-ggm875.zip) (Errno::EACCES)\r\n```\r\n\r\n## Root-cause\r\nAfter some testing and debugging, it seems to come down to the following;\r\n - At initialize a Tempfile is created and handle stored in ```@zip_file``` ([temp_zip_file.rb#L35](https://github.com/WinRb/winrm-fs/blob/master/lib/winrm-fs/core/temp_zip_file.rb#L35))\r\n\r\n - After this files and folders are \"added\", and RubyZip will build this in a tempfile of it's own.\r\nOnce the zipfile has been build, it renames it to the targeted filename (which was specified above as Tempfile) (found in [file.rb#L408](https://github.com/rubyzip/rubyzip/blob/master/lib/zip/file.rb#L408))\r\n\r\n - But as the Tempfile already has this name/file locked, RubyZip is getting an error.\r\n\r\nThis appears to be a difference between Windows/Linux?\r\nOn Windows (during debugging) I cannot delete the Tempfile\r\nOn Linux/Centos7 I CAN delete the Tempfile.\r\n\r\nSo apparently on Linux, RubyZip is allowed to overwrite the Tempfile even though a handle is open?\r\n\r\nTested this on Windows 7/Windows 2012R2 with the same result.\r\n\r\n## Reproduce\r\nTo demonstrate this, the following code can be used:\r\n(procedure create_temp_zip_file slightly modified, taken from [upload_orchestrator.rb](https://github.com/WinRb/winrm-fs/blob/master/lib/winrm-fs/core/upload_orchestrator.rb#L110), apart from this using the `class TempZipFile` from the gem.\r\n```\r\nrequire 'winrm-fs'\r\n\r\n@thisfile = __FILE__\r\n@errmsg  = \"###### This will generate an error (Windows 7\\\\2012R2 / ruby 2.1.5p273 (2014-11-13 revision 48405) [i386-mingw32])(chefdk-0.6.2-1)\"\r\n@origerr = \"Y:/Dev/winrm-fs-fllock/vendor/bundle/ruby/2.1.0/gems/rubyzip-1.1.7/lib/zip/file.rb:416:in `rename': Permission denied @ sys_fail2 - (C:/Users/WINUSER/AppData/Local/Temp/winrm_upload20150723-5064-ggm875.zip20150723-5064-12t705i, C:/Users/WINUSER/AppData/Local/Temp/winrm_upload20150723-5064-ggm875.zip) (Errno::EACCES)\"\r\n\r\ndef create_temp_zip_file(local_path, close)\r\n  zip = WinRM::FS::Core::TempZipFile.new(local_path, recurse_paths: true)\r\n  if close\r\n    zip.zip_file.close unless zip.zip_file.respond_to?(\"closed?\") and zip.zip_file.closed?\r\n  end\r\n  zip.add(local_path)\r\n  puts @errmsg unless close\r\n  binding.pry\r\n  zip.build\r\n  zip.delete\r\n  puts \"###### Reached end of create_temp_zip_file\"\r\nend\r\n\r\nputs \"\\n\\n###### Demonstration using the actual Gem, with \\\"patch\\\"\"\r\ncreate_temp_zip_file( File.expand_path(@thisfile), true )\r\n\r\nputs \"\\n\\n###### Demonstration using the actual Gem, without \\\"patch\\\"\"\r\ncreate_temp_zip_file( File.expand_path(@thisfile), false )\r\n\r\nputs \"\\n\\nFYI: Following error is generated on Windows:\"\r\nputs @origerr\r\n```\r\nOutput on Windows:\r\n```\r\n\r\n###### Demonstration using the actual Gem, with \"patch\"\r\n###### Reached end of create_temp_zip_file\r\n\r\n\r\n###### Demonstration using the actual Gem, without \"patch\"\r\n###### This will generate an error (Windows 7\\2012R2 / ruby 2.1.5p273 (2014-11-13 revision 48405) [i386-mingw32])(chefdk-0.6.2-1)\r\nY:/Dev/winrm-fs-fllock/vendor/bundle/gems/rubyzip-1.1.7/lib/zip/file.rb:416:in `rename': Permission denied @ sys_fail2 - (C:/Users/WINUSER/AppData/Local/Temp/winrm_upload20150723-4080-17vztcj.zip20150723-4080-ckbeit, C:/Users/WINUSER/AppData/Local/Temp/winrm_upload20150723-4080-17vztcj.zip) (Errno::EACCES)\r\n        from Y:/Dev/winrm-fs-fllock/vendor/bundle/gems/rubyzip-1.1.7/lib/zip/file.rb:416:in `on_success_replace'\r\n        from Y:/Dev/winrm-fs-fllock/vendor/bundle/gems/rubyzip-1.1.7/lib/zip/file.rb:308:in `commit'\r\n        from Y:/Dev/winrm-fs-fllock/vendor/bundle/gems/rubyzip-1.1.7/lib/zip/file.rb:332:in `close'\r\n        from Y:/Dev/winrm-fs-fllock/vendor/bundle/bundler/gems/winrm-fs-7727c5644e20/lib/winrm-fs/core/temp_zip_file.rb:143:in `close'\r\n        from Y:/Dev/winrm-fs-fllock/vendor/bundle/bundler/gems/winrm-fs-7727c5644e20/lib/winrm-fs/core/temp_zip_file.rb:139:in `build'\r\n        from Y:/Dev/winrm-fs-fllock/vendor/bundle/bundler/gems/winrm-fs-7727c5644e20/lib/winrm-fs/core/temp_zip_file.rb:58:in `build'\r\n        from winrm-fs-fllock.rb:14:in `create_temp_zip_file'\r\n        from winrm-fs-fllock.rb:23:in `<main>'\r\n```\r\n\r\n## Suggested solution\r\nImmediately close the filehandle of the Tempfile, also because the filehandle is not actually used?\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/WinRb/winrm-fs/issues/comments/124138079","html_url":"https://github.com/WinRb/winrm-fs/issues/16#issuecomment-124138079","issue_url":"https://api.github.com/repos/WinRb/winrm-fs/issues/16","id":124138079,"user":{"login":"sneal","id":197023,"avatar_url":"https://avatars.githubusercontent.com/u/197023?v=3","gravatar_id":"","url":"https://api.github.com/users/sneal","html_url":"https://github.com/sneal","followers_url":"https://api.github.com/users/sneal/followers","following_url":"https://api.github.com/users/sneal/following{/other_user}","gists_url":"https://api.github.com/users/sneal/gists{/gist_id}","starred_url":"https://api.github.com/users/sneal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sneal/subscriptions","organizations_url":"https://api.github.com/users/sneal/orgs","repos_url":"https://api.github.com/users/sneal/repos","events_url":"https://api.github.com/users/sneal/events{/privacy}","received_events_url":"https://api.github.com/users/sneal/received_events","type":"User","site_admin":false},"created_at":"2015-07-23T15:21:05Z","updated_at":"2015-07-23T15:21:05Z","body":"Fixed by #17 "}}