mpd/test/run_storage.cxx

144 lines
2.9 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
2014-10-07 06:07:00 +02:00
#include "event/Thread.hxx"
2014-10-07 06:07:00 +02:00
#include "storage/Registry.hxx"
#include "storage/StorageInterface.hxx"
#include "storage/FileInfo.hxx"
2018-02-24 22:33:30 +01:00
#include "net/Init.hxx"
2019-05-08 15:47:58 +02:00
#include "time/ChronoUtil.hxx"
2024-05-13 10:36:15 +02:00
#include "time/ISO8601.hxx"
2018-07-17 21:56:43 +02:00
#include "util/PrintException.hxx"
2024-05-13 10:35:28 +02:00
#include "util/StringAPI.hxx"
2024-05-13 10:36:15 +02:00
#include "util/StringBuffer.hxx"
2014-10-07 06:07:00 +02:00
#include <memory>
#include <stdexcept>
2014-10-07 06:07:00 +02:00
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
2014-10-07 06:07:00 +02:00
#include <string.h>
static std::unique_ptr<Storage>
MakeStorage(EventLoop &event_loop, const char *uri)
2014-10-07 06:07:00 +02:00
{
auto storage = CreateStorageURI(event_loop, uri);
if (storage == nullptr)
throw std::runtime_error("Unrecognized storage URI");
2014-10-07 06:07:00 +02:00
return storage;
}
static int
Ls(Storage &storage, const char *path)
{
auto dir = storage.OpenDirectory(path);
2014-10-07 06:07:00 +02:00
const char *name;
while ((name = dir->Read()) != nullptr) {
const auto info = dir->GetInfo(false);
2014-10-07 06:07:00 +02:00
const char *type = "unk";
switch (info.type) {
case StorageFileInfo::Type::OTHER:
2014-10-07 06:07:00 +02:00
type = "oth";
break;
case StorageFileInfo::Type::REGULAR:
2014-10-07 06:07:00 +02:00
type = "reg";
break;
case StorageFileInfo::Type::DIRECTORY:
2014-10-07 06:07:00 +02:00
type = "dir";
break;
}
2024-05-13 10:36:15 +02:00
StringBuffer<64> mtime_buffer;
const char *mtime = " ";
if (!IsNegative(info.mtime)) {
2024-05-13 10:36:15 +02:00
mtime_buffer = FormatISO8601(info.mtime);
mtime = mtime_buffer;
}
2014-10-07 06:07:00 +02:00
printf("%s %10llu %s %s\n",
type, (unsigned long long)info.size,
mtime, name);
}
return EXIT_SUCCESS;
}
2019-12-22 19:54:31 +01:00
static int
Stat(Storage &storage, const char *path)
{
const auto info = storage.GetInfo(path, false);
switch (info.type) {
case StorageFileInfo::Type::OTHER:
printf("other\n");
break;
case StorageFileInfo::Type::REGULAR:
printf("regular\n");
break;
case StorageFileInfo::Type::DIRECTORY:
printf("directory\n");
break;
}
printf("size: %llu\n", (unsigned long long)info.size);
return EXIT_SUCCESS;
}
2014-10-07 06:07:00 +02:00
int
main(int argc, char **argv)
try {
2014-10-07 06:07:00 +02:00
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];
2018-02-24 22:33:30 +01:00
const ScopeNetInit net_init;
EventThread io_thread;
io_thread.Start();
2024-05-13 10:35:28 +02:00
if (StringIsEqual(command, "ls")) {
2014-10-07 06:07:00 +02:00
if (argc != 4) {
fprintf(stderr, "Usage: run_storage ls URI PATH\n");
return EXIT_FAILURE;
}
const char *const path = argv[3];
auto storage = MakeStorage(io_thread.GetEventLoop(),
storage_uri);
2014-10-07 06:07:00 +02:00
return Ls(*storage, path);
2024-05-13 10:35:28 +02:00
} else if (StringIsEqual(command, "stat")) {
2019-12-22 19:54:31 +01:00
if (argc != 4) {
fprintf(stderr, "Usage: run_storage stat URI PATH\n");
return EXIT_FAILURE;
}
const char *const path = argv[3];
auto storage = MakeStorage(io_thread.GetEventLoop(),
storage_uri);
return Stat(*storage, path);
2014-10-07 06:07:00 +02:00
} else {
fprintf(stderr, "Unknown command\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
2018-07-17 21:56:43 +02:00
} catch (...) {
PrintException(std::current_exception());
return EXIT_FAILURE;
2014-10-07 06:07:00 +02:00
}