# Test that locally copied files keep their sparsness
body common control
{
      inputs => { "../default.sub.cf" };
      bundlesequence  => { default("$(this.promise_filename)") };
      version => "1.0";
}

bundle agent init
{
  files:
    "$(test.file[sparse])"
      delete => tidy,
      handle => "init_tidy_sparse";

    "$(test.file[copy])"
      delete => tidy,
      handle => "init_tidy_copy";


  commands:
    "/bin/dd"
      args => "if=/dev/zero of=$(test.file[sparse]) seek=1024 bs=1024 count=0",
      if => not( fileexists( "$(test.file[sparse])" )),
      depends_on => { "init_tidy_sparse", "init_tidy_copy" };
}

bundle agent test
{
  meta:
    "description" string => "ENT-2769: Test that when a sparse file is copied locally the sparseness is preserved";

    # Various exotic filesystems don't support sparseness, see the unit
    # test "files_copy_test.c" for details.
    "test_skip_unsupported" string => "!linux";

  vars:
    "file[sparse]" string => "$(G.testdir)/sparse";
    "file[copy]" string => "$(G.testdir)/copy";

  files:
    "$(file[copy])"
      copy_from => local_dcp( "$(file[sparse])" );
}

bundle agent check
{
  vars:
    "size[sparse]"
      string => filestat( "$(test.file[sparse])", size ),
      if => fileexists( "$(test.file[sparse])" );

    "size[copy]"
      string => filestat( "$(test.file[copy])", size ),
      if => fileexists( "$(test.file[copy])" );

    "du[sparse]"
      string => execresult( "expr    `du -k $(test.file[sparse]) | cut -f1`    '*' 1024", useshell),
      if => fileexists( "$(test.file[sparse])" );

    "du[copy]"
      string => execresult( "expr    `du -k $(test.file[copy])   | cut -f1`    '*' 1024", useshell),
      if => fileexists( "$(test.file[copy])" );

  classes:
    "size_match" expression => strcmp( "$(size[sparse])", "$(size[copy])" );
    "du_less"    expression => islessthan( "$(du[copy])", "$(size[copy])" );

    "OK" and => { "size_match", "du_less" };

  reports:
    OK::
      "$(this.promise_filename) Pass";

    !OK::
      "$(this.promise_filename) FAIL";

    DEBUG::
      "Sparse filestat.size: $(size[sparse])";
      "Sparse du -k: '$(du[sparse])'";
      "Copy filestat.size: $(size[copy])";
      "Copy du -k: '$(du[copy])'";
    DEBUG.size_match::
      "Sparse file and copy match in apparent size.";
    DEBUG.du_less::
      "Copy of sparse file is using more bytes on disk than the real size of the file, in bytes, so it's probably non-sparse!";

}
