blob: 93a2adcde0b871661326ce644b96e431e38b9eaa [file] [log] [blame]
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SMBPROVIDER_PROTO_H_
#define SMBPROVIDER_PROTO_H_
#include <string>
#include <vector>
#include <base/logging.h>
#include "smbprovider/proto_bindings/directory_entry.pb.h"
namespace smbprovider {
// Used as buffer for serialized protobufs.
using ProtoBlob = std::vector<uint8_t>;
// Serializes |proto| to the byte array |proto_blob|. Returns ERROR_OK on
// success and ERROR_FAILED on failure.
template <typename ProtoType>
ErrorType SerializeProtoToBlob(const ProtoType& proto, ProtoBlob* proto_blob) {
DCHECK(proto_blob);
proto_blob->resize(proto.ByteSizeLong());
return proto.SerializeToArray(proto_blob->data(), proto.ByteSizeLong())
? ERROR_OK
: ERROR_FAILED;
}
// Helper method to check whether a Proto has valid fields.
bool IsValidOptions(const MountOptionsProto& options);
bool IsValidOptions(const UnmountOptionsProto& options);
bool IsValidOptions(const ReadDirectoryOptionsProto& options);
bool IsValidOptions(const GetMetadataEntryOptionsProto& options);
bool IsValidOptions(const OpenFileOptionsProto& options);
bool IsValidOptions(const CloseFileOptionsProto& options);
bool IsValidOptions(const DeleteEntryOptionsProto& options);
bool IsValidOptions(const ReadFileOptionsProto& options);
bool IsValidOptions(const CreateFileOptionsProto& options);
bool IsValidOptions(const TruncateOptionsProto& options);
bool IsValidOptions(const WriteFileOptionsProto& options);
bool IsValidOptions(const CreateDirectoryOptionsProto& options);
bool IsValidOptions(const MoveEntryOptionsProto& options);
bool IsValidOptions(const CopyEntryOptionsProto& options);
bool IsValidOptions(const GetDeleteListOptionsProto& options);
bool IsValidOptions(const GetSharesOptionsProto& options);
// Helper method to get the entry path from a proto.
std::string GetEntryPath(const ReadDirectoryOptionsProto& options);
std::string GetEntryPath(const GetMetadataEntryOptionsProto& options);
std::string GetEntryPath(const OpenFileOptionsProto& options);
std::string GetEntryPath(const DeleteEntryOptionsProto& options);
std::string GetEntryPath(const CreateFileOptionsProto& options);
std::string GetEntryPath(const TruncateOptionsProto& options);
std::string GetEntryPath(const CreateDirectoryOptionsProto& options);
std::string GetEntryPath(const GetDeleteListOptionsProto& options);
std::string GetSourcePath(const MoveEntryOptionsProto& options);
std::string GetDestinationPath(const MoveEntryOptionsProto& options);
std::string GetSourcePath(const CopyEntryOptionsProto& options);
std::string GetDestinationPath(const CopyEntryOptionsProto& options);
// Helper method to get the corresponding method name for each proto.
const char* GetMethodName(const MountOptionsProto& unused);
const char* GetMethodName(const UnmountOptionsProto& unused);
const char* GetMethodName(const GetMetadataEntryOptionsProto& unused);
const char* GetMethodName(const ReadDirectoryOptionsProto& unused);
const char* GetMethodName(const OpenFileOptionsProto& unused);
const char* GetMethodName(const CloseFileOptionsProto& unused);
const char* GetMethodName(const DeleteEntryOptionsProto& unused);
const char* GetMethodName(const ReadFileOptionsProto& unused);
const char* GetMethodName(const CreateFileOptionsProto& unused);
const char* GetMethodName(const TruncateOptionsProto& unused);
const char* GetMethodName(const WriteFileOptionsProto& unused);
const char* GetMethodName(const CreateDirectoryOptionsProto& unused);
const char* GetMethodName(const MoveEntryOptionsProto& unused);
const char* GetMethodName(const CopyEntryOptionsProto& unused);
const char* GetMethodName(const GetDeleteListOptionsProto& unused);
const char* GetMethodName(const GetSharesOptionsProto& unused);
// Struct mapping to DirectoryEntryProto.
struct DirectoryEntry {
bool is_directory;
std::string name;
std::string full_path;
int64_t size;
int64_t last_modified_time;
DirectoryEntry() = default;
DirectoryEntry(bool is_directory,
const std::string& name,
const std::string& full_path,
int64_t size,
int64_t last_modified_time)
: is_directory(is_directory),
name(name),
full_path(full_path),
size(size),
last_modified_time(last_modified_time) {}
DirectoryEntry(bool is_directory,
const std::string& name,
const std::string& full_path)
: DirectoryEntry(is_directory,
name,
full_path,
-1 /* size */,
-1 /* last_modified_time */) {}
};
// Converts a vector of DirectoryEnts into a DirectoryEntryListProto.
void SerializeDirEntryVectorToProto(
const std::vector<DirectoryEntry>& entries_vector,
DirectoryEntryListProto* entries_proto);
void AddDirectoryEntry(const DirectoryEntry& entry,
DirectoryEntryListProto* proto);
void ConvertToProto(const DirectoryEntry& entry, DirectoryEntryProto* proto);
void AddToDeleteList(const std::string& entry_path, DeleteListProto* proto);
} // namespace smbprovider
#endif // SMBPROVIDER_PROTO_H_