[Bf-blender-cvs] [32d4623f446] master: Cleanup: alias: use const, remove unused variable.

Ankit Meel noreply at git.blender.org
Fri Oct 9 19:18:13 CEST 2020


Commit: 32d4623f4467df230c08d24916759ac4996dfb52
Author: Ankit Meel
Date:   Fri Oct 9 14:39:26 2020 +0530
Branches: master
https://developer.blender.org/rB32d4623f4467df230c08d24916759ac4996dfb52

Cleanup: alias: use const, remove unused variable.

`targetIsDirectory` slipped through the code review of
{D6679}/{rBafb1a64ccb81}.
`BLI_is_dir` exists to check for directory status of a file.

Remove some `else-after-return`s.

Use `r_` prefix for return value arguments, and move it to the end
in the list of arguments.

===================================================================

M	source/blender/blenlib/BLI_fileops.h
M	source/blender/blenlib/intern/storage.c
M	source/blender/blenlib/intern/storage_apple.mm
M	source/blender/editors/space_file/filelist.c

===================================================================

diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h
index 922039e8862..74a491898b6 100644
--- a/source/blender/blenlib/BLI_fileops.h
+++ b/source/blender/blenlib/BLI_fileops.h
@@ -146,7 +146,7 @@ int BLI_access(const char *filename, int mode) ATTR_WARN_UNUSED_RESULT ATTR_NONN
 
 bool BLI_file_is_writable(const char *file) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
 bool BLI_file_touch(const char *file) ATTR_NONNULL();
-bool BLI_file_alias_target(char *target, const char *filepath);
+bool BLI_file_alias_target(const char *filepath, char *r_targetpath) ATTR_WARN_UNUSED_RESULT;
 
 #if 0 /* UNUSED */
 int BLI_file_gzip(const char *from, const char *to) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c
index a841068bfdb..628bdc1a31f 100644
--- a/source/blender/blenlib/intern/storage.c
+++ b/source/blender/blenlib/intern/storage.c
@@ -288,11 +288,11 @@ eFileAttributes BLI_file_attributes(const char *path)
 
 /* Return alias/shortcut file target. Apple version is defined in storage_apple.mm */
 #ifndef __APPLE__
