-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsamba_interface.h
159 lines (128 loc) · 6.54 KB
/
samba_interface.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2020 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 SMBFS_SAMBA_INTERFACE_H_
#define SMBFS_SAMBA_INTERFACE_H_
#include <libsmbclient.h>
#include <sys/types.h>
#include <memory>
#include <string>
#include <base/memory/weak_ptr.h>
namespace smbfs {
struct SmbCredential;
// Lightweight wrapper to make libsmbclient functions available without having
// to define and retrieve their function pointers in multiple places.
//
// Methods map 1:1 onto libsmbclient functions but unlike libsmbclient provide
// a consistent interface where the return value is 0 on success or errno on
// failure. Methods that natively return other data (ie. OpenFile()) do so via
// pointer-based output parameters.
//
// All methods must be called from the same thread, unless noted.
class SambaInterface {
public:
SambaInterface() = default;
virtual ~SambaInterface() = default;
SambaInterface(const SambaInterface&) = delete;
SambaInterface& operator=(const SambaInterface&) = delete;
// Weak pointer type.
using WeakPtr = ::base::WeakPtr<SambaInterface>;
// Gets a weak pointer to this object. Should only be called from, and
// the resultant pointer dereferenced from, the constructor thread.
virtual WeakPtr AsWeakPtr() = 0;
// Sets the password to be used for libsmbclient auth callbacks (if
// implemented). Can be called from any thread.
virtual void UpdateCredentials(
std::unique_ptr<SmbCredential> credentials) = 0;
// Retrieve stat information for the filesystem that backs |path| into
// |out_statvfs|.
// Returns 0 on success and errno on failure.
virtual int StatVfs(const std::string& path,
struct statvfs* out_statvfs) WARN_UNUSED_RESULT = 0;
// Opens a file at a given |file_path| with |flags| (O_RDONLY, O_WRONLY etc),
// returning a handle to it in |out_file|, which will be nullptr on failure.
// |mode| is used when using OpenFile() to create a new file.
// Returns 0 on success and errno on failure.
virtual int OpenFile(const std::string& file_path,
int flags,
mode_t mode,
SMBCFILE** out_file) WARN_UNUSED_RESULT = 0;
// Closes |file|, which is from OpenFile().
// Returns 0 on success and errno on failure.
virtual int CloseFile(SMBCFILE* file) WARN_UNUSED_RESULT = 0;
// Seeks to |offset| in |file| from position |whence|.
// Returns 0 on success and errno on failure.
virtual int SeekFile(SMBCFILE* file,
off_t offset,
int whence) WARN_UNUSED_RESULT = 0;
// Reads |count| bytes from |file| into |buf|. On success, |out_bytes_read|
// contains the number of bytes read.
// Returns 0 on success and errno on failure.
virtual int ReadFile(SMBCFILE* file,
void* buf,
size_t count,
size_t* out_bytes_read) WARN_UNUSED_RESULT = 0;
// Writes |count| bytes from |buf| into |file|. On success,
// |out_bytes_written| contains the number of bytes written. Returns 0 on
// success and errno on failure.
virtual int WriteFile(SMBCFILE* file,
const void* buf,
size_t count,
size_t* out_bytes_written) WARN_UNUSED_RESULT = 0;
// Truncates |file| to |size| bytes.
// Returns 0 on success and errno on failure.
virtual int TruncateFile(SMBCFILE* file, off_t size) WARN_UNUSED_RESULT = 0;
// Retrieves a stat structure for the file or directory at |path| into
// |out_stat|. Returns 0 on success and errno on failure.
virtual int Stat(const std::string& path,
struct stat* out_stat) WARN_UNUSED_RESULT = 0;
// Sets the access and modification times for the file or directory at |path|.
// Returns 0 on success and errno on failure.
virtual int SetUtimes(const std::string& path,
const struct timespec& atime,
const struct timespec& mtime) = 0;
// Renames |old_path| to |new_path|.
// Returns 0 on success and errno on failure.
virtual int Rename(const std::string& old_path,
const std::string& new_path) WARN_UNUSED_RESULT = 0;
// Removes the file at |file_path|. Use RmDir() to remove a directory.
// Returns 0 on success and errno on failure.
virtual int UnlinkFile(const std::string& file_path) WARN_UNUSED_RESULT = 0;
// Creates directory at |dir_path| with mode |mode|.
// Returns 0 on success and errno on failure.
virtual int CreateDirectory(const std::string& dir_path,
mode_t mode) WARN_UNUSED_RESULT = 0;
// Opens directory at |dir_path|, returning a handle to it in |out_dir|,
// which will be nullptr on failure.
// Returns 0 on success and errno on failure.
virtual int OpenDirectory(const std::string& dir_path,
SMBCFILE** out_dir) WARN_UNUSED_RESULT = 0;
// Closes |dir|, which is from OpenDirectory().
// Returns 0 on success and errno on failure.
virtual int CloseDirectory(SMBCFILE* dir) WARN_UNUSED_RESULT = 0;
// Seeks |offset| entries into |directory|. |offset| should be 0 to seek to
// the beginning of the directory, or a value returned by TellDirectory().
// Returns 0 on success and errno on failure.
virtual int SeekDirectory(SMBCFILE* dir, off_t offset) WARN_UNUSED_RESULT = 0;
// Retrieve the current offset in |dir| into |out_offset|. The value returned
// in |out_offset| is opaque and should not be interpreted. It should only be
// passed into SeekDirectory().
// Returns 0 on success and errno on failure.
virtual int TellDirectory(SMBCFILE* dir,
off_t* out_offset) WARN_UNUSED_RESULT = 0;
// Reads a directory entry from |dir|, returning metadata into
// |out_file_info| and an associated stat structure into |out_stat|. Is used
// in conjunction with SeekDirectory() to set the read offset. If no more
// directory entries are available it will return 0 and set |out_file_info|
// to a nullptr and |out_stat| to an empty struct.
// Returns 0 on success and errno on failure.
virtual int ReadDirectory(SMBCFILE* dir,
const struct libsmb_file_info** out_file_info,
struct stat* out_stat) WARN_UNUSED_RESULT = 0;
// Removes the directory at |dir_path|.
// Returns 0 on success and errno on failure.
virtual int RemoveDirectory(const std::string& dir_path)
WARN_UNUSED_RESULT = 0;
};
} // namespace smbfs
#endif // SMBFS_SAMBA_INTERFACE_H_