Repository: conan-io/conan-center-index

Issue:
[package] msys2/20200517:  Failed to build msys2 20200517 on x86 arch
I need to build `msys2` package on a x86 architecture but the process is failing.

It seems that it cannot found `libgdbm-1.18.1-2-i686.pkg.tar.xz`, `gdbm-1.18.1-2-i686.pkg.tar.xz` and `libiconv-devel-1.16-1-i686.pkg.tar.xz` packages when installing `msys2`.
When looking at https://repo.msys2.org/msys/i686, the above packages are not present indeed.

### Package and Environment Details
  * Package Name/Version: **msys2/20200517**
  * Operating System+version: **Windows 10 18363**
  * Compiler+version: **MSVC 1826**
  * Conan version: **conan 1.31.4**
  * Python version: **Python 3.6.1**

### Conan profile
```
[settings]
os=Windows
os_build=Windows
arch=x86
arch_build=x86
compiler=Visual Studio
compiler.version=16
build_type=Debug
[options]
[build_requires]
[env]
```

### Error logs:
```
error : failed to retrieve « libgdbm-1.18.1-2-i686.pkg.tar.xz » from repo.msys2.org : The requested URL returned error: 404
...
error : failed to retrieve « gdbm-1.18.1-2-i686.pkg.tar.xz » from repo.msys2.org : The requested URL returned error: 404
...
error : failed to retrieve « libiconv-devel-1.16-1-i686.pkg.tar.xz » from repo.msys2.org : The requested URL returned error: 404
...
msys2/20200517:
msys2/20200517: ERROR: Package '02335dedca0a2cdcde89828f81194929ef12d610' build failed
msys2/20200517: WARN: Build folder C:\.conan\0f056c\1
ERROR: msys2/20200517: Error in build() method, line 60
        self.run('bash -l -c "pacman -S %s --noconfirm"' % package)
        ConanException: Error 1 while executing bash -l -c "pacman -S base-devel --noconfirm"
```

