//打开 T_hFILE Fwl_FileOpen(T_pCSTR path, T_U32 mode) { T_U32 file = 0; T_U32 i, len; //获取路径的长度 len = strlen((CHAR *)path); for (i=len; i !=0; i--) { if (path[i] == '/' || path[i] == '\\') break; } //通过路径创建文件夹 if (i != 0 && path[i-1] != ':') { T_U8 buf[300]; if (i > 300) return FS_INVALID_HANDLE; memcpy(buf, path, 300); buf[i] = 0; if (!File_MkdirsAsc(buf)) { return FS_INVALID_HANDLE; } } //打开文件夹 file = File_OpenAsc(0, (T_U8 *)path, mode); if (!File_IsFile(file)) { File_Close(file); printf("FWL FILE ERROR: Fwl_FileOpen: \n"); return FS_INVALID_HANDLE; } return (T_hFILE)file; }
VmBackupOp * VmBackup_NewScriptOp(VmBackupScriptType type, // IN VmBackupState *state) // IN { Bool fail = FALSE; char **fileList = NULL; char *scriptDir = NULL; int numFiles = 0; size_t i; VmBackupScriptOp *op = NULL; scriptDir = VmBackupGetScriptPath(); if (scriptDir == NULL) { goto exit; } op = calloc(1, sizeof *op); if (op == NULL) { goto exit; } op->state = state; op->type = type; op->callbacks.queryFn = VmBackupScriptOpQuery; op->callbacks.cancelFn = VmBackupScriptOpCancel; op->callbacks.releaseFn = VmBackupScriptOpRelease; g_debug("Trying to run scripts from %s\n", scriptDir); /* * Load the list of scripts to run when freezing. The same list will be * used later in case of failure, or when thawing, in reverse order. * * This logic won't recurse into directories, so only files directly under * the script dir will be considered. * * Legacy scripts will be the first ones to run (or last ones in the * case of thawing). If either the legacy freeze or thaw script * exist, the first entry in the script list will be reserved for * them, and their path might not exist (in case, for example, the * freeze script exists but the thaw script doesn't). */ if (type == VMBACKUP_SCRIPT_FREEZE) { VmBackupScript *scripts = NULL; int legacy = 0; size_t idx = 0; state->scripts = NULL; state->currentScript = 0; if (File_IsFile(LEGACY_FREEZE_SCRIPT) || File_IsFile(LEGACY_THAW_SCRIPT)) { legacy = 1; } if (File_IsDirectory(scriptDir)) { numFiles = File_ListDirectory(scriptDir, &fileList); } if (numFiles + legacy > 0) { scripts = calloc(numFiles + legacy + 1, sizeof *scripts); if (scripts == NULL) { fail = TRUE; goto exit; } /* * VmBackupRunNextScript increments the index, so need to make it point * to "before the first script". */ state->currentScript = -1; state->scripts = scripts; } if (legacy > 0) { scripts[idx++].path = Util_SafeStrdup(LEGACY_FREEZE_SCRIPT); } if (numFiles > 0) { size_t i; if (numFiles > 1) { qsort(fileList, (size_t) numFiles, sizeof *fileList, VmBackupStringCompare); } for (i = 0; i < numFiles; i++) { char *script; script = Str_Asprintf(NULL, "%s%c%s", scriptDir, DIRSEPC, fileList[i]); if (script == NULL) { fail = TRUE; goto exit; } else if (File_IsFile(script)) { scripts[idx++].path = script; } else { free(script); } } } } else if (state->scripts != NULL) { VmBackupScript *scripts = state->scripts; if (strcmp(scripts[0].path, LEGACY_FREEZE_SCRIPT) == 0) { vm_free(scripts[0].path); scripts[0].path = Util_SafeStrdup(LEGACY_THAW_SCRIPT); } } /* * If there are any scripts to be executed, start the first one. If we get to * this point, we won't free the scripts array until VmBackupScriptOpRelease * is called after thawing (or after the sync provider failed and the "fail" * scripts are run). */ fail = (state->scripts != NULL && VmBackupRunNextScript(op) == -1); exit: /* Free the file list. */ for (i = 0; i < numFiles; i++) { free(fileList[i]); } free(fileList); if (fail && op != NULL) { VmBackup_Release((VmBackupOp *) op); op = NULL; } free(scriptDir); return (VmBackupOp *) op; }
void VMTools_BindTextDomain(const char *domain, const char *lang, const char *catdir) { char *dfltdir = NULL; gchar *file; gchar *usrlang = NULL; MsgState *state = MsgGetState(); MsgCatalog *catalog; ASSERT(domain); /* * If the caller has asked for the default user language, detect it and * translate to our internal language string representation. */ if (lang == NULL || *lang == '\0') { usrlang = MsgGetUserLanguage(); lang = usrlang; } g_debug("%s: user locale=%s\n", __FUNCTION__, lang); /* * Use the default install directory if none is provided. */ if (catdir == NULL) { #if defined(OPEN_VM_TOOLS) dfltdir = Util_SafeStrdup(VMTOOLS_DATA_DIR); #else dfltdir = GuestApp_GetInstallPath(); #endif catdir = (dfltdir) ? dfltdir : "."; } file = g_strdup_printf("%s%smessages%s%s%s%s.vmsg", catdir, DIRSEPS, DIRSEPS, lang, DIRSEPS, domain); if (!File_IsFile(file)) { /* * If we couldn't find the catalog file for the user's language, see if * we can find a more generic language (e.g., for "en_US", also try "en"). */ char *sep = Str_Strrchr(lang, '_'); if (sep != NULL) { if (usrlang == NULL) { usrlang = Util_SafeStrdup(lang); } usrlang[sep - lang] = '\0'; g_free(file); file = g_strdup_printf("%s%smessages%s%s%s%s.vmsg", catdir, DIRSEPS, DIRSEPS, usrlang, DIRSEPS, domain); } } catalog = MsgLoadCatalog(file); if (catalog == NULL) { if (Str_Strncmp(lang, "en", 2)) { /* * Don't warn about english dictionary, which may not exist (it is the * default translation). */ g_message("Cannot load message catalog for domain '%s', language '%s', " "catalog dir '%s'.\n", domain, lang, catdir); } } else { g_static_mutex_lock(&state->lock); MsgSetCatalog(domain, catalog); g_static_mutex_unlock(&state->lock); } g_free(file); free(dfltdir); g_free(usrlang); }
static int VmBackupRunNextScript(VmBackupScriptOp *op) // IN/OUT { const char *scriptOp; int ret = 0; ssize_t index; VmBackupScript *scripts = op->state->scripts; switch (op->type) { case VMBACKUP_SCRIPT_FREEZE: index = ++op->state->currentScript; scriptOp = "freeze"; break; case VMBACKUP_SCRIPT_FREEZE_FAIL: index = --op->state->currentScript; scriptOp = "freezeFail"; break; case VMBACKUP_SCRIPT_THAW: index = --op->state->currentScript; scriptOp = "thaw"; break; default: NOT_REACHED(); } while (index >= 0 && scripts[index].path != NULL) { char *cmd; if (File_IsFile(scripts[index].path)) { if (op->state->scriptArg != NULL) { cmd = Str_Asprintf(NULL, "\"%s\" %s \"%s\"", scripts[index].path, scriptOp, op->state->scriptArg); } else { cmd = Str_Asprintf(NULL, "\"%s\" %s", scripts[index].path, scriptOp); } if (cmd != NULL) { g_debug("Running script: %s\n", cmd); scripts[index].proc = ProcMgr_ExecAsync(cmd, NULL); } else { g_debug("Failed to allocate memory to run script: %s\n", scripts[index].path); scripts[index].proc = NULL; } vm_free(cmd); if (scripts[index].proc == NULL) { if (op->type == VMBACKUP_SCRIPT_FREEZE) { ret = -1; break; } else { op->thawFailed = TRUE; } } else { ret = 1; break; } } if (op->type == VMBACKUP_SCRIPT_FREEZE) { index = ++op->state->currentScript; } else { index = --op->state->currentScript; } /* * This happens if all thaw/fail scripts failed to start. Since the first * entry may be a legacy script (which may not exist), need to check * whether the interesting failure is the first or the second entry in * the script list. */ if (index == -1) { size_t failIdx = 0; if (!File_IsFile(scripts[0].path)) { failIdx = 1; } if (scripts[failIdx].proc == NULL && scripts[failIdx].path != NULL) { ret = -1; } } } return ret; }