diff --git a/mercurial/changegroup.py b/mercurial/changegroup.py
index 37cdf1fca1b27ae970c7d5ea9a6abf5f39d95c6f_bWVyY3VyaWFsL2NoYW5nZWdyb3VwLnB5..c20f4898631e2f5f954d7e8afe5fc05b14b7e55f_bWVyY3VyaWFsL2NoYW5nZWdyb3VwLnB5 100644
--- a/mercurial/changegroup.py
+++ b/mercurial/changegroup.py
@@ -59,7 +59,7 @@
 # hgweb uses this list to communicate its preferred type
 bundlepriority = ['HG10GZ', 'HG10BZ', 'HG10UN']
 
-def writebundle(cg, filename, bundletype):
+def writebundle(cg, filename, bundletype, vfs=None):
     """Write a bundle file and return its filename.
 
     Existing files will not be overwritten.
@@ -72,7 +72,10 @@
     cleanup = None
     try:
         if filename:
-            fh = open(filename, "wb")
+            if vfs:
+                fh = vfs.open(filename, "wb")
+            else:
+                fh = open(filename, "wb")
         else:
             fd, filename = tempfile.mkstemp(prefix="hg-bundle-", suffix=".hg")
             fh = os.fdopen(fd, "wb")
@@ -112,7 +115,10 @@
         if fh is not None:
             fh.close()
         if cleanup is not None:
-            os.unlink(cleanup)
+            if filename and vfs:
+                vfs.unlink(cleanup)
+            else:
+                os.unlink(cleanup)
 
 def decompressor(fh, alg):
     if alg == 'UN':