App-FileSync

 view release on metacpan or  search on metacpan

lib/App/FileSync/Admin.pm  view on Meta::CPAN


Side Effects: reads the repo config.

Usage:

	my $info = $admin->repo_show('docs');

=head3 EXAMPLE

	my $info = $admin->repo_show('docs');
	print "trash kept $info->{config}{trash_days} days\n";

=head3 API SPECIFICATION

=head4 INPUT

	[ { name => 'name', type => 'string', min => 1 } ]

=head4 OUTPUT

	{ type => 'hashref' }

=head3 MESSAGES

	Key              Meaning                  Resolution
	admin.bad_repo   the name is not an id    check the spelling
	config.no_repo   the repo does not exist  filesync-admin repo list

=head3 FORMAL SPECIFICATION

	repo_show(n) = ⟨config ↦ load_repo(n), master ↦ master_dir(n), meta ↦ meta_dir(n), clients ↦ sort dom config.clients⟩

=cut

sub repo_show {
	my ($self, $name) = @_;

	my $store = $self->_store($name);
	return {
		config  => $store->config,
		master  => $store->master_dir(),
		meta    => $store->meta_dir(),
		clients => [ sort keys %{ $store->config->{clients} } ],
	};
}

=head2 client_allow

Purpose: let a client (or replica) sync a repo.

Args: C<repo>, C<client> (a client id), optional C<role> (C<client>, the
default, or C<replica>).

Returns: C<{ client, role }>.

Side Effects: rewrites the repo config.  Allowing an existing client again
changes its role.

Usage:

	$admin->client_allow(repo => 'docs', client => 'backup', role => 'replica');

=head3 EXAMPLE

	$admin->client_allow(repo => 'docs', client => 'laptop');

=head3 API SPECIFICATION

=head4 INPUT

	{
		repo   => { type => 'string', min => 1 },
		client => { type => 'string', optional => 1 },
		role   => { type => 'string', optional => 1, memberof => [ 'client', 'replica' ] },
	}

=head4 OUTPUT

	{ type => 'hashref', schema => { client => { type => 'string' }, role => { type => 'string' } } }

=head3 MESSAGES

	Key               Meaning                         Resolution
	admin.bad_client  the client id is not valid      letters, digits, . _ - ; at most 64
	admin.bad_role    the role is not client/replica  choose one of those
	admin.bad_repo    the repo name is not an id      check the spelling
	config.no_repo    the repo does not exist         create it first

=head3 FORMAL SPECIFICATION

	ΔRepoConfig
	pre client ∈ Id ∧ role ∈ {client, replica}
	clients' = clients ⊕ {client ↦ role}

=cut

sub client_allow {
	my $self = shift;
	my $args = named(\@_, {
		%REPO,
		client => { type => 'string', optional => 1 },
		role   => { type => 'string', optional => 1 },
	}, 'Admin::client_allow');

	my $client = $self->_valid_client($args->{client});
	my $role = $args->{role} || $ROLE_CLIENT;
	$self->_throw('admin.bad_role', { role => $role, roles => join(', ', @ROLES) })
		unless any { $_ eq $role } @ROLES;

	my $repo = $self->_valid_repo($args->{repo});
	my $config = App::FileSync::Config::load_repo($self->{root}, $repo);
	$config->{clients}{$client} = { role => $role };
	App::FileSync::Config::save_repo($self->{root}, $repo, $config);

	return { client => $client, role => $role };
}

=head2 client_list

Purpose: the clients allowed on a repo.

lib/App/FileSync/Admin.pm  view on Meta::CPAN

=head3 API SPECIFICATION

=head4 INPUT

	{
		repo   => { type => 'string', min => 1 },
		deep   => { type => 'boolean', optional => 1 },
		sample => { type => 'integer', min => 0, optional => 1 },
	}

=head4 OUTPUT

	{ type => 'hashref' }

=head3 MESSAGES

	Key              Meaning                  Resolution
	config.no_repo   the repo does not exist  check the name
	verify.*         see App::FileSync::Verify

=head3 FORMAL SPECIFICATION

	verify(r) = Verify(master_dir(r), index(r), ignore(r)) ⊕ ⟨repo ↦ r, master ↦ master_dir(r)⟩

=cut

sub verify {
	my $self = shift;
	my $args = named(\@_, {
		%REPO,
		deep   => { type => 'scalar', optional => 1 },
		sample => { type => 'integer', min => 0, optional => 1 },
	}, 'Admin::verify');

	my $repo = $args->{repo};
	my $store = $self->_store($repo);

	my $report = App::FileSync::Verify::verify(
		root    => $store->master_dir(),
		entries => $store->load_index(),
		ignore  => App::FileSync::Ignore->new(patterns => $store->config->{ignore}),
		deep    => $args->{deep},
		sample  => $args->{sample},
	);
	@{$report}{qw(repo master)} = ($repo, $store->master_dir());

	$self->{logger}->debug('verify finished', {
		repo => $repo,
		files => $report->{files},
		(map { $_ => scalar @{ $report->{$_} } } qw(modified missing extra)),
		ok => $report->{ok} ? 1 : 0,
	});

	return $report;
}

=head2 reindex

Purpose: rebuild F<master.json> from the tree, hashing every file.

Used after a manual repair (a rollback from a backup, say) so the index
describes the restored master without waiting for the next client session.
The old index is deliberately not reused: a restored file can keep its size
and mtime while its content differs, which is exactly the case reindex exists
for.

Args: C<repo>, optional C<dry_run>.

Returns: C<{ repo, master, index, files, dirs, bytes, skipped, dry_run, written }>.

Side Effects: holds the repo lock (the same one the server takes), so it never
races a session; writes the index and a journal event unless C<dry_run>.

Usage:

	my $result = $admin->reindex(repo => 'docs');

=head3 EXAMPLE

	my $plan = $admin->reindex(repo => 'docs', dry_run => 1);
	print "would index $plan->{files} files\n";

=head3 API SPECIFICATION

=head4 INPUT

	{
		repo    => { type => 'string', min => 1 },
		dry_run => { type => 'boolean', optional => 1 },
	}

=head4 OUTPUT

	{ type => 'hashref' }

=head3 MESSAGES

	Key              Meaning                              Resolution
	admin.locked     a sync session holds the repo lock   retry when it has finished
	config.no_repo   the repo does not exist              check the name

=head3 FORMAL SPECIFICATION

	ΔFS
	pre lock(r) is free
	¬dry_run ⇒ index'(r) = scan(master_dir(r))

=head3 PSEUDOCODE

	open the repo and take its lock
	walk the master, hashing everything
	unless dry run: save the index and journal the reindex
	log and report the counts

=cut

sub reindex {
	my $self = shift;
	my $args = named(\@_, { %REPO, dry_run => { type => 'scalar', optional => 1 } }, 'Admin::reindex');

	my $repo = $args->{repo};



( run in 2.515 seconds using v1.01-cache-2.11-cpan-99fa8243a42 )