blob: 7a8d5feb5417cb3f45446b1da3023408cb292058 [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);
// 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);
// 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);
// Struct mapping to DirectoryEntryProto.
struct DirectoryEntry {
bool is_directory;
std::string name;
int64_t size;
int64_t last_modified_time;
DirectoryEntry(bool is_directory,
const std::string& name,
int64_t size,
int64_t last_modified_time)
: is_directory(is_directory),
name(name),
size(size),
last_modified_time(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);
} // namespace smbprovider
#endif // SMBPROVIDER_PROTO_H_