blob: b25d5908c36f5c72702df16e5a666d5cb8be09cc [file] [log] [blame]
// Copyright 2017 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.
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include <memory>
#include <string>
#include "smbprovider/samba_interface_impl.h"
namespace smbprovider {
std::unique_ptr<SambaInterfaceImpl> SambaInterfaceImpl::Create() {
SMBCCTX* context = smbc_new_context();
if (!context) {
LOG(ERROR) << "Could not create smbc context";
return nullptr;
}
if (!smbc_init_context(context)) {
smbc_free_context(context, 0);
LOG(ERROR) << "Could not initialize smbc context";
return nullptr;
}
smbc_set_context(context);
return std::unique_ptr<SambaInterfaceImpl>(new SambaInterfaceImpl(context));
}
int32_t SambaInterfaceImpl::OpenDirectory(const std::string& directory_path,
int32_t* dir_id) {
DCHECK(dir_id);
*dir_id = smbc_opendir(directory_path.c_str());
if (*dir_id < 0) {
*dir_id = -1;
return errno;
}
return 0;
}
int32_t SambaInterfaceImpl::CloseDirectory(int32_t dir_id) {
return smbc_closedir(dir_id) >= 0 ? 0 : errno;
}
int32_t SambaInterfaceImpl::GetDirectoryEntries(int32_t dir_id,
smbc_dirent* dirp,
int32_t dirp_buffer_size,
int32_t* bytes_read) {
DCHECK(dirp);
DCHECK(bytes_read);
*bytes_read = smbc_getdents(dir_id, dirp, dirp_buffer_size);
if (*bytes_read < 0) {
*bytes_read = -1;
return errno;
}
return 0;
}
int32_t SambaInterfaceImpl::GetEntryStatus(const std::string& full_path,
struct stat* stat) {
DCHECK(stat);
return smbc_stat(full_path.c_str(), stat) >= 0 ? 0 : errno;
}
SambaInterfaceImpl::~SambaInterfaceImpl() {
smbc_free_context(context_, 0);
}
SambaInterfaceImpl::SambaInterfaceImpl(SMBCCTX* context) : context_(context) {}
} // namespace smbprovider