Skip to content

Commit

Permalink
Test for removing file
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 621691408
  • Loading branch information
realp2us authored and copybara-github committed Apr 8, 2024
1 parent b9184d3 commit d1cba60
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 15 deletions.
2 changes: 2 additions & 0 deletions internal/platform/implementation/windows/file_path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ wchar_t const* kForbiddenPathNames[] = {

std::wstring FilePath::GetCustomSavePath(std::wstring parent_folder,
std::wstring file_name) {
NEARBY_LOGS(INFO) << "GetCustomSavePath called.";
std::wstring path;
path += parent_folder + kPathDelimiter + file_name;
return CreateOutputFileWithRename(path);
}

std::wstring FilePath::GetDownloadPath(std::wstring parent_folder,
std::wstring file_name) {
NEARBY_LOGS(INFO) << "GetDownloadPath called.";
return CreateOutputFileWithRename(
GetDownloadPathInternal(parent_folder, file_name));
}
Expand Down
8 changes: 8 additions & 0 deletions sharing/nearby_connections_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <string>
#include <vector>

#include "absl/container/flat_hash_set.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "sharing/common/nearby_share_enums.h"
Expand Down Expand Up @@ -162,6 +163,13 @@ class NearbyConnectionsManager {
// Sets a custom save path.
virtual void SetCustomSavePath(absl::string_view custom_save_path) = 0;

// Gets the file paths to delete.
virtual absl::flat_hash_set<std::filesystem::path>
GetUnknownFilePathsToDelete() = 0;

// Deletes the file paths to delete.
virtual void ClearUnknownFilePathsToDelete() = 0;

// Dump internal state for debugging purposes.
virtual std::string Dump() const = 0;
};
Expand Down
41 changes: 37 additions & 4 deletions sharing/nearby_connections_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <utility>
#include <vector>

#include "absl/base/attributes.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/meta/type_traits.h"
Expand All @@ -36,6 +37,7 @@
#include "internal/platform/device_info.h"
#include "internal/platform/mutex_lock.h"
#include "sharing/advertisement.h"
#include "sharing/common/compatible_u8_string.h"
#include "sharing/common/nearby_share_enums.h"
#include "sharing/constants.h"
#include "sharing/flags/generated/nearby_sharing_feature_flags.h"
Expand Down Expand Up @@ -136,8 +138,7 @@ std::string MediumSelectionToString(const MediumSelection& mediums) {
} // namespace

NearbyConnectionsManagerImpl::NearbyConnectionsManagerImpl(
Context* context,
ConnectivityManager& connectivity_manager,
Context* context, ConnectivityManager& connectivity_manager,
nearby::DeviceInfo& device_info,
std::unique_ptr<NearbyConnectionsService> nearby_connections_service)
: context_(context),
Expand Down Expand Up @@ -489,8 +490,7 @@ void NearbyConnectionsManagerImpl::Send(

if (transfer_managers_.contains(endpoint_id) && payload->content.is_file()) {
NL_LOG(INFO) << __func__ << ": Send payload " << payload->id << " to "
<< endpoint_id
<< " to transfer manager. payload is file: "
<< endpoint_id << " to transfer manager. payload is file: "
<< payload->content.is_file() << ", is bytes "
<< payload->content.is_bytes();
transfer_managers_.at(endpoint_id)
Expand Down Expand Up @@ -531,7 +531,10 @@ void NearbyConnectionsManagerImpl::RegisterPayloadStatusListener(
void NearbyConnectionsManagerImpl::RegisterPayloadPath(
int64_t payload_id, const std::filesystem::path& file_path,
ConnectionsCallback callback) {
NL_LOG(INFO) << __func__ << "RegisterPayloadPath called.";
NL_DCHECK(!file_path.empty());
NL_LOG(INFO) << __func__ << "RegisterPayloadPath called. file_path: "
<< GetCompatibleU8String(file_path.u8string());

// Create file is put into Nearby Connections, don't need to create file in
// Nearby Sharing.
Expand Down Expand Up @@ -803,13 +806,21 @@ void NearbyConnectionsManagerImpl::OnPayloadTransferUpdate(
// anything more with the payload.
auto listener_it = payload_status_listeners_.find(update.payload_id);
if (listener_it != payload_status_listeners_.end()) {
NL_LOG(INFO) << "PayloadStatusListener found for payload id="
<< update.payload_id;
std::weak_ptr<PayloadStatusListener> listener = listener_it->second;
switch (update.status) {
case PayloadStatus::kInProgress:
NL_LOG(INFO) << " PayloadStatus::kInProgress";
break;
case PayloadStatus::kSuccess:
NL_LOG(INFO) << " PayloadStatus::kSuccess";
ABSL_FALLTHROUGH_INTENDED;
case PayloadStatus::kCanceled:
NL_LOG(INFO) << " PayloadStatus::kCanceled";
ABSL_FALLTHROUGH_INTENDED;
case PayloadStatus::kFailure:
NL_LOG(INFO) << " PayloadStatus::kFailure";
payload_status_listeners_.erase(update.payload_id);
break;
}
Expand All @@ -829,8 +840,20 @@ void NearbyConnectionsManagerImpl::OnPayloadTransferUpdate(
auto payload_it = incoming_payloads_.find(update.payload_id);
if (payload_it == incoming_payloads_.end()) return;

NL_LOG(INFO) << "Payload content type="
<< (int)payload_it->second.content.type;
if (payload_it->second.content.type != PayloadContent::Type::kBytes) {
NL_LOG(WARNING) << "Received unknown payload of file type. Cancelling.";
NL_LOG(WARNING) << "endpoint_id=" << endpoint_id.data();
if (payload_it->second.content.type == PayloadContent::Type::kFile) {
NL_LOG(INFO)
<< __func__ << ": Payload is file type. file_path: "
<< GetCompatibleU8String(
payload_it->second.content.file_payload.file.path.u8string());
file_paths_to_delete_.insert(
payload_it->second.content.file_payload.file.path);
}
// if we get kFile and have file_path, delete the file path.
nearby_connections_service_->CancelPayload(kServiceId, payload_it->first,
[](Status status) {});
return;
Expand Down Expand Up @@ -898,6 +921,16 @@ void NearbyConnectionsManagerImpl::SetCustomSavePath(
});
}

absl::flat_hash_set<std::filesystem::path>
NearbyConnectionsManagerImpl::GetUnknownFilePathsToDelete() {
return file_paths_to_delete_;
}

void NearbyConnectionsManagerImpl::ClearUnknownFilePathsToDelete() {
MutexLock lock(&mutex_);
file_paths_to_delete_.clear();
}

std::string NearbyConnectionsManagerImpl::Dump() const {
return nearby_connections_service_->Dump();
}
Expand Down
11 changes: 9 additions & 2 deletions sharing/nearby_connections_manager_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ namespace sharing {
class NearbyConnectionsManagerImpl : public NearbyConnectionsManager {
public:
explicit NearbyConnectionsManagerImpl(
Context* context,
nearby::ConnectivityManager& connectivity_manager,
Context* context, nearby::ConnectivityManager& connectivity_manager,
nearby::DeviceInfo& device_info,
std::unique_ptr<NearbyConnectionsService> nearby_connections_service);
~NearbyConnectionsManagerImpl() override;
Expand Down Expand Up @@ -85,6 +84,10 @@ class NearbyConnectionsManagerImpl : public NearbyConnectionsManager {
absl::string_view endpoint_id) override;
void UpgradeBandwidth(absl::string_view endpoint_id) override;
void SetCustomSavePath(absl::string_view custom_save_path) override;
absl::flat_hash_set<std::filesystem::path> GetUnknownFilePathsToDelete()
override;
void ClearUnknownFilePathsToDelete() override;

std::string Dump() const override;

NearbyConnectionsService* GetNearbyConnectionsService() const {
Expand Down Expand Up @@ -176,6 +179,10 @@ class NearbyConnectionsManagerImpl : public NearbyConnectionsManager {
// Avoid calling to disconnect on an endpoint multiple times.
absl::flat_hash_set<std::string> disconnecting_endpoints_
ABSL_GUARDED_BY(mutex_);

// A set of file paths to delete.
absl::flat_hash_set<std::filesystem::path> file_paths_to_delete_
ABSL_GUARDED_BY(mutex_);
};

} // namespace sharing
Expand Down
3 changes: 3 additions & 0 deletions sharing/nearby_connections_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ void NearbyConnectionsServiceImpl::AcceptConnection(

switch (payload.GetType()) {
case NcPayloadType::kBytes:
NEARBY_LOGS(VERBOSE)
<< "AcceptConnection: NcPayloadType::kBytes";
[[fallthrough]];
case NcPayloadType::kFile:
payload_listener->second.payload_cb(
endpoint_id, ConvertToPayload(std::move(payload)));
Expand Down
4 changes: 4 additions & 0 deletions sharing/nearby_file_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "sharing/nearby_file_handler.h"

#include <stdint.h>

#include <filesystem> // NOLINT(build/c++17)
#include <functional>
#include <memory>
Expand Down Expand Up @@ -87,11 +88,14 @@ void NearbyFileHandler::GetUniquePath(const std::filesystem::path& file_path,
void NearbyFileHandler::DeleteFilesFromDisk(
std::vector<std::filesystem::path> file_paths,
DeleteFilesFromDiskCallback callback) {
NL_VLOG(1) << __func__ << ": DeleteFilesFromDisk called.";
sequenced_task_runner_->PostTask([callback = std::move(callback),
file_paths = std::move(file_paths)]() {
// wait 1 second to make the file being released from another process.
absl::SleepFor(absl::Seconds(1));
for (const auto& file_path : file_paths) {
NL_VLOG(1) << __func__ << ": Removeing file. File="
<< GetCompatibleU8String(file_path.u8string());
if (!std::filesystem::exists(file_path)) {
continue;
}
Expand Down
29 changes: 20 additions & 9 deletions sharing/nearby_sharing_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ void NearbySharingServiceImpl::Shutdown(
foreground_receive_callbacks_.Clear();
background_receive_callbacks_.Clear();

device_info_.UnregisterScreenLockedListener(
kScreenStateListenerName);
device_info_.UnregisterScreenLockedListener(kScreenStateListenerName);

settings_->RemoveSettingsObserver(this);

Expand Down Expand Up @@ -1160,8 +1159,7 @@ std::string NearbySharingServiceImpl::Dump() const {
sstream << " IsSendingFile: " << IsSendingFile() << std::endl;
sstream << " IsReceivingFile: " << IsReceivingFile() << std::endl;

sstream << " IsScreenLocked: " << device_info_.IsScreenLocked()
<< std::endl;
sstream << " IsScreenLocked: " << device_info_.IsScreenLocked() << std::endl;
sstream << " IsBluetoothPresent: " << IsBluetoothPresent() << std::endl;
sstream << " IsBluetoothPowered: " << IsBluetoothPowered() << std::endl;
sstream << " IsExtendedAdvertisingSupported: "
Expand Down Expand Up @@ -2535,6 +2533,8 @@ NearbySharingService::StatusCodes NearbySharingServiceImpl::SendPayloads(
void NearbySharingServiceImpl::OnUniquePathFetched(
int64_t attachment_id, int64_t payload_id,
std::function<void(Status)> callback, std::filesystem::path file_path) {
NL_LOG(WARNING) << __func__ << ": OnUniquePathFetched called. file_path="
<< GetCompatibleU8String(file_path.u8string());
attachment_info_map_[attachment_id].file_path = file_path;
nearby_connections_manager_->RegisterPayloadPath(payload_id, file_path,
std::move(callback));
Expand Down Expand Up @@ -3616,7 +3616,7 @@ void NearbySharingServiceImpl::OnReceivedIntroduction(
if (introduction_frame.has_start_transfer() &&
introduction_frame.start_transfer()) {
if (share_target.GetTotalAttachmentsSize() >=
kAttachmentsSizeThresholdOverHighQualityMedium) {
kAttachmentsSizeThresholdOverHighQualityMedium) {
NL_LOG(INFO)
<< __func__
<< ": Upgrade bandwidth when receiving an introduction frame.";
Expand Down Expand Up @@ -3966,7 +3966,6 @@ std::optional<ShareTarget> NearbySharingServiceImpl::CreateShareTarget(
absl::string_view endpoint_id, const Advertisement& advertisement,
std::optional<NearbyShareDecryptedPublicCertificate> certificate,
bool is_incoming) {

if (!advertisement.device_name() && !certificate.has_value()) {
NL_VLOG(1) << __func__
<< ": Failed to retrieve public certificate for contact "
Expand Down Expand Up @@ -4109,9 +4108,8 @@ bool NearbySharingServiceImpl::OnIncomingPayloadsComplete(

connection->SetDisconnectionListener([&, share_target_id]() {
RunOnNearbySharingServiceThread(
"disconnection_listener", [&, share_target_id]() {
UnregisterShareTarget(share_target_id);
});
"disconnection_listener",
[&, share_target_id]() { UnregisterShareTarget(share_target_id); });
});

if (!update_file_paths_in_progress_) {
Expand Down Expand Up @@ -4235,7 +4233,17 @@ void NearbySharingServiceImpl::RemoveIncomingPayloads(
NL_LOG(INFO) << __func__ << ": Cleaning up payloads due to transfer failure";
nearby_connections_manager_->ClearIncomingPayloads();
std::vector<std::filesystem::path> files_for_deletion;
auto file_paths_to_delete =
nearby_connections_manager_->GetUnknownFilePathsToDelete();
for (auto it = file_paths_to_delete.begin();
it != file_paths_to_delete.end();) {
NL_VLOG(1) << __func__ << ": file_path_to_delete="
<< GetCompatibleU8String((*it).u8string());
files_for_deletion.push_back(*it);
}
nearby_connections_manager_->ClearUnknownFilePathsToDelete();
for (const auto& file : share_target.file_attachments) {
NL_VLOG(1) << __func__ << ": file_path1=" << file.file_name().data();
if (!file.file_path().has_value()) continue;
auto file_path = *file.file_path();
NL_VLOG(1) << __func__
Expand Down Expand Up @@ -4438,6 +4446,9 @@ void NearbySharingServiceImpl::ClearOutgoingShareTargetInfoMap() {

void NearbySharingServiceImpl::SetAttachmentPayloadId(
const Attachment& attachment, int64_t payload_id) {
NL_VLOG(1) << __func__
<< ": Clearing SetAttachmentPayloadId called. payload_id="
<< payload_id;
attachment_info_map_[attachment.id()].payload_id = payload_id;
}

Expand Down

0 comments on commit d1cba60

Please sign in to comment.