123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199 |
- #include "Common.h"
- #include "BFPlatform.h"
- #include <sys/stat.h>
- #include <sys/sysinfo.h>
- #include <sys/wait.h>
- #include <dlfcn.h>
- #include <wchar.h>
- #include <fcntl.h>
- #include <time.h>
- #include <link.h>
- #include <dlfcn.h>
- #include <dirent.h>
- #include <syslog.h>
- #include <unistd.h>
- #include <execinfo.h>
- #include <signal.h>
- #include <spawn.h>
- #include "../PlatformInterface.h"
- #include "../PlatformHelper.h"
- #include "../util/CritSect.h"
- #include "../util/Dictionary.h"
- #include "../util/Hash.h"
- #include "backtrace.h"
- #include "backtrace-supported.h"
- #include "../third_party/stb/stb_sprintf.h"
- #include <cxxabi.h>
- #include <random>
- USING_NS_BF;
- struct BfpPipeInfo
- {
- String mPipePath;
- int mWriteHandle;
- };
- struct BfpFile
- {
- BfpPipeInfo* mPipeInfo;
- int mHandle;
- bool mNonBlocking;
- bool mAllowTimeout;
- bool mIsStd;
- BfpFile()
- {
- mPipeInfo = NULL;
- mHandle = -1;
- mNonBlocking = false;
- mAllowTimeout = false;
- mIsStd = false;
- }
- BfpFile(int handle)
- {
- mPipeInfo = NULL;
- mHandle = handle;
- mNonBlocking = false;
- mAllowTimeout = false;
- mIsStd = false;
- }
- ~BfpFile()
- {
- delete mPipeInfo;
- }
- };
- BfpTimeStamp BfpToTimeStamp(const timespec& ts)
- {
- return (int64)(ts.tv_sec * 10000000) + (int64)(ts.tv_nsec / 100) + 116444736000000000;
- }
- int gBFPlatformLastError = 0;
- uint32 Beefy::BFTickCount()
- {
- struct timespec now;
- if (clock_gettime(CLOCK_MONOTONIC, &now))
- return 0;
- return (uint32)((uint64)now.tv_sec * 1000.0 + (uint64)now.tv_nsec / 1000000);
- }
- int64 Beefy::EndianSwap(int64 val)
- {
- return __builtin_bswap64(val);
- }
- /*int* GetStdHandle(int32 handleId)
- {
- if (handleId == STD_INPUT_HANDLE)
- return (int*)STDIN_FILENO;
- if (handleId == STD_OUTPUT_HANDLE)
- return (int*)STDOUT_FILENO;
- return (int*)STDERR_FILENO;
- }*/
- /*int32 GetFileType(HANDLE fileHandle)
- {
- if (isatty(file->mHandleHandle))
- return FILE_TYPE_CHAR;
- return FILE_TYPE_DISK;
- }*/
- /*bool WriteFile(HANDLE hFile, void* lpBuffer, uint32 nNumberOfBytesToWrite, uint32* lpNumberOfBytesWritten, OVERLAPPED* lpOverlapped)
- {
- #ifdef BF_PLATFORM_IOS
- int logType = -1;
- if (hFile == (int*)STDOUT_FILENO)
- logType = LOG_WARNING;
- else if (hFile == (int*)STDERR_FILENO)
- logType = LOG_ERR;
-
- if (logType != -1)
- {
- static std::string strOut;
- strOut.resize(nNumberOfBytesToWrite);
- memcpy(&strOut[0], lpBuffer, nNumberOfBytesToWrite);
- if ((strOut[0] != '\r') && (strOut[0] != '\n'))
- syslog(LOG_WARNING, "%s", strOut.c_str());
- }
- #endif
-
- int writeCount = (int)::write((int)(intptr)hFile, lpBuffer, nNumberOfBytesToWrite);
- if (writeCount == -1)
- {
- //TODO: set gBFPlatformLastError
- lpNumberOfBytesWritten = 0;
- return false;
- }
-
- *lpNumberOfBytesWritten = (uint32)writeCount;
- return true;
- }*/
- int64 Beefy::GetFileTimeWrite(const StringImpl& path)
- {
- struct stat statbuf = {0};
- int result = stat(path.c_str(), &statbuf);
- if (result == -1)
- return 0;
-
- //int64 fileTime = 0;
- //BFSystemTimeToFileTime(statbuf.st_mtime, 0, &fileTime);
- return statbuf.st_mtime;
- }
- /*DWORD GetTimeZoneInformation(TIME_ZONE_INFORMATION* lpTimeZoneInformation)
- {
- std::wstring tzName0 = Beefy::UTF8Decode(tzname[0]);
- std::wstring tzName1 = Beefy::UTF8Decode(tzname[1]);
-
- bool isDST = false;
-
- time_t timeNow;
- time(&timeNow);
- tm tmNow = *gmtime(&timeNow);
- isDST = tmNow.tm_isdst;
-
- struct tm checkTM;
- memset(&checkTM, 0, sizeof(tm));
- checkTM.tm_mday = 1;
- checkTM.tm_year = tmNow.tm_year;
- time_t checkTime = mktime(&checkTM);
-
- time_t lastOffset = 0;
- time_t minOffset = 0;
- time_t maxOffset = 0;
-
- for (int pass = 0; pass < 2; pass++)
- {
- int searchDir = 60*60*24;
- int thresholdCount = 0;
-
- while (true)
- {
- checkTime += searchDir;
-
- tm checkTM = *gmtime(&checkTime);
-
- if (checkTM.tm_year != tmNow.tm_year)
- break; // No DST
-
- mktime(&checkTM);
-
- time_t offset = checkTM.tm_gmtoff;
- if (lastOffset != offset)
- {
- if (thresholdCount == 0)
- {
- minOffset = offset;
- maxOffset = offset;
- }
- else if (thresholdCount == 3)
- {
- SYSTEMTIME* sysTimeP = (offset == minOffset) ?
- &lpTimeZoneInformation->StandardDate :
- &lpTimeZoneInformation->DaylightDate;
-
- if (offset == minOffset)
- tzName0 = Beefy::UTF8Decode(checkTM.tm_zone);
- else
- tzName1 = Beefy::UTF8Decode(checkTM.tm_zone);
-
- sysTimeP->wDay = 0;
- sysTimeP->wDayOfWeek = 0;
- sysTimeP->wYear = checkTM.tm_year + 1900;
- sysTimeP->wMonth = checkTM.tm_mon;
- sysTimeP->wDay = checkTM.tm_mday + 1;
- sysTimeP->wHour = checkTM.tm_hour;
- sysTimeP->wMinute = checkTM.tm_min;
- sysTimeP->wSecond = checkTM.tm_sec;
- sysTimeP->wMilliseconds = 0;
-
- break;
- }
- else
- {
- if (thresholdCount == 1)
- searchDir /= -24;
- else
- searchDir /= -60;
- minOffset = std::min(minOffset, offset);
- maxOffset = std::max(maxOffset, offset);
- }
- thresholdCount++;
- lastOffset = offset;
- }
- }
- }
-
- wcsncpy(lpTimeZoneInformation->StandardName, tzName0.c_str(), 32);
- wcsncpy(lpTimeZoneInformation->DaylightName, tzName1.c_str(), 32);
-
- lpTimeZoneInformation->DaylightBias = (int32)maxOffset;
- lpTimeZoneInformation->StandardBias = (int32)minOffset;
-
- if (minOffset == maxOffset)
- return 0;
- return isDST ? 2 : 1;
- }*/
- bool Beefy::FileExists(const StringImpl& path, String* outActualName)
- {
- struct stat statbuf = {0};
- int result = stat(path.c_str(), &statbuf);
- if (result != 0)
- return false;
- return !S_ISDIR(statbuf.st_mode);
- }
- bool Beefy::DirectoryExists(const StringImpl& path, String* outActualName)
- {
- struct stat statbuf = {0};
- int result = stat(path.c_str(), &statbuf);
- if (result != 0)
- return false;
- return S_ISDIR(statbuf.st_mode);
- }
- uint64 Beefy::BFGetTickCountMicro()
- {
- struct timespec now;
- if (clock_gettime(CLOCK_MONOTONIC, &now))
- return 0;
- return ((uint64)now.tv_sec * 1000000.0 + (uint64)now.tv_nsec / 1000);
- }
- uint64 Beefy::BFGetTickCountMicroFast()
- {
- return BFGetTickCountMicro();
- }
- /*
- int64 abs(int64 val)
- {
- return llabs(val);
- }
- */
- void mkdir(const char* path)
- {
- mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
- }
- typedef void(*CrashInfoFunc)();
- static CritSect gSysCritSect;
- static String gCrashInfo;
- static Array<CrashInfoFunc> gCrashInfoFuncs;
- struct bt_ctx {
- struct backtrace_state *state;
- int error;
- };
- static void error_callback(void *data, const char *msg, int errnum)
- {
- struct bt_ctx *ctx = (bt_ctx*)data;
- fprintf(stderr, "ERROR: %s (%d)", msg, errnum);
- ctx->error = 1;
- }
- static void syminfo_callback (void *data, uintptr_t pc, const char *symname, uintptr_t symval, uintptr_t symsize)
- {
- char str[4096];
- if (symname)
- stbsp_snprintf(str, 4096, "%@ %s\n", pc, symname);
- else
- stbsp_snprintf(str, 4096, "%@\n", pc);
- fputs(str, stderr);
- }
- static int full_callback(void *data, uintptr_t pc, const char* filename, int lineno, const char* function)
- {
- struct bt_ctx *ctx = (bt_ctx*)data;
- if (function)
- {
- int status = -1;
- char* demangledName = abi::__cxa_demangle(function, NULL, NULL, &status );
- const char* showName = (demangledName != NULL) ? demangledName : function;
- char str[4096];
- stbsp_snprintf(str, 4096, "%@ %s %s:%d\n", pc, showName, filename?filename:"??", lineno);
- fputs(str, stderr);
- if (demangledName != NULL)
- free(demangledName);
- }
- else
- backtrace_syminfo (ctx->state, pc, syminfo_callback, error_callback, data);
- return 0;
- }
- static int simple_callback(void *data, uintptr_t pc)
- {
- struct bt_ctx *ctx = (bt_ctx*)data;
- backtrace_pcinfo(ctx->state, pc, full_callback, error_callback, data);
- return 0;
- }
- static inline void bt(struct backtrace_state *state)
- {
- struct bt_ctx ctx = {state, 0};
- //backtrace_print(state, 0, stdout);
- backtrace_simple(state, 2, simple_callback, error_callback, &ctx);
- }
- static String gCmdLine;
- static String gExePath;
- static void Crashed()
- {
- //
- {
- AutoCrit autoCrit(gSysCritSect);
- String debugDump;
- debugDump += "**** FATAL APPLICATION ERROR ****\n";
- for (auto func : gCrashInfoFuncs)
- func();
- if (!gCrashInfo.IsEmpty())
- {
- debugDump += gCrashInfo;
- debugDump += "\n";
- }
- fprintf(stderr, "%s", debugDump.c_str());
- }
- struct backtrace_state *state = backtrace_create_state(gExePath.c_str(), BACKTRACE_SUPPORTS_THREADS, error_callback, NULL);
- bt(state);
- /*void* array[10];
- size_t size;
- char** strings;
- size_t i;
- size = backtrace(array, 10);
- strings = backtrace_symbols(array, size);
- for (i = 0; i < size; i++)
- fprintf(stderr, "%s\n", strings[i]);
- free(strings);*/
- exit(1);
- }
- static void SigHandler(int sig)
- {
- //printf("SigHandler paused...\n");
- const char* sigName = NULL;
- switch (sig)
- {
- case SIGFPE:
- sigName = "SIGFPE";
- break;
- case SIGSEGV:
- sigName = "SIGSEGV";
- break;
- case SIGABRT:
- sigName = "SIGABRT";
- break;
- case SIGILL:
- sigName = "SIGILL";
- break;
- }
- if (sigName != NULL)
- gCrashInfo += StrFormat("Signal: %s\n", sigName);
- else
- gCrashInfo += StrFormat("Signal: %d\n", sig);
- Crashed();
- }
- BFP_EXPORT void BFP_CALLTYPE BfpSystem_Init(int version, BfpSystemInitFlags flags)
- {
- if (version != BFP_VERSION)
- {
- BfpSystem_FatalError(StrFormat("Bfp build version '%d' does not match requested version '%d'", BFP_VERSION, version).c_str(), "BFP FATAL ERROR");
- }
- signal(SIGSEGV, SigHandler);
- signal(SIGFPE, SigHandler);
- signal(SIGABRT, SigHandler);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpSystem_SetCommandLine(int argc, char** argv)
- {
- char* relPath = argv[0];
- char* cwd = getcwd(NULL, 0);
- gExePath = GetAbsPath(relPath, cwd);
- free(cwd);
- for (int i = 0; i < argc; i++)
- {
- if (i != 0)
- gCmdLine.Append(' ');
- String arg = argv[i];
- if ((arg.Contains(' ')) || (arg.Contains('\"')))
- {
- arg.Replace("\"", "\\\"");
- gCmdLine.Append("\"");
- gCmdLine.Append(arg);
- gCmdLine.Append("\"");
- }
- else
- gCmdLine.Append(arg);
- }
- }
- BFP_EXPORT void BFP_CALLTYPE BfpSystem_SetCrashReportKind(BfpCrashReportKind crashReportKind)
- {
- }
- BFP_EXPORT void BFP_CALLTYPE BfpSystem_AddCrashInfoFunc(BfpCrashInfoFunc crashInfoFunc)
- {
- AutoCrit autoCrit(gSysCritSect);
- gCrashInfoFuncs.Add(crashInfoFunc);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpSystem_AddCrashInfo(const char* str) // Can do at any time, or during CrashInfoFunc callbacks
- {
- AutoCrit autoCrit(gSysCritSect);
- gCrashInfo.Append(str);
- }
- void BfpSystem_Shutdown()
- {
- }
- BFP_EXPORT uint32 BFP_CALLTYPE BfpSystem_TickCount()
- {
- return Beefy::BFTickCount();
- }
- BFP_EXPORT BfpTimeStamp BFP_CALLTYPE BfpSystem_GetTimeStamp()
- {
- struct timeval tv;
- BfpTimeStamp result = 11644473600LL;
- gettimeofday(&tv, NULL);
- result += tv.tv_sec;
- result *= 10000000LL;
- result += tv.tv_usec * 10;
- return result;
- }
- BFP_EXPORT uint16 BFP_CALLTYPE BfpSystem_EndianSwap16(uint16 val)
- {
- return __builtin_bswap16(val);
- }
- BFP_EXPORT uint32 BFP_CALLTYPE BfpSystem_EndianSwap32(uint32 val)
- {
- return __builtin_bswap32(val);
- }
- BFP_EXPORT uint64 BFP_CALLTYPE BfpSystem_EndianSwap64(uint64 val)
- {
- return __builtin_bswap64(val);
- }
- BFP_EXPORT uint32 BFP_CALLTYPE BfpSystem_InterlockedExchange32(uint32* ptr, uint32 val)
- {
- // __sync_lock_test_and_set only has Acquire semantics, so we need a __sync_synchronize to enforce a full barrier
- uint32 prevVal = __sync_lock_test_and_set(ptr, val);
- __sync_synchronize();
- return prevVal;
- }
- BFP_EXPORT uint64 BFP_CALLTYPE BfpSystem_InterlockedExchange64(uint64* ptr, uint64 val)
- {
- // __sync_lock_test_and_set only has Acquire semantics, so we need a __sync_synchronize to enforce a full barrier
- uint64 prevVal = __sync_lock_test_and_set(ptr, val);
- __sync_synchronize();
- return prevVal;
- }
- BFP_EXPORT uint32 BFP_CALLTYPE BfpSystem_InterlockedExchangeAdd32(uint32* ptr, uint32 val)
- {
- return __sync_fetch_and_add(ptr, val);
- }
- BFP_EXPORT uint64 BFP_CALLTYPE BfpSystem_InterlockedExchangeAdd64(uint64* ptr, uint64 val)
- {
- return __sync_fetch_and_add(ptr, val);
- }
- BFP_EXPORT uint32 BFP_CALLTYPE BfpSystem_InterlockedCompareExchange32(uint32* ptr, uint32 oldVal, uint32 newVal)
- {
- return __sync_val_compare_and_swap(ptr, oldVal, newVal);
- }
- BFP_EXPORT uint64 BFP_CALLTYPE BfpSystem_InterlockedCompareExchange64(uint64* ptr, uint64 oldVal, uint64 newVal)
- {
- return __sync_val_compare_and_swap(ptr, oldVal, newVal);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpSystem_FatalError(const char* error, const char* title)
- {
- fprintf(stderr, "%s\n", error);
- fflush(stderr);
- Crashed();
- }
- BFP_EXPORT void BFP_CALLTYPE BfpSystem_GetCommandLine(char* outStr, int* inOutStrSize, BfpSystemResult* outResult)
- {
- TryStringOut(gCmdLine, outStr, inOutStrSize, (BfpResult*)outResult);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpSystem_GetExecutablePath(char* outStr, int* inOutStrSize, BfpSystemResult* outResult)
- {
- TryStringOut(gExePath, outStr, inOutStrSize, (BfpResult*)outResult);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpSystem_GetEnvironmentStrings(char* outStr, int* inOutStrSize, BfpSystemResult* outResult)
- {
- String env;
- char** envPtr = environ;
- while (true)
- {
- char* envStr = *envPtr;
- env.Append(envStr, strlen(envStr) + 1);
- ++envPtr;
- }
- TryStringOut(env, outStr, inOutStrSize, (BfpResult*)outResult);
- }
- BFP_EXPORT int BFP_CALLTYPE BfpSystem_GetNumLogicalCPUs(BfpSystemResult* outResult)
- {
- OUTRESULT(BfpSystemResult_Ok);
- return get_nprocs_conf();
- }
- BFP_EXPORT int64 BFP_CALLTYPE BfpSystem_GetCPUTick()
- {
- return 10000000;
- }
- BFP_EXPORT int64 BFP_CALLTYPE BfpSystem_GetCPUTickFreq()
- {
- struct timespec now;
- clock_gettime(CLOCK_MONOTONIC, &now);
- return (now.tv_sec * 10000000LL) + now.tv_nsec / 100;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpSystem_CreateGUID(BfpGUID* outGuid)
- {
- // uuid_t guid;
- // uuid_generate(guid);
- // BfpGUID bfpGuid;
- // memcpy(&bfpGuid, guid, 16);
- // return bfpGuid;
- uint8* ptr = (uint8*)outGuid;
- std::random_device rd;
- std::mt19937 gen(rd());
- std::uniform_int_distribution<uint8> dis(0, 255);
- for (int i = 0; i < 16; i++)
- ptr[i] = dis(gen);
- // variant must be 10xxxxxx
- ptr[8] &= 0xBF;
- ptr[8] |= 0x80;
- // version must be 0100xxxx
- ptr[6] &= 0x4F;
- ptr[6] |= 0x40;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpSystem_GetComputerName(char* outStr, int* inOutStrSize, BfpSystemResult* outResult)
- {
- char hostName[1024];
- gethostname(hostName, 1024);
- TryStringOut(hostName, outStr, inOutStrSize, (BfpResult*)outResult);
- }
- // BfpProcess
- BFP_EXPORT intptr BFP_CALLTYPE BfpProcess_GetCurrentId()
- {
- return getpid();
- }
- BFP_EXPORT bool BFP_CALLTYPE BfpProcess_IsRemoteMachine(const char* machineName)
- {
- return false;
- }
- BFP_EXPORT BfpProcess* BFP_CALLTYPE BfpProcess_GetById(const char* machineName, int processId, BfpProcessResult* outResult)
- {
- NOT_IMPL;
- return NULL;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpProcess_Enumerate(const char* machineName, BfpProcess** outProcesses, int* inOutProcessesSize, BfpProcessResult* outResult)
- {
- NOT_IMPL;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpProcess_Release(BfpProcess* process)
- {
- NOT_IMPL;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpProcess_GetMainWindowTitle(BfpProcess* process, char* outTitle, int* inOutTitleSize, BfpProcessResult* outResult)
- {
- NOT_IMPL;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpProcess_GetProcessName(BfpProcess* process, char* outName, int* inOutNameSize, BfpProcessResult* outResult)
- {
- NOT_IMPL;
- }
- BFP_EXPORT int BFP_CALLTYPE BfpProcess_GetProcessId(BfpProcess* process)
- {
- NOT_IMPL;
- return 0;
- }
- // BfpSpawn
- struct BfpSpawn
- {
- int mPid;
- bool mExited;
- int mStatus;
- int mStdInFD;
- int mStdOutFD;
- int mStdErrFD;
- };
- BFP_EXPORT BfpSpawn* BFP_CALLTYPE BfpSpawn_Create(const char* inTargetPath, const char* args, const char* workingDir, const char* env, BfpSpawnFlags flags, BfpSpawnResult* outResult)
- {
- Beefy::Array<Beefy::StringView> stringViews;
- //printf("Executing: %s %s %x\n", inTargetPath, args, flags);
- if ((workingDir != NULL) && (workingDir[0] != 0))
- {
- if (chdir(workingDir) != 0)
- {
- //printf("CHDIR failed %s\n", workingDir);
- OUTRESULT(BfpSpawnResult_UnknownError);
- return NULL;
- }
- }
- String newArgs;
- String tempFileName;
- if ((flags & BfpSpawnFlag_UseArgsFile) != 0)
- {
- char tempFileNameStr[256];
- int size = 256;
- BfpFileResult fileResult;
- BfpFile_GetTempFileName(tempFileNameStr, &size, &fileResult);
- if (fileResult == BfpFileResult_Ok)
- {
- tempFileName = tempFileNameStr;
- BfpFileResult fileResult;
- BfpFile* file = BfpFile_Create(tempFileNameStr, BfpFileCreateKind_CreateAlways, BfpFileCreateFlag_Write, BfpFileAttribute_Normal, &fileResult);
- if (file == NULL)
- {
- OUTRESULT(BfpSpawnResult_TempFileError);
- return NULL;
- }
- if ((flags & BfpSpawnFlag_UseArgsFile_Native) != 0)
- {
- UTF16String wStr = UTF8Decode(args);
- if ((flags & BfpSpawnFlag_UseArgsFile_BOM) != 0)
- {
- uint8 bom[2] = { 0xFF, 0xFE };
- BfpFile_Write(file, bom, 2, -1, NULL);
- }
- BfpFile_Write(file, wStr.c_str(), wStr.length() * 2, -1, NULL);
- }
- else
- BfpFile_Write(file, args, strlen(args), -1, NULL);
- BfpFile_Release(file);
- newArgs.Append("@");
- newArgs.Append(tempFileName);
- if (newArgs.Contains(' '))
- {
- newArgs.Insert(0, '\"');
- newArgs.Append('\"');
- }
- args = newArgs.c_str();
- }
- }
- int32 firstCharIdx = -1;
- bool inQuote = false;
- String targetPath = inTargetPath;
- String verb;
- if ((flags & BfpSpawnFlag_UseShellExecute) != 0)
- {
- String target = targetPath;
- int barPos = (int)target.IndexOf('|');
- if (barPos != -1)
- {
- verb = targetPath.Substring(barPos + 1);
- targetPath.RemoveToEnd(barPos);
- }
- }
- int32 i = 0;
- for ( ; true; i++)
- {
- char c = args[i];
- if (c == '\0')
- break;
- if ((c == ' ') && (!inQuote))
- {
- if (firstCharIdx != -1)
- {
- stringViews.Add(Beefy::StringView(args, firstCharIdx, i - firstCharIdx));
- firstCharIdx = -1;
- }
- }
- else
- {
- if (firstCharIdx == -1)
- firstCharIdx = i;
- if (c == '"')
- inQuote = !inQuote;
- else if ((inQuote) && (c == '\\'))
- {
- c = args[i + 1];
- if (c == '"')
- i++;
- }
- }
- }
- if (firstCharIdx != -1)
- stringViews.Add(Beefy::StringView(args, firstCharIdx, i - firstCharIdx));
- Beefy::Array<char*> argvArr;
- if ((flags & BfpSpawnFlag_ArgsIncludesTarget) == 0)
- argvArr.Add(strdup(targetPath.c_str()));
- for (int32 i = 0; i < (int32)stringViews.size(); i++)
- {
- Beefy::StringView stringView = stringViews[i];
- char* str = NULL;
- for (int32 pass = 0; pass < 2; pass++)
- {
- char* strPtr = str;
- int32 strPos = 0;
- for (int32 char8Idx = 0; char8Idx < stringView.mLength; char8Idx++)
- {
- char c = stringView.mPtr[char8Idx];
- if (c == '"')
- inQuote = !inQuote;
- else
- {
- if ((inQuote) && (c == '\\') && (char8Idx < stringView.mLength - 1))
- {
- char nextC = stringView.mPtr[char8Idx + 1];
- if (nextC == '"')
- {
- c = nextC;
- char8Idx++;
- }
- }
- if (strPtr != NULL)
- *(strPtr++) = c;
- strPos++;
- }
- }
- if (pass == 0)
- str = (char*)malloc(strPos + 1);
- else
- *(strPtr++) = 0;
- }
- argvArr.Add(str);
- }
- argvArr.Add(NULL);
- char** argv = NULL;
- //pid_t pid = 0;
- //int status = posix_spawn(&pid, targetPath, NULL, NULL, &argvArr[0], environ);
- Beefy::Array<char*> envArr;
- if (env != NULL)
- {
- char* envPtr = (char*)env;
- while (true)
- {
- if (*envPtr == 0)
- break;
- envArr.Add(envPtr);
- envPtr += strlen(envPtr) + 1;
- }
- }
- envArr.Add(NULL);
- int stdInFD[2];
- int stdOutFD[2];
- int stdErrFD[2];
- bool failed = false;
- if ((flags & BfpSpawnFlag_RedirectStdInput) != 0)
- if (pipe(stdInFD) != 0)
- failed = true;
- if ((flags & BfpSpawnFlag_RedirectStdOutput) != 0)
- if (pipe(stdOutFD) != 0)
- failed = true;
- if ((flags & BfpSpawnFlag_RedirectStdError) != 0)
- if (pipe(stdErrFD) != 0)
- failed = true;
- if (failed)
- {
- //printf("Pipe failed\n");
- OUTRESULT(BfpSpawnResult_UnknownError);
- return NULL;
- }
- BfpSpawn* spawn;
- pid_t pid = fork();
- if (pid == -1) // Error
- {
- OUTRESULT(BfpSpawnResult_UnknownError);
- return NULL;
- }
- else if (pid == 0) // Child
- {
- if ((flags & BfpSpawnFlag_RedirectStdInput) != 0)
- while ((dup2(stdInFD[0], STDIN_FILENO) == -1) && (errno == EINTR)) {}
- if ((flags & BfpSpawnFlag_RedirectStdOutput) != 0)
- while ((dup2(stdOutFD[1], STDOUT_FILENO) == -1) && (errno == EINTR)) {}
- if ((flags & BfpSpawnFlag_RedirectStdError) != 0)
- while ((dup2(stdErrFD[1], STDERR_FILENO) == -1) && (errno == EINTR)) {}
- // If successful then this shouldn't return at all:
- int result;
- if (env != NULL)
- result = execve(targetPath.c_str(), (char* const*)&argvArr[0], (char* const*)&envArr[0]);
- else
- result = execv(targetPath.c_str(), (char* const*)&argvArr[0]);
- printf("Couldn't execute %s\n", targetPath.c_str());
- exit(-1);
- }
- else // Parent
- {
- spawn = new BfpSpawn();
- if ((flags & BfpSpawnFlag_RedirectStdInput) != 0)
- {
- spawn->mStdInFD = stdInFD[1];
- close(stdInFD[0]);
- }
- else
- spawn->mStdInFD = 0;
- if ((flags & BfpSpawnFlag_RedirectStdOutput) != 0)
- {
- spawn->mStdOutFD = stdOutFD[0];
- close(stdOutFD[1]);
- }
- else
- spawn->mStdOutFD = 0;
- if ((flags & BfpSpawnFlag_RedirectStdError) != 0)
- {
- spawn->mStdErrFD = stdErrFD[0];
- close(stdErrFD[1]);
- }
- else
- spawn->mStdErrFD = 0;
- }
- for (auto val : argvArr)
- free(val);
- //printf("Spawn pid:%d status:%d\n", pid, status);
- spawn->mPid = pid;
- spawn->mExited = false;
- spawn->mStatus = 0;
- return spawn;
- }
- void BfpSpawn_Release(BfpSpawn* spawn)
- {
- // We don't support 'detaching' currently- this can create zombie processes since we
- // don't have a reaper strategy
- BfpSpawn_WaitFor(spawn, -1, NULL, NULL);
- delete spawn;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpSpawn_GetStdHandles(BfpSpawn* spawn, BfpFile** outStdIn, BfpFile** outStdOut, BfpFile** outStdErr)
- {
- if (outStdIn != NULL)
- {
- *outStdIn = new BfpFile(spawn->mStdInFD);
- spawn->mStdInFD = 0;
- }
- if (outStdOut != NULL)
- {
- *outStdOut = new BfpFile(spawn->mStdOutFD);
- spawn->mStdOutFD = 0;
- }
- if (outStdErr != NULL)
- {
- *outStdErr = new BfpFile(spawn->mStdErrFD);
- spawn->mStdErrFD = 0;
- }
- }
- bool BfpSpawn_WaitFor(BfpSpawn* spawn, int waitMS, int* outExitCode, BfpSpawnResult* outResult)
- {
- OUTRESULT(BfpSpawnResult_Ok);
- if (!spawn->mExited)
- {
- int flags = 0;
- if (waitMS != -1)
- {
- flags = WNOHANG;
- }
- //TODO: Implement values other than 0 or -1 for waitMS?
- pid_t result = waitpid(spawn->mPid, &spawn->mStatus, flags);
- if (result != spawn->mPid)
- return false;
- spawn->mExited = true;
- }
- if (!WIFEXITED(spawn->mStatus) && !WIFSIGNALED(spawn->mStatus))
- return false;
- if (outExitCode != NULL)
- *outExitCode = WEXITSTATUS(spawn->mStatus);
- return true;
- }
- // BfpFileWatcher
- BFP_EXPORT BfpFileWatcher* BFP_CALLTYPE BfpFileWatcher_WatchDirectory(const char* path, BfpDirectoryChangeFunc callback, BfpFileWatcherFlags flags, void* userData, BfpFileResult* outResult)
- {
- NOT_IMPL;
- return NULL;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFileWatcher_Release(BfpFileWatcher* fileWatcher)
- {
- NOT_IMPL;
- }
- // BfpThread
- struct BfpThread
- {
- bool mPThreadReleased;
- BfpThreadStartProc mStartProc;
- void* mThreadParam;
- pthread_t mPThread;
- int mRefCount;
- int mPriority;
- BfpThread()
- {
- }
- void Release()
- {
- int refCount = __sync_fetch_and_sub(&mRefCount, 1) - 1;
- if (refCount == 0)
- delete this;
- }
- };
- struct BfpThreadInfo
- {
- intptr mStackBase;
- int mStackLimit;
- };
- static __thread BfpThread* gCurrentThread;
- static __thread BfpThreadInfo gCurrentThreadInfo;
- void* ThreadFunc(void* threadParam)
- {
- BfpThread* thread = (BfpThread*)threadParam;
- gCurrentThread = thread;
- thread->mStartProc(thread->mThreadParam);
- thread->Release();
- }
- BFP_EXPORT BfpThread* BFP_CALLTYPE BfpThread_Create(BfpThreadStartProc startProc, void* threadParam, intptr stackSize, BfpThreadCreateFlags flags, BfpThreadId* outThreadId)
- {
- BfpThread* thread = new BfpThread();
- thread->mPThreadReleased = false;
- thread->mStartProc = startProc;
- thread->mThreadParam = threadParam;
- thread->mRefCount = 2;
- thread->mPriority = 0;
- BF_ASSERT(sizeof(pthread_t) <= sizeof(void*));
- pthread_attr_t params;
- pthread_attr_init(¶ms);
- pthread_attr_setstacksize(¶ms, stackSize);
- //pthread_attr_setdetachstate(¶ms,PTHREAD_CREATE_DETACHED);
- pthread_create(&thread->mPThread, ¶ms, ThreadFunc, (void*)thread);
- pthread_attr_destroy(¶ms);
-
- if (outThreadId != NULL)
- *outThreadId = (BfpThreadId)thread->mPThread;
- return thread;
- }
- #define FIXTHREAD() \
- pthread_t pt; \
- if (((intptr)thread & 1) != 0) \
- { \
- pt = (pthread_t)((intptr)thread & ~3); \
- thread = NULL; \
- } else \
- pt = thread->mPThread
- BFP_EXPORT void BFP_CALLTYPE BfpThread_Release(BfpThread* thread)
- {
- FIXTHREAD();
- if (thread == NULL)
- return;
- if (!thread->mPThreadReleased)
- {
- pthread_detach(thread->mPThread);
- thread->mPThreadReleased = true;
- }
- thread->Release();
- }
- BFP_EXPORT void BFP_CALLTYPE BfpThread_SetName(BfpThread* thread, const char* name, BfpThreadResult* outResult)
- {
- OUTRESULT(BfpThreadResult_Ok);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpThread_GetName(BfpThread* thread, char* outName, int* inOutNameSize, BfpThreadResult* outResult)
- {
- String str = "";
- TryStringOut(str, outName, inOutNameSize, (BfpResult*)outResult);
- }
- BFP_EXPORT BfpThread* BFP_CALLTYPE BfpThread_GetCurrent()
- {
- if (gCurrentThread == NULL)
- {
- // Not a "true" BfpThread, this is either the main thread or a thread we didn't create
- return (BfpThread*)((intptr)pthread_self() | 1);
- }
- return gCurrentThread;
- }
- BFP_EXPORT BfpThreadId BFP_CALLTYPE BfpThread_GetCurrentId()
- {
- if (gCurrentThread == NULL)
- {
- return (BfpThreadId)((intptr)pthread_self());
- }
- return (BfpThreadId)gCurrentThread->mPThread;
- }
- BFP_EXPORT bool BFP_CALLTYPE BfpThread_WaitFor(BfpThread* thread, int waitMS)
- {
- FIXTHREAD();
- if (waitMS == -1)
- {
- pthread_join(pt, NULL);
- thread->mPThreadReleased = true;
- return true;
- }
- struct timespec waitTime;
- waitTime.tv_sec = waitMS / 1000;
- waitTime.tv_nsec = (waitMS % 1000) * 1000000;
- int result = pthread_timedjoin_np(pt, NULL, &waitTime);
- if (result == 0)
- {
- if (thread != NULL)
- thread->mPThreadReleased = true;
- return true;
- }
- return false;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpSpawn_Kill(BfpSpawn* spawn, int exitCode, BfpKillFlags killFlags, BfpSpawnResult* outResult)
- {
- //TODO: Implement
- OUTRESULT(BfpSpawnResult_UnknownError);
- }
- BFP_EXPORT BfpThreadPriority BFP_CALLTYPE BfpThread_GetPriority(BfpThread* thread, BfpThreadResult* outResult)
- {
- FIXTHREAD();
- OUTRESULT(BfpThreadResult_Ok);
- if (thread == NULL)
- return (BfpThreadPriority)0;
- return (BfpThreadPriority)thread->mPriority;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpThread_SetPriority(BfpThread* thread, BfpThreadPriority threadPriority, BfpThreadResult* outResult)
- {
- // In effect, we have two 'nice' settings: 0 (normal) or 10 (low)
- // High-priority settings just don't do anything
- //pid_t tid = syscall(SYS_gettid);
- //int ret = setpriority(PRIO_PROCESS, tid, -std::min(nPriority, 0) * 10);
- OUTRESULT(BfpThreadResult_Ok);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpThread_Suspend(BfpThread* thread, BfpThreadResult* outResult)
- {
- NOT_IMPL;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpThread_Resume(BfpThread* thread, BfpThreadResult* outResult)
- {
- NOT_IMPL;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpThread_GetIntRegisters(BfpThread* thread, intptr* outStackPtr, intptr* outIntRegs, int* inOutIntRegCount, BfpThreadResult* outResult)
- {
- NOT_IMPL;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpThread_GetStackInfo(BfpThread* thread, intptr* outStackBase, int* outStackLimit, BfpThreadResult* outResult)
- {
- if (gCurrentThreadInfo.mStackBase == 0)
- {
- void* stackBase = 0;
- size_t stackLimit = 0;
- pthread_attr_t attr;
- pthread_getattr_np(pthread_self(), &attr);
- pthread_attr_getstack(&attr, &stackBase, &stackLimit);
- gCurrentThreadInfo.mStackBase = (intptr)stackBase + stackLimit;
- gCurrentThreadInfo.mStackLimit = (int)stackLimit;
- pthread_attr_destroy(&attr);
- }
- *outStackBase = gCurrentThreadInfo.mStackBase;
- *outStackLimit = gCurrentThreadInfo.mStackLimit;
- OUTRESULT(BfpThreadResult_Ok);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpThread_Sleep(int sleepMS)
- {
- usleep(sleepMS * 1000);
- }
- BFP_EXPORT bool BFP_CALLTYPE BfpThread_Yield()
- {
- return sched_yield() == 0;
- }
- struct BfpCritSect
- {
- pthread_mutex_t mPMutex;
- };
- BFP_EXPORT BfpCritSect* BFP_CALLTYPE BfpCritSect_Create()
- {
- BfpCritSect* critSect = new BfpCritSect();
- pthread_mutexattr_t attributes;
- pthread_mutexattr_init(&attributes);
- pthread_mutexattr_settype(&attributes, PTHREAD_MUTEX_RECURSIVE);
- pthread_mutex_init(&critSect->mPMutex, &attributes);
- pthread_mutexattr_destroy(&attributes);
- return critSect;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpCritSect_Release(BfpCritSect* critSect)
- {
- pthread_mutex_destroy(&critSect->mPMutex);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpCritSect_Enter(BfpCritSect* critSect)
- {
- pthread_mutex_lock(&critSect->mPMutex);
- }
- BFP_EXPORT bool BFP_CALLTYPE BfpCritSect_TryEnter(BfpCritSect* critSect, int waitMS)
- {
- if (waitMS == -1)
- {
- BfpCritSect_Enter(critSect);
- return true;
- }
- else if (waitMS == 0)
- {
- return pthread_mutex_trylock(&critSect->mPMutex) == 0;
- }
-
- uint32 start = Beefy::BFTickCount();
- while ((int)(Beefy::BFTickCount() - start) < waitMS)
- {
- if (pthread_mutex_trylock(&critSect->mPMutex) == 0)
- {
- return true;
- }
- }
- return false;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpCritSect_Leave(BfpCritSect* critSect)
- {
- pthread_mutex_unlock(&critSect->mPMutex);
- }
- BFP_EXPORT BfpTLS* BFP_CALLTYPE BfpTLS_Create()
- {
- pthread_key_t key = 0;
- pthread_key_create(&key, NULL);
- return (BfpTLS*)(intptr)key;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpTLS_Release(BfpTLS* tls)
- {
- pthread_key_delete((pthread_key_t)(intptr)tls);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpTLS_SetValue(BfpTLS* tls, void* value)
- {
- pthread_setspecific((pthread_key_t)(intptr)tls, value);
- }
- BFP_EXPORT void* BFP_CALLTYPE BfpTLS_GetValue(BfpTLS* tls)
- {
- return pthread_getspecific((pthread_key_t)(intptr)tls);
- }
- struct BfpEvent
- {
- pthread_mutex_t mMutex;
- pthread_cond_t mCondVariable;
- bool mSet;
- bool mManualReset;
- };
- BFP_EXPORT BfpEvent* BFP_CALLTYPE BfpEvent_Create(BfpEventFlags flags)
- {
- BfpEvent* event = new BfpEvent();
- pthread_mutex_init(&event->mMutex, NULL);
- pthread_cond_init(&event->mCondVariable, NULL);
- event->mSet = (flags & (BfpEventFlag_InitiallySet_Auto | BfpEventFlag_InitiallySet_Manual)) != 0;
- event->mManualReset = (flags & BfpEventFlag_InitiallySet_Manual) != 0;
- return event;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpEvent_Release(BfpEvent* event)
- {
- pthread_cond_destroy(&event->mCondVariable);
- pthread_mutex_destroy(&event->mMutex);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpEvent_Set(BfpEvent* event, bool requireManualReset)
- {
- pthread_mutex_lock(&event->mMutex);
- event->mSet = true;
- if (requireManualReset)
- event->mManualReset = true;
- if (event->mManualReset)
- pthread_cond_broadcast(&event->mCondVariable);
- else
- pthread_cond_signal(&event->mCondVariable);
- pthread_mutex_unlock(&event->mMutex);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpEvent_Reset(BfpEvent* event, BfpEventResult* outResult)
- {
- event->mSet = false;
- event->mManualReset = false;
- }
- BFP_EXPORT bool BFP_CALLTYPE BfpEvent_WaitFor(BfpEvent* event, int waitMS)
- {
- int result = pthread_mutex_lock(&event->mMutex);
- BF_ASSERT(result == 0);
- while (!event->mSet)
- {
- if (waitMS == -1)
- {
- pthread_cond_wait(&event->mCondVariable, &event->mMutex);
- }
- else
- {
- timespec ts;
- clock_gettime(CLOCK_REALTIME, &ts);
- ts.tv_sec += waitMS / 1000;
- ts.tv_nsec += (waitMS % 1000) * 1000000;
-
- result = pthread_cond_timedwait(&event->mCondVariable, &event->mMutex, &ts);
-
- if (waitMS == (uint32)-1)
- BF_ASSERT(result == 0);
-
- if (result != 0)
- {
- // Timeout
- pthread_mutex_unlock(&event->mMutex);
- return false;
- }
- }
- }
- if (!event->mManualReset)
- event->mSet = false;
- pthread_mutex_unlock(&event->mMutex);
- return true;
- }
- BFP_EXPORT BfpDynLib* BFP_CALLTYPE BfpDynLib_Load(const char* fileName)
- {
- BfpDynLib* mod = NULL;
-
- static const char* prefixes[] = {NULL, "lib"};
- static const char* suffixes[] = {NULL, ".so", ".dylib"};
-
- for (int prefixIdx = 0; prefixIdx < sizeof(prefixes)/sizeof(prefixes[0]); prefixIdx++)
- {
- for (int suffixIdx = 0; suffixIdx < sizeof(suffixes)/sizeof(suffixes[0]); suffixIdx++)
- {
- const char* prefix = prefixes[prefixIdx];
- const char* suffix = suffixes[suffixIdx];
-
- Beefy::String checkName = fileName;
- if (prefix != NULL)
- checkName = Beefy::String(prefix) + checkName;
- if (suffix != NULL)
- {
- int dotPos = checkName.LastIndexOf('.');
- if (dotPos != -1)
- checkName.RemoveToEnd(dotPos);
- checkName += suffix;
- }
-
- mod = (BfpDynLib*)dlopen(checkName.c_str(), RTLD_LAZY);
- if (mod != NULL)
- return mod;
- }
- }
-
- /*mod = (BfpDynLib*)dlopen("/var/Beef/qt-build/Debug/bin/libIDEHelper.so", RTLD_LAZY);;
- if (mod == NULL)
- {
- printf("Err: %s\n", dlerror());
- fflush(stdout);
- }*/
- return NULL;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpDynLib_Release(BfpDynLib* lib)
- {
- dlclose((void*)lib);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpDynLib_GetFilePath(BfpDynLib* lib, char* outPath, int* inOutPathSize, BfpLibResult* outResult)
- {
- Beefy::String path;
- link_map* linkMap = NULL;
- dlinfo((void*)lib, RTLD_DI_LINKMAP, &linkMap);
- if (linkMap == NULL)
- {
- OUTRESULT(BfpLibResult_UnknownError);
- return;
- }
- path = linkMap->l_name;
- TryStringOut(path, outPath, inOutPathSize, (BfpResult*)outResult);
- }
- BFP_EXPORT void* BFP_CALLTYPE BfpDynLib_GetProcAddress(BfpDynLib* lib, const char* name)
- {
- return dlsym((void*)lib, name);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpDirectory_Create(const char* path, BfpFileResult* outResult)
- {
- if (mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
- {
- switch (errno)
- {
- case EEXIST:
- OUTRESULT(BfpFileResult_AlreadyExists);
- break;
- case ENOENT:
- OUTRESULT(BfpFileResult_NotFound);
- break;
- default:
- OUTRESULT(BfpFileResult_UnknownError);
- break;
- }
- }
- else
- OUTRESULT(BfpFileResult_Ok);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpDirectory_Rename(const char* oldName, const char* newName, BfpFileResult* outResult)
- {
- NOT_IMPL;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpDirectory_Delete(const char* path, BfpFileResult* outResult)
- {
- if (rmdir(path) != 0)
- {
- switch (errno)
- {
- case ENOENT:
- OUTRESULT(BfpFileResult_NotFound);
- break;
- default:
- OUTRESULT(BfpFileResult_UnknownError);
- break;
- }
- }
- else
- OUTRESULT(BfpFileResult_Ok);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpDirectory_GetCurrent(char* outPath, int* inOutPathSize, BfpFileResult* outResult)
- {
- char* str = getcwd(NULL, 0);
- Beefy::String path = str;
- free(str);
- TryStringOut(path, outPath, inOutPathSize, (BfpResult*)outResult);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpDirectory_SetCurrent(const char* path, BfpFileResult* outResult)
- {
- if (chdir(path) != 0)
- OUTRESULT(BfpFileResult_NotFound);
- else
- OUTRESULT(BfpFileResult_Ok);
- }
- BFP_EXPORT bool BFP_CALLTYPE BfpDirectory_Exists(const char* path)
- {
- struct stat statbuf = {0};
- int result = stat(path, &statbuf);
- if (result != 0)
- return false;
- return S_ISDIR(statbuf.st_mode);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpDirectory_GetSysDirectory(BfpSysDirectoryKind sysDirKind, char* outPath, int* inOutPathLen, BfpFileResult* outResult)
- {
- String path = "~";
- TryStringOut(path, outPath, inOutPathLen, (BfpResult*)outResult);
- }
- BFP_EXPORT BfpFile* BFP_CALLTYPE BfpFile_Create(const char* inName, BfpFileCreateKind createKind, BfpFileCreateFlags createFlags, BfpFileAttributes createdFileAttrs, BfpFileResult* outResult)
- {
- auto _DoCreate = [&](String& name)
- {
- int flags = 0;
- int mode = 0;
- int pipePairHandle = -1;
- if ((createFlags & (BfpFileCreateFlag_Read | BfpFileCreateFlag_Write)) == (BfpFileCreateFlag_Read | BfpFileCreateFlag_Write))
- flags |= O_RDWR;
- else if ((createFlags & BfpFileCreateFlag_Read) != 0)
- flags |= O_RDONLY;
- else if ((createFlags & BfpFileCreateFlag_Write) != 0)
- flags |= O_WRONLY;
- if ((createFlags & BfpFileCreateFlag_Append) != 0)
- flags |= O_APPEND;
- if ((createFlags & BfpFileCreateFlag_Truncate) != 0)
- flags |= O_TRUNC;
- if ((createFlags & (BfpFileCreateFlag_NonBlocking | BfpFileCreateFlag_AllowTimeouts)) != 0)
- flags |= O_NONBLOCK;
- if ((createFlags & BfpFileCreateFlag_Pipe) != 0)
- {
- name = "/tmp/" + name;
- if ((createKind == BfpFileCreateKind_CreateAlways) ||
- (createKind == BfpFileCreateKind_CreateIfNotExists))
- {
- for (int pass = 0; pass < 2; pass++)
- {
- int result = mknod(name.c_str(), S_IFIFO | ALLPERMS, 0);
- if (result == 0)
- break;
- int err = errno;
- if (err == EEXIST)
- {
- err = remove(name.c_str());
- if (err == 0)
- continue;
- OUTRESULT(BfpFileResult_AlreadyExists);
- return -1;
- }
- OUTRESULT(BfpFileResult_UnknownError);
- return -1;
- }
- }
- }
- else
- {
- if (createKind == BfpFileCreateKind_CreateAlways)
- flags |= O_CREAT;
- else if (createKind == BfpFileCreateKind_CreateIfNotExists)
- flags |= O_CREAT | O_EXCL;
- }
- mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
- int result = open(name.c_str(), flags, mode);
- //printf("BfpFile_Create %s %d %d %d\n", name.c_str(), result, flags, mode);
- if (result <= 0)
- {
- switch (errno)
- {
- case EEXIST:
- OUTRESULT(BfpFileResult_AlreadyExists);
- break;
- case ENOENT:
- OUTRESULT(BfpFileResult_NotFound);
- break;
- case EACCES:
- OUTRESULT(BfpFileResult_AccessError);
- break;
- default:
- OUTRESULT(BfpFileResult_UnknownError);
- break;
- }
- return -1;
- }
- return result;
- };
- BfpFile* bfpFile = NULL;
- int result;
- if ((createFlags & BfpFileCreateFlag_Pipe) != 0)
- {
- int readHandle;
- int writeHandle;
- String name = inName;
- String altName = name + "__";
- bool isCreating = false;
- if ((createKind == BfpFileCreateKind_CreateAlways) ||
- (createKind == BfpFileCreateKind_CreateIfNotExists))
- {
- readHandle = _DoCreate(name);
- writeHandle = _DoCreate(altName);
- isCreating = true;
- }
- else
- {
- readHandle = _DoCreate(altName);
- writeHandle = _DoCreate(name);
- }
- if ((readHandle != -1) && (writeHandle != -1))
- {
- OUTRESULT(BfpFileResult_Ok);
- BfpPipeInfo* pipeInfo = new BfpPipeInfo();
- pipeInfo->mWriteHandle = writeHandle;
- if (isCreating)
- pipeInfo->mPipePath = name;
- bfpFile = new BfpFile();
- bfpFile->mHandle = readHandle;
- bfpFile->mPipeInfo = pipeInfo;
- }
- else
- {
- if (readHandle != -1)
- close(readHandle);
- if (writeHandle != -1)
- close(writeHandle);
- return NULL;
- }
- }
- else
- {
- String name = inName;
- int handle = _DoCreate(name);
- if (handle == -1)
- return NULL;
- OUTRESULT(BfpFileResult_Ok);
- bfpFile = new BfpFile();
- bfpFile->mHandle = handle;
- }
- OUTRESULT(BfpFileResult_Ok);
- if ((createFlags & (BfpFileCreateFlag_NonBlocking | BfpFileCreateFlag_AllowTimeouts)) != 0)
- bfpFile->mNonBlocking = true;
- if ((createFlags & BfpFileCreateFlag_AllowTimeouts) != 0)
- bfpFile->mAllowTimeout = true;
- return bfpFile;
- }
- BFP_EXPORT BfpFile* BFP_CALLTYPE BfpFile_GetStd(BfpFileStdKind kind, BfpFileResult* outResult)
- {
- int h = -1;
- switch (kind)
- {
- case BfpFileStdKind_StdOut:
- h = STDOUT_FILENO;
- break;
- case BfpFileStdKind_StdError:
- h = STDERR_FILENO;
- break;
- case BfpFileStdKind_StdIn:
- h = STDIN_FILENO;
- break;
- }
- if (h == -1)
- {
- OUTRESULT(BfpFileResult_NotFound);
- return NULL;
- }
- BfpFile* bfpFile = new BfpFile();
- bfpFile->mHandle = h;
- bfpFile->mIsStd = true;
- return bfpFile;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFile_Release(BfpFile* file)
- {
- if ((file->mHandle != -1) && (!file->mIsStd))
- close(file->mHandle);
- if (file->mPipeInfo != NULL)
- {
- if (file->mPipeInfo->mWriteHandle != -1)
- close(file->mPipeInfo->mWriteHandle);
-
- if (!file->mPipeInfo->mPipePath.IsEmpty())
- {
- int worked = remove(file->mPipeInfo->mPipePath.c_str());
- remove((file->mPipeInfo->mPipePath + "__").c_str());
- //printf("Removing %s %d\n", file->mPipeInfo->mPipePath.c_str(), worked);
- }
- }
- delete file;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFile_Close(BfpFile* file, BfpFileResult* outResult)
- {
- if (file->mHandle != -1)
- {
- close(file->mHandle);
- file->mHandle = -1;
- if (file->mPipeInfo != NULL)
- {
- close(file->mPipeInfo->mWriteHandle);
- file->mPipeInfo->mWriteHandle = -1;
- }
- OUTRESULT(BfpFileResult_Ok);
- }
- else
- OUTRESULT(BfpFileResult_UnknownError);
- }
- BFP_EXPORT intptr BFP_CALLTYPE BfpFile_Write(BfpFile* file, const void* buffer, intptr size, int timeoutMS, BfpFileResult* outResult)
- {
- int writeHandle = file->mHandle;
- if (file->mPipeInfo != NULL)
- writeHandle = file->mPipeInfo->mWriteHandle;
- intptr writeCount = ::write(writeHandle, buffer, size);
- // if ((writeCount > 0) && (file->mIsPipe))
- // {
- // ::fsync(file->mHandle);
- // }
- if (writeCount < 0)
- OUTRESULT(BfpFileResult_UnknownError);
- else if (writeCount != size)
- OUTRESULT(BfpFileResult_PartialData);
- else
- OUTRESULT(BfpFileResult_Ok);
- return writeCount;
- }
- BFP_EXPORT intptr BFP_CALLTYPE BfpFile_Read(BfpFile* file, void* buffer, intptr size, int timeoutMS, BfpFileResult* outResult)
- {
- if (file->mNonBlocking)
- {
- if (!file->mAllowTimeout)
- timeoutMS = -1;
-
- timeval timeout;
- timeout.tv_sec = 0;
- timeout.tv_usec = timeoutMS * 1000;
- fd_set readFDSet;
- FD_ZERO(&readFDSet);
- FD_SET(file->mHandle, &readFDSet);
- fd_set errorFDSet;
- FD_ZERO(&errorFDSet);
- FD_SET(file->mHandle, &errorFDSet);
- if (select(file->mHandle + 1, &readFDSet, NULL, &errorFDSet, (timeoutMS == -1) ? NULL : &timeout) < 0)
- {
- OUTRESULT(BfpFileResult_Timeout);
- return 0;
- }
- }
- intptr readCount = ::read(file->mHandle, buffer, size);
- if (readCount < 0)
- OUTRESULT(BfpFileResult_UnknownError);
- else if (readCount != size)
- OUTRESULT(BfpFileResult_PartialData);
- else
- OUTRESULT(BfpFileResult_Ok);
- return readCount;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFile_Flush(BfpFile* file)
- {
- ::fsync(file->mHandle);
- }
- BFP_EXPORT int64 BFP_CALLTYPE BfpFile_GetFileSize(BfpFile* file)
- {
- int64 oldPos = (int64)lseek64(file->mHandle, 0, SEEK_CUR);
- int64 size = (int64)lseek64(file->mHandle, 0, SEEK_END);
- lseek64(file->mHandle, oldPos, SEEK_SET);
- return (int64)size;
- }
- BFP_EXPORT int64 BFP_CALLTYPE BfpFile_Seek(BfpFile* file, int64 offset, BfpFileSeekKind seekKind)
- {
- int whence;
- if (seekKind == BfpFileSeekKind_Absolute)
- whence = SEEK_SET;
- else if (seekKind == BfpFileSeekKind_Relative)
- whence = SEEK_CUR;
- else
- whence = SEEK_END;
- return lseek64(file->mHandle, offset, whence);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFile_Truncate(BfpFile* file)
- {
- int64 curPos = (int64)lseek64(file->mHandle, 0, SEEK_CUR);
- if (ftruncate64(file->mHandle, curPos) != 0)
- {
- //TODO: Report error?
- }
- }
- BFP_EXPORT BfpTimeStamp BFP_CALLTYPE BfpFile_GetTime_LastWrite(const char* path)
- {
- struct stat statbuf = {0};
- int result = stat(path, &statbuf);
- if (result != 0)
- return 0;
- return statbuf.st_mtime;
- }
- BFP_EXPORT BfpFileAttributes BFP_CALLTYPE BfpFile_GetAttributes(const char* path, BfpFileResult* outResult)
- {
- NOT_IMPL;
- return (BfpFileAttributes)0;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFile_SetAttributes(const char* path, BfpFileAttributes attribs, BfpFileResult* outResult)
- {
- NOT_IMPL;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFile_Copy(const char* oldPath, const char* newPath, BfpFileCopyKind copyKind, BfpFileResult* outResult)
- {
- int fd_to, fd_from;
- char buf[4096];
- ssize_t nread;
- fd_from = open(oldPath, O_RDONLY);
- if (fd_from < 0)
- {
- OUTRESULT(BfpFileResult_NotFound);
- return;
- }
- fd_to = open(newPath, O_WRONLY | O_CREAT | O_EXCL, 0666);
- if (fd_to < 0)
- {
- OUTRESULT(BfpFileResult_AlreadyExists);
- goto out_error;
- }
- while (nread = read(fd_from, buf, sizeof buf), nread > 0)
- {
- char *out_ptr = buf;
- ssize_t nwritten;
- do {
- nwritten = write(fd_to, out_ptr, nread);
- if (nwritten >= 0)
- {
- nread -= nwritten;
- out_ptr += nwritten;
- }
- else if (errno != EINTR)
- {
- OUTRESULT(BfpFileResult_UnknownError);
- goto out_error;
- }
- } while (nread > 0);
- }
- if (nread == 0)
- {
- if (close(fd_to) < 0)
- {
- fd_to = -1;
- OUTRESULT(BfpFileResult_UnknownError);
- goto out_error;
- }
- close(fd_from);
- /* Success! */
- OUTRESULT(BfpFileResult_Ok);
- return;
- }
- out_error:
- close(fd_from);
- if (fd_to >= 0)
- close(fd_to);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFile_Rename(const char* oldPath, const char* newPath, BfpFileResult* outResult)
- {
- NOT_IMPL;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFile_Delete(const char* path, BfpFileResult* outResult)
- {
- if (remove(path) != 0)
- {
- switch (errno)
- {
- case ENOENT:
- OUTRESULT(BfpFileResult_NotFound);
- break;
- default:
- OUTRESULT(BfpFileResult_UnknownError);
- break;
- }
- }
- else
- OUTRESULT(BfpFileResult_Ok);
- }
- BFP_EXPORT bool BFP_CALLTYPE BfpFile_Exists(const char* path)
- {
- struct stat statbuf = {0};
- int result = stat(path, &statbuf);
- if (result != 0)
- return false;
- return !S_ISDIR(statbuf.st_mode);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFile_GetTempPath(char* outPath, int* inOutPathSize, BfpFileResult* outResult)
- {
- NOT_IMPL;
- }
- static const char cHash64bToChar[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
- 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
- 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
- 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_' };
- static void HashEncode64(StringImpl& outStr, uint64 val)
- {
- for (int i = 0; i < 10; i++)
- {
- int charIdx = (int)((val >> (i * 6)) & 0x3F) - 1;
- if (charIdx != -1)
- outStr.Append(cHash64bToChar[charIdx]);
- }
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFile_GetTempFileName(char* outName, int* inOutNameSize, BfpFileResult* outResult)
- {
- static uint32 uniqueIdx = 0;
- BfpSystem_InterlockedExchangeAdd32(&uniqueIdx, 1);
- Beefy::HashContext ctx;
- ctx.Mixin(uniqueIdx);
- ctx.Mixin(getpid());
- ctx.Mixin(Beefy::BFGetTickCountMicro());
- uint64 hash = ctx.Finish64();
- String str = "/tmp/bftmp_";
- HashEncode64(str, hash);
- TryStringOut(str, outName, inOutNameSize, (BfpResult*)outResult);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFile_GetFullPath(const char* inPath, char* outPath, int* inOutPathSize, BfpFileResult* outResult)
- {
- String str;
- if (inPath[0] == '/')
- {
- str = inPath;
- }
- else
- {
- char* cwdPtr = getcwd(NULL, 0);
- Beefy::String cwdPath = cwdPtr;
- free(cwdPtr);
- str = GetAbsPath(inPath, cwdPath);
- }
- TryStringOut(str, outPath, inOutPathSize, (BfpResult*)outResult);
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFile_GetActualPath(const char* inPath, char* outPath, int* inOutPathSize, BfpFileResult* outResult)
- {
- NOT_IMPL;
- }
- // BfpFindFileData
- struct BfpFindFileData
- {
- BfpFindFileFlags mFlags;
- DIR* mDirStruct;
- Beefy::String mWildcard;
- Beefy::String mDirPath;
- dirent* mDirEnt;
- bool mHasStat;
- struct stat mStat;
- };
- BFP_EXPORT BfpFindFileData* BFP_CALLTYPE BfpFindFileData_FindFirstFile(const char* path, BfpFindFileFlags flags, BfpFileResult* outResult)
- {
- Beefy::String findStr = path;
- Beefy::String wildcard;
-
- int lastSlashPos = std::max((int)findStr.LastIndexOf('/'), (int)findStr.LastIndexOf('\\'));
- if (lastSlashPos != -1)
- {
- wildcard = findStr.Substring(lastSlashPos + 1);
- findStr = findStr.Substring(0, lastSlashPos);
- }
- if (wildcard == "*.*")
- wildcard = "*";
-
- DIR* dir = opendir(findStr.c_str());
- if (dir == NULL)
- {
- OUTRESULT(BfpFileResult_NotFound);
- return NULL;
- }
- BfpFindFileData* findData = new BfpFindFileData();
- findData->mFlags = flags;
- findData->mDirPath = findStr;
- findData->mDirStruct = dir;
- findData->mWildcard = wildcard;
- findData->mHasStat = false;
- findData->mDirEnt = NULL;
-
- if (!BfpFindFileData_FindNextFile(findData))
- {
- OUTRESULT(BfpFileResult_NoResults);
- delete findData;
- return NULL;
- }
- OUTRESULT(BfpFileResult_Ok);
- return findData;
- }
- static void GetStat(BfpFindFileData* findData)
- {
- if (findData->mHasStat)
- return;
- Beefy::String filePath = findData->mDirPath + "/" + findData->mDirEnt->d_name;
-
- findData->mStat = { 0 };
- int result = stat(filePath.c_str(), &findData->mStat);
- findData->mHasStat = true;
- }
- static bool BfpFindFileData_CheckFilter(BfpFindFileData* findData)
- {
- bool isDir = false;
- if (findData->mDirEnt->d_type == DT_DIR)
- isDir = true;
- if (findData->mDirEnt->d_type == DT_LNK)
- {
- GetStat(findData);
- isDir = S_ISDIR(findData->mStat.st_mode);
- }
- if (isDir)
- {
- if ((findData->mFlags & BfpFindFileFlag_Directories) == 0)
- return false;
-
- if ((strcmp(findData->mDirEnt->d_name, ".") == 0) || (strcmp(findData->mDirEnt->d_name, "..") == 0))
- return false;
- }
- else
- {
- if ((findData->mFlags & BfpFindFileFlag_Files) == 0)
- return false;
- }
- //TODO: Check actual wildcards.
- return true;
- }
- BFP_EXPORT bool BFP_CALLTYPE BfpFindFileData_FindNextFile(BfpFindFileData* findData)
- {
- while (true)
- {
- findData->mHasStat = false;
- findData->mDirEnt = readdir(findData->mDirStruct);
- if (findData->mDirEnt == NULL)
- return false;
- if (BfpFindFileData_CheckFilter(findData))
- break;
- }
- return true;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFindFileData_GetFileName(BfpFindFileData* findData, char* outName, int* inOutNameSize, BfpFileResult* outResult)
- {
- Beefy::String name = findData->mDirEnt->d_name;
- TryStringOut(name, outName, inOutNameSize, (BfpResult*)outResult);
- }
- BFP_EXPORT BfpTimeStamp BFP_CALLTYPE BfpFindFileData_GetTime_LastWrite(BfpFindFileData* findData)
- {
- GetStat(findData);
- return BfpToTimeStamp(findData->mStat.st_mtim);
- }
- BFP_EXPORT BfpTimeStamp BFP_CALLTYPE BfpFindFileData_GetTime_Created(BfpFindFileData* findData)
- {
- GetStat(findData);
- return BfpToTimeStamp(findData->mStat.st_ctim);
- }
- BFP_EXPORT BfpTimeStamp BFP_CALLTYPE BfpFindFileData_GetTime_Access(BfpFindFileData* findData)
- {
- GetStat(findData);
- return BfpToTimeStamp(findData->mStat.st_atim);
- }
- BFP_EXPORT BfpFileAttributes BFP_CALLTYPE BfpFindFileData_GetFileAttributes(BfpFindFileData* findData)
- {
- BfpFileAttributes flags = BfpFileAttribute_None;
- if (S_ISDIR(findData->mStat.st_mode))
- flags = (BfpFileAttributes)(flags | BfpFileAttribute_Directory);
- if (S_ISREG(findData->mStat.st_mode))
- flags = (BfpFileAttributes)(flags | BfpFileAttribute_Normal);
- else if (!S_ISLNK(findData->mStat.st_mode))
- flags = (BfpFileAttributes)(flags | BfpFileAttribute_Device);
- if ((findData->mStat.st_mode & S_IRUSR) == 0)
- flags = (BfpFileAttributes)(flags | BfpFileAttribute_ReadOnly);
- return flags;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpFindFileData_Release(BfpFindFileData* findData)
- {
- delete findData;
- }
- BFP_EXPORT int BFP_CALLTYPE BfpStack_CaptureBackTrace(int framesToSkip, intptr* outFrames, int wantFrameCount)
- {
- //
- return 0;
- }
- BFP_EXPORT void BFP_CALLTYPE BfpOutput_DebugString(const char* str)
- {
- fputs(str, stdout);
- fflush(stdout);
- }
- //////////////////////////////////////////////////////////////////////////
- void Beefy::BFFatalError(const StringImpl& message, const StringImpl& file, int line)
- {
- String error;
- error += "ERROR: ";
- error += message;
- error += " in ";
- error += file;
- error += StrFormat(" line %d", line);
- BfpSystem_FatalError(error.c_str(), "FATAL ERROR");
- }
|