diff --git a/common/menu-entry.c b/common/menu-entry.c index 3c04a33..b2ed75f 100644 --- a/common/menu-entry.c +++ b/common/menu-entry.c @@ -202,19 +202,19 @@ static bool menuEntryLoadExternalNacp(menuEntry_s* me, const char* path) { } while (lastc); }*/ -bool menuEntryLoad(menuEntry_s* me, const char* name, bool shortcut) { +bool menuEntryLoad(menuEntry_s* me, const char* name, bool shortcut, bool check_exists) { int i=0, tmplen; menu_s *menu_fileassoc = menuFileassocGetCurrent(); menuEntry_s* fileassoc_me = NULL; char *strptr = NULL; - static char tempbuf[PATH_MAX+1]; + char tempbuf[PATH_MAX+1]; //bool isOldAppFolder = false; - if (!fsobjExists(me->path)) return false; + if (check_exists && !fsobjExists(me->path)) return false; tempbuf[PATH_MAX] = 0; - strcpy(me->name, name); - + strncpy(me->name, name, sizeof(me->name)-1); + if (me->type == ENTRY_TYPE_FOLDER) { //Check for /.nro @@ -285,9 +285,8 @@ bool menuEntryLoad(menuEntry_s* me, const char* name, bool shortcut) { if (me->type == ENTRY_TYPE_FILE) { - //strcpy(me->name, name);//This is already done before both if statements - strcpy(me->author, textGetString(StrId_DefaultPublisher)); - strcpy(me->version, "1.0.0"); + strncpy(me->author, textGetString(StrId_DefaultPublisher), sizeof(me->author)-1); + strncpy(me->version, "1.0.0", sizeof(me->version)-1); //shortcut_s sc; @@ -554,7 +553,7 @@ void menuEntryFileassocLoad(const char* filepath) { strptr = getSlash(app_path); if(strptr[0] == '/') strptr++; - if (menuEntryLoad(me, strptr, 0)) { + if (menuEntryLoad(me, strptr, 0, true)) { strncpy(app_author, me->author, sizeof(app_author)); app_author[sizeof(app_author)-1] = 0; strncpy(app_version, me->version, sizeof(app_version)); diff --git a/common/menu-list.c b/common/menu-list.c index 3013f89..f22c66a 100644 --- a/common/menu-list.c +++ b/common/menu-list.c @@ -207,7 +207,7 @@ int menuScan(const char* target) { strncpy(me->path, tmp_path, sizeof(me->path)-1); me->path[sizeof(me->path)-1] = 0; - if (menuEntryLoad(me, dp->d_name, shortcut)) + if (menuEntryLoad(me, dp->d_name, shortcut, true)) menuAddEntry(me); else menuDeleteEntry(me, 0); @@ -253,7 +253,7 @@ int themeMenuScan(const char* target) { strncpy(me->path, tmp_path, sizeof(me->path)-1); me->path[sizeof(me->path)-1] = 0; - if (menuEntryLoad(me, dp->d_name, shortcut)) + if (menuEntryLoad(me, dp->d_name, shortcut, true)) menuAddEntry(me); else menuDeleteEntry(me, 0); @@ -265,7 +265,7 @@ int themeMenuScan(const char* target) { menuEntry_s* me = menuCreateEntry(ENTRY_TYPE_THEME); if(me) { - if(menuEntryLoad(me, textGetString(StrId_DefaultThemeName), false))//Create Default theme Menu Entry + if(menuEntryLoad(me, textGetString(StrId_DefaultThemeName), false, false))//Create Default theme Menu Entry menuAddEntryToFront(me); else menuDeleteEntry(me, 0); diff --git a/common/menu.h b/common/menu.h index ff54a4f..7ac6aa2 100644 --- a/common/menu.h +++ b/common/menu.h @@ -86,7 +86,7 @@ extern "C" { void menuEntryInit(menuEntry_s* me, MenuEntryType type); void menuEntryFree(menuEntry_s* me, bool skip_icongfx); bool fileExists(const char* path); -bool menuEntryLoad(menuEntry_s* me, const char* name, bool shortcut); +bool menuEntryLoad(menuEntry_s* me, const char* name, bool shortcut, bool check_exists); void menuEntryParseIcon(menuEntry_s* me); uint8_t *downscaleImg(const uint8_t *image, int srcWidth, int srcHeight, int destWidth, int destHeight, ImageMode mode); void menuEntryParseNacp(menuEntry_s* me); diff --git a/common/netloader.c b/common/netloader.c index 1464c24..aa5a09a 100644 --- a/common/netloader.c +++ b/common/netloader.c @@ -368,7 +368,7 @@ static int decompress(int sock, FILE *fh, size_t filesize) { int loadnro(menuEntry_s *me, int sock, struct in_addr remote) { //--------------------------------------------------------------------------------- int len, namelen, filelen; - char filename[PATH_MAX+1]; + char filepath[PATH_MAX+1]; len = recvall(sock, &namelen, 4, 0); if (len != 4) { @@ -376,19 +376,19 @@ int loadnro(menuEntry_s *me, int sock, struct in_addr remote) { return -1; } - if (namelen >= sizeof(filename)-1) { - netloader_error("Filename length is too large",errno); + if (namelen >= sizeof(filepath)-1) { + netloader_error("File-path length is too large",errno); return -1; } - len = recvall(sock, filename, namelen, 0); + len = recvall(sock, filepath, namelen, 0); if (len != namelen) { - netloader_error("Error getting filename", errno); + netloader_error("Error getting file-path", errno); return -1; } - filename[namelen] = 0; + filepath[namelen] = 0; len = recvall(sock, &filelen, 4, 0); @@ -403,41 +403,56 @@ int loadnro(menuEntry_s *me, int sock, struct in_addr remote) { int response = 0; - sanitisePath(filename); + sanitisePath(filepath); - snprintf(me->path, sizeof(me->path)-1, "%s%s%s", menuGetRootPath(), DIRECTORY_SEPARATOR, filename); - me->path[PATH_MAX] = 0; + snprintf(me->path, sizeof(me->path)-1, "%s%s%s", menuGetRootPath(), DIRECTORY_SEPARATOR, filepath); // make sure it's terminated - me->path[PATH_MAX] = 0; + me->path[sizeof(me->path)-1] = 0; + strncpy(filepath, me->path, sizeof(filepath)-1); // menuEntryLoad() below will overwrite me->path, so copy me->path to filepath and use that instead. + filepath[sizeof(filepath)-1] = 0; argData_s* ad = &me->args; ad->dst = (char*)&ad->buf[1]; ad->nxlink_host = remote; - launchAddArg(ad, me->path); + const char* ext = getExtension(me->path); + if (ext && strcasecmp(ext, ".nro")==0) + launchAddArg(ad, me->path); + else { + me->type = ENTRY_TYPE_FILE_OTHER; // Handle fileassoc when extension isn't .nro. + if (!menuEntryLoad(me, "", false, false)) { + response = -3; + errno = EINVAL; + netloader_error("File-extension/filename not recognized",0); + } + menuEntryFree(me, false); // We don't need any of the buffers which may have been allocated. + } #ifndef _WIN32 - int fd = open(me->path,O_CREAT|O_WRONLY, ACCESSPERMS); + if (response == 0) { + int fd = open(filepath,O_CREAT|O_WRONLY, ACCESSPERMS); - if (fd < 0) { - response = -1; - netloader_error("open", errno); - } else { - if (ftruncate(fd,filelen) == -1) { - response = -2; - netloader_error("ftruncate",errno); + if (fd < 0) { + response = -1; + netloader_error("open", errno); + } else { + if (ftruncate(fd,filelen) == -1) { + response = -2; + netloader_error("ftruncate",errno); + } + close(fd); } - close(fd); } #endif FILE *file = NULL; - if (response == 0) file = fopen(me->path,"wb"); - - if(NULL == file) { - perror("file"); - response = -1; + if (response == 0) { + file = fopen(filepath,"wb"); + if(file == NULL) { + perror("file"); + response = -1; + } } send(sock,(char *)&response,sizeof(response),0); @@ -449,12 +464,14 @@ int loadnro(menuEntry_s *me, int sock, struct in_addr remote) { netloader_error("Failed to allocate memory",ENOMEM); response = -1; } - else + else { + memset(writebuffer, 0, FILE_BUFFER_SIZE); setvbuf(file,writebuffer,_IOFBF, FILE_BUFFER_SIZE); + } } if (response == 0 ) { - //printf("transferring %s\n%d bytes.\n", filename, filelen); + //printf("transferring %s\n%d bytes.\n", filepath, filelen); if (decompress(sock,file,filelen)==Z_OK) { int netloaded_cmdlen = 0; @@ -499,13 +516,14 @@ int loadnro(menuEntry_s *me, int sock, struct in_addr remote) { } else { response = -1; } + } + if (file) { fflush(file); fclose(file); - free(writebuffer); - - if (response == -1) unlink(me->path); } + if (response == -1) unlink(filepath); + free(writebuffer); return response; }