2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2014-10-07 06:07:00 +02:00
|
|
|
|
2017-02-10 22:14:07 +01: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>
|
2016-10-27 08:40:40 +02:00
|
|
|
#include <stdexcept>
|
2014-10-07 06:07:00 +02:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2016-07-02 14:19:47 +02:00
|
|
|
#include <stdio.h>
|
2014-10-07 06:07:00 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2018-01-02 16:11:17 +01:00
|
|
|
static std::unique_ptr<Storage>
|
2017-02-10 22:14:07 +01:00
|
|
|
MakeStorage(EventLoop &event_loop, const char *uri)
|
2014-10-07 06:07:00 +02:00
|
|
|
{
|
2018-01-02 16:11:17 +01:00
|
|
|
auto storage = CreateStorageURI(event_loop, uri);
|
2016-10-27 08:40:40 +02:00
|
|
|
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)
|
|
|
|
{
|
2016-10-27 08:40:40 +02:00
|
|
|
auto dir = storage.OpenDirectory(path);
|
2014-10-07 06:07:00 +02:00
|
|
|
|
|
|
|
const char *name;
|
|
|
|
while ((name = dir->Read()) != nullptr) {
|
2016-10-27 08:40:40 +02:00
|
|
|
const auto info = dir->GetInfo(false);
|
2014-10-07 06:07:00 +02:00
|
|
|
|
|
|
|
const char *type = "unk";
|
|
|
|
switch (info.type) {
|
2015-02-28 20:50:15 +01:00
|
|
|
case StorageFileInfo::Type::OTHER:
|
2014-10-07 06:07:00 +02:00
|
|
|
type = "oth";
|
|
|
|
break;
|
|
|
|
|
2015-02-28 20:50:15 +01:00
|
|
|
case StorageFileInfo::Type::REGULAR:
|
2014-10-07 06:07:00 +02:00
|
|
|
type = "reg";
|
|
|
|
break;
|
|
|
|
|
2015-02-28 20:50:15 +01:00
|
|
|
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;
|
2017-01-08 10:22:25 +01:00
|
|
|
const char *mtime = " ";
|
2017-02-11 22:47:05 +01:00
|
|
|
if (!IsNegative(info.mtime)) {
|
2024-05-13 10:36:15 +02:00
|
|
|
mtime_buffer = FormatISO8601(info.mtime);
|
2017-01-08 10:22:25 +01:00
|
|
|
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)
|
2016-10-27 08:40:40 +02:00
|
|
|
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;
|
2017-02-10 22:14:07 +01:00
|
|
|
EventThread io_thread;
|
|
|
|
io_thread.Start();
|
2014-10-07 19:45:40 +02:00
|
|
|
|
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];
|
|
|
|
|
2018-01-02 16:11:17 +01:00
|
|
|
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;
|
|
|
|
}
|
2016-10-27 08:40:40 +02:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2018-07-17 21:56:43 +02:00
|
|
|
} catch (...) {
|
|
|
|
PrintException(std::current_exception());
|
2016-10-27 08:40:40 +02:00
|
|
|
return EXIT_FAILURE;
|
2014-10-07 06:07:00 +02:00
|
|
|
}
|