### Full logs:
```
###################################################################
#                                                                 #
#                                                                 #
#                   C   A   U   T   I   O   N                     #
#                                                                 #
#                  This is first start of MSYS2.                  #
#       You MUST restart shell to apply necessary actions.        #
#                                                                 #
#                                                                 #
###################################################################


:: There are 58 members in base-devel group :
:: msys repository
   1) asciidoc  2) autoconf  3) autoconf2.13  4) autogen  5) automake-wrapper  6) automake1.10  7) automake1.11
   8) automake1.12  9) automake1.13  10) automake1.14  11) automake1.15  12) automake1.16  13) automake1.6
   14) automake1.7  15) automake1.8  16) automake1.9  17) bison  18) btyacc  19) diffstat  20) diffutils  21) dos2unix
   22) file  23) flex  24) gawk  25) gdb  26) gettext  27) gettext-devel  28) gperf  29) grep  30) groff  31) help2man
   32) intltool  33) lemon  34) libtool  35) libunrar  36) libunrar-devel  37) m4  38) make  39) man-db  40) pacman
   41) pactoys-git  42) patch  43) patchutils  44) perl  45) pkg-config  46) pkgfile  47) quilt  48) rcs  49) reflex
   50) scons  51) sed  52) swig  53) texinfo  54) texinfo-tex  55) ttyrec  56) unrar  57) wget  58) xmlto

Enter a selection (everything is selectioned by default):
warning : file-5.38-3 up to date -- réinstallation
warning : flex-2.6.4-1 up to date -- réinstallation
warning : gawk-5.1.0-1 up to date -- réinstallation
warning : gettext-0.19.8.1-1 up to date -- réinstallation
warning : grep-3.0-2 up to date -- réinstallation
warning : m4-1.4.18-2 up to date -- réinstallation
warning : pacman-5.2.1-6 up to date-- réinstallation
warning : pactoys-git-r2.07ca37f-1 up to date -- réinstallation
warning : pkgfile-21-1 up to date -- réinstallation
warning : sed-4.8-1 up to date -- réinstallation
warning : ttyrec-1.0.8-2 up to date -- réinstallation
warning : wget-1.20.3-1 up to date -- réinstallation
dependencies resolving…
searching conflicts between packages…

Packages (79) db-5.3.28-2  docbook-xml-4.5-2  docbook-xsl-1.79.2-1  expat-2.2.9-1  gdbm-1.18.1-2  libatomic_ops-7.6.10-1
             libgc-7.6.8-1  libgdbm-1.18.1-2  libguile-2.2.7-1  libiconv-devel-1.16-1  libltdl-2.4.6-9
             libpipeline-1.5.2-1  mpdecimal-2.4.2-2  perl-Locale-Gettext-1.07-4  perl-Module-Build-0.4231-1
             perl-Test-Pod-1.52-1  perl-XML-Parser-2.46-1  perl-YAML-Syck-1.32-1  perl-inc-latest-0.500-1
             python-3.8.2-1  tar-1.32-1  asciidoc-9.0.0rc2-1  autoconf-2.69-5  autoconf2.13-2.13-2  autogen-5.18.16-1
             automake-wrapper-11-1  automake1.10-1.10.3-3  automake1.11-1.11.6-3  automake1.12-1.12.6-3
             automake1.13-1.13.4-4  automake1.14-1.14.1-3  automake1.15-1.15.1-1  automake1.16-1.16.1-1
             automake1.6-1.6.3-2  automake1.7-1.7.9-2  automake1.8-1.8.5-3  automake1.9-1.9.6-2  bison-3.5.4-1
             btyacc-20191125-1  diffstat-1.63-1  diffutils-3.7-1  dos2unix-7.4.1-1  file-5.38-3  flex-2.6.4-1
             gawk-5.1.0-1  gdb-9.1-1  gettext-0.19.8.1-1  gettext-devel-0.19.8.1-1  gperf-3.1-1  grep-3.0-2
             groff-1.22.4-1  help2man-1.47.13-1  intltool-0.51.0-2  lemon-3.31.1-1  libtool-2.4.6-9  libunrar-5.9.2-1
             libunrar-devel-5.9.2-1  m4-1.4.18-2  make-4.3-1  man-db-2.9.1-1  pacman-5.2.1-6  pactoys-git-r2.07ca37f-1
             patch-2.7.6-1  patchutils-0.3.4-1  perl-5.30.2-1  pkg-config-0.29.2-1  pkgfile-21-1  quilt-0.66-2
             rcs-5.9.4-2  reflex-20191123-1  scons-3.1.2-4  sed-4.8-1  swig-4.0.1-1  texinfo-6.7-1  texinfo-tex-6.7-1
             ttyrec-1.0.8-2  unrar-5.9.2-1  wget-1.20.3-1  xmlto-0.0.28-2

Download size :   69,24 MiB
Installed size :        305,32 MiB
True update size :    240,76 MiB

:: Procceed to installation ? [O/n]
:: Fetching packages…
 mpdecimal-2.4.2-2-i686                       77,0 KiB  1166 KiB/s 00:00 [#######################################] 100%
 gettext-0.19.8.1-1-i686                    1539,7 KiB  1143 KiB/s 00:01 [#######################################] 100%
 python-3.8.2-1-i686                           9,7 MiB  2,24 MiB/s 00:04 [#######################################] 100%
 docbook-xml-4.5-2-any                        81,4 KiB  3,61 MiB/s 00:00 [#######################################] 100%
 docbook-xsl-1.79.2-1-any                    738,4 KiB  3,55 MiB/s 00:00 [#######################################] 100%
 asciidoc-9.0.0rc2-1-any                     179,7 KiB  3,82 MiB/s 00:00 [#######################################] 100%
 gawk-5.1.0-1-i686                          1051,0 KiB  3,25 MiB/s 00:00 [#######################################] 100%
 m4-1.4.18-2-i686                            210,0 KiB  4,18 MiB/s 00:00 [#######################################] 100%
 diffutils-3.7-1-i686                        333,9 KiB  2,09 MiB/s 00:00 [#######################################] 100%
 db-5.3.28-2-i686                             39,2 KiB  38,3 MiB/s 00:00 [#######################################] 100%
error : failed to retrieve « libgdbm-1.18.1-2-i686.pkg.tar.xz » from repo.msys2.org : The requested URL returned error: 404
error : failed to retrieve « libgdbm-1.18.1-2-i686.pkg.tar.xz » from sourceforge.net : Maximum file size exceeded
error : failed to retrieve « libgdbm-1.18.1-2-i686.pkg.tar.xz » from www2.futureware.at : The requested URL returned error: 404
error : failed to retrieve « libgdbm-1.18.1-2-i686.pkg.tar.xz » from mirror.yandex.ru : The requested URL returned error: 404
error : failed to retrieve « libgdbm-1.18.1-2-i686.pkg.tar.xz » from mirrors.tuna.tsinghua.edu.cn : The requested URL returned error: 404
error : failed to retrieve « libgdbm-1.18.1-2-i686.pkg.tar.xz » from mirrors.ustc.edu.cn : The requested URL returned error: 404
error : failed to retrieve « libgdbm-1.18.1-2-i686.pkg.tar.xz » from mirror.bit.edu.cn : The requested URL returned error: 404
error : failed to retrieve « libgdbm-1.18.1-2-i686.pkg.tar.xz » from mirror.selfnet.de : The requested URL returned error: 404
warning : error while retrieving some files
error : failed to retrieve « gdbm-1.18.1-2-i686.pkg.tar.xz » from repo.msys2.org : The requested URL returned error: 404
error : failed to retrieve « gdbm-1.18.1-2-i686.pkg.tar.xz » from sourceforge.net : The requested URL returned error: 404
error : failed to retrieve « gdbm-1.18.1-2-i686.pkg.tar.xz » from www2.futureware.at : The requested URL returned error: 404
error : failed to retrieve « gdbm-1.18.1-2-i686.pkg.tar.xz » from mirror.yandex.ru : The requested URL returned error: 404
error : failed to retrieve « gdbm-1.18.1-2-i686.pkg.tar.xz » from mirrors.tuna.tsinghua.edu.cn : The requested URL returned error: 404
error : failed to retrieve « gdbm-1.18.1-2-i686.pkg.tar.xz » from mirrors.ustc.edu.cn : The requested URL returned error: 404
error : failed to retrieve « gdbm-1.18.1-2-i686.pkg.tar.xz » from mirror.bit.edu.cn : The requested URL returned error: 404
error : failed to retrieve « gdbm-1.18.1-2-i686.pkg.tar.xz » from mirror.selfnet.de : The requested URL returned error: 404
warning : error while retrieving some files
 perl-5.30.2-1-i686                           13,5 MiB  2,65 MiB/s 00:05 [#######################################] 100%
 autoconf-2.69-5-any                         597,2 KiB  4,14 MiB/s 00:00 [#######################################] 100%
 autoconf2.13-2.13-2-any                     134,8 KiB  5,49 MiB/s 00:00 [#######################################] 100%
 libatomic_ops-7.6.10-1-any                   17,6 KiB  0,00   B/s 00:00 [#######################################] 100%
 libgc-7.6.8-1-i686                          167,3 KiB  6,53 MiB/s 00:00 [#######################################] 100%
 libltdl-2.4.6-9-i686                         33,7 KiB  32,9 MiB/s 00:00 [#######################################] 100%
 libguile-2.2.7-1-i686                         4,8 MiB  4,20 MiB/s 00:01 [#######################################] 100%
 autogen-5.18.16-1-i686                      646,7 KiB  4,33 MiB/s 00:00 [#######################################] 100%
 automake1.6-1.6.3-2-any                     253,7 KiB  5,27 MiB/s 00:00 [#######################################] 100%
 automake1.7-1.7.9-2-any                     296,1 KiB  5,90 MiB/s 00:00 [#######################################] 100%
 automake1.8-1.8.5-3-any                     343,0 KiB  4,85 MiB/s 00:00 [#######################################] 100%
 automake1.9-1.9.6-2-any                     385,6 KiB  4,14 MiB/s 00:00 [#######################################] 100%
 automake1.10-1.10.3-3-any                   439,6 KiB  4,62 MiB/s 00:00 [#######################################] 100%
 automake1.11-1.11.6-3-any                   489,6 KiB  5,14 MiB/s 00:00 [#######################################] 100%
 automake1.12-1.12.6-3-any                   502,3 KiB  5,22 MiB/s 00:00 [#######################################] 100%
 automake1.13-1.13.4-4-any                   500,1 KiB  5,25 MiB/s 00:00 [#######################################] 100%
 automake1.14-1.14.1-3-any                   502,3 KiB  4,27 MiB/s 00:00 [#######################################] 100%
 automake1.15-1.15.1-1-any                   521,0 KiB  5,36 MiB/s 00:00 [#######################################] 100%
 automake1.16-1.16.1-1-any                   523,4 KiB  5,50 MiB/s 00:00 [#######################################] 100%
 automake-wrapper-11-1-any                    12,3 KiB  12,0 MiB/s 00:00 [#######################################] 100%
 bison-3.5.4-1-i686                          623,3 KiB  5,20 MiB/s 00:00 [#######################################] 100%
 btyacc-20191125-1-i686                       60,8 KiB  59,4 MiB/s 00:00 [#######################################] 100%
 diffstat-1.63-1-i686                         29,6 KiB  28,9 MiB/s 00:00 [#######################################] 100%
 dos2unix-7.4.1-1-i686                       332,1 KiB  6,24 MiB/s 00:00 [#######################################] 100%
 file-5.38-3-i686                            552,9 KiB  2025 KiB/s 00:00 [#######################################] 100%
 flex-2.6.4-1-i686                           279,0 KiB  1965 KiB/s 00:00 [#######################################] 100%
 expat-2.2.9-1-i686                           34,8 KiB  34,0 MiB/s 00:00 [#######################################] 100%
 gdb-9.1-1-i686                                3,5 MiB  2,27 MiB/s 00:02 [#######################################] 100%
error : failed to retrieve « libiconv-devel-1.16-1-i686.pkg.tar.xz » from repo.msys2.org : The requested URL returned error: 404
error : failed to retrieve « libiconv-devel-1.16-1-i686.pkg.tar.xz » from sourceforge.net : The requested URL returned error: 404
error : failed to retrieve « libiconv-devel-1.16-1-i686.pkg.tar.xz » from www2.futureware.at : The requested URL returned error: 404
error : failed to retrieve « libiconv-devel-1.16-1-i686.pkg.tar.xz » from mirror.yandex.ru : The requested URL returned error: 404
error : failed to retrieve « libiconv-devel-1.16-1-i686.pkg.tar.xz » from mirrors.tuna.tsinghua.edu.cn : The requested URL returned error: 404
error : failed to retrieve « libiconv-devel-1.16-1-i686.pkg.tar.xz » from mirrors.ustc.edu.cn : The requested URL returned error: 404
error : failed to retrieve « libiconv-devel-1.16-1-i686.pkg.tar.xz » from mirror.bit.edu.cn : The requested URL returned error: 404
error : failed to retrieve « libiconv-devel-1.16-1-i686.pkg.tar.xz » from mirror.selfnet.de : The requested URL returned error: 404
avertissement : error while retrieving some files
 gettext-devel-0.19.8.1-1-i686              1325,9 KiB  4,64 MiB/s 00:00 [#######################################] 100%
 gperf-3.1-1-i686                            102,1 KiB  49,9 MiB/s 00:00 [#######################################] 100%
 grep-3.0-2-i686                             239,7 KiB  39,0 MiB/s 00:00 [#######################################] 100%
 groff-1.22.4-1-i686                        1911,7 KiB  10,5 MiB/s 00:00 [#######################################] 100%
 perl-Locale-Gettext-1.07-4-i686              22,3 KiB  21,7 MiB/s 00:00 [#######################################] 100%
 help2man-1.47.13-1-i686                     170,1 KiB  33,2 MiB/s 00:00 [#######################################] 100%
 perl-XML-Parser-2.46-1-i686                 155,4 KiB  37,9 MiB/s 00:00 [#######################################] 100%
 intltool-0.51.0-2-i686                       39,4 KiB  0,00   B/s 00:00 [#######################################] 100%
 lemon-3.31.1-1-i686                          44,8 KiB  43,8 MiB/s 00:00 [#######################################] 100%
 tar-1.32-1-i686                             752,7 KiB  9,19 MiB/s 00:00 [#######################################] 100%
 libtool-2.4.6-9-i686                        385,0 KiB  8,17 MiB/s 00:00 [#######################################] 100%
 libunrar-5.9.2-1-i686                       117,7 KiB  38,3 MiB/s 00:00 [#######################################] 100%
 libunrar-devel-5.9.2-1-i686                 147,9 KiB  36,1 MiB/s 00:00 [#######################################] 100%
 make-4.3-1-i686                             440,6 KiB  9,35 MiB/s 00:00 [#######################################] 100%
 libpipeline-1.5.2-1-i686                     31,2 KiB  30,5 MiB/s 00:00 [#######################################] 100%
 man-db-2.9.1-1-i686                        1039,5 KiB  7,87 MiB/s 00:00 [#######################################] 100%
 sed-4.8-1-i686                              220,1 KiB  9,77 MiB/s 00:00 [#######################################] 100%
 pacman-5.2.1-6-i686                          12,6 MiB  9,91 MiB/s 00:01 [#######################################] 100%
 pkgfile-21-1-i686                            33,8 KiB  33,0 MiB/s 00:00 [#######################################] 100%
 wget-1.20.3-1-i686                          671,7 KiB  12,6 MiB/s 00:00 [#######################################] 100%
 pactoys-git-r2.07ca37f-1-i686                30,4 KiB  29,7 MiB/s 00:00 [#######################################] 100%
 patch-2.7.6-1-i686                          105,2 KiB  34,3 MiB/s 00:00 [#######################################] 100%
 patchutils-0.3.4-1-i686                      72,7 KiB  35,5 MiB/s 00:00 [#######################################] 100%
 pkg-config-0.29.2-1-i686                    180,7 KiB  7,35 MiB/s 00:00 [#######################################] 100%
 quilt-0.66-2-any                            511,2 KiB  9,79 MiB/s 00:00 [#######################################] 100%
 rcs-5.9.4-2-i686                            169,6 KiB  3,60 MiB/s 00:00 [#######################################] 100%
 reflex-20191123-1-i686                       99,3 KiB  4,21 MiB/s 00:00 [#######################################] 100%
 scons-3.1.2-4-any                           796,6 KiB  3,12 MiB/s 00:00 [#######################################] 100%
 swig-4.0.1-1-i686                           995,5 KiB  3,25 MiB/s 00:00 [#######################################] 100%
 texinfo-6.7-1-i686                         1084,8 KiB  3,30 MiB/s 00:00 [#######################################] 100%
 texinfo-tex-6.7-1-i686                       29,8 KiB  5,83 MiB/s 00:00 [#######################################] 100%
 ttyrec-1.0.8-2-i686                          19,5 KiB  19,0 MiB/s 00:00 [#######################################] 100%
 unrar-5.9.2-1-i686                          123,1 KiB  5,23 MiB/s 00:00 [#######################################] 100%
 perl-YAML-Syck-1.32-1-i686                   69,4 KiB  33,9 MiB/s 00:00 [#######################################] 100%
 perl-inc-latest-0.500-1-any                  19,9 KiB  19,4 MiB/s 00:00 [#######################################] 100%
 perl-Module-Build-0.4231-1-any              179,1 KiB  3,80 MiB/s 00:00 [#######################################] 100%
 perl-Test-Pod-1.52-1-any                     17,3 KiB  16,9 MiB/s 00:00 [#######################################] 100%
 xmlto-0.0.28-2-i686                          34,5 KiB  33,7 MiB/s 00:00 [#######################################] 100%
error : la validation de la transaction a échoué (erreur non prévue)
Errors occurred, no package has been updated.
msys2/20200517:
msys2/20200517: ERROR: Package '02335dedca0a2cdcde89828f81194929ef12d610' build failed
msys2/20200517: WARN: Build folder C:\.conan\0f056c\1
ERROR: msys2/20200517: Error in build() method, line 60
        self.run('bash -l -c "pacman -S %s --noconfirm"' % package)
        ConanException: Error 1 while executing bash -l -c "pacman -S base-devel --noconfirm"
```


