! 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 */
| 923 | * @returns -1 for error |
| 924 | */ |
| 925 | static int |
| 926 | archive_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); |
nothing calls this directly
no test coverage detected