run_storage.cxx 3.01 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */

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

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

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

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

	return storage;
}

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

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

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

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

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

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

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

	return EXIT_SUCCESS;
}

int
main(int argc, char **argv)
96
try {
97 98 99 100 101 102 103 104
	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];

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

109 110 111 112 113 114 115 116
	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];

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

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

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