Discussion:
Please use the issue template for package bugs next time. `conan.io/center` is the label for the website
is it french? it would be helpful if you can provide a translated summary of errors
> is it french? it would be helpful if you can provide a translated summary of errors

I translated important error messages above but I can translate more if you want
there are `gdbm-1.18.1-3-i686.pkg.tar.zst` and `libiconv-1.16-2-i686.pkg.tar.zst` on the repo:
https://repo.msys2.org/msys/i686/
perhaps, it needs to update an index first?
I mean, if we run `pacman -Syyu` just before https://github.com/conan-io/conan-center-index/blob/689e2663fe41689259f61a5dbd3fe245e24c03b9/recipes/msys2/all/conanfile.py#L60
> perhaps, it needs to update an index first?

I think so, we can give it a try

Resolving patch:
diff --git a/recipes/msys2/all/conanfile.py b/recipes/msys2/all/conanfile.py
--- a/recipes/msys2/all/conanfile.py
+++ b/recipes/msys2/all/conanfile.py
@@ -1,7 +1,38 @@
 from conans import ConanFile, tools
-from conans.errors import ConanInvalidConfiguration
+from conans.errors import ConanInvalidConfiguration, ConanException
+import fnmatch
 import os
 import shutil
+import subprocess
+
+try:
+    import ctypes
+    from ctypes import wintypes
+except ImportError:
+    pass
+except ValueError:
+    pass
+
+class lock:
+    def __init__(self):
+        self.handle = ctypes.windll.kernel32.CreateMutexA(None, 0, "Global\\ConanMSYS2".encode())
+        if not self.handle:
+            raise ctypes.WinError()
+
+    def __enter__(self):
+        status = ctypes.windll.kernel32.WaitForSingleObject(self.handle, 0xFFFFFFFF)
+        if status not in [0, 0x80]:
+            raise ctypes.WinError()
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        status = ctypes.windll.kernel32.ReleaseMutex(self.handle)
+        if not status:
+            raise ctypes.WinError()
+
+    def close(self):
+        ctypes.windll.kernel32.CloseHandle(self.handle)
+
+    __del__ = close
 
 
 class MSYS2Conan(ConanFile):
