00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef __EMULATORUTILITY_VIRTUALSYSTEM_H__
00033 #define __EMULATORUTILITY_VIRTUALSYSTEM_H__
00034
00035 #include "SysDeps/posix.h"
00036
00037 namespace Onikiri {
00038 namespace EmulatorUtility {
00039 typedef POSIX::posix_struct_stat HostStat;
00040
00041
00042 class FDConv
00043 {
00044 public:
00045 static const int InvalidFD = -1;
00046
00047 FDConv();
00048 ~FDConv();
00049
00050
00051 int TargetToHost(int targetFD) const;
00052 int HostToTarget(int hostFD) const;
00053
00054
00055 bool AddMap(int targetFD, int hostFD);
00056
00057
00058 bool RemoveMap(int targetFD);
00059
00060
00061 int GetFirstFreeFD();
00062 private:
00063 void ExtendFDMap();
00064 void ExtendFDMap(size_t size);
00065
00066
00067 std::vector<int> m_FDTargetToHostTable;
00068 };
00069
00070
00071
00072
00073 class DelayUnlinker
00074 {
00075 public:
00076 DelayUnlinker();
00077 ~DelayUnlinker();
00078
00079
00080 bool AddMap(int targetFD, std::string path);
00081
00082 bool RemoveMap(int targetFD);
00083
00084 std::string GetMapPath(int targetFD);
00085
00086 bool AddUnlinkPath(std::string path);
00087
00088 bool IfUnlinkable(int targetFD);
00089
00090 bool RemoveUnlinkPath(std::string path);
00091 private:
00092 std::map<int, std::string> m_targetFDToPathTable;
00093 std::list<std::string> m_delayUnlinkPathList;
00094 };
00095
00096
00097
00098
00099
00100 class VirtualSystem : public ParamExchange
00101 {
00102 public:
00103
00104 VirtualSystem();
00105 ~VirtualSystem();
00106
00107
00108 bool AddFDMap(int targetFD, int hostFD, bool autoclose = false);
00109
00110 void SetInitialWorkingDir(const boost::filesystem::path& dir);
00111
00112
00113 int GetErrno();
00114
00115 int GetPID();
00116 int GetUID();
00117 int GetEUID();
00118 int GetGID();
00119 int GetEGID();
00120
00121 char* GetCWD(char* buf, int maxlen);
00122 int ChDir(const char* path);
00123
00124
00125 int Open(const char* filename,int oflag);
00126
00127 int Read(int targetFD, void *buffer, unsigned int count);
00128 int Write(int targetFD, void* buffer, unsigned int count);
00129 int Close(int targetFD);
00130
00131 int Stat(const char* path, HostStat* s);
00132 int FStat(int fd, HostStat* s);
00133 int LStat(const char* path, HostStat* s);
00134
00135 s64 LSeek(int fd, s64 offset, int whence);
00136 int Dup(int fd);
00137
00138 int Access(const char* path, int mode);
00139 int Unlink(const char* path);
00140 int Rename(const char* oldpath, const char* newpath);
00141
00142 int Truncate(const char* path, s64 length);
00143 int FTruncate(int fd, s64 length);
00144
00145 int MkDir(const char* path, int mode);
00146
00147
00148 int FDTargetToHost(int targetFD) const
00149 {
00150 return m_fdConv.TargetToHost(targetFD);
00151 }
00152 int FDHostToTarget(int hostFD) const
00153 {
00154 return m_fdConv.HostToTarget(hostFD);
00155 }
00156
00157
00158 s64 GetTime();
00159 s64 GetClock();
00160 void AddInsnTick()
00161 {
00162 m_executedInsnTick++;
00163 }
00164 s64 GetInsnTick()
00165 {
00166 return m_executedInsnTick;
00167 }
00168
00169 DelayUnlinker* GetDelayUnlinker(){
00170 return &m_delayUnlinker;
00171 };
00172
00173 BEGIN_PARAM_MAP("/Session/Emulator/System/Time/")
00174 PARAM_ENTRY( "@UnixTime", m_unixTime )
00175 PARAM_ENTRY( "@EmulationMode", m_timeEmulationModeStr )
00176 END_PARAM_MAP()
00177 private:
00178 boost::filesystem::path GetHostPath(const char* targetPath);
00179 void AddAutoCloseFD(int fd);
00180 void RemoveAutoCloseFD(int fd);
00181
00182 FDConv m_fdConv;
00183
00184 std::vector<int> m_autoCloseFD;
00185 DelayUnlinker m_delayUnlinker;
00186
00187 boost::filesystem::path m_cwd;
00188
00189
00190 int EmulationModeStrToInt( const std::string& );
00191 u64 m_unixTime;
00192 u64 m_executedInsnTick;
00193 std::string m_timeEmulationModeStr;
00194 int m_timeEmulationMode;
00195 enum {
00196 TIME_HOST,
00197 TIME_FIXED,
00198 TIME_INSTRUCTION,
00199 };
00200 };
00201
00202 }
00203 }
00204
00205 #endif