Method: OcflTools::OcflValidator#verify_structure

Defined in:
lib/ocfl_tools/ocfl_validator.rb

#verify_structureOcflTools::OcflResults

Do all the files and directories in the object_dir conform to spec? Are there inventory.json files in each version directory? (warn if not in version dirs) Deduce version dir naming convention by finding the v1 directory; apply that format to other dirs.

Returns:

  • of event results



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/ocfl_tools/ocfl_validator.rb', line 316

def verify_structure
  error = nil
  @my_results ||= OcflTools::OcflResults.new
  # 1. use get_version_format to determine the format used for version directories.
  #    If we can't deduce it by inspection of the object_root, ERROR and try and process using site-wide defaults.
  if get_version_format == false
    @my_results.error('E111', 'verify_structure', 'OCFL unable to determine version format by inspection of directories.')
    @error = true
  end

  object_root_dirs  = []
  object_root_files = []

  Dir.chdir(@ocfl_object_root)
  Dir.glob('*').select do |file|
    object_root_dirs << file if File.directory? file
    object_root_files << file if File.file? file
  end

  # 1b. What happens if some this directory is just completely empty?
  if object_root_dirs.size == 0 && object_root_files.size == 0
    @my_results.error('E100', 'verify_sructure', "Object root directory #{@ocfl_object_root} is empty.")
    return @my_results
  end

  # 2. Check object root directory for required files.
  # We have to check the top of inventory.json to get the appropriate digest algo.
  # This is so we don't cause get_digestAlgorithm to throw up if inventory.json doesn't exist.
  file_checks = ['inventory.json']

  # 2a. What digest should the inventory.json sidecar be using? Ask inventory.json.
  # 2b. What's the highest version we should find here?
  # 2c. What should our contentDirectory value be?
  if File.exist? "#{@ocfl_object_root}/inventory.json"
    begin
      @inventory = load_inventory("#{@ocfl_object_root}/inventory.json")
      json_digest      = OcflTools::Utils::Inventory.get_digestAlgorithm("#{@ocfl_object_root}/inventory.json")
      contentDirectory = OcflTools::Utils::Inventory.get_contentDirectory("#{@ocfl_object_root}/inventory.json")
      expect_head      = OcflTools::Utils::Inventory.get_value("#{@ocfl_object_root}/inventory.json", 'head')
      file_checks << "inventory.json.#{json_digest}"
    rescue OcflTools::Errors::ValidationError
      # We couldn't load up the inventory; use site defaults.
      contentDirectory = OcflTools.config.content_directory
      json_digest      = OcflTools.config.digest_algorithm
      file_checks << "inventory.json.#{json_digest}"
    end
  else
    # If we can't get these values from a handy inventory.json, use the site defaults.
    contentDirectory = OcflTools.config.content_directory
    json_digest      = OcflTools.config.digest_algorithm
    file_checks << "inventory.json.#{json_digest}"
  end

  # Error if a required file is not found in the object root.
  # This is now just the check for inventory.json and sidecar file.
  file_checks.each do |file|
    unless object_root_files.include? file
      @my_results.error('E102', 'verify_structure', "Object root does not include required file #{file}")
      error = true
    end
    object_root_files.delete(file)
  end

  # NamAsTe file checks:
  # C1: There should be only 1 file in the root dir beginning with '0=ocfl_object_'
  # C2: That file should match the expected value of OCFL_version (e.g. '0=ocfl_object_1.0')
  # C3: The content of that file should match the filename, less the leading '0='
  root_namaste_files = []
  Dir.glob('0=ocfl_object_*').select do |file|
    root_namaste_files << file if File.file? file
  end

  # C1: We need EXACTLY ONE of these files.
  if root_namaste_files.size == 0
    @my_results.error('E103', 'verify_structure', 'Object root does not include required NamAsTe file.')
    error = true
  end

  if root_namaste_files.size > 1
    @my_results.error('E104', 'verify_structure', "Object root contains multiple NamAsTe files: #{root_namaste_files}")
    error = true
  end

  # C2 and C3 here.
  # If we're dealing with 1 or more ocfl_object_files, process them for correctness.
  unless root_namaste_files.size == 0 || root_namaste_files.size == nil

    # What OCFL version are we looking for? Pull the default value if not otherwise set.
    @ocfl_version ||= OcflTools.config.ocfl_version

    root_namaste_files.each do | file |

      # C2: Is this file the expected version?
      if file != "0=ocfl_object_#{@ocfl_version}"
        @my_results.error('E107', 'verify_structure', "Required NamAsTe file in object root is for unexpected OCFL version: #{file}")
        error = true
      end

      # C3: does the file content match the file name?
      # Cut the first 2 characters from the filename; what remains is the expected content.
      expected_content = file.slice(2..file.size)

      # We use &:gets here instead of &:readline so we don't throw an exception if the file doesn't have content.
      first_line = File.open("#{@ocfl_object_root}/#{file}", &:gets)

      # Handle 'the Namaste file is empty' case.
      if first_line == nil
        @my_results.error('E105', 'verify_structure', 'Required NamAsTe file in object root directory has no content!')
        error = true
        object_root_files.delete(file)
        next
      end

      # it'll have a \n on the end. Remove it, then verify for correct content.
      if first_line.chomp! != expected_content
        @my_results.error('E106', 'verify_structure', 'Required NamAsTe file in object root directory does not contain expected string.')
        error = true
      end
      object_root_files.delete(file)
    end
  end

  # 3. Error if there are extraneous files in object root.
  unless object_root_files.empty?
    @my_results.error('E101', 'verify_structure', "Object root contains noncompliant files: #{object_root_files}")
    error = true
  end

  # 4. Warn if the optional 'logs' directory is found in the object root.
  if object_root_dirs.include? 'logs'
    @my_results.warn('W111', 'verify_structure', 'OCFL 3.1 optional logs directory found in object root.')
    object_root_dirs.delete('logs')
  end

  # 5. Warn if the optional 'extensions' directory is found in object root.
  if object_root_dirs.include? 'extensions'
    @my_results.warn('W111', 'verify_structure', 'OCFL 3.1 optional extensions directory found in object root.')
    object_root_dirs.delete('extensions')
  end

  begin
    version_directories = OcflTools::Utils::Files.get_version_directories(@ocfl_object_root)
  rescue OcflTools::Errors::ValidationError => e
    e.details.each do | code, messages |
      messages.each do | msg |
        @my_results.error(code, 'verify_structure', msg)
      end
    end
    # If we actually throw a validation error, we can't proceed: no version directories found!
    return @my_results
  end

  remaining_dirs = object_root_dirs - version_directories

  # 6. Error if there are extraneous/unexpected directories in the object root.
  unless remaining_dirs.empty?
    @my_results.error('E100', 'verify_structure', "Object root contains noncompliant directories: #{remaining_dirs}")
    error = true
  end

  # 7. Version directories must be a continuous sequence, starting at v1.
  version_dir_count = version_directories.size
  count = 0

  until count == version_dir_count
    count += 1
    expected_directory = @version_format % count
    # just check to see if it's in the array version_directories.
    # We're not *SURE* that what we have is a continous sequence starting at 1;
    # just that they're valid version dir names, sorted in ascending order, and they exist.
    if version_directories.include? expected_directory
      # Could verbose log this here.
      # @my_results.info('I200', 'verify_sructure', "Expected version directory #{expected_directory} found.")
    else
      @my_results.error('E013', 'verify_structure', "Expected version directory #{expected_directory} missing from directory list #{version_directories} ")
      error = true
    end
  end

  # 8. Error if the head version in the inventory does not match the highest version directory discovered in the object root.
  unless expect_head.nil? # No point checking this is we've already failed the root inventory.json check.
    if version_directories[-1] != expect_head
      @my_results.error('E111', 'verify_structure', "Inventory file expects a highest version of #{expect_head} but directory list contains #{version_directories} ")
      error = true
    else
      # could log an 'expected head version found' here.
    end
  end

  # CHECK VERSION DIRECTORY CONTENTS
  # This is setup for the next round of checks.
  # For the version_directories we *do* have, are they cool?
  version_directories.each do |ver|
    version_dirs  = []
    version_files = []

    Dir.chdir("#{@ocfl_object_root}/#{ver}")
    Dir.glob('*').select do |file|
      version_dirs << file if File.directory? file
      version_files << file if File.file? file
    end

    # 9. Warn if inventory.json and sidecar are not present in version directory.
    file_checks = []
    if File.exist? "#{@ocfl_object_root}/#{ver}/inventory.json"
      begin
        @inventory = load_inventory("#{@ocfl_object_root}/#{ver}/inventory.json")
        json_digest      = OcflTools::Utils::Inventory.get_digestAlgorithm("#{@ocfl_object_root}/#{ver}/inventory.json")
        file_checks << 'inventory.json'
        file_checks << "inventory.json.#{json_digest}"
        versionContentDirectory = OcflTools::Utils::Inventory.get_contentDirectory("#{@ocfl_object_root}/#{ver}/inventory.json")
        if versionContentDirectory != contentDirectory
          @my_results.error('E111', 'verify_structure', "contentDirectory value #{versionContentDirectory} in version #{ver} does not match expected contentDirectory value #{contentDirectory}.")
          error = true
        end
      rescue OcflTools::Errors::ValidationError
        # We couldn't load up the inventory; use site defaults.
        # We should also record the error in @my_results?
        json_digest = OcflTools.config.digest_algorithm
        file_checks << 'inventory.json'
        file_checks << "inventory.json.#{json_digest}"
        error = true
      end
    else
      file_checks << 'inventory.json'         # We look for it, even though we know we won't find it, so we can log the omission.
      file_checks << 'inventory.json.sha512'  # We look for it, even though we know we won't find it, so we can log the omission.
    end

    file_checks.each do |file|
      if version_files.include? file
        # The inventory file in the highest version directory MUST match the inventory file in the object root.
        case file
          when 'inventory.json'
            case ver
              # expect_head is nil if there's no inventory.json in the object root, so this test won't run against nothing.
              when expect_head
                # expand_filepaths(@ocfl_object_root)
                my_files = ["#{@ocfl_object_root}/inventory.json", "#{@ocfl_object_root}/#{ver}/inventory.json"]
                # create_digests
                my_digests = OcflTools::Utils::Files.create_digests(my_files, 'sha512')
                # We know there must be only 2 values here, and they should be equal.
                if my_digests.values[0] != my_digests.values[1]
                  @my_results.error('E111', 'verify_structure', "Inventory.json in root and highest version #{ver} MUST match.")
                end
                # Only 1 case block, no 'else'
              end
            # Only 1 case block, no 'else'
          end
        # Expected file exists; delete it from our check list and move on.
        version_files.delete(file)
      else
        @my_results.warn('W111', 'verify_structure', "OCFL 3.1 optional #{file} missing from #{ver} directory")
        version_files.delete(file)
      end
    end

    # 10. Error if files other than inventory & sidecar found in version directory.
    unless version_files.empty?
      @my_results.error('E011', 'verify_structure', "non-compliant files #{version_files} in #{ver} directory")
      error = true
    end

    ###### CHECK THIS; I think this is superseded by the verify_manifest content_dir check.
    # 11. WARN if a contentDirectory exists, but is empty.
    if version_dirs.include? contentDirectory
      version_dirs.delete(contentDirectory)
    end

    # 12. Warn if any directories other than the expected 'content' directory are found in the version directory.
    # This is the "Moab Excepion" to allow for legacy Moab object migration - a 'manifests' directory would be here.
    unless version_dirs.empty?
      @my_results.warn('W101', 'version_structure', "OCFL 3.3 version directory should not contain any directories other than the designated content sub-directory. Additional directories found: #{version_dirs}")
      error = true
    end
  end

  # If we get here without errors (warnings are OK), we passed!
  if error.nil?
    @my_results.ok('O111', 'verify_structure', 'OCFL 3.1 Object root passed file structure test.')
  end
  @my_results
end