@@ -11,24 +42,28 @@ class MSYS2Conan(ConanFile):
     homepage = "http://www.msys2.org"
     license = "MSYS license"
     topics = ("conan", "msys", "unix", "subsystem")
-    build_requires = "7zip/19.00"
     short_paths = True
-    options = {"exclude_files": "ANY",  # Comma separated list of file patterns to exclude from the package
-               "packages": "ANY",  # Comma separated
-               "additional_packages": "ANY"}    # Comma separated
-    default_options = {"exclude_files": "*/link.exe",
-                       "packages": "base-devel,binutils,gcc",
-                       "additional_packages": None}
-    settings = "os_build", "arch_build"
-
-    def configure(self):
-        if self.settings.os_build != "Windows":
+    # "exclude_files" "packages" "additional_packages" values are a comma separated list
+    options = {
+        "exclude_files": "ANY",
+        "packages": "ANY",
+        "additional_packages": "ANY"
+    }
+    default_options = {
+        "exclude_files": "*/link.exe",
+        "packages": "base-devel,binutils,gcc",
+        "additional_packages": None
+    }
+    settings = "os", "arch"
+
+
+    def validate(self):
+        if self.settings.os != "Windows":
             raise ConanInvalidConfiguration("Only Windows supported")
-
-    def source(self):
-        # build tools have to download files in build method when the
-        # source files downloaded will be different based on architecture or OS
-        pass
+        if tools.Version(self.version) >= "20210105" and self.settings.arch != "x86_64":
+            raise ConanInvalidConfiguration("Only Windows x64 supported")
+        if tools.Version(self.version) <= "20161025":
+            raise ConanInvalidConfiguration("msys2 v.20161025 is no longer supported")    
 
     def _download(self, url, sha256):
         from six.moves.urllib.parse import urlparse
