static void tr_migrateResume( const char *oldDirectory, const char *newDirectory ) { int fd; struct dirent * dirp; unsigned char *buff = MALLOC(1024); char oldFile[MAX_PATH_LENGTH]; char newFile[MAX_PATH_LENGTH]; dirp = (struct dirent *)buff; fd = fs_opendir(oldDirectory); if (fd >= 0) { while(( fs_readdir(fd, dirp)) > 0) { if ( strncmp( "resume.", dirp->d_name, 7 ) ) { continue; } snprintf( oldFile, MAX_PATH_LENGTH, "%s/%s", oldDirectory, dirp->d_name ); snprintf( newFile, MAX_PATH_LENGTH, "%s/%s", newDirectory, dirp->d_name ); fs_rename( oldFile, newFile ); } fs_closedir( fd ); } FREE(buff); }
void CUpdater::ExecuteExit() { if (!NeedResetClient() || !m_Updated) return; SelfDelete(); #ifdef CONF_FAMILY_WINDOWS ShellExecuteA(0,0,"du.bat",0,0,SW_HIDE); #else if (fs_rename("tw_tmp","teeworlds")) dbg_msg("autoupdate", "Error renaming binary file"); if (system("chmod +x teeworlds")) dbg_msg("autoupdate", "Error setting executable bit"); pid_t pid; pid = fork(); if (pid == 0) { char* argv[1]; argv[0] = NULL; execv("teeworlds", argv); } else return; #endif }
virtual bool RenameFile(const char* pOldFilename, const char* pNewFilename, int Type) { if(Type < 0 || Type >= m_NumPaths) return false; char aOldBuffer[MAX_PATH_LENGTH]; char aNewBuffer[MAX_PATH_LENGTH]; return !fs_rename(GetPath(Type, pOldFilename, aOldBuffer, sizeof(aOldBuffer)), GetPath(Type, pNewFilename, aNewBuffer, sizeof (aNewBuffer))); }
static void make_dirs_and_migrate(void) { Array items; int i; const char *src; char *dst; if (fs_mkdir("Replays")) { if ((items = fs_dir_scan("", is_replay))) { for (i = 0; i < array_len(items); i++) { src = DIR_ITEM_GET(items, i)->path; dst = concat_string("Replays/", src, NULL); fs_rename(src, dst); free(dst); } fs_dir_free(items); } } if (fs_mkdir("Scores")) { if ((items = fs_dir_scan("", is_score))) { for (i = 0; i < array_len(items); i++) { src = DIR_ITEM_GET(items, i)->path; dst = concat_string("Scores/", src + sizeof ("neverballhs-") - 1, ".txt", NULL); fs_rename(src, dst); free(dst); } fs_dir_free(items); } } fs_mkdir("Screenshots"); }
static void fs_sis_replace_hash_file(struct sis_fs_file *file) { const char *hash_fname, *path = fs_file_path(&file->file); struct fs *super_fs = file->super->fs; string_t *temp_path; int ret; if (file->hash_input == NULL) { /* hash file didn't exist previously. we should be able to create it with link() */ if (fs_link(super_fs, path, file->hash_path) < 0) { if (errno == EEXIST) { /* the file was just created. it's probably a duplicate, but it's too much trouble trying to deduplicate it anymore */ } else { i_error("fs-sis: %s", fs_last_error(super_fs)); } } return; } temp_path = t_str_new(256); hash_fname = strrchr(file->hash_path, '/'); if (hash_fname == NULL) hash_fname = file->hash_path; else { str_append_n(temp_path, file->hash_path, (hash_fname-file->hash_path) + 1); hash_fname++; } str_printfa(temp_path, "%s%s.tmp", super_fs->set.temp_file_prefix, hash_fname); /* replace existing hash file atomically */ ret = fs_link(super_fs, path, str_c(temp_path)); if (ret < 0 && errno == EEXIST) { /* either someone's racing us or it's a stale file. try to continue. */ if (fs_unlink(super_fs, str_c(temp_path)) < 0 && errno != ENOENT) i_error("fs-sis: %s", fs_last_error(super_fs)); ret = fs_link(super_fs, path, str_c(temp_path)); } if (ret < 0) { i_error("fs-sis: %s", fs_last_error(super_fs)); return; } if (fs_rename(super_fs, str_c(temp_path), file->hash_path) < 0) { if (errno == ENOENT) { /* apparently someone else just renamed it. ignore. */ } else { i_error("fs-sis: %s", fs_last_error(super_fs)); } (void)fs_unlink(super_fs, str_c(temp_path)); } }
virtual bool RenameBinaryFile(const char* pOldFilename, const char* pNewFilename) { char aOldBuffer[MAX_PATH_LENGTH]; char aNewBuffer[MAX_PATH_LENGTH]; GetBinaryPath(pOldFilename, aOldBuffer, sizeof(aOldBuffer)); GetBinaryPath(pNewFilename, aNewBuffer, sizeof(aNewBuffer)); if(fs_makedir_rec_for(aNewBuffer) < 0) dbg_msg("storage", "cannot create folder for: %s", aNewBuffer); return !fs_rename(aOldBuffer, aNewBuffer); }
/* mv command */ LOCAL void cmd_mv(INT ac, B *av[]) { ER er; if (ac < 3) return; er = fs_rename(av[1], av[2]); if (er >= E_OK) { P("file '%s' -> '%s' moved\n", av[1], av[2]); } else { P("fs_rename(%s,%s) ERR [%#x]\n", av[1], av[2], er); } }
virtual bool RenameFile(const char *pOldFilename, const char *pNewFilename, int Type) { if(Type < 0 || Type >= m_NumPaths) return false; char aOldBuffer[MAX_PATH_LENGTH]; char aNewBuffer[MAX_PATH_LENGTH]; GetPath(Type, pOldFilename, aOldBuffer, sizeof(aOldBuffer)); GetPath(Type, pNewFilename, aNewBuffer, sizeof(aNewBuffer)); bool success = !fs_rename(aOldBuffer, aNewBuffer); if(!success) dbg_msg("storage", "failed to rename: %s -> %s", aOldBuffer, aNewBuffer); return success; }
void CMain::JSONUpdateThread(void *pUser) { CJSONUpdateThreadData *m_pJSONUpdateThreadData = (CJSONUpdateThreadData *)pUser; CClient *pClients = m_pJSONUpdateThreadData->pClients; CConfig *pConfig = m_pJSONUpdateThreadData->pConfig; while(gs_Running) { char aFileBuf[2048*NET_MAX_CLIENTS]; char *pBuf = aFileBuf; str_format(pBuf, sizeof(aFileBuf), "{\n\"servers\": [\n"); pBuf += strlen(pBuf); for(int i = 0; i < NET_MAX_CLIENTS; i++) { if(!pClients[i].m_Active || pClients[i].m_Disabled) continue; if(pClients[i].m_Connected) { // Connectivity bool Online4; bool Online6; Online4 = pClients[i].m_Stats.m_Online4; Online6 = pClients[i].m_Stats.m_Online6; // Uptime char aUptime[16]; int Days = pClients[i].m_Stats.m_Uptime/60.0/60.0/24.0; if(Days > 0) { if(Days > 1) str_format(aUptime, sizeof(aUptime), "%d days", Days); else str_format(aUptime, sizeof(aUptime), "%d day", Days); } else str_format(aUptime, sizeof(aUptime), "%02d:%02d:%02d", (int)(pClients[i].m_Stats.m_Uptime/60.0/60.0), (int)((pClients[i].m_Stats.m_Uptime/60)%60), (int)((pClients[i].m_Stats.m_Uptime)%60)); // Load float Load = pClients[i].m_Stats.m_Load; // CPU int CPU = pClients[i].m_Stats.m_CPU; // Memory int Memory; if(pClients[i].m_Stats.m_MemTotal) Memory = round(((float)pClients[i].m_Stats.m_MemUsed/pClients[i].m_Stats.m_MemTotal)*100.0); else Memory = 0; // HDD int HDD; if(pClients[i].m_Stats.m_HDDTotal) HDD = round(((float)pClients[i].m_Stats.m_HDDUsed/pClients[i].m_Stats.m_HDDTotal)*100.0); else HDD = 0; str_format(pBuf, sizeof(aFileBuf) - (pBuf - aFileBuf), "{ \"name\": \"%s\", \"type\": \"%s\", \"host\": \"%s\", \"location\": \"%s\", \"online4\": %s, \"online6\": %s, \"uptime\": \"%s\", \"load\": \"%.2f\", \"network_rx\": %d, \"network_tx\": %d, \"cpu\": %d, \"memory\": %d, \"memory_total\": %" PRId64 ", \"memory_used\": %" PRId64 ", \"hdd\": %d, \"hdd_total\": %" PRId64 ", \"hdd_used\": %" PRId64 " },\n", pClients[i].m_aName, pClients[i].m_aType, pClients[i].m_aHost, pClients[i].m_aLocation, Online4 ? "true" : "false", Online6 ? "true" : "false", aUptime, Load, pClients[i].m_Stats.m_NetworkRx, pClients[i].m_Stats.m_NetworkTx, CPU, Memory, pClients[i].m_Stats.m_MemTotal, pClients[i].m_Stats.m_MemUsed, HDD, pClients[i].m_Stats.m_HDDTotal, pClients[i].m_Stats.m_HDDUsed); pBuf += strlen(pBuf); } else { str_format(pBuf, sizeof(aFileBuf) - (pBuf - aFileBuf), "{ \"name\": \"%s\", \"type\": \"%s\", \"host\": \"%s\", \"location\": \"%s\", \"online4\": false, \"online6\": false },\n", pClients[i].m_aName, pClients[i].m_aType, pClients[i].m_aHost, pClients[i].m_aLocation); pBuf += strlen(pBuf); } } if(!m_pJSONUpdateThreadData->m_ReloadRequired) str_format(pBuf - 2, sizeof(aFileBuf) - (pBuf - aFileBuf), "\n],\n\"updated\": \"%lld\"\n}", (long long)time(/*ago*/0)); else { str_format(pBuf - 2, sizeof(aFileBuf) - (pBuf - aFileBuf), "\n],\n\"updated\": \"%lld\",\n\"reload\": true\n}", (long long)time(/*ago*/0)); m_pJSONUpdateThreadData->m_ReloadRequired--; } pBuf += strlen(pBuf); char aJSONFileTmp[1024]; str_format(aJSONFileTmp, sizeof(aJSONFileTmp), "%s~", pConfig->m_aJSONFile); IOHANDLE File = io_open(aJSONFileTmp, IOFLAG_WRITE); if(!File) { dbg_msg("main", "Couldn't open %s", aJSONFileTmp); exit(1); } io_write(File, aFileBuf, (pBuf - aFileBuf)); io_flush(File); io_close(File); fs_rename(aJSONFileTmp, pConfig->m_aJSONFile); thread_sleep(1000); } fs_remove(pConfig->m_aJSONFile); }