MCPcopy Create free account
hub / github.com/devkitPro/libctru / archive_rename

Function archive_rename

libctru/source/archive_dev.c:925–988  ·  view source on GitHub ↗

! Rename a file * * @param[in,out] r newlib reentrancy struct * @param[in] oldName Path to rename from * @param[in] newName Path to rename to * * @returns 0 for success * @returns -1 for error */

Source from the content-addressed store, hash-verified

923 * @returns -1 for error
924 */
925static int
926archive_rename(struct _reent *r,
927 const char *oldName,
928 const char *newName)
929{
930 Result rc;
931 FS_Path fs_path_old, fs_path_new;
932 archive_fsdevice *sourceDevice = r->deviceData;
933 archive_fsdevice *destDevice = NULL;
934
935 /* treat extdata as read-only */
936 if(sourceDevice->is_extdata)
937 {
938 r->_errno = ENOTSUP;
939 return -1;
940 }
941
942 fs_path_old = archive_utf16path(r, oldName, &sourceDevice);
943 if(fs_path_old.data == NULL)
944 return -1;
945
946 uint8_t old_path_copy[fs_path_old.size];
947 memcpy(old_path_copy, fs_path_old.data, fs_path_old.size);
948 fs_path_old.data = old_path_copy;
949
950 fs_path_new = archive_utf16path(r, newName, &destDevice);
951 if(fs_path_new.data == NULL)
952 return -1;
953
954 /* moving between archives is not supported */
955 if(sourceDevice->archive != destDevice->archive)
956 {
957 r->_errno = ENOTSUP;
958 return -1;
959 }
960
961 rc = FSUSER_RenameFile(sourceDevice->archive, fs_path_old, sourceDevice->archive, fs_path_new);
962 /* if the file at the target destination exists, overwrite it */
963 if(R_FAILED(rc) && R_DESCRIPTION(rc) == RD_ALREADY_EXISTS) {
964 rc = FSUSER_DeleteFile(sourceDevice->archive, fs_path_new);
965 if(R_FAILED(rc)) {
966 r->_errno = archive_translate_error(rc);
967 return -1;
968 }
969 rc = FSUSER_RenameFile(sourceDevice->archive, fs_path_old, sourceDevice->archive, fs_path_new);
970 if(R_SUCCEEDED(rc)) return 0;
971 } else if(R_SUCCEEDED(rc)) return 0;
972
973 rc = FSUSER_RenameDirectory(sourceDevice->archive, fs_path_old, sourceDevice->archive, fs_path_new);
974 /* if the directory at the target destination exists, overwrite it */
975 if(R_FAILED(rc) && R_DESCRIPTION(rc) == RD_ALREADY_EXISTS) {
976 /* only overwrite empty directories */
977 rc = FSUSER_DeleteDirectory(sourceDevice-> archive, fs_path_new);
978 if(R_FAILED(rc)) {
979 r->_errno = archive_translate_error(rc);
980 return -1;
981 }
982 rc = FSUSER_RenameDirectory(sourceDevice->archive, fs_path_old, sourceDevice->archive, fs_path_new);

Callers

nothing calls this directly

Calls 6

archive_utf16pathFunction · 0.85
FSUSER_RenameFileFunction · 0.85
FSUSER_DeleteFileFunction · 0.85
archive_translate_errorFunction · 0.85
FSUSER_RenameDirectoryFunction · 0.85
FSUSER_DeleteDirectoryFunction · 0.85

Tested by

no test coverage detected