@@ -36,29 +71,146 @@ def _download(self, url, sha256):
         tools.download(url=url, filename=filename, sha256=sha256)
         return filename
 
+    @property
+    def _keyring_subfolder(self):
+        return os.path.join(self.package_folder, "bin", "keyring")
+
+    @property
+    def _keyring_file(self):
+        return os.path.join(self._keyring_subfolder, "msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz")
+    
+    @property
+    def _keyring_sig(self):
+        return os.path.join(self._keyring_subfolder, "msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz.sig")    
+
+    # download the new keyring
+    # https://www.msys2.org/news/#2020-06-29-new-packagers
+    def _download_keyring(self):
+        tools.download( "http://repo.msys2.org/msys/x86_64/msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz",
+                        self._keyring_file,
+                        sha256="f1cc152902fd6018868b64d015cab9bf547ff9789d8bd7c0d798fb2b22367b2b" )
+        tools.download( "http://repo.msys2.org/msys/x86_64/msys2-keyring-r21.b39fb11-1-any.pkg.tar.xz.sig",
+                        self._keyring_sig,
+                        sha256="bbd22e88f33c81c40b145c34d8027d60f714d4fd1d0dccd456895f398cc56297" )
+
+    def _install_pacman_keyring(self):
+        with tools.chdir(os.path.join(self._msys_dir, "usr", "bin")):
+            self.run('bash -l -c  "pacman-key --init;pacman-key --populate"')
+            verify_command = 'bash -l -c "pacman-key --verify %s"' % self._keyring_sig.replace("\\", "/")
+            self.run(verify_command)
+
+            install_command = 'bash -l -c "pacman -U %s --noconfirm"' % self._keyring_file.replace("\\", "/")
+            self.run(install_command)
+            self._kill_pacman()
+
+    def _update_very_old_pacman(self):
+        if (tools.Version(self.version) < "20200517"):
+            # Fix (https://github.com/msys2/MSYS2-packages/issues/1967)
+            # install zstd and pacman manually
+            pacman_packages_x64=["http://repo.msys2.org/msys/x86_64/libzstd-1.4.4-2-x86_64.pkg.tar.xz",
+                                 "http://repo.msys2.org/msys/x86_64/zstd-1.4.4-2-x86_64.pkg.tar.xz",
+                                 "http://repo.msys2.org/msys/x86_64/pacman-5.2.1-6-x86_64.pkg.tar.xz"]
+            
+            if (self.settings.arch == "x86_64"):
+                for i in pacman_packages_x64:
+                    self.run('bash -l -c "pacman --noconfirm -U %s"' % i)
+
+            pacman_packages_x32=["http://repo.msys2.org/msys/i686/libzstd-1.4.4-2-i686.pkg.tar.xz",
+                                 "http://repo.msys2.org/msys/i686/zstd-1.4.4-2-i686.pkg.tar.xz",
+                                 "http://repo.msys2.org/msys/i686/pacman-5.2.1-6-i686.pkg.tar.xz"]
+
+            if (self.settings.arch == "x86"):                     
+                for i in pacman_packages_x32:
+                    self.run('bash -l -c "pacman --noconfirm -U %s"' % i)
+
+            self._kill_pacman()
+
+
+    def _update_pacman(self):
+        with tools.chdir(os.path.join(self._msys_dir, "usr", "bin")):
+            try:
+                self._update_very_old_pacman()
+
+                self._kill_pacman()
+                # https://www.msys2.org/news/   see  2020-05-31 - Update may fail with "could not open file"
+                # update pacman separately first
+                self.run('bash -l -c "pacman --debug --noconfirm -Sydd pacman"')
+
+                # https://www.msys2.org/docs/ci/
+                self.run('bash -l -c "pacman --debug --noconfirm --ask 20 -Syuu"')  # Core update (in case any core packages are outdated)
+                self._kill_pacman()
+                self.run('bash -l -c "pacman --debug --noconfirm --ask 20 -Syuu"')  # Normal update
+                self._kill_pacman()
+                self.run('bash -l -c "pacman --debug -Rc dash --noconfirm"')
+            except ConanException:
+                self.run('bash -l -c "cat /var/log/pacman.log || echo nolog"')
+                raise
+
+    # https://github.com/msys2/MSYS2-packages/issues/1966
+    def _kill_pacman(self):
+        if (self.settings.os == "Windows"):
+            taskkill_exe = os.path.join(os.environ.get('SystemRoot'), 'system32', 'taskkill.exe')
+
+            log_out = True
+            if log_out:
+                out = subprocess.PIPE
+                err = subprocess.STDOUT
+            else:
+                out = file(os.devnull, 'w')
+                err = subprocess.PIPE
+
+            if os.path.exists(taskkill_exe):
+                taskkill_cmds = [taskkill_exe + " /f /t /im pacman.exe",
+                                 taskkill_exe + " /f /im gpg-agent.exe",
+                                 taskkill_exe + " /f /im dirmngr.exe",
+                                 taskkill_exe + ' /fi "MODULES eq msys-2.0.dll"']
+                for taskkill_cmd in taskkill_cmds:
+                    try:
+                        proc = subprocess.Popen(taskkill_cmd, stdout=out, stderr=err, bufsize=1)
+                        proc.wait()
+                    except OSError as e:
+                        if e.errno == errno.ENOENT:
+                            raise ConanException("Cannot kill pacman")
+
     @property
     def _msys_dir(self):
