void MoveNewNand (void) { int i; // As there is a nand, we must found the empty folder in usb://nands that was containing it... printd ("Moving selected nand"); // search and empty nand folder for (i = 0; i < NANDSUBS; i++) { char path[256]; char source[256]; sprintf (source, "%s/%s", nandSource, nandsubs[i]); sprintf (path, "usb://%s", nandsubs[i]); if (DirExist (source)) { if (rename (source, path) == 0) printd ("."); else { printd ("\n Error on mooving %s to %s\n", nandsubs[i], path); errors++; } } } printd ("\n"); }
/* there is a nand in the root of the drive (usb). We need to copy back. We absolutely must not loose it's content */ void StoreCurrentNand (void) { int i; int foundIt; char target[256]; if (!IsNandFolder ("usb:/")) return; // As there is a nand, we must found the empty folder in usb://nands that was containing it... printd ("Backing up current nand folder"); // search and empty nand folder foundIt = 0; for (i = 0; i < pln.nandsCnt; i++) if (pln.nands[i].count == 0) { foundIt = 1; break; } // if we not found an empty folder, user (or I LOL) should have made some erros... go to create empty one if (foundIt) strcpy (target, pln.nands[i].path); else { sprintf (target, BASEPATH"/backup%u", (unsigned int)time(0)); mkdir (target, S_IREAD | S_IWRITE); // Add the new folder to struct sprintf (pln.nands[pln.nandsCnt].path, "%s", target); pln.nands[pln.nandsCnt].count = 0; pln.nandsCnt++; } for (i = 0; i < NANDSUBS; i++) { char path[256]; char source[256]; sprintf (source, "usb://%s", nandsubs[i]); sprintf (path, "%s/%s", target, nandsubs[i]); if (DirExist (source)) { if (rename (source, path) == 0) printd ("."); else { printd ("\n Error on mooving %s to %s\n", nandsubs[i], path); sleep (5); errors++; } } } printd ("\n"); }
/* this function will check if a folder "may" contain a valid nand */ bool IsNandFolder (char *path) { char npath[300]; sprintf (npath, "%s/TITLE", path); if (!DirExist(npath)) return FALSE; sprintf (npath, "%s/TICKET", path); if (!DirExist(npath)) return FALSE; sprintf (npath, "%s/SHARED1", path); if (!DirExist(npath)) return FALSE; sprintf (npath, "%s/SYS", path); if (!DirExist(npath)) return FALSE; return TRUE; }
//------------------------------------------------------------ //描述:创建多极目录 void CCommonFunc::CreateDir(wchar_t* strDir) { if(DirExist(strDir)) { return; } wchar_t strTemp[256]; //首先确定有多少级目录 wchar_t* pChar = strDir; int iPos[20]; //最多允许创建20级目录 int iLayNum = 0; //目录的级数 int iCurrPos = 0; while(*pChar != L'\0') { if(*pChar == L'\\') { iPos[iLayNum++] = iCurrPos; } pChar++; iCurrPos++; } iPos[iLayNum++] = iCurrPos; //创建目录 for(int i = 1; i < iLayNum; i++) { wcscpy_s(strTemp, _countof(strTemp), strDir); strTemp[iPos[i]] = L'\0'; //判断目录是否存在 HANDLE hDir = ::CreateFileW(strTemp, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); if(INVALID_HANDLE_VALUE == hDir) { ::CreateDirectoryW(strTemp,NULL); } ::CloseHandle(hDir); } }
/* this function will retrive all available nand foder in usb://nands */ void GetNandsFolders (void) { DIR *pdir; struct dirent *pent; char path[256]; // As there is a nand, we must found the empty folder in usb://nands that was containing it... printd ("Scanning for nand folders..."); pln.nandsCnt = 0; strcpy (path, BASEPATH); pdir=opendir(path); while ((pent=readdir(pdir)) != NULL) { //printd (" '%s'\n", pent->d_name); sprintf (path, BASEPATH"/%s", pent->d_name); // Let's check if we have really a folder if (strcmp (pent->d_name, ".") != 0 && strcmp (pent->d_name, "..") != 0 && DirExist (path)) { strcpy (pln.nands[pln.nandsCnt].path, path); pln.nands[pln.nandsCnt].count = CountObjectsInFolder (pln.nands[pln.nandsCnt].path); if (pln.nands[pln.nandsCnt].count == 0 || IsNandFolder (pln.nands[pln.nandsCnt].path)) { //printd (" %s: %d\n", pln.nands[pln.nandsCnt].path, pln.nands[pln.nandsCnt].count); pln.nandsCnt++; // Added } } } closedir(pdir); printd ("%d found\n", pln.nandsCnt); }
int __cdecl main(int argc, char* argv[]) { ::SetDllDirectory(""); #ifndef _WIN64 HINSTANCE hKernel32 = ::LoadLibrary("Kernel32.dll"); typedef BOOL (WINAPI * SPDEPP)(DWORD); SPDEPP pSPDEPP = (SPDEPP)::GetProcAddress(hKernel32, "SetProcessDEPPolicy"); if(pSPDEPP != NULL) { pSPDEPP(PROCESS_DEP_ENABLE); } ::FreeLibrary(hKernel32); #endif sTitle = "PtokaX DC Hub " + string(PtokaXVersionString); #ifdef _DEBUG sTitle += " [debug]"; #endif #ifdef _DEBUG // AllocConsole(); // hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Cout("PtokaX Debug console\n"); #endif char sBuf[MAX_PATH+1]; ::GetModuleFileName(NULL, sBuf, MAX_PATH); char * sPath = strrchr(sBuf, '\\'); if(sPath != NULL) { PATH = string(sBuf, sPath-sBuf); } else { PATH = sBuf; } char * sServiceName = NULL; bool bInstallService = false; for(int i = 0; i < argc; i++) { if(stricmp(argv[i], "-s") == NULL || stricmp(argv[i], "/service") == NULL) { if(++i == argc) { AppendLog("Missing service name!"); return EXIT_FAILURE; } sServiceName = argv[i]; bService = true; } else if(stricmp(argv[i], "-c") == 0) { if(++i == argc) { printf("Missing config directory!"); return EXIT_FAILURE; } size_t szLen = strlen(argv[i]); if(szLen >= 1 && argv[i][0] != '\\' && argv[i][0] != '/') { if(szLen < 4 || (argv[i][1] != ':' || (argv[i][2] != '\\' && argv[i][2] != '/'))) { printf("Config directory must be absolute path!"); return EXIT_FAILURE; } } if(argv[i][szLen - 1] == '/' || argv[i][szLen - 1] == '\\') { PATH = string(argv[i], szLen - 1); } else { PATH = string(argv[i], szLen); } if(DirExist(PATH.c_str()) == false) { if(CreateDirectory(PATH.c_str(), NULL) == 0) { printf("Config directory not exist and can't be created!"); return EXIT_FAILURE; } } } else if(stricmp(argv[i], "-i") == NULL || stricmp(argv[i], "/install") == NULL) { if(++i == argc) { printf("Please specify service name!"); return EXIT_FAILURE; } sServiceName = argv[i]; bInstallService = true; } else if(stricmp(argv[i], "-u") == NULL || stricmp(argv[i], "/uninstall") == NULL) { if(++i == argc) { printf("Please specify service name!"); return EXIT_FAILURE; } sServiceName = argv[i]; return UninstallService(sServiceName); } else if(stricmp(argv[i], "-v") == NULL || stricmp(argv[i], "/version") == NULL) { printf((sTitle+" built on "+__DATE__+" "+__TIME__).c_str()); return EXIT_SUCCESS; } else if(stricmp(argv[i], "-h") == NULL || stricmp(argv[i], "/help") == NULL) { printf("PtokaX [-c <configdir>] [-i <servicename>] [-u <servicename>] [-v]"); return EXIT_SUCCESS; } else if(stricmp(argv[i], "/nokeycheck") == NULL) { bCmdNoKeyCheck = true; } else if(stricmp(argv[i], "/generatexmllanguage") == NULL) { LangMan::GenerateXmlExample(); return EXIT_SUCCESS; } } if(bInstallService == true) { if(sPath == NULL && strcmp(PATH.c_str(), sBuf) == 0) { return InstallService(sServiceName, NULL); } else { return InstallService(sServiceName, PATH.c_str()); } } ExceptionHandlingInitialize(PATH, sBuf); if(bService == false) { ServerInitialize(); if(ServerStart() == false) { printf("Server start failed!"); ExceptionHandlingUnitialize(); return EXIT_FAILURE; } else { printf((sTitle+" running...\n").c_str()); } MSG msg; BOOL bRet; while((bRet = ::GetMessage(&msg, NULL, 0, 0)) != 0) { if(bRet == -1) { // handle the error and possibly exit } else { if(msg.message == WM_USER+1) { break; } else if(msg.message == WM_TIMER) { if(msg.wParam == srvLoopTimer) { srvLoop->Looper(); } else if(msg.wParam == regtimer) { ServerOnRegTimer(); } else { //Must be script timer ScriptOnTimer(msg.wParam); } } ::TranslateMessage(&msg); ::DispatchMessage(&msg); } } ExceptionHandlingUnitialize(); } else { SERVICE_TABLE_ENTRY DispatchTable[] = { { sServiceName, StartService }, { NULL, NULL } }; if(StartServiceCtrlDispatcher(DispatchTable) == false) { AppendLog("StartServiceCtrlDispatcher failed ("+string((uint32_t)GetLastError())+")!"); ExceptionHandlingUnitialize(); return EXIT_FAILURE; } } return EXIT_SUCCESS; }
//--------------------------------------------------------------------------------- int main(int argc, char **argv) { InitVideo (); printf ("\n"); printd ("---------------------------------------------------------------------------\n"); printd (" priibooter "VER" by stfour\n"); printd (" (part of postLoader project)\n"); printd ("---------------------------------------------------------------------------\n"); PAD_Init(); WPAD_Init(); // Let's mount devices int sd,usb; sd = Fat_Mount (DEV_SD, &keypressed); usb = Fat_Mount (DEV_USB, &keypressed); if (sd == 0 && usb == 0) { printd ("SD & USB not available, booting to System Menu\n"); sleep(3); BootToMenu (); } if (sd && FileExist (POSTLOADER_SD)) pl_sd = 1; if (sd && FileExist (POSTLOADER_SDAPP)) pl_sd = 1; if (usb && FileExist (POSTLOADER_USB)) pl_usb = 1; if (usb && FileExist (POSTLOADER_USBAPP)) pl_usb = 1; printf ("\n"); if (sd && !DirExist ("sd://ploader")) mkdir ("sd://ploader", S_IREAD | S_IWRITE); LoadPLN (); if (pl_sd == 0 && pl_usb == 0) { printd ("postLoader.dol not found on sd/usb"); if (pln.bootMode != PLN_BOOT_NEEK) keypressed = 1; } // On the sd we should have plneek.dat... it will instruct us what to do nandSource = NULL; if (sd) { printd ("Checking "PLNEEK_DAT"\n"); nandSource = (char*)ReadFile2Buffer (PLNEEK_DAT, NULL, NULL); } if (nandSource) { GetNandsFolders (); char *p; p = nandSource+strlen(nandSource)-1; while (*p != '/' && p > nandSource) p--; if (*p == '/') p++; strcpy (pln.name, p); printd (PLNEEK_DAT" found -> '%s'\n", nandSource); // If nandSource isn't null, we have to copy selected nand. But before check if we have a nand on the root... if exist it must be backed up StoreCurrentNand (); // Now we can move back the selected nand MoveNewNand (); } // After read remove the configuration file. If something fail, doesn't do it again remove (PLNEEK_DAT); GetNandsFolders (); Boot (); exit (0); }
int main(int argc, char* argv[]) { sTitle = "PtokaX DC Hub " + string(PtokaXVersionString); #ifdef _DEBUG sTitle += " [debug]"; #endif for(int i = 0; i < argc; i++) { if(strcasecmp(argv[i], "-d") == 0) { bDaemon = true; } else if(strcasecmp(argv[i], "-c") == 0) { if(++i == argc) { printf("Missing config directory!\n"); return EXIT_FAILURE; } if(argv[i][0] != '/') { printf("Config directory must be absolute path!\n"); return EXIT_FAILURE; } size_t szLen = strlen(argv[i]); if(argv[i][szLen - 1] == '/') { PATH = string(argv[i], szLen - 1); } else { PATH = string(argv[i], szLen); } if(DirExist(PATH.c_str()) == false) { if(mkdir(PATH.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP) == -1) { if(bDaemon == true) { syslog(LOG_USER | LOG_ERR, "Config directory not exist and can't be created!\n"); } else { printf("Config directory not exist and can't be created!"); } } } } else if(strcasecmp(argv[i], "-v") == 0) { printf("%s built on %s %s\n", sTitle.c_str(), __DATE__, __TIME__); return EXIT_SUCCESS; } else if(strcasecmp(argv[i], "-h") == 0) { printf("PtokaX [-d] [-c <configdir>] [-v]\n"); return EXIT_SUCCESS; } else if(strcasecmp(argv[i], "/generatexmllanguage") == 0) { LangMan::GenerateXmlExample(); return EXIT_SUCCESS; } } if(PATH.size() == 0) { char* home; char curdir[PATH_MAX]; if(bDaemon == true && (home = getenv("HOME")) != NULL) { PATH = string(home) + "/.PtokaX"; if(DirExist(PATH.c_str()) == false) { if(mkdir(PATH.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP) == -1) { syslog(LOG_USER | LOG_ERR, "Config directory not exist and can't be created!\n"); } } } else if(getcwd(curdir, PATH_MAX) != NULL) { PATH = curdir; } else { PATH = "."; } } if(bDaemon == true) { printf("Starting %s as daemon using %s as config directory.\n", sTitle.c_str(), PATH.c_str()); pid_t pid1 = fork(); if(pid1 == -1) { syslog(LOG_USER | LOG_ERR, "First fork failed!\n"); return EXIT_FAILURE; } else if(pid1 > 0) { return EXIT_SUCCESS; } if(setsid() == -1) { syslog(LOG_USER | LOG_ERR, "Setsid failed!\n"); return EXIT_FAILURE; } pid_t pid2 = fork(); if(pid2 == -1) { syslog(LOG_USER | LOG_ERR, "Second fork failed!\n"); return EXIT_FAILURE; } else if(pid2 > 0) { return EXIT_SUCCESS; } chdir("/"); close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); umask(117); if(open("/dev/null", O_RDWR) == -1) { syslog(LOG_USER | LOG_ERR, "Failed to open /dev/null!\n"); return EXIT_FAILURE; } dup(0); dup(0); } sigset_t sst; sigemptyset(&sst); sigaddset(&sst, SIGPIPE); sigaddset(&sst, SIGURG); sigaddset(&sst, SIGALRM); sigaddset(&sst, SIGSCRTMR); sigaddset(&sst, SIGREGTMR); if(bDaemon == true) { sigaddset(&sst, SIGHUP); } pthread_sigmask(SIG_BLOCK, &sst, NULL); struct sigaction sigact; sigact.sa_handler = SigHandler; sigemptyset(&sigact.sa_mask); sigact.sa_flags = 0; if(sigaction(SIGINT, &sigact, NULL) == -1) { AppendDebugLog("%s - [ERR] Cannot create sigaction SIGINT in main\n", 0); exit(EXIT_FAILURE); } if(sigaction(SIGTERM, &sigact, NULL) == -1) { AppendDebugLog("%s - [ERR] Cannot create sigaction SIGTERM in main\n", 0); exit(EXIT_FAILURE); } if(sigaction(SIGQUIT, &sigact, NULL) == -1) { AppendDebugLog("%s - [ERR] Cannot create sigaction SIGQUIT in main\n", 0); exit(EXIT_FAILURE); } if(bDaemon == false && sigaction(SIGHUP, &sigact, NULL) == -1) { AppendDebugLog("%s - [ERR] Cannot create sigaction SIGHUP in main\n", 0); exit(EXIT_FAILURE); } ServerInitialize(); if(ServerStart() == false) { if(bDaemon == false) { printf("Server start failed!\n"); } else { syslog(LOG_USER | LOG_ERR, "Server start failed!\n"); } return EXIT_FAILURE; } else if(bDaemon == false) { printf("%s running...\n", sTitle.c_str()); } struct timespec sleeptime; sleeptime.tv_sec = 0; sleeptime.tv_nsec = 100000000; while(true) { srvLoop->Looper(); if(bServerTerminated == true) { break; } nanosleep(&sleeptime, NULL); } if(bDaemon == false) { printf("%s ending...\n", sTitle.c_str()); } return EXIT_SUCCESS; }