#!/home/product/code/webui-perl/bin/perl -T -I /home/product/code/firmware/current/lib/perl5
#SUDOER_USERS: nobody
#SUDOER_RUNAS_USERS: root, mta
#SUDOER_TAGS: NOPASSWD, EXEC
use strict;
use warnings;
use Barracuda::Sudo::Lib;
use Fcntl;
=pod

=head1 NAME

make_unique_test_file

=head1 DESCRIPTION

Touches and removes a file to see if location has write/delete permissions
via the mount point, which may not even if we are root.

=cut

use Error qw(:try);
use Barracuda::Environment;

my $in_bcs_backup = dir_prefix("/mail/bcs_backup/");
my $in_mnt = dir_prefix("/mnt/");

my ($mount_path) = get_argv(sub {
    -d $_[0] && ($in_bcs_backup->($_[0]) || $in_mnt->($_[0]))
});

my $serial = ($Barracuda::Environment::serial =~ qr/^(\d+)$/);
my $file_name = $mount_path."/Barracuda_test.".$serial.".".time.".$$.txt";
my $fh = Barracuda::Sudo::Lib::open_hardlink_path($file_name, (O_EXCL | O_CREAT));

if(!defined $fh) {
    print STDERR "Failed to create the file $file_name.\n";
    exit 1;
}
close $fh;
unlink $file_name;
exit 0;