-        return "msys64" if self.settings.arch_build == "x86_64" else "msys32"
+        subdir = "msys64" if (tools.Version(self.version) >= "20210105" or self.settings.arch == "x86_64") else "msys32"
+        return os.path.join(self.package_folder, "bin", subdir)
+
+    def source(self):
+        # sources are different per configuration - do download in build
+        pass
+
+    def _do_source(self):
+        filename = self._download(**self.conan_data["sources"][self.version][str(self.settings.arch)])
+        tools.unzip(filename)
+        self._download_keyring()
 
     def build(self):
-        arch = 0 if self.settings.arch_build == "x86" else 1  # index in the sources list
-        filename = self._download(**self.conan_data["sources"][self.version][arch])
-        tar_name = filename.replace(".xz", "")
-        self.run("7z.exe x {0}".format(filename))
-        self.run("7z.exe x {0}".format(tar_name))
-        os.unlink(filename)
-        os.unlink(tar_name)
+        os.makedirs(os.path.join(self.package_folder, "bin"))
+        with tools.chdir(os.path.join(self.package_folder, "bin")):
+            self._do_source()
+        with lock():
+            self._do_build()
 
+    def _do_build(self):
         packages = []
         if self.options.packages:
             packages.extend(str(self.options.packages).split(","))
         if self.options.additional_packages:
             packages.extend(str(self.options.additional_packages).split(","))
 
