@@ -21,21 +21,18 @@ namespace ErrorCodes
2121 extern const int NOT_IMPLEMENTED;
2222 extern const int BAD_ARGUMENTS;
2323 extern const int CANNOT_UNLINK;
24- extern const int CANNOT_RMDIR;
25- extern const int READONLY;
2624}
2725
28- LocalObjectStorage::LocalObjectStorage (LocalObjectStorageSettings settings_ )
29- : settings (std::move(settings_ ))
26+ LocalObjectStorage::LocalObjectStorage (String key_prefix_ )
27+ : key_prefix (std::move(key_prefix_ ))
3028 , log(getLogger(" LocalObjectStorage" ))
3129{
3230 if (auto block_device_id = tryGetBlockDeviceId (" /" ); block_device_id.has_value ())
3331 description = *block_device_id;
3432 else
3533 description = " /" ;
3634
37- if (!settings.read_only )
38- fs::create_directories (settings.key_prefix );
35+ fs::create_directories (key_prefix);
3936}
4037
4138bool LocalObjectStorage::exists (const StoredObject & object) const
@@ -72,8 +69,6 @@ std::unique_ptr<WriteBufferFromFileBase> LocalObjectStorage::writeObject( /// NO
7269 size_t buf_size,
7370 const WriteSettings & /* write_settings */ )
7471{
75- throwIfReadonly ();
76-
7772 if (mode != WriteMode::Rewrite)
7873 throw Exception (ErrorCodes::BAD_ARGUMENTS, " LocalObjectStorage doesn't support append to files" );
7974
@@ -88,52 +83,28 @@ std::unique_ptr<WriteBufferFromFileBase> LocalObjectStorage::writeObject( /// NO
8883
8984void LocalObjectStorage::removeObject (const StoredObject & object) const
9085{
91- throwIfReadonly ();
92-
9386 // / For local object storage files are actually removed when "metadata" is removed.
9487 if (!exists (object))
9588 return ;
9689
9790 if (0 != unlink (object.remote_path .data ()))
9891 ErrnoException::throwFromPath (ErrorCodes::CANNOT_UNLINK, object.remote_path , " Cannot unlink file {}" , object.remote_path );
99-
100- // / Remove empty directories.
101- fs::path dir = fs::path (object.remote_path ).parent_path ();
102- fs::path root = fs::weakly_canonical (settings.key_prefix );
103- while (dir.has_parent_path () && dir.has_relative_path () && dir != root && pathStartsWith (dir, root))
104- {
105- LOG_TEST (log, " Removing empty directory {}, has_parent_path: {}, has_relative_path: {}, root: {}, starts with root: {}" ,
106- std::string (dir), dir.has_parent_path (), dir.has_relative_path (), std::string (root), pathStartsWith (dir, root));
107-
108- std::string dir_str = dir;
109- if (0 != rmdir (dir_str.data ()))
110- {
111- if (errno == ENOTDIR || errno == ENOTEMPTY)
112- break ;
113- ErrnoException::throwFromPath (ErrorCodes::CANNOT_RMDIR, dir_str, " Cannot remove directory {}" , dir_str);
114- }
115-
116- dir = dir.parent_path ();
117- }
11892}
11993
12094void LocalObjectStorage::removeObjects (const StoredObjects & objects) const
12195{
122- throwIfReadonly ();
12396 for (const auto & object : objects)
12497 removeObject (object);
12598}
12699
127100void LocalObjectStorage::removeObjectIfExists (const StoredObject & object)
128101{
129- throwIfReadonly ();
130102 if (exists (object))
131103 removeObject (object);
132104}
133105
134106void LocalObjectStorage::removeObjectsIfExist (const StoredObjects & objects)
135107{
136- throwIfReadonly ();
137108 for (const auto & object : objects)
138109 removeObjectIfExists (object);
139110}
@@ -142,21 +113,14 @@ ObjectMetadata LocalObjectStorage::getObjectMetadata(const std::string & path) c
142113{
143114 ObjectMetadata object_metadata;
144115 LOG_TEST (log, " Getting metadata for path: {}" , path);
145-
146- auto time = fs::last_write_time (path);
147-
148116 object_metadata.size_bytes = fs::file_size (path);
149- object_metadata.etag = std::to_string (std::chrono::duration_cast<std::chrono::nanoseconds>(time.time_since_epoch ()).count ());
150117 object_metadata.last_modified = Poco::Timestamp::fromEpochTime (
151- std::chrono::duration_cast<std::chrono::seconds>(time .time_since_epoch ()).count ());
118+ std::chrono::duration_cast<std::chrono::seconds>(fs::last_write_time (path) .time_since_epoch ()).count ());
152119 return object_metadata;
153120}
154121
155122void LocalObjectStorage::listObjects (const std::string & path, RelativePathsWithMetadata & children, size_t /* max_keys */ ) const
156123{
157- if (!fs::is_directory (path))
158- return ;
159-
160124 for (const auto & entry : fs::directory_iterator (path))
161125 {
162126 if (entry.is_directory ())
@@ -183,7 +147,6 @@ void LocalObjectStorage::copyObject( // NOLINT
183147 const WriteSettings & write_settings,
184148 std::optional<ObjectAttributes> /* object_to_attributes */ )
185149{
186- throwIfReadonly ();
187150 auto in = readObject (object_from, read_settings);
188151 auto out = writeObject (object_to, WriteMode::Rewrite, /* attributes= */ {}, /* buf_size= */ DBMS_DEFAULT_BUFFER_SIZE, write_settings);
189152 copyData (*in, *out);
@@ -198,12 +161,6 @@ void LocalObjectStorage::startup()
198161{
199162}
200163
201- void LocalObjectStorage::throwIfReadonly () const
202- {
203- if (settings.read_only )
204- throw Exception (ErrorCodes::READONLY, " Local object storage `{}` is readonly" , getName ());
205- }
206-
207164std::unique_ptr<IObjectStorage> LocalObjectStorage::cloneObjectStorage (
208165 const std::string & /* new_namespace */ ,
209166 const Poco::Util::AbstractConfiguration & /* config */ ,
@@ -216,7 +173,7 @@ ObjectStorageKey
216173LocalObjectStorage::generateObjectKeyForPath (const std::string & /* path */ , const std::optional<std::string> & /* key_prefix */ ) const
217174{
218175 constexpr size_t key_name_total_size = 32 ;
219- return ObjectStorageKey::createAsRelative (settings. key_prefix , getRandomASCIIString (key_name_total_size));
176+ return ObjectStorageKey::createAsRelative (key_prefix, getRandomASCIIString (key_name_total_size));
220177}
221178
222179}
0 commit comments