[PATCH 07/11] shell32: _SHCreateSymbolicLinks() join User Profile directory creation code blocks, into a single unified loop

Rob Walker bob.mt.wya at gmail.com
Mon Jul 16 17:47:01 CDT 2018


Move creation of the 5 User Profile directories:

  '%USERPROFILE%\\My Documents'
  '%USERPROFILE%\\My Music'
  '%USERPROFILE%\\My Pictures'
  '%USERPROFILE%\\My Videos'
  '%USERPROFILE%\\Desktop'

into a single unified loop.
This improves code readability and reduces code length / complexity.
Make the arrays, of encoded directory designators, use a consistent ordering.
Reword inline code comments as necessary to be consistent with new code structure.

Signed-off-by: Rob Walker <bob.mt.wya at gmail.com>
---
 dlls/shell32/shellpath.c | 109 +++++++--------------------------------
 1 file changed, 18 insertions(+), 91 deletions(-)

diff --git a/dlls/shell32/shellpath.c b/dlls/shell32/shellpath.c
index faa8c892e9..ce8af7760c 100644
--- a/dlls/shell32/shellpath.c
+++ b/dlls/shell32/shellpath.c
@@ -4409,27 +4409,20 @@ static inline BOOL _SHAppendToUnixPath(char *szBasePath, LPCWSTR pwszSubPath) {
  */
 static void _SHCreateSymbolicLinks(void)
 {
-    UINT aidsMyStuff[] = { IDS_MYPICTURES, IDS_MYVIDEOS, IDS_MYMUSIC }, i;
-    const WCHAR* MyOSXStuffW[] = { PicturesW, MoviesW, MusicW };
-    int acsidlMyStuff[] = { CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_MYMUSIC };
-    static const char * const xdg_dirs[] = { "PICTURES", "VIDEOS", "MUSIC", "DOCUMENTS", "DESKTOP" };
+    UINT aidsMyStuff[] = { IDS_PERSONAL, IDS_MYMUSIC, IDS_MYPICTURES, IDS_MYVIDEOS, IDS_DESKTOPDIRECTORY };
+    const WCHAR* MyOSXStuffW[] = { DocumentsW, MusicW, PicturesW, MoviesW, DesktopW };
+    int acsidlMyStuff[] = { CSIDL_PERSONAL, CSIDL_MYMUSIC, CSIDL_MYPICTURES, CSIDL_MYVIDEO, CSIDL_DESKTOPDIRECTORY };
+    static const char * const xdg_dirs[] = { "DOCUMENTS", "MUSIC", "PICTURES", "VIDEOS", "DESKTOP" };
     static const unsigned int num = ARRAY_SIZE(xdg_dirs);
     WCHAR wszTempPath[MAX_PATH];
-    char szPersonalTarget[FILENAME_MAX], *pszPersonal;
     char szMyStuffTarget[FILENAME_MAX], *pszMyStuff;
-    char szDesktopTarget[FILENAME_MAX], *pszDesktop;
     struct stat statFolder;
     const char *pszHome;
     HRESULT hr;
     BOOL target_ok;
     char ** xdg_results;
-
-    /* Create the '%USERPROFILE%\\My Documents' directory path. */
-    hr = SHGetFolderPathW(NULL, CSIDL_PERSONAL|CSIDL_FLAG_CREATE, NULL,
-                          SHGFP_TYPE_DEFAULT, wszTempPath);
-    if (FAILED(hr)) return;
-    pszPersonal = wine_get_unix_file_name(wszTempPath);
-    if (!pszPersonal) return;
+    int ret;
+    UINT i;
 
     hr = XDG_UserDirLookup(xdg_dirs, num, &xdg_results);
     if (FAILED(hr)) xdg_results = NULL;
@@ -4438,45 +4431,9 @@ static void _SHCreateSymbolicLinks(void)
     if (!(pszHome && !stat(pszHome, &statFolder) && S_ISDIR(statFolder.st_mode)))
         pszHome = NULL;
 
-    if (pszHome)
+    for (i = 0; i < num; ++i)
     {
-        target_ok = FALSE;
-
-        /* Try to target a pre-existing '$HOME/My Documents' folder. */
-        strcpy(szPersonalTarget, pszHome);
-        if (_SHAppendToUnixPath(szPersonalTarget, MAKEINTRESOURCEW(IDS_PERSONAL)) &&
-            !stat(szPersonalTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
-        {
-            target_ok = TRUE;
-        }
-        /* Try to target the XDG_DOCUMENTS_DIR folder. */
-        else if (xdg_results && xdg_results[num-2])
-        {
-            strcpy(szPersonalTarget, xdg_results[num-2]);
-            target_ok = TRUE;
-        }
-        /* Try to target the hardcoded / OS X 'Documents' folder. */
-        else
-        {
-            strcpy(szPersonalTarget, pszHome);
-            target_ok = _SHAppendToUnixPath(szPersonalTarget, DocumentsW)
-                                            && !stat(szPersonalTarget, &statFolder)
-                                            && S_ISDIR(statFolder.st_mode);
-        }
-
-        if (target_ok)
-        {
-            remove(pszPersonal);
-            symlink(szPersonalTarget, pszPersonal);
-        }
-
-        heap_free(pszPersonal);
-    }
-
-    /* Create symbolic links for 'My Pictures', 'My Videos' and 'My Music'. */
-    for (i=0; i < ARRAY_SIZE(aidsMyStuff); i++)
-    {
-        /* Create the '%USERPROFILE%\\My XXX' directory path. */
+        /* Create the '%USERPROFILE%\\XXX' directory path. */
         hr = SHGetFolderPathW(NULL, acsidlMyStuff[i]|CSIDL_FLAG_CREATE, NULL,
                               SHGFP_TYPE_DEFAULT, wszTempPath);
         if (FAILED(hr) || !pszHome) continue;
@@ -4485,10 +4442,10 @@ static void _SHCreateSymbolicLinks(void)
         if (!pszMyStuff)  continue;
 
         target_ok = FALSE;
-
-        /* Try to target a pre-existing '$HOME/My Documents/My XXX' folder. */
+        /* Try to target a pre-existing '$HOME/My XXX' folder. */
         strcpy(szMyStuffTarget, pszHome);
-        if (_SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
+        if ((aidsMyStuff[i] != IDS_DESKTOPDIRECTORY) &&
+            _SHAppendToUnixPath(szMyStuffTarget, MAKEINTRESOURCEW(aidsMyStuff[i])) &&
             !stat(szMyStuffTarget, &statFolder) && S_ISDIR(statFolder.st_mode))
         {
             target_ok = TRUE;
@@ -4510,47 +4467,17 @@ static void _SHCreateSymbolicLinks(void)
 
         if (target_ok)
         {
-            remove(pszMyStuff);
-            symlink(szMyStuffTarget, pszMyStuff);
+            ret = remove(pszMyStuff);
+            TRACE("remove directory (%s): %s\n",
+                  (ret ? "soft failed" : "OK"), debugstr_a(pszMyStuff));
+            ret = symlink(szMyStuffTarget, pszMyStuff);
+            TRACE("symlink directory (%s): %s -> %s\n",
+                  (ret ? "soft failed" : "OK"),
+                  debugstr_a(pszMyStuff), debugstr_a(szMyStuffTarget));
         }
         heap_free(pszMyStuff);
     }
 
-    /* Get the '%USERPROFILE%\\Desktop' directory path. */
-    hr = SHGetFolderPathW(NULL, CSIDL_DESKTOPDIRECTORY|CSIDL_FLAG_CREATE, NULL,
-                              SHGFP_TYPE_DEFAULT, wszTempPath);
-    if (SUCCEEDED(hr) && pszHome)
-    {
-         /* Create a symbolic link for the 'Desktop' folder. */
-        pszDesktop = wine_get_unix_file_name(wszTempPath);
-        if (pszDesktop)
-        {
-            strcpy(szDesktopTarget, pszHome);
-            i = num - 1;
-            /* Try to target the XDG_DESKTOP_DIR folder. */
-            if (xdg_results && xdg_results[i])
-            {
-                strcpy(szDesktopTarget, xdg_results[i]);
-                target_ok = TRUE;
-            }
-            /* Try to target the hardcoded / OS X 'Desktop' folder. */
-            else
-            {
-                strcpy(szDesktopTarget, pszHome);
-                target_ok = _SHAppendToUnixPath(szDesktopTarget, DesktopW)
-                            && !stat(szDesktopTarget, &statFolder)
-                            && S_ISDIR(statFolder.st_mode);
-            }
-
-            if (target_ok)
-            {
-                remove(pszDesktop);
-                symlink(szDesktopTarget, pszDesktop);
-            }
-            heap_free(pszDesktop);
-        }
-    }
-
     if (xdg_results)
     {
         for (i = 0; i < num; i++)
-- 
2.18.0




More information about the wine-devel mailing list