+        if (tools.Version(self.version) < "20210105"):
+            self._install_pacman_keyring()
+
+        self._update_pacman()
+
         with tools.chdir(os.path.join(self._msys_dir, "usr", "bin")):
             for package in packages:
                 self.run('bash -l -c "pacman -S %s --noconfirm"' % package)
 
+        self._kill_pacman()
+
         # create /tmp dir in order to avoid
         # bash.exe: warning: could not find /tmp, please create!
         tmp_dir = os.path.join(self._msys_dir, 'tmp')
@@ -76,12 +228,18 @@ def package(self):
         excludes = None
         if self.options.exclude_files:
             excludes = tuple(str(self.options.exclude_files).split(","))
-        self.copy("*", dst="bin", src=self._msys_dir, excludes=excludes)
-        shutil.copytree(os.path.join(self.package_folder, "bin", "usr", "share", "licenses"),
+        #self.copy("*", dst="bin", src=self._msys_dir, excludes=excludes)
+        for exclude in excludes:
+            for root, _, filenames in os.walk(self._msys_dir):
+                for filename in filenames:
+                    fullname = os.path.join(root, filename)
+                    if fnmatch.fnmatch(fullname, exclude):
+                        os.unlink(fullname)
+        shutil.copytree(os.path.join(self._msys_dir, "usr", "share", "licenses"),
                         os.path.join(self.package_folder, "licenses"))
 
     def package_info(self):
-        msys_root = os.path.join(self.package_folder, "bin")
+        msys_root = self._msys_dir
         msys_bin = os.path.join(msys_root, "usr", "bin")
 
         self.output.info("Creating MSYS_ROOT env var : %s" % msys_root)


Test patch:
diff --git a/recipes/msys2/all/test_package/conanfile.py b/recipes/msys2/all/test_package/conanfile.py
--- a/recipes/msys2/all/test_package/conanfile.py
+++ b/recipes/msys2/all/test_package/conanfile.py
@@ -3,7 +3,10 @@
 from io import StringIO
 
 class TestPackage(ConanFile):
-        
+    
+    def build(self):
+        pass # nothing to do, skip hook warning
+
     def test(self):
         bash = tools.which("bash.exe")