[2/2] debuginfod: ensure X-DEBUGINFOD-SIZE contains file size

Message ID 20220422222641.316502-1-amerey@redhat.com
State Superseded
Headers
Series None |

Commit Message

Aaron Merey April 22, 2022, 10:26 p.m. UTC
  For archived files X-DEBUGINFOD-SIZE currently contains the size of the
archive instead of the size of the uncompressed file.  Fix this.

Also add testcases to verify X-DEBUGINFOD-SIZE contains uncompressed
file sizes.

Signed-off-by: Aaron Merey <amerey@redhat.com>
---
 debuginfod/debuginfod.cxx                | 11 +++++++++--
 tests/run-debuginfod-response-headers.sh |  8 ++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
  

Comments

Aaron Merey April 22, 2022, 10:31 p.m. UTC | #1
Please ignore the "2/2" in the subject line, this patch is not part of a series.

Aaron
  
Frank Ch. Eigler April 22, 2022, 11:27 p.m. UTC | #2
Hi -

> -          add_mhd_response_header (r, "X-DEBUGINFOD-SIZE",
> -                                   to_string(fs.st_size).c_str());

> +          rc = fstat (fd, &fs);
> +          if (rc == 0)
> +            add_mhd_response_header (r, "X-DEBUGINFOD-SIZE",
> +                                     to_string(fs.st_size).c_str());
> +          else
> +            {
> +              close (fd);
> +              throw libc_exception (errno, string("stat ") + b_source1 + " archive " + b_source0);
> +            }


It shouldn't require a new fstat -- the archive component file's size
should be available from libarchive already: archive_entry_size(e);

- FChE
  

Patch

diff --git a/debuginfod/debuginfod.cxx b/debuginfod/debuginfod.cxx
index 9c0217f6..d61757ae 100644
--- a/debuginfod/debuginfod.cxx
+++ b/debuginfod/debuginfod.cxx
@@ -1789,11 +1789,18 @@  handle_buildid_r_match (bool internal_req_p,
           std::string file = b_source1.substr(b_source1.find_last_of("/")+1, b_source1.length());
           add_mhd_response_header (r, "Content-Type",
                                    "application/octet-stream");
-          add_mhd_response_header (r, "X-DEBUGINFOD-SIZE",
-                                   to_string(fs.st_size).c_str());
           add_mhd_response_header (r, "X-DEBUGINFOD-ARCHIVE",
                                    b_source0.c_str());
           add_mhd_response_header (r, "X-DEBUGINFOD-FILE", file.c_str());
+          rc = fstat (fd, &fs);
+          if (rc == 0)
+            add_mhd_response_header (r, "X-DEBUGINFOD-SIZE",
+                                     to_string(fs.st_size).c_str());
+          else
+            {
+              close (fd);
+              throw libc_exception (errno, string("stat ") + b_source1 + " archive " + b_source0);
+            }
           add_mhd_last_modified (r, archive_entry_mtime(e));
           if (verbose > 1)
             obatched(clog) << "serving archive " << b_source0 << " file " << b_source1 << endl;
diff --git a/tests/run-debuginfod-response-headers.sh b/tests/run-debuginfod-response-headers.sh
index 10b2ab49..62c43887 100755
--- a/tests/run-debuginfod-response-headers.sh
+++ b/tests/run-debuginfod-response-headers.sh
@@ -86,6 +86,14 @@  grep 'X-DEBUGINFOD-FILE: ' vlog-find$PORT1.2
 grep 'X-DEBUGINFOD-SIZE: ' vlog-find$PORT1.2
 grep 'X-DEBUGINFOD-ARCHIVE: ' vlog-find$PORT1.2
 
+# Check that X-DEBUGINFOD-SIZE matches the size of each file
+for file in vlog-find$PORT1.1 vlog-find$PORT1.2
+do
+    st_size=$(stat -c%s $(tail -n 1 $file))
+    x_debuginfod_size=$(grep 'X-DEBUGINFOD-SIZE' $file | egrep -o '[0-9]+')
+    test $st_size -eq $x_debuginfod_size
+done
+
 kill $PID1
 wait $PID1
 PID1=0