-bool BLI_file_alias_target(
-    /* This parameter can only be const on non-windows platforms.
-     * NOLINTNEXTLINE: readability-non-const-parameter. */
-    char target[FILE_MAXDIR],
-    const char *filepath)
+bool BLI_file_alias_target(const char *filepath,
+                           /* This parameter can only be `const` on Linux since
+                            * redirections are not supported there.
+                            * NOLINTNEXTLINE: readability-non-const-parameter. */
+                           char r_targetpath[FILE_MAXDIR])
 {
 #  ifdef WIN32
   if (!BLI_path_extension_check(filepath, ".lnk")) {
@@ -318,7 +318,7 @@ bool BLI_file_alias_target(
             wchar_t target_utf16[FILE_MAXDIR] = {0};
             hr = Shortcut->lpVtbl->GetPath(Shortcut, target_utf16, FILE_MAXDIR, NULL, 0);
             if (SUCCEEDED(hr)) {
-              success = (conv_utf_16_to_8(target_utf16, target, FILE_MAXDIR) == 0);
+              success = (conv_utf_16_to_8(target_utf16, r_targetpath, FILE_MAXDIR) == 0);
             }
           }
           PersistFile->lpVtbl->Release(PersistFile);
@@ -328,9 +328,9 @@ bool BLI_file_alias_target(
     Shortcut->lpVtbl->Release(Shortcut);
   }
 
-  return (success && target[0]);
+  return (success && r_targetpath[0]);
 #  else
-  UNUSED_VARS(target, filepath);
+  UNUSED_VARS(r_targetpath, filepath);
   /* File-based redirection not supported. */
   return false;
 #  endif
diff --git a/source/blender/blenlib/intern/storage_apple.mm b/source/blender/blenlib/intern/storage_apple.mm
index 08d2cfdf4a4..16a2fd338fa 100644
--- a/source/blender/blenlib/intern/storage_apple.mm
+++ b/source/blender/blenlib/intern/storage_apple.mm
@@ -28,7 +28,11 @@
 #include "BLI_fileops.h"
 #include "BLI_path_util.h"
 
-bool BLI_file_alias_target(char targetpath[FILE_MAXDIR], const char *filepath)
+/**
+ * \param r_targetpath Buffer for the target path an alias points to.
+ * \return Whether the file at the input path is an alias.
+ */
+bool BLI_file_alias_target(const char *filepath, char r_targetpath[FILE_MAXDIR])
 {
   /* clang-format off */
   @autoreleasepool {
@@ -37,26 +41,24 @@ bool BLI_file_alias_target(char targetpath[FILE_MAXDIR], const char *filepath)
     NSURL *shortcutURL = [[NSURL alloc] initFileURLWithFileSystemRepresentation:filepath
                                                                     isDirectory:NO
                                                                   relativeToURL:nil];
-    NSURL *targetURL = [NSURL URLByResolvingAliasFileAtURL:shortcutURL
-                                                   options:NSURLBookmarkResolutionWithoutUI
-                                                     error:&error];
-    BOOL isSame = [shortcutURL isEqual:targetURL] and
-                  ([[[shortcutURL path] stringByStandardizingPath]
-                      isEqualToString:[[targetURL path] stringByStandardizingPath]]);
+    const NSURL *targetURL = [NSURL URLByResolvingAliasFileAtURL:shortcutURL
+                                                         options:NSURLBookmarkResolutionWithoutUI
+                                                           error:&error];
+    const BOOL isSame = [shortcutURL isEqual:targetURL] and
+                        ([[[shortcutURL path] stringByStandardizingPath]
+                            isEqualToString:[[targetURL path] stringByStandardizingPath]]);
 
     if (targetURL == nil) {
       return false;
     }
-    else if (isSame) {
-      [targetURL getFileSystemRepresentation:targetpath maxLength:FILE_MAXDIR];
+    if (isSame) {
+      [targetURL getFileSystemRepresentation:r_targetpath maxLength:FILE_MAXDIR];
       return false;
     }
-    else if (![targetURL getFileSystemRepresentation:targetpath maxLength:FILE_MAXDIR]) {
+    /* Note that the if-condition may also change the value of `r_targetpath`. */
+    if (![targetURL getFileSystemRepresentation:r_targetpath maxLength:FILE_MAXDIR]) {
       return false;
     }
-
-    NSNumber *targetIsDirectory = 0;
-    [targetURL getResourceValue:&targetIsDirectory forKey:NSURLIsDirectoryKey error:nil];
   }
 
   return true;
@@ -69,13 +71,13 @@ eFileAttributes BLI_file_attributes(const char *path)
   /* clang-format off */
   @autoreleasepool {
     /* clang-format on */
-    NSURL *fileURL = [[NSURL alloc] initFileURLWithFileSystemRepresentation:path
-                                                                isDirectory:NO
-                                                              relativeToURL:nil];
+    const NSURL *fileURL = [[NSURL alloc] initFileURLWithFileSystemRepresentation:path
+                                                                      isDirectory:NO
+                                                                    relativeToURL:nil];
     NSArray *resourceKeys =
         @[ NSURLIsAliasFileKey, NSURLIsHiddenKey, NSURLIsReadableKey, NSURLIsWritableKey ];
 
-    NSDictionary *resourceKeyValues = [fileURL resourceValuesForKeys:resourceKeys error:nil];
+    const NSDictionary *resourceKeyValues = [fileURL resourceValuesForKeys:resourceKeys error:nil];
 
     const bool is_alias = [resourceKeyValues[(void)(@"@%"), NSURLIsAliasFileKey] boolValue];
     const bool is_hidden = [resourceKeyValues[(void)(@"@%"), NSURLIsHiddenKey] boolValue];
diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c
index 48ee87662ea..8d0239a182d 100644
--- a/source/blender/editors/space_file/filelist.c
+++ b/source/blender/editors/space_file/filelist.c
@@ -2511,7 +2511,7 @@ static int filelist_readjob_list_dir(const char *root,
       /* Is this a file that points to another file? */
       if (entry->attributes & FILE_ATTR_ALIAS) {
         entry->redirection_path = MEM_callocN(FILE_MAXDIR, __func__);
-        if (BLI_file_alias_target(entry->redirection_path, full_path)) {
+        if (BLI_file_alias_target(full_path, entry->redirection_path)) {
           if (BLI_is_dir(entry->redirection_path)) {
             entry->typeflag = FILE_TYPE_DIR;
             BLI_path_slash_ensure(entry->redirection_path);



More information about the Bf-blender-cvs mailing list