run_storage.cxx 2.99 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "event/Thread.hxx"
21 22 23
#include "storage/Registry.hxx"
#include "storage/StorageInterface.hxx"
#include "storage/FileInfo.hxx"
24
#include "net/Init.hxx"
25
#include "time/ChronoUtil.hxx"
26
#include "util/PrintException.hxx"
27 28

#include <memory>
29
#include <stdexcept>
30 31 32

#include <unistd.h>
#include <stdlib.h>
33
#include <stdio.h>
34 35 36
#include <string.h>
#include <time.h>

37
static std::unique_ptr<Storage>
38
MakeStorage(EventLoop &event_loop, const char *uri)
39
{
40
	auto storage = CreateStorageURI(event_loop, uri);
41 42
	if (storage == nullptr)
		throw std::runtime_error("Unrecognized storage URI");
43 44 45 46 47 48 49

	return storage;
}

static int
Ls(Storage &storage, const char *path)
{
50
	auto dir = storage.OpenDirectory(path);
51 52 53

	const char *name;
	while ((name = dir->Read()) != nullptr) {
54
		const auto info = dir->GetInfo(false);
55 56 57

		const char *type = "unk";
		switch (info.type) {
58
		case StorageFileInfo::Type::OTHER:
59 60 61
			type = "oth";
			break;

62
		case StorageFileInfo::Type::REGULAR:
63 64 65
			type = "reg";
			break;

66
		case StorageFileInfo::Type::DIRECTORY:
67 68 69 70
			type = "dir";
			break;
		}

71 72
		char mtime_buffer[32];
		const char *mtime = "          ";
73 74
		if (!IsNegative(info.mtime)) {
			time_t t = std::chrono::system_clock::to_time_t(info.mtime);
75 76 77 78 79 80
			strftime(mtime_buffer, sizeof(mtime_buffer),
#ifdef _WIN32
				 "%Y-%m-%d",
#else
				 "%F",
#endif
81
				 gmtime(&t));
82 83
			mtime = mtime_buffer;
		}
84 85 86 87 88 89 90 91 92 93 94

		printf("%s %10llu %s %s\n",
		       type, (unsigned long long)info.size,
		       mtime, name);
	}

	return EXIT_SUCCESS;
}

int
main(int argc, char **argv)
95
try {
96 97 98 99 100 101 102 103
	if (argc < 3) {
		fprintf(stderr, "Usage: run_storage COMMAND URI ...\n");
		return EXIT_FAILURE;
	}

	const char *const command = argv[1];
	const char *const storage_uri = argv[2];

104
	const ScopeNetInit net_init;
105 106
	EventThread io_thread;
	io_thread.Start();
107

108 109 110 111 112 113 114 115
	if (strcmp(command, "ls") == 0) {
		if (argc != 4) {
			fprintf(stderr, "Usage: run_storage ls URI PATH\n");
			return EXIT_FAILURE;
		}

		const char *const path = argv[3];

116 117
		auto storage = MakeStorage(io_thread.GetEventLoop(),
					   storage_uri);
118 119 120 121 122 123

		return Ls(*storage, path);
	} else {
		fprintf(stderr, "Unknown command\n");
		return EXIT_FAILURE;
	}
124 125

	return EXIT_SUCCESS;
126 127
} catch (...) {
	PrintException(std::current_exception());
128
	return EXIT_FAILURE;
129
}