leaves12138 commented on code in PR #41: URL: https://github.com/apache/paimon-cpp/pull/41#discussion_r3339740830
########## src/paimon/fs/local/local_file_system.cpp: ########## @@ -0,0 +1,327 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "paimon/fs/local/local_file_system.h" + +#include <fcntl.h> + +#include <cassert> +#include <cerrno> +#include <cstdio> +#include <cstring> +#include <utility> + +#include "fmt/format.h" +#include "paimon/common/utils/path_util.h" +#include "paimon/common/utils/string_utils.h" +#include "paimon/fs/local/local_file_status.h" + +namespace paimon { + +Result<LocalFile> LocalFileSystem::ToFile(const std::string& path_string) const { + // local file system does not support path_string with scheme, e.g., "file:/tmp" will be + // rewritten to "/tmp" + PAIMON_ASSIGN_OR_RAISE(Path path, PathUtil::ToPath(path_string)); + if (!path.scheme.empty() && StringUtils::ToLowerCase(path.scheme) != "file") { + return Status::Invalid(fmt::format("invalid scheme {} for local file system", path.scheme)); + } + return LocalFile(path.path); +} + +Result<bool> LocalFileSystem::Exists(const std::string& path) const { + PAIMON_ASSIGN_OR_RAISE(LocalFile file, ToFile(path)); + return file.Exists(); +} + +Result<std::unique_ptr<InputStream>> LocalFileSystem::Open(const std::string& path) const { + PAIMON_ASSIGN_OR_RAISE(bool is_exist, Exists(path)); + if (!is_exist) { + return Status::NotExist(fmt::format("File '{}' not exists", path)); + } + PAIMON_ASSIGN_OR_RAISE(LocalFile file, ToFile(path)); + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<LocalInputStream> in, LocalInputStream::Create(file)); + return in; +} + +Result<std::unique_ptr<OutputStream>> LocalFileSystem::Create(const std::string& path, + bool overwrite) const { + PAIMON_ASSIGN_OR_RAISE(bool is_exist, Exists(path)); Review Comment: This calls `Exists(path)` before creating the parent directories. For a new file under a non-existing parent (for example `/tmp/a/b/file`), `ToFile(path).Exists()` returns an IOError because `access()` fails with `ENOENT` on the missing parent path, so `Create` returns before lines 68-71 can create the parent directory and open the output stream. The `Create` path should treat missing targets as non-existing, then create parents before opening the file; otherwise `FileSystem::Create` only works when the parent directory already exists. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
