FskErr KplFileRename(const char *fullPath, const char *newName) { int err; char *p, newPath[PATH_MAX]; KplFileInfo itemInfo; err = sCheckFullPath(fullPath, kKplPathIsFile); if (err) return err; err = KplFileGetFileInfo(fullPath, &itemInfo); if (err) return err; if (itemInfo.filetype == kKplDirectoryItemIsDirectory) return kFskErrIsDirectory; p = FskStrRChr((char *)newName, '/'); if (p) return kFskErrOperationFailed; // newName contains path elements FskStrCopy(newPath, fullPath); p = FskStrRChr(newPath, '/'); if (p) *++p = '\0'; FskStrCat(newPath, newName); err = rename(fullPath, newPath); if (err == -1) return errnoToFskErr(errno); else return kFskErrNone; }
FskErr FskFSFileRename(const char *fullPath, const char *newName) { int err; char *p, newPath[PATH_MAX]; FskFileInfo itemInfo; err = sCheckFullPath(fullPath, kFskPathIsFile); BAIL_IF_ERR(err); err = FskFSFileGetFileInfo(fullPath, &itemInfo); BAIL_IF_ERR(err); if (itemInfo.filetype == kFskDirectoryItemIsDirectory) BAIL(kFskErrIsDirectory); p = FskStrRChr((char *)newName, '/'); if (p) BAIL(kFskErrOperationFailed); // newName contains path elements FskStrCopy(newPath, fullPath); p = FskStrRChr(newPath, '/'); if (p) *++p = '\0'; FskStrCat(newPath, newName); err = rename(fullPath, newPath); if (err == -1) BAIL(errnoToFskErr(errno)); err = kFskErrNone; bail: return err; }
xsIndex fxFindModuleKPR(xsMachine* the, xsIndex moduleID, xsSlot* slot) { char name[1024]; xsBooleanValue absolute, relative; xsStringValue dot, slash; xsIndex id; xsToStringBuffer(*slot, name, sizeof(name)); if ((!FskStrCompareWithLength(name, "./", 2)) || (!FskStrCompareWithLength(name, "../", 3))) { absolute = 0; relative = (moduleID == XS_NO_ID) ? 0 : 1; } else if ((!FskStrCompareWithLength(name, "/", 1))) { FskMemMove(name, name + 1, FskStrLen(name)); absolute = 1; relative = 0; } else { absolute = 1; relative = (moduleID == XS_NO_ID) ? 0 : 1; } slash = FskStrRChr(name, '/'); if (!slash) slash = name; dot = FskStrRChr(slash, '.'); if (dot) { xsBooleanValue known = 0; xsStringValue* extension; for (extension = gxExtensions; *extension; extension++) { if (!FskStrCompare(dot, *extension)) { known = 1; break; } } if (!known) dot = NULL; } if (!dot) dot = name + FskStrLen(name); if (relative) { if (fxFindURI(the, xsName(moduleID), name, dot, &id)) return id; } if (absolute) { xsStringValue* bases = gModulesBases; UInt32 c = gModulesBasesCount; UInt32 i = 1; while (i < c) { if (fxFindURI(the, bases[i], name, dot, &id)) return id; i++; } } return XS_NO_ID; }
FskErr FskFSFileRenameDirectory(const char *fullPath, const char *newName) { int err, len; char *p, newPath[PATH_MAX]; FskFileInfo itemInfo; err = sCheckFullPath(fullPath, kFskPathIsDirectory); BAIL_IF_ERR(err); len = FskStrLen(fullPath); if (len < 2) BAIL(kFskErrOperationFailed); // can't rename root err = FskFSFileGetFileInfo(fullPath, &itemInfo); BAIL_IF_ERR(err); if (itemInfo.filetype != kFskDirectoryItemIsDirectory) BAIL(kFskErrNotDirectory); p = FskStrRChr((char *)newName, '/'); if (p) BAIL(kFskErrOperationFailed); // newName contains path elements FskStrCopy(newPath, fullPath); if (newPath[len - 1] == '/') newPath[len - 1] = '\0'; // remove trailing slash p = FskStrRChr(newPath, '/'); if (p) *++p = '\0'; FskStrCat(newPath, newName); err = rename(fullPath, newPath); if (err == -1) BAIL(errnoToFskErr(errno)); err = kFskErrNone; bail: return err; }
char *copyNiceName(char *name) { char *b; b = FskStrRChr(name, '/'); if (b) b++; else b = name; return FskStrDoCopy(b); }
FskErr KplFileRenameDirectory(const char *fullPath, const char *newName) { int err, len; char *p, newPath[PATH_MAX]; KplFileInfo itemInfo; err = sCheckFullPath(fullPath, kKplPathIsDirectory); if (err) return err; len = FskStrLen(fullPath); if (len < 2) return kFskErrOperationFailed; // can't rename root err = KplFileGetFileInfo(fullPath, &itemInfo); if (err) return err; if (itemInfo.filetype != kKplDirectoryItemIsDirectory) return kFskErrNotDirectory; p = FskStrRChr((char *)newName, '/'); if (p) return kFskErrOperationFailed; // newName contains path elements FskStrCopy(newPath, fullPath); if (newPath[len - 1] == '/') newPath[len - 1] = '\0'; // remove trailing slash p = FskStrRChr(newPath, '/'); if (p) *++p = '\0'; FskStrCat(newPath, newName); err = rename(fullPath, newPath); if (err == -1) return errnoToFskErr(errno); else return kFskErrNone; }
xsBooleanValue fxFindModuleLoadAsDirectory(xsMachine* the, xsStringValue base, xsStringValue name, xsIndex* id) { char node[1024]; UInt32 nodeSize; xsStringValue slash; FskErr err; FskFileMapping map = NULL; xsBooleanValue result = 0; xsStringValue path = NULL; unsigned char* data; FskInt64 size; xsSlot slot; nodeSize = sizeof(node); FskStrNCopy(node, base, nodeSize); slash = FskStrRChr(node, '/'); if (slash) *slash = 0; FskStrNCat(node, "/", nodeSize); FskStrNCat(node, name, nodeSize); if (FskStrTail(node, "/") != 0) FskStrNCat(node, "/", nodeSize); FskStrNCat(node, "package.json", nodeSize); err = KprURLToPath(node, &path); if (err == kFskErrNone) { err = FskFileMap(path, &data, &size, 0, &map); if (err == kFskErrNone) { xsTry { slot = xsNewInstanceOf(xsChunkPrototype); xsSetHostData(slot, data); xsSetHostDestructor(slot, NULL); xsSet(slot, xsID("length"), xsInteger(size)); slot = xsCall1(xsGet(xsGlobal, xsID("JSON")), xsID("parse"), slot); } xsCatch { slot = xsUndefined; } if (xsTest(slot)) { slot = xsGet(slot, xsID("main")); if (xsTest(slot)) { FskStrNCopy(node, name, nodeSize); if (FskStrTail(node, "/") != 0) FskStrNCat(node, "/", nodeSize); FskStrNCat(node, xsToString(slot), nodeSize); if (fxFindModuleLoadAsFile(the, base, node, id)) result = 1; } } }
void fxLoadModule(txMachine* the, txID moduleID) { txArchive* archive = the->archive; txSlot* key = fxGetKey(the, moduleID); txString path = NULL; char buffer[PATH_MAX]; txString dot = NULL; txString* extension; txLoader* loader; KprURLToPath(key->value.key.string, &path); c_strcpy(buffer, path); c_free(path); path = buffer; if (archive) { if (!c_strncmp(path, archive->base, archive->baseLength)) { txInteger c = archive->scriptCount, i; txScript* script = archive->scripts; path += archive->baseLength; #if mxWindows { char* separator = path; while (*separator) { if (*separator == '/') *separator = mxSeparator; separator++; } } #endif for (i = 0; i < c; i++) { if (!c_strcmp(path, script->path)) { fxResolveModule(the, moduleID, script, C_NULL, C_NULL); return; } script++; } } } dot = FskStrRChr(path, '.'); for (extension = gxExtensions, loader = gxLoaders; *extension; extension++, loader++) { if (!FskStrCompare(dot, *extension)) { (**loader)(the, buffer, moduleID); break; } } }
FskErr FskECMAScriptLoadLibrary(const char *name) { FskErr err = kFskErrBadData; FskLibraryLoadProc libLoad; char *extension = NULL, *symbolName = NULL, *dot, *slash, *fullPath = NULL, *temp = NULL; FskLibrary library = NULL; char* root = NULL; FskFileInfo itemInfo; FskDebugStr("FskECMAScriptLoadLibrary: %s", name); dot = FskStrRChr(name, '.'); slash = FskStrRChr(name, '/'); if ((NULL == dot) || (slash && (slash > dot))) { #if TARGET_OS_WIN32 extension = ".dll"; #elif TARGET_OS_MAC || TARGET_OS_LINUX extension = ".so"; #elif TARGET_OS_KPL extension = (char*)KplECMAScriptGetExtension(); #endif } if (extension) fullPath = FskStrDoCat(name, extension); else fullPath = FskStrDoCopy(name); if (kFskErrNone != FskFileGetFileInfo(fullPath, &itemInfo)) { #if TARGET_OS_ANDROID char *libDir; libDir = FskStrDoCat(gAndroidCallbacks->getStaticDataDirCB(), "../lib/lib"); temp = FskStrDoCat(libDir, fullPath); FskMemPtrDispose(libDir); #else root = FskGetApplicationPath(); temp = FskStrDoCat(root, fullPath); FskMemPtrDispose(root); #endif if (kFskErrNone != FskFileGetFileInfo(temp, &itemInfo)) { FskDebugStr(" - no file: %s", temp); goto bail; } FskMemPtrDispose(fullPath); fullPath = temp; temp = NULL; } err = FskLibraryLoad(&library, fullPath); FskDebugStr(" - try: %s -> %d", fullPath, err); if (err) goto bail; symbolName = FskStrDoCat(name, "_fskLoad"); if ((kFskErrNone == FskLibraryGetSymbolAddress(library, symbolName, &libLoad)) || (kFskErrNone == FskLibraryGetSymbolAddress(library, "fskLoad", &libLoad))) { err = (libLoad)(library); FskDebugStr(" - symbolName: %x -> %d", libLoad, err); if (err) goto bail; err = kFskErrNone; } bail: if (err) { FskLibraryUnload(library); } FskMemPtrDispose(symbolName); FskMemPtrDispose(fullPath); return err; }
void fxIncludeScript(txParser* parser, txString string) { txBoolean done = 0; char* base = NULL; char name[PATH_MAX]; char* slash = NULL; char* dot = NULL; char* extension = NULL; char* url = NULL; char* path = NULL; FskFileInfo fileInfo; FskFileMapping map= NULL; txFileMapStream fileMapStream; txStringStream stringStream; txMachine* the = parser->console; fxBeginHost(the); { mxTry(the) { xsThrowIfFskErr(KprPathToURL(parser->path->string, &base)); FskStrCopy(name, string); slash = FskStrRChr(name, '/'); if (!slash) slash = name; dot = FskStrRChr(slash, '.'); if (dot) extension = dot; else extension = name + FskStrLen(name); if (!dot) FskStrCopy(extension, ".js"); if (!FskStrCompare(extension, ".js")) { xsThrowIfFskErr(KprURLMerge(base, name, &url)); xsThrowIfFskErr(KprURLToPath(url, &path)); if (kFskErrNone == FskFileGetFileInfo(path, &fileInfo)) { xsThrowIfFskErr(FskFileMap(path, (unsigned char**)&(fileMapStream.buffer), &(fileMapStream.size), 0, &map)); fileMapStream.offset = 0; fxIncludeTree(parser, &fileMapStream, fxFileMapGetter, parser->flags, path); done = 1; FskFileDisposeMap(map); map = NULL; } FskMemPtrDisposeAt(&path); FskMemPtrDisposeAt(&url); } if (!dot) FskStrCopy(extension, ".xml"); if (!FskStrCompare(extension, ".xml")) { xsThrowIfFskErr(KprURLMerge(base, name, &url)); xsThrowIfFskErr(KprURLToPath(url, &path)); if (kFskErrNone == FskFileGetFileInfo(path, &fileInfo)) { xsThrowIfFskErr(FskFileMap(path, &fileMapStream.buffer, &fileMapStream.size, 0, &map)); fileMapStream.offset = 0; mxPushInteger(0); mxPushInteger(0); mxPushInteger(0); mxPushInteger(0); mxPushInteger(3); fxParse(the, &fileMapStream, fxFileMapGetter, path, 1, xsSourceFlag | xsDebugFlag); fxCallID(the, fxID(the, "generate")); fxToString(the, the->stack); stringStream.slot = the->stack; stringStream.size = c_strlen(stringStream.slot->value.string); stringStream.offset = 0; fxIncludeTree(parser, &stringStream, fxStringGetter, parser->flags, path); done = 1; FskFileDisposeMap(map); map = NULL; } FskMemPtrDisposeAt(&path); FskMemPtrDisposeAt(&url); } FskMemPtrDispose(base); } mxCatch(the) { FskFileDisposeMap(map); FskMemPtrDispose(path); FskMemPtrDispose(url); FskMemPtrDispose(base); break; } } fxEndHost(the); if (!done) fxReportParserError(parser, "include file not found: %s", string); }