Merge branch '10649-squeue-stderr' closes #10649
[arvados.git] / services / api / lib / salvage_collection.rb
index 2ffe0f446340fb1f8787189728dcbdb2ba6ebe3e..c6664b1ec916413fc3d2e431bb12551a303bf1ed 100755 (executable)
@@ -14,7 +14,7 @@ module SalvageCollection
   require 'tempfile'
   require 'shellwords'
 
-  def self.salvage_collection_arv_put cmd
+  def salvage_collection_arv_put cmd
     new_manifest = %x(#{cmd})
     if $?.success?
       new_manifest
@@ -23,34 +23,25 @@ module SalvageCollection
     end
   end
 
-  LOCATOR_REGEXP = /^([[:xdigit:]]{32})(\+(.*))?\z/
-  def self.salvage_collection_locator_data manifest
-      # Get all the locators from the original manifest
-      locators = []
-      size = 0
-      manifest.each_line do |line|
-        line.split(' ').each do |word|
-          if match = LOCATOR_REGEXP.match(word)
-            if match.size > 3 and match[3]
-              size_str = match[3].split('+')[0]
-              if size_str.to_i.to_s == size_str
-                word = match[1] + '+' + size_str     # get rid of any hints
-                size += size_str.to_i
-              else
-                word = match[1]
-              end
-            else
-              word = match[1]
-            end
-            locators << word
-          end
-        end
+  # Get all the locators (and perhaps other strings that look a lot
+  # like a locators) from the original manifest, even if they don't
+  # appear in the correct positions with the correct space delimiters.
+  def salvage_collection_locator_data manifest
+    locators = []
+    size = 0
+    manifest.scan(/(^|[^[:xdigit:]])([[:xdigit:]]{32})((\+\d+)(\+|\b))?/) do |_, hash, _, sizehint, _|
+      if sizehint
+        locators << hash.downcase + sizehint
+        size += sizehint.to_i
+      else
+        locators << hash.downcase
       end
-      locators << 'd41d8cd98f00b204e9800998ecf8427e+0' if !locators.any?
-      return [locators, size]
+    end
+    locators << 'd41d8cd98f00b204e9800998ecf8427e+0' if !locators.any?
+    return [locators, size]
   end
 
-  def self.salvage_collection uuid, reason='salvaged - see #6277, #6859'
+  def salvage_collection uuid, reason='salvaged - see #6277, #6859'
     act_as_system_user do
       if !ENV['ARVADOS_API_TOKEN'].present? or !ENV['ARVADOS_API_HOST'].present?
         raise "ARVADOS environment variables missing. Please set your admin user credentials as ARVADOS environment variables."
@@ -70,6 +61,7 @@ module SalvageCollection
       # create new collection using 'arv-put' with original manifest_text as the data
       temp_file = Tempfile.new('temp')
       temp_file.write(src_manifest)
+
       temp_file.close
 
       new_manifest = salvage_collection_arv_put "arv-put --as-stream --use-filename invalid_manifest_text.txt #{Shellwords::shellescape(temp_file.path)}"