Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/intern/fileops.c
| Show First 20 Lines • Show All 295 Lines • ▼ Show 20 Lines | int BLI_access(const char *filename, int mode) | ||||
| return uaccess(filename, mode); | return uaccess(filename, mode); | ||||
| } | } | ||||
| static bool delete_soft(const wchar_t *path_16, const char **error_message) | static bool delete_soft(const wchar_t *path_16, const char **error_message) | ||||
| { | { | ||||
| /* Deletes file or directory to recycling bin. The latter moves all contained files and | /* Deletes file or directory to recycling bin. The latter moves all contained files and | ||||
| * directories recursively to the recycling bin as well. */ | * directories recursively to the recycling bin as well. */ | ||||
| IFileOperation *pfo; | IFileOperation *pfo; | ||||
| IShellItem *pSI; | IShellItem *psi; | ||||
| bool error = false; | |||||
| bool com_initialized = false; | |||||
| bool pfo_created = false; | |||||
| bool psi_created = false; | |||||
| HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); | HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); | ||||
| if (FAILED(hr)) { | if (FAILED(hr)) { | ||||
| *error_message = "Failed to initialize COM"; | *error_message = "Failed to initialize COM"; | ||||
LazyDodo: can't you just return here and skip the `com_initialized` variable all together? | |||||
Done Inline ActionsThe else-case is for when the COM initialization is successful and only then should COM be uninitialized later on. rjg: The else-case is for when the COM initialization is successful and only then should COM be… | |||||
Done Inline ActionsI may have misunderstood your comment. Did you mean you want an early return in the if-case? rjg: I may have misunderstood your comment. Did you mean you want an early return in the if-case? | |||||
Not Done Inline Actionsyes, i mean, if the com init call failed, unlikely any of the other stuff is going to execute? so you could bail out early in that case i think? LazyDodo: yes, i mean, if the com init call failed, unlikely any of the other stuff is going to execute? | |||||
Done Inline ActionsYes, you are right. In this particular case we could do a return in the if case and eliminate the bool. Technically you can do an early return in every step, as each one depends on the successful execution of the previous step, but then you have to duplicate code for the release and uninitialization in later parts. rjg: Yes, you are right. In this particular case we could do a return in the if case and eliminate… | |||||
| goto error_1; | error = true; | ||||
| } | |||||
| else { | |||||
| /* This is also the case when COM was previously initialized and CoInitializeEx returns | |||||
| * S_FALSE, which is not an error. Both HRESULT values S_OK and S_FALSE indicate success. */ | |||||
| com_initialized = true; | |||||
| } | } | ||||
| if (!error) { | |||||
| hr = CoCreateInstance( | hr = CoCreateInstance( | ||||
| &CLSID_FileOperation, NULL, CLSCTX_ALL, &IID_IFileOperation, (void **)&pfo); | &CLSID_FileOperation, NULL, CLSCTX_ALL, &IID_IFileOperation, (void **)&pfo); | ||||
| if (FAILED(hr)) { | if (FAILED(hr)) { | ||||
| *error_message = "Failed to create FileOperation instance"; | *error_message = "Failed to create FileOperation instance"; | ||||
| goto error_2; | error = true; | ||||
| } | |||||
| else { | |||||
| pfo_created = true; | |||||
| } | |||||
| } | } | ||||
| if (!error) { | |||||
| /* Flags for deletion: | /* Flags for deletion: | ||||
| * FOF_ALLOWUNDO: Enables moving file to recycling bin. | * FOF_ALLOWUNDO: Enables moving file to recycling bin. | ||||
| * FOF_SILENT: Don't show progress dialog box. | * FOF_SILENT: Don't show progress dialog box. | ||||
| * FOF_WANTNUKEWARNING: Show dialog box if file can't be moved to recycling bin. */ | * FOF_WANTNUKEWARNING: Show dialog box if file can't be moved to recycling bin. */ | ||||
| hr = pfo->lpVtbl->SetOperationFlags(pfo, FOF_ALLOWUNDO | FOF_SILENT | FOF_WANTNUKEWARNING); | hr = pfo->lpVtbl->SetOperationFlags(pfo, FOF_ALLOWUNDO | FOF_SILENT | FOF_WANTNUKEWARNING); | ||||
| if (FAILED(hr)) { | if (FAILED(hr)) { | ||||
| *error_message = "Failed to set operation flags"; | *error_message = "Failed to set operation flags"; | ||||
| goto error_2; | error = true; | ||||
| } | |||||
| } | } | ||||
| hr = SHCreateItemFromParsingName(path_16, NULL, &IID_IShellItem, (void **)&pSI); | if (!error) { | ||||
| hr = SHCreateItemFromParsingName(path_16, NULL, &IID_IShellItem, (void **)&psi); | |||||
| if (FAILED(hr)) { | if (FAILED(hr)) { | ||||
| *error_message = "Failed to parse path"; | *error_message = "Failed to parse path"; | ||||
| goto error_2; | error = true; | ||||
| } | |||||
| else { | |||||
| psi_created = true; | |||||
| } | } | ||||
| } | |||||
| if (!error) { | |||||
| hr = pfo->lpVtbl->DeleteItem(pfo, psi, NULL); | |||||
| hr = pfo->lpVtbl->DeleteItem(pfo, pSI, NULL); | |||||
| if (FAILED(hr)) { | if (FAILED(hr)) { | ||||
| *error_message = "Failed to prepare delete operation"; | *error_message = "Failed to prepare delete operation"; | ||||
| goto error_2; | error = true; | ||||
| } | |||||
| } | } | ||||
| if (!error) { | |||||
| hr = pfo->lpVtbl->PerformOperations(pfo); | hr = pfo->lpVtbl->PerformOperations(pfo); | ||||
| if (FAILED(hr)) { | if (FAILED(hr)) { | ||||
| *error_message = "Failed to delete file or directory"; | *error_message = "Failed to delete file or directory"; | ||||
| error = true; | |||||
| } | |||||
| } | |||||
| if (psi_created) { | |||||
| psi->lpVtbl->Release(psi); | |||||
| } | } | ||||
| error_2: | if (pfo_created) { | ||||
| pfo->lpVtbl->Release(pfo); | pfo->lpVtbl->Release(pfo); | ||||
| CoUninitialize(); /* Has to be uninitialized when CoInitializeEx returns either S_OK or S_FALSE | } | ||||
| */ | |||||
| error_1: | if (com_initialized) { | ||||
| CoUninitialize(); | |||||
| } | |||||
| return FAILED(hr); | return FAILED(hr); | ||||
| } | } | ||||
| static bool delete_unique(const char *path, const bool dir) | static bool delete_unique(const char *path, const bool dir) | ||||
| { | { | ||||
| bool err; | bool err; | ||||
| UTF16_ENCODE(path); | UTF16_ENCODE(path); | ||||
| ▲ Show 20 Lines • Show All 918 Lines • Show Last 20 Lines | |||||
can't you just return here and skip the com_initialized variable all together?