s32 MountNTFS(u32 sector) { s32 ret; if (fs_ntfs_mount) return 0; //printf("mounting NTFS\n"); //Wpad_WaitButtons(); _FAT_mem_init(); ntfsInit(); // Call ntfs init here, to prevent locale resets // ntfsInit resets locale settings // which breaks unicode in console // so we change it back to C-UTF-8 setlocale(LC_CTYPE, "C-UTF-8"); setlocale(LC_MESSAGES, "C-UTF-8"); if (wbfsDev == WBFS_DEVICE_USB) { /* Initialize WBFS interface */ // if (!__io_wiiums.startup()) { ret = __io_usbstorage2.startup(); if (!ret) { return -1; } // } /* Mount device */ // if (!ntfsMount("NTFS", &__io_wiiums, sector, CACHE, SECTORS, NTFS_SHOW_HIDDEN_FILES | NTFS_RECOVER)) { ret = ntfsMount("NTFS", &__io_usbstorage2, sector, CACHE, SECTORS, NTFS_SHOW_HIDDEN_FILES | NTFS_READ_ONLY | NTFS_RECOVER); if (!ret) { return -2; } // } } else if (wbfsDev == WBFS_DEVICE_SDHC) { if (sdhc_mode_sd == 0) { ret = ntfsMount("NTFS", &__io_sdhc, 0, CACHE, SECTORS, NTFS_SHOW_HIDDEN_FILES | NTFS_READ_ONLY | NTFS_RECOVER); } else { ret = ntfsMount("NTFS", &__io_sdhc, 0, CACHE, SECTORS_SD, NTFS_SHOW_HIDDEN_FILES | NTFS_READ_ONLY | NTFS_RECOVER); } if (!ret) { return -5; } } fs_ntfs_mount = 1; fs_ntfs_sec = sector; //_FAT_startSector; return 0; }
int ntfsMountAll (ntfs_md **mounts, u32 flags) { const INTERFACE_ID *discs = ntfsGetDiscInterfaces(); const INTERFACE_ID *disc = NULL; ntfs_md mount_points[NTFS_MAX_MOUNTS]; sec_t *partitions = NULL; int mount_count = 0; int partition_count = 0; char name[128]; int i, j, k; // Initialise ntfs-3g ntfsInit(); // Find and mount all NTFS partitions on all known devices for (i = 0; discs[i].name != NULL && discs[i].interface != NULL; i++) { disc = &discs[i]; partition_count = ntfsFindPartitions(disc->interface, &partitions); if (partition_count > 0 && partitions) { for (j = 0, k = 0; j < partition_count; j++) { // Find the next unused mount name do { sprintf(name, "%s%i", NTFS_MOUNT_PREFIX, k++); if (k >= NTFS_MAX_MOUNTS) { ntfs_free(partitions); errno = EADDRNOTAVAIL; return -1; } } while (ntfsGetDevice(name, false)); // Mount the partition if (mount_count < NTFS_MAX_MOUNTS) { if (ntfsMount(name, disc->interface, partitions[j], CACHE_DEFAULT_PAGE_SIZE, CACHE_DEFAULT_PAGE_COUNT, flags)) { strcpy(mount_points[mount_count].name, name); mount_points[mount_count].interface = disc->interface; mount_points[mount_count].startSector = partitions[j]; mount_count++; } } } ntfs_free(partitions); } } // Return the mounts (if any) if (mount_count > 0 && mounts) { *mounts = (ntfs_md*)ntfs_alloc(sizeof(ntfs_md) * mount_count); if (*mounts) { memcpy(*mounts, &mount_points, sizeof(ntfs_md) * mount_count); return mount_count; } } return 0; }
bool PartitionHandle::Mount(int pos, const char * name) { if(!valid(pos)) return false; if(!name) return false; UnMount(pos); if(pos >= (int) MountNameList.size()) MountNameList.resize(GetPartitionCount()); MountNameList[pos] = name; if(strncmp(GetFSName(pos), "FAT", 3) == 0 || strcmp(GetFSName(pos), "GUID-Entry") == 0) { if (fatMount(MountNameList[pos].c_str(), interface, GetLBAStart(pos), CACHE, SECTORS)) { if(strcmp(GetFSName(pos), "GUID-Entry") == 0) PartitionList[pos].FSName = "FAT"; return true; } } if(strncmp(GetFSName(pos), "NTFS", 4) == 0 || strcmp(GetFSName(pos), "GUID-Entry") == 0) { if(ntfsMount(MountNameList[pos].c_str(), interface, GetLBAStart(pos), CACHE, SECTORS, NTFS_SHOW_HIDDEN_FILES | NTFS_RECOVER)) { PartitionList[pos].FSName = "NTFS"; return true; } } if(strncmp(GetFSName(pos), "LINUX", 5) == 0 || strcmp(GetFSName(pos), "GUID-Entry") == 0) { if(ext2Mount(MountNameList[pos].c_str(), interface, GetLBAStart(pos), CACHE, SECTORS, EXT2_FLAG_DEFAULT)) { PartitionList[pos].FSName = "LINUX"; return true; } } MountNameList[pos].clear(); return false; }
static void AddPartition(sec_t sector, int device, int type, int *devnum) { int i; if (*devnum >= MAX_DEVICES) return; for (i = 0; i < *devnum; i++) if (part[device][i].sector == sector) return; // to avoid mount same partition again DISC_INTERFACE *disc = (DISC_INTERFACE *) & xenon_ata_ops; if (device == DEVICE_USB) disc = (DISC_INTERFACE *) & usb2mass_ops; else if(device == DEVICE_ATAPI) disc = (DISC_INTERFACE *) & xenon_atapi_ops; char mount[10]; sprintf(mount, "%s%i", prefix[device], *devnum); char *name; switch (type) { case T_FAT: if (!fatMount(mount, disc, sector, 2, 64)) return; fatGetVolumeLabel(mount, part[device][*devnum].name); break; case T_NTFS: if (!ntfsMount(mount, disc, sector, 2, 64, NTFS_DEFAULT | NTFS_RECOVER)) return; name = (char *) ntfsGetVolumeName(mount); if (name && name[0]) strcpy(part[device][*devnum].name, name); else part[device][*devnum].name[0] = 0; break; case T_EXT2: if (!ext2Mount(mount, disc, sector, 2, 128, EXT2_FLAG_DEFAULT)) return; name = (char *) ext2GetVolumeName(mount); if (name && name[0]) strcpy(part[device][*devnum].name, name); else part[device][*devnum].name[0] = 0; break; case T_ISO9660: if (!ISO9660_Mount(mount, disc)) return; name = (char *) ISO9660_GetVolumeLabel(mount); if (name && name[0]) strcpy(part[device][*devnum].name, name); else strcpy(part[device][*devnum].name, "DVD"); break; } int c = strlen(part[device][*devnum].name) - 1; while (c >= 0 && part[device][*devnum].name[c] == ' ') part[device][*devnum].name[c--] = 0; strcpy(part[device][*devnum].mount, mount); part[device][*devnum].interface = disc; part[device][*devnum].sector = sector; part[device][*devnum].type = type; ++*devnum; }
int main(void) { int ret; int k; int fd; char temp[128]; ntfs_md *mounts; ret = cellSysmoduleLoadModule(CELL_SYSMODULE_FS); if (ret != CELL_OK) return ret; ret = cellFsOpen("/dev_hdd0/libntfs_sample_log.txt", CELL_FS_O_RDWR|CELL_FS_O_CREAT, &LOG, NULL, 0); if(ret) return ret; log_printf("*** LOG ***\n"); log_printf("\n*** PS3_NTFS_IsInserted ***\n\n"); for(k = 0; k < 8; k++) { if(PS3_NTFS_IsInserted(k)) log_printf("- PS3_NTFS_IsInserted(%d) = true\n", k); else log_printf("- PS3_NTFS_IsInserted(%d) = false\n", k); } log_printf("\n*** ntfsFindPartitions ***\n"); sec_t *partitions = NULL; int partition_number = ntfsFindPartitions(disc_ntfs[0], &partitions); log_printf("- ntfsFindPartitions = %d\n", partition_number); log_printf("\n*** ntfsMount ***\n"); if(ntfsMount("ntfs0", disc_ntfs[0], partitions[0], CACHE_DEFAULT_PAGE_COUNT, CACHE_DEFAULT_PAGE_SIZE, NTFS_DEFAULT | NTFS_RECOVER)) log_printf("- ntfsMount = true\n"); else log_printf("- ntfsMount = false\n"); if(partitions) free(partitions); log_printf("\n*** ntfsUnmount ***\n"); ntfsUnmount("ntfs0", 1); log_printf("\n*** ntfsMountDevice ***\n"); ret = ntfsMountDevice(disc_ntfs[0], &mounts, NTFS_DEFAULT | NTFS_RECOVER); log_printf("- ntfsMountDevice = %d\n", ret); log_printf("- mount->name = %s\n", mounts->name); log_printf("\n*** ntfsUnmount ***\n"); ntfsUnmount(mounts->name, 1); log_printf("\n*** ntfsMountAll ***\n"); int mountCount = ntfsMountAll(&mounts, NTFS_DEFAULT | NTFS_RECOVER ); log_printf("- ntfsMountAll = %d\n", mountCount); log_printf("- mount[0].name = %s\n", mounts[0].name); log_printf("\n*** ntfsGetVolumeName ***\n"); const char *OldName = ntfsGetVolumeName(mounts[0].name); if(OldName) log_printf("- Old name : '%s'\n", OldName); else log_printf("- Error %d\n", ps3ntfs_errno()); /* need to re-mount the device after SetVolumeName to 'update' the value of GetVolumeName log_printf("\n*** ntfsSetVolumeName ***\n"); if(ntfsSetVolumeName(mounts[0].name, "NTFS_VOLUME")) log_printf("- ntfsSetVolumeName = true\n"); else log_printf("- ntfsSetVolumeName = false - %d \n", ps3ntfs_errno()); const char *NewName = ntfsGetVolumeName(mounts[0].name); if(NewName) log_printf("- New name : '%s'\n", NewName); else log_printf("- Error %d\n", ps3ntfs_errno()); */ log_printf("\n*** ps3ntfs_mkdir ***\n"); sprintf(temp, "%s:/viper6", mounts[0].name); if(ps3ntfs_mkdir(temp, 0777) == 0) log_printf("- ps3ntfs_mkdir = true\n"); else log_printf("- ps3ntfs_mkdir = false\n"); log_printf("\n*** ps3ntfs_open ***\n"); strcat(temp, (char*) "/ntfs.txt"); for(k=0; k<5000; k++) { // force fd = ps3ntfs_open(temp, O_CREAT | O_WRONLY | O_TRUNC, 0777); if(fd>0) break; } if(fd > 0) { log_printf("- ps3ntfs_open = success\n"); log_printf("\n*** ps3ntfs_write ***\n"); ret = ps3ntfs_write(fd, message, strlen(message)); log_printf("- ps3ntfs_write = %d\n", ret); if(ret != (int) strlen(message)) log_printf("- Error writing the file!\n"); log_printf("\n*** ps3ntfs_close ***\n"); ret = ps3ntfs_close(fd); log_printf("- ps3ntfs_close = %d\n", ret); } else log_printf("- ps3ntfs_open = failed - %s\n", temp); struct stat st; log_printf("\n*** ps3ntfs_stat ***\n"); ret = ps3ntfs_stat(temp, &st); log_printf("- ps3ntfs_stat = %d\n", ret); log_printf("- SIZE = %d\n", st.st_size); log_printf("- last_access_time = %d\n", st.st_atime); log_printf("- last_mft_change_time = %d\n", st.st_ctime); log_printf("- last_data_change_time = %d\n", st.st_mtime); log_printf("- st_dev (id) = %d\n", st.st_dev); log_printf("- st uid = %d\n", st.st_uid); log_printf("- st gid = %d\n", st.st_gid); log_printf("- st ino = %d\n", st.st_ino); log_printf("\n*** ps3ntfs_open ***\n"); for(k=0; k<5000; k++) { // force fd = ps3ntfs_open(temp, O_RDONLY, 0); if(fd > 0) break; } log_printf("- ps3ntfs_open = %d\n", fd); if(fd > 0) { log_printf("- ps3ntfs_open = success\n"); log_printf("\n*** ps3ntfs_fstat ***\n"); ret = ps3ntfs_fstat(fd, &st); log_printf("- ps3ntfs_fstat = %d\n", ret); log_printf("- SIZE = %d\n", st.st_size); log_printf("- last_access_time = %d\n", st.st_atime); log_printf("- last_mft_change_time = %d\n", st.st_ctime); log_printf("- last_data_change_time = %d\n", st.st_mtime); log_printf("- st_dev (id) = %d\n", st.st_dev); log_printf("- st uid = %d\n", st.st_uid); log_printf("- st gid = %d\n", st.st_gid); log_printf("- st ino = %d\n", st.st_ino); log_printf("\n*** ps3ntfs_seek ***\n"); int size = ps3ntfs_seek(fd, 0, SEEK_END); log_printf("- ps3ntfs_seek - size = %d\n", size); ps3ntfs_seek(fd, 0, SEEK_SET); log_printf("\n*** ps3ntfs_read ***\n"); ret = ps3ntfs_read(fd, buffer, size); log_printf("- ps3ntfs_read : '%s'\n", buffer); if(ret != size) log_printf("Error reading the file!\n"); log_printf("\n*** ps3ntfs_close ***\n"); ret = ps3ntfs_close(fd); log_printf("- ps3ntfs_close = %d\n", ret); } else log_printf("- ps3ntfs_open = failed - %s\n", temp); sprintf(buffer, "%s:/viper6/ntfs_newname.txt", mounts[0].name); log_printf("\n*** ps3ntfs_rename ***\n"); for(k=0; k<5000; k++) { // force ret = ps3ntfs_rename(temp, buffer); if(ret==0) break; } if(ret==0) log_printf("- ps3ntfs_rename = %d\n", ret); else log_printf("- ps3ntfs_rename = %d - %s > %s\n", ret, temp, buffer); DIR_ITER *pdir; char filename[255]; log_printf("\n*** ps3ntfs_diropen ***\n"); sprintf(temp, "%s:/viper6", mounts[0].name); for(k=0; k<5000; k++) { // force pdir = ps3ntfs_diropen(temp); if (pdir) break; } if (pdir) { log_printf("- ps3ntfs_diropen = success\n"); log_printf("\n*** ps3ntfs_dirnext ***\n"); while (ps3ntfs_dirnext(pdir, filename, &st) == 0) { if ((strcmp(filename, ".") == 0) || (strcmp(filename, "..") == 0)) continue; log_printf("- ps3ntfs_dirnext = File : %s/\n", filename); log_printf("- last_access_time = %d\n", st.st_atime); log_printf("- last_mft_change_time = %d\n", st.st_ctime); log_printf("- last_data_change_time = %d\n", st.st_mtime); } log_printf("\n*** ps3ntfs_dirreset ***\n"); ret = ps3ntfs_dirreset(pdir); log_printf("- ps3ntfs_dirreset = %d\n", ret); log_printf("\n*** ps3ntfs_dirclose ***\n"); ret = ps3ntfs_dirclose(pdir); log_printf("- ps3ntfs_dirclose = %d\n", ret); } else log_printf("- ps3ntfs_diropen = failed\n"); log_printf("\n*** ps3ntfs_open ***\n"); strcat(temp, (char*)"/unlink.txt"); for(k=0; k<5000; k++) { // force fd = ps3ntfs_open(temp, O_CREAT | O_WRONLY | O_TRUNC, 0777); if(fd > 0) break; } if(fd > 0) { log_printf("- ps3ntfs_open = success\n"); log_printf("\n*** ps3ntfs_close ***\n"); ret = ps3ntfs_close(fd); log_printf("- ps3ntfs_close = %d\n", ret); } else log_printf("- ps3ntfs_open = failed\n"); log_printf("\n*** ps3ntfs_unlink ***\n"); for(k=0; k<5000; k++) { // force ret = ps3ntfs_unlink(temp); if(ret==0) break; } log_printf("- ps3ntfs_unlink = %d\n", ret); log_printf("\n*** ntfsUnmount ***\n"); for (k = 0; k < mountCount; k++) ntfsUnmount(mounts[k].name, 1); log_printf("\n*** PS3_NTFS_Shutdown ***\n\n"); for(k = 0; k < 8; k++) { if(PS3_NTFS_Shutdown(k)) log_printf("- PS3_NTFS_Shutdown(%d) = true\n", k); else log_printf("- PS3_NTFS_Shutdown(%d) = false\n", k); } /* // Debugging for time related functions in ntfstime.h struct timespec { time_t tv_sec; long tv_nsec; } ; struct timespec now; typedef uint64_t u64; typedef u64 sle64; typedef sle64 ntfs_time; //sys_time_sec_t time_s; //sys_time_nsec_t time_n_s; //sys_time_get_current_time(&time_s, &time_n_s); //now.tv_sec = time_s; //now.tv_nsec = time_n_s; now.tv_sec = time((time_t*)NULL); now.tv_nsec = 0; log_printf("- test now.tv_sec = %d\n", now.tv_sec); log_printf("- test now.tv_nsec = %d\n", now.tv_nsec); ntfs_time ntfstime; struct timespec unixtime; #define NTFS_TIME_OFFSET ((s64)(369 * 365 + 89) * 24 * 3600 * 10000000) s64 units; units = (s64)now.tv_sec * 10000000 + NTFS_TIME_OFFSET + now.tv_nsec/100; ntfstime = (cpu_to_sle64(units)); log_printf("- timespec2ntfs = %d\n", ntfstime); struct timespec spec; s64 cputime; cputime = sle64_to_cpu(ntfstime); spec.tv_sec = (cputime - (NTFS_TIME_OFFSET)) / 10000000; spec.tv_nsec = (cputime - (NTFS_TIME_OFFSET) - (s64)spec.tv_sec*10000000)*100; //force zero nsec for overflowing dates if ((spec.tv_nsec < 0) || (spec.tv_nsec > 999999999)) spec.tv_nsec = 0; unixtime = spec; log_printf("- ntfs2timespec = %d\n", unixtime); */ // End debugging /* TODO int ps3ntfs_file_to_sectors(const char *path, uint32_t *sec_out, uint32_t *size_out, int max, int phys); int ps3ntfs_get_fd_from_FILE(FILE *fp); s64 ps3ntfs_seek64(int fd, s64 pos, int dir); int ps3ntfs_link(const char *existing, const char *newLink); int ps3ntfs_statvfs(const char *path, struct statvfs *buf); int ps3ntfs_ftruncate(int fd, off_t len); int ps3ntfs_fsync(int fd); void NTFS_init_system_io(void); void NTFS_deinit_system_io(void); Standard functions supported: open_r -> for stdio.h fopen()... close_r -> for stdio.h fclose()... read_r -> for stdio.h fread()... write_r -> for stdio.h fwrite()... lseek_r -> for stdio.h fseek()... lseek64_r -> for using with large files (see ps3_example_stdio for this) fstat_r -> for stat.h fstat() stat_r -> for stat.h stat() ftruncate_r -> for unistd.h ftruncate() truncate_r -> for unistd.h truncate() fsync_r -> for stdio.h fflush() link_r -> for unistd.h link() unlink_r -> for unistd.h unlink() rename_r -> for stdio.h rename() mkdir_r -> for stat.h mkdir() rmdir_r -> for unistd.h rmdir() */ cellFsClose(LOG); return 0; }
bool PartitionHandle::Mount(int pos, const char *name, bool forceFAT) { if(valid(pos)) UnMount(pos); if(!name) return false; if(pos >= (int)MountNameList.size()) MountNameList.resize(pos + 1); MountNameList[pos] = name; char DeviceSyn[10]; memcpy(DeviceSyn, name, 8); strcat(DeviceSyn, ":"); DeviceSyn[9] = '\0'; //! Some stupid partition manager think they don't need to edit the freaken MBR. //! So we need to check the first 64 sectors and see if some partition is there. //! libfat does that by default so let's use it. if(forceFAT && (strlen(GetFSName(pos)) == 0 || strcmp(GetFSName(pos), "Unknown") == 0)) { if(fatMount(MountNameList[pos].c_str(), interface, 0, CACHE, SECTORS)) { sec_t FAT_startSector = FindFirstValidPartition(interface); AddPartition("FAT", FAT_startSector, 0xdeadbeaf, true, 0x0c, 0); gprintf("FAT Partition at %s (forceFAT) mounted.\n", DeviceSyn); SetWbfsHandle(pos, NULL); return true; } } if(!valid(pos)) return false; SetWbfsHandle(pos, NULL); if(strncmp(GetFSName(pos), "FAT", 3) == 0 || strcmp(GetFSName(pos), "GUID-Entry") == 0) { if(fatMount(MountNameList[pos].c_str(), interface, GetLBAStart(pos), CACHE, SECTORS)) { gprintf("FAT Partition at %s mounted.\n", DeviceSyn); PartitionList[pos].FSName = "FAT"; return true; } } else if(strncmp(GetFSName(pos), "NTFS", 4) == 0 || strcmp(GetFSName(pos), "GUID-Entry") == 0) { if(ntfsMount(MountNameList[pos].c_str(), interface, GetLBAStart(pos), CACHE, SECTORS, NTFS_SHOW_HIDDEN_FILES | NTFS_RECOVER)) { gprintf("NTFS Partition at %s mounted.\n", DeviceSyn); PartitionList[pos].FSName = "NTFS"; return true; } } else if(strncmp(GetFSName(pos), "LINUX", 5) == 0 || strcmp(GetFSName(pos), "GUID-Entry") == 0) { if(ext2Mount(MountNameList[pos].c_str(), interface, GetLBAStart(pos), CACHE, SECTORS, EXT2_FLAG_DEFAULT)) { gprintf("EXT Partition at %s mounted.\n", DeviceSyn); PartitionList[pos].FSName = "LINUX"; return true; } } else if(strncmp(GetFSName(pos), "WBFS", 4) == 0) { if(interface == &__io_usbstorage2_port0 || interface == &__io_usbstorage2_port1) SetWbfsHandle(pos, wbfs_open_partition(__WBFS_ReadUSB, __WBFS_WriteUSB, NULL, USBStorage2_GetSectorSize(), GetSecCount(pos), GetLBAStart(pos), 0)); else if(interface == &__io_sdhc) SetWbfsHandle(pos, wbfs_open_partition(__WBFS_ReadSDHC, __WBFS_WriteSDHC, NULL, 512, GetSecCount(pos), GetLBAStart(pos), 0)); if(GetWbfsHandle(pos)) { gprintf("WBFS Partition at %s mounted.\n", DeviceSyn); PartitionList[pos].FSName = "WBFS"; return true; } } /* FAIL */ MountNameList[pos].clear(); return false; }