Edgewall Software
Modify

Ticket #3903 (closed defect: fixed)

Opened 5 years ago

Last modified 3 years ago

Binary files got currupted when viewing via TracMercurial plugin

Reported by: anonymous Owned by: cboos
Priority: high Milestone: not applicable
Component: plugin/mercurial Version: 0.10
Severity: normal Keywords: mercurial
Cc: asl@…
Release Notes:
API Changes:

Description

I have Trac 0.10 installed among with TracMercurial plugin. Repository browsing works fine for text files, but doesn't for binary files. E.g. I have some images in the repository. Browser shows correct size for them, but downloaded file differs in size. Even more, only first 0x1000 bytes are valid. Others are just the first 0x1000 bytes iterating until the file ends.

Attachments

Change History

comment:1 Changed 5 years ago by cboos

  • Keywords mercurial added
  • Priority changed from normal to high
  • Status changed from new to assigned

Can you please try the following patch?

Index: backend.py
===================================================================
--- backend.py	(revision 3880)
+++ backend.py	(working copy)
@@ -323,6 +323,7 @@
         Node.__init__(self, path, rev, kind)
         self.created_path = path
         self.created_rev = rev
+        self.data = None
 
     def get_content(self):
         if self.isdir:
@@ -334,8 +335,14 @@
             return TracError("Can't read from directory %s" % self.path)
         file_n = self.manifest[self.path]
         file = self.repos.repo.file(self.path)
-        data = file.read(file_n)
-        return size and data[:size] or data
+        if self.data is None:
+            self.data = file.read(file_n)
+            self.pos = 0
+        if size:
+            prev_pos = self.pos
+            self.pos += size
+            return self.data[prev_pos:self.pos]
+        return data
 
     def get_entries(self):
         if self.isfile:

comment:2 Changed 5 years ago by anonymous

Thank you for fast respond! Everything works fine, but, please note, that "self.data" should be returned at line 345, not just "data".

comment:3 Changed 5 years ago by anonymous

Well, another check showed, that text files appeared as binary after the patch: it says something like "HTML preview not available".

Sometimes (in really random fashion) it displays html-formatted text, but only the last "chunk".

comment:4 Changed 5 years ago by anonymous

It seems, I've tracked down the problem.

  1. Everything works ok for "text" or "raw" format
  1. versioncontrol/web_ui/browser.py uses get_content() to pass file contents to renderer. get_content() call just returns the "self". But at the moment of get_content() call 1 chunk is already read (to detect mime type), thus self.pos is already set to CHUNK_SIZE. Surely, renderer gets just part of the file (or nothing at all). This patch works for me (I don't know, whether it's correct or not):
--- backend.py  2006-10-11 17:28:57.000000000 +0400
+++ backend.py  2006-10-11 17:30:56.540676287 +0400
@@ -328,8 +328,9 @@
     def get_content(self):
         if self.isdir:
             return None
-       self.pos = 0
-        return self # something that can be `read()` ...
+       obj = self
+       obj.pos = 0
+        return obj # something that can be `read()` ...

     def read(self, size=None):
         if self.isdir:

comment:5 Changed 5 years ago by cboos

  • Resolution set to fixed
  • Status changed from assigned to closed

Ha, at the same time I committed r3894, with a similar fix. Thanks for the feedback!

comment:6 Changed 3 years ago by cboos

  • Component changed from version control to plugin/mercurial
  • Milestone set to not applicable
View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
to The owner will be changed from cboos. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.