Skip to content

Implement deleting and checking for files #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ add_library(
STATIC
filetransfer_service_upload.cpp
filetransfer_service_download.cpp
filetransfer_service_delete.cpp
filetransfer_service_info.cpp
sha1_digest.cpp
exception_handling.cpp
)
Expand Down
25 changes: 25 additions & 0 deletions src/lib/filetransfer_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,31 @@ class FileTransferServiceImpl final : public ::ansys::api::tools::filetransfer::
::ansys::api::tools::filetransfer::v1::UploadFileRequest>* stream
) -> ::grpc::Status override;

/**
* @brief Implements the "DeleteFile" operation.
* @param context Server context to use.
* @param request Request to process.
* @param response Response to populate.
*/
virtual auto DeleteFile(
::grpc::ServerContext* context,
const ::ansys::api::tools::filetransfer::v1::DeleteFileRequest* request,
::ansys::api::tools::filetransfer::v1::DeleteFileResponse* response
) -> ::grpc::Status override;

/**
* @brief Implements the "GetFileInfo" operation.
* @param context Server context to use.
* @param request Request to process.
* @param response Response to populate.
*/
virtual auto GetFileInfo(
::grpc::ServerContext* context,
const ::ansys::api::tools::filetransfer::v1::GetFileInfoRequest*
request,
::ansys::api::tools::filetransfer::v1::GetFileInfoResponse* response
) -> ::grpc::Status override;

private:
};

Expand Down
79 changes: 79 additions & 0 deletions src/lib/filetransfer_service_delete.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (C) 2022 - 2024 ANSYS, Inc. and/or its affiliates.
// SPDX-License-Identifier: MIT
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "filetransfer_service.h"

#ifdef _MSC_VER
#pragma warning(push, 3)
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/log/trivial.hpp>

#ifdef _MSC_VER
#pragma warning(pop)
#else
#pragma GCC diagnostic pop
#endif

#include "exception_handling.h"
#include "exception_types.h"

namespace file_transfer {

auto FileTransferServiceImpl::DeleteFile(
::grpc::ServerContext* /* unused */,
const ::ansys::api::tools::filetransfer::v1::DeleteFileRequest* request,
::ansys::api::tools::filetransfer::v1::DeleteFileResponse* /* unused */
) -> ::grpc::Status {

return exceptions::convert_exceptions_to_status_codes(
std::function<void()>([&]() {
const auto& filename = request->filename();
const auto file_path = boost::filesystem::path{filename};

BOOST_LOG_TRIVIAL(info) << "Got deletion request for file: "
<< file_path.generic_string();

if (!boost::filesystem::exists(file_path)) {
throw exceptions::failed_precondition(
"File does not exist: " + filename
);
}
if (!boost::filesystem::is_regular_file(file_path)) {
throw exceptions::failed_precondition(
"Path " + filename + " is not a regular file."
);
}

boost::filesystem::remove(file_path);
BOOST_LOG_TRIVIAL(info)
<< "Deleted file: " << file_path.generic_string();
})
);
}

} // namespace file_transfer
80 changes: 80 additions & 0 deletions src/lib/filetransfer_service_info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (C) 2022 - 2024 ANSYS, Inc. and/or its affiliates.
// SPDX-License-Identifier: MIT
//
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include "filetransfer_service.h"

#ifdef _MSC_VER
#pragma warning(push, 3)
#else
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/log/trivial.hpp>
#include <boost/numeric/conversion/cast.hpp>

#ifdef _MSC_VER
#pragma warning(pop)
#else
#pragma GCC diagnostic pop
#endif

#include "exception_handling.h"
#include "exception_types.h"
#include "sha1_digest.h"

namespace file_transfer {

auto FileTransferServiceImpl::GetFileInfo(
::grpc::ServerContext* /* unused */,
const ::ansys::api::tools::filetransfer::v1::GetFileInfoRequest* request,
::ansys::api::tools::filetransfer::v1::GetFileInfoResponse* response
) -> ::grpc::Status {

return exceptions::convert_exceptions_to_status_codes(
std::function<void()>([&]() {
const auto& filename = request->filename();
const auto file_path = boost::filesystem::path{filename};
const auto file_exists = boost::filesystem::exists(file_path);
response->set_exists(file_exists);

if (file_exists) {
auto& file_info = *(response->mutable_file_info());
if (request->compute_sha1_checksum()) {
const auto hex_digest =
detail::get_sha1_hex_digest(file_path);
file_info.mutable_sha1()->set_hex_digest(hex_digest);
}

file_info.set_name(file_path.string());
const std::size_t file_size =
boost::filesystem::file_size(file_path);
file_info.set_size(boost::numeric_cast<pb_filesize_t>(file_size)
);
}
})
);
}

} // namespace file_transfer