#######################################################
#
# Copy a file, then ensure that subsequent create=true doesn't
# overwrite mode, size
#
#######################################################

body common control
{
      inputs => { "../../default.sub.cf" };
      bundlesequence  => { default("$(this.promise_filename)") };
      version => "1.0";
}

#######################################################

bundle agent init
{
  vars:
    any::
      "mode" int => "01751";

    pass2.!windows::
      "expect[permoct]" string => "$(test.mode)";
      "expect[uid]" string => "0";
      "expect[gid]" string => "0";

    pass2.any::
      "expect[nlink]" string => "1";
      "expect[size]" string => "0";
      "fields" slist => getindices("expect");
      "result[$(fields)]" string => filestat("$(G.testfile)", "$(fields)");

  files:
      "$(G.testfile)"
      copy_from => init_copy("$(G.etc_group)"),
      perms => init_perms("$(mode)"),
      classes => init_set_class("pass2");
}

body copy_from init_copy(file)
{
      source => "$(file)";
}

body perms init_perms(m)
{
      mode => "$(m)";
}

body classes init_set_class(class)
{
      promise_kept => { "$(class)" };
      promise_repaired => { "$(class)" };
}

#######################################################

bundle agent test
{

  files:
      "$(G.testfile)"
      create => "true",
      perms => test_perms("$(init.mode)");
}

body perms test_perms(m)
{
      mode => "$(m)";
}

#######################################################

bundle agent check
{
  vars:
    !windows::
      "expect[permoct]" string => "$(test.mode)";
      "expect[uid]" string => "0";
      "expect[gid]" string => "0";
    any::
      "expect[nlink]" string => "1";
      "expect[size]" string => "0";

      "fields" slist => getindices("expect");
      "result[$(fields)]" string => filestat("$(G.testfile)", "$(fields)");

  classes:
      "ok" expression => strcmp("$(init.result)", "$(result)");
      "not_ok" not => regcmp("$(init.result[$(fields)])", "$(result[$(fields)])");

  reports:
    DEBUG::
      "expected: $(fields) = '$(init.result[$(fields)])'";
      "got:      $(fields) = '$(check.result[$(fields)])'";
    !not_ok::
      "$(this.promise_filename) Pass";
    not_ok::
      "$(this.promise_filename) FAIL";
}

### PROJECT_ID: core
### CATEGORY_ID: 27
