-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathfile_system.h
193 lines (155 loc) · 6.47 KB
/
file_system.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2012 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FILE_SYSTEM_H
#define FILE_SYSTEM_H
#include <assert.h>
#include <errno.h>
#include <memory.h>
#include <netdb.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include <map>
#include <string>
#include "ppapi/cpp/file_ref.h"
#include "ppapi/cpp/file_system.h"
#include "ppapi/cpp/private/host_resolver_private.h"
#include "ppapi/utility/completion_callback_factory.h"
#include "file_interfaces.h"
#include "pthread_helpers.h"
class FileSystem {
public:
FileSystem(pp::Instance* instance, OutputInterface* out);
~FileSystem();
static FileStream* const kBadFileStream;
// Return current file system for process. Instance of FileSystem must be
// created before calling this function.
static FileSystem* GetFileSystem();
// Same as above function but return NULL if FileSystem doesn't exist yet.
static FileSystem* GetFileSystemNoCrash();
void WaitForStdFiles();
Cond& cond() { return cond_; }
Mutex& mutex() { return mutex_; }
pp::Instance* instance() { return instance_; }
void SetTerminalSize(unsigned short col, unsigned short row);
bool GetTerminalSize(unsigned short* col, unsigned short* row);
// Switch TCP sockets between JS and Pepper implementations.
void UseJsSocket(bool use_js);
static bool CreateNetAddress(const sockaddr* saddr, socklen_t addrlen,
PP_NetAddress_Private* addr);
static bool CreateSocketAddress(const PP_NetAddress_Private& addr,
sockaddr* saddr, socklen_t* addrlen);
// Syscall implementations.
int open(const char* pathname, int oflag, mode_t cmode, int* newfd);
int close(int fd);
int read(int fd, char* buf, size_t count, size_t* nread);
int write(int fd, const char* buf, size_t count, size_t* nwrote);
int seek(int fd, nacl_abi_off_t offset, int whence,
nacl_abi_off_t* new_offset);
int dup(int fd, int* newfd);
int dup2(int fd, int newfd);
int fstat(int fd, nacl_abi_stat* out);
int stat(const char* pathname, nacl_abi_stat* out);
void readpass(const char* prompt, char* buf, size_t buf_len, bool echo);
int isatty(int fd);
int tcgetattr(int fd, struct termios* termios_p);
int tcsetattr(int fd, int optional_actions, const struct termios* termios_p);
int fcntl(int fd, int cmd, va_list ap);
int ioctl(int fd, int request, va_list ap);
int select(int nfds, fd_set* readfds, fd_set* writefds,
fd_set* exceptfds, struct timeval* timeout);
int getaddrinfo(const char* hostname, const char* servname,
const addrinfo* hints, addrinfo** res);
void freeaddrinfo(addrinfo* ai);
int getnameinfo(const sockaddr* sa, socklen_t salen,
char* host, size_t hostlen,
char* serv, size_t servlen, int flags);
int socket(int socket_family, int socket_type, int protocol);
int connect(int sockfd, const sockaddr* serv_addr, socklen_t addrlen);
int shutdown(int sockfd, int how);
int bind(int sockfd, const sockaddr* serv_addr, socklen_t addrlen);
int listen(int sockfd, int backlog);
int accept(int sockfd, sockaddr* addr, socklen_t* addrlen);
int getsockname(int s, sockaddr* name, socklen_t* namelen);
ssize_t sendto(int sockfd, const char* buf, size_t len, int flags,
const sockaddr* dest_addr, socklen_t addrlen);
ssize_t recvfrom(int socket, char* buffer, size_t len, int flags,
sockaddr* addr, socklen_t* addrlen);
int mkdir(const char* pathname, mode_t mode);
int sigaction(int signum,
const struct sigaction* act,
struct sigaction* oldact);
void exit(int status);
void ExitCodeAcked();
void ReadPassResult(const std::string pass);
private:
typedef std::map<int, FileStream*> FileStreamMap;
typedef std::map<std::string, PathHandler*> PathHandlerMap;
typedef std::map<std::string, unsigned long> HostMap;
typedef std::map<unsigned long, std::string> AddressMap;
typedef std::map<int, int> SocketTypesMap;
struct GetAddrInfoParams {
const char* hostname;
const char* servname;
const struct addrinfo* hints;
struct addrinfo** res;
};
void AddPathHandler(const std::string& path, PathHandler* handler);
void AddFileStream(int fd, FileStream* stream);
void RemoveFileStream(int fd);
bool IsKnowDescriptor(int fd);
FileStream* GetStream(int fd);
uint32_t AddHostAddress(const char* name, uint32_t addr);
addrinfo* CreateAddrInfo(const PP_NetAddress_Private& addr,
const addrinfo* hints,
const char* name);
addrinfo* GetFakeAddress(const char* hostname, uint16_t port,
const addrinfo* hints);
bool GetHostPort(const sockaddr* serv_addr, socklen_t addrlen,
std::string* hostname, uint16_t* port);
bool IsAgentConnect(const sockaddr* serv_addr, socklen_t addrlen,
std::string* hostname, uint16_t* port);
void Resolve(int32_t result, GetAddrInfoParams* params, int32_t* pres);
void OnResolve(int32_t result, GetAddrInfoParams* params, int32_t* pres);
void OnOpen(int32_t result, pp::FileSystem* fs);
void MakeDirectory(int32_t result, const char* pathname, int32_t* pres);
void OnMakeDirectory(int32_t result, pp::FileRef* file_ref, int32_t* pres);
int GetFirstUnusedDescriptor();
int IsReady(int nfds, fd_set* fds, bool (FileStream::*is_ready)(),
bool apply);
bool IsInterrupted();
static const int kFileIDOffset = 16;
static const unsigned long kFirstAddr = 0x00000000;
static FileSystem* file_system_;
pp::Instance* instance_;
OutputInterface* output_;
Cond cond_;
Mutex mutex_;
PathHandlerMap paths_;
FileStreamMap streams_;
pp::FileSystem* ppfs_;
PathHandler* ppfs_path_handler_;
bool fs_initialized_;
pp::CompletionCallbackFactory<FileSystem> factory_;
bool exit_code_acked_;
std::string read_pass_result_;
bool read_pass_available_;
pp::HostResolverPrivate* host_resolver_;
HostMap hosts_;
AddressMap addrs_;
unsigned long first_unused_addr_;
bool use_js_socket_;
unsigned short col_;
unsigned short row_;
bool is_resize_;
void (*handler_sigwinch_)(int);
// TODO(dpolukhin): remove this map and put all socket related info into
// FileStream with type socket.
SocketTypesMap socket_types_;
DISALLOW_COPY_AND_ASSIGN(FileSystem);
};
#endif // FILE_SYSTEM_H