GitHub issue #5348: The data downloaded in the download folder of the cache does not respect `umask`
State: open | Created: 2022-12-09T15:46:27Z

### Describe the bug

For a project on a cluster we are several users to share the same cache for the datasets library. And we have a problem with the permissions on the data downloaded in the cache. 

Indeed, it seems that the data is downloaded by giving read and write permissions only to the user launching the command (and no permissions to the group). In our case, those permissions don't respect the `umask` of this user, which was `0007`.

Traceback:
```
Using custom data configuration default
Downloading and preparing dataset text_caps/default to /gpfswork/rech/cnw/commun/datasets/HuggingFaceM4___text_caps/default/1.0.0/2b9ad220cd90fcf2bfb454645bc54364711b83d6d39401ffdaf8cc40882e9141...
Downloading data files: 100%|████████████████████| 3/3 [00:00<00:00, 921.62it/s]
---------------------------------------------------------------------------
PermissionError                           Traceback (most recent call last)
Cell In [3], line 1
----> 1 ds = load_dataset(dataset_name)

File /gpfswork/rech/cnw/commun/conda/lucile-m4_3/lib/python3.8/site-packages/datasets/load.py:1746, in load_dataset(path, name, data_dir, data_files, split, cache_dir, features, download_config, download_mode, ignore_verifications, keep_in_memory, save_infos, revision, use_auth_token, task, streaming, **config_kwargs)
   1743 try_from_hf_gcs = path not in _PACKAGED_DATASETS_MODULES
   1745 # Download and prepare data
-> 1746 builder_instance.download_and_prepare(
   1747     download_config=download_config,
   1748     download_mode=download_mode,
   1749     ignore_verifications=ignore_verifications,
   1750     try_from_hf_gcs=try_from_hf_gcs,
   1751     use_auth_token=use_auth_token,
   1752 )
   1754 # Build dataset for splits
   1755 keep_in_memory = (
   1756     keep_in_memory if keep_in_memory is not None else is_small_dataset(builder_instance.info.dataset_size)
   1757 )

File /gpfswork/rech/cnw/commun/conda/lucile-m4_3/lib/python3.8/site-packages/datasets/builder.py:704, in DatasetBuilder.download_and_prepare(self, download_config, download_mode, ignore_verifications, try_from_hf_gcs, dl_manager, base_path, use_auth_token, **download_and_prepare_kwargs)
    702         logger.warning("HF google storage unreachable. Downloading and preparing it from source")
    703 if not downloaded_from_gcs:
--> 704     self._download_and_prepare(
    705         dl_manager=dl_manager, verify_infos=verify_infos, **download_and_prepare_kwargs
    706     )
    707 # Sync info
    708 self.info.dataset_size = sum(split.num_bytes for split in self.info.splits.values())

File /gpfswork/rech/cnw/commun/conda/lucile-m4_3/lib/python3.8/site-packages/datasets/builder.py:1227, in GeneratorBasedBuilder._download_and_prepare(self, dl_manager, verify_infos)
   1226 def _download_and_prepare(self, dl_manager, verify_infos):
-> 1227     super()._download_and_prepare(dl_manager, verify_infos, check_duplicate_keys=verify_infos)

File /gpfswork/rech/cnw/commun/conda/lucile-m4_3/lib/python3.8/site-packages/datasets/builder.py:771, in DatasetBuilder._download_and_prepare(self, dl_manager, verify_infos, **prepare_split_kwargs)
    769 split_dict = SplitDict(dataset_name=self.name)
    770 split_generators_kwargs = self._make_split_generators_kwargs(prepare_split_kwargs)
--> 771 split_generators = self._split_generators(dl_manager, **split_generators_kwargs)
    773 # Checksums verification
    774 if verify_infos and dl_manager.record_checksums:

File /gpfswork/rech/cnw/commun/modules/datasets_modules/datasets/HuggingFaceM4--TextCaps/2b9ad220cd90fcf2bfb454645bc54364711b83d6d39401ffdaf8cc40882e9141/TextCaps.py:125, in TextCapsDataset._split_generators(self, dl_manager)
    123 def _split_generators(self, dl_manager):
    124     # urls = _URLS[self.config.name] # TODO later
--> 125     data_dir = dl_manager.download_and_extract(_URLS)
    126     gen_kwargs = {
    127         split_name: {
    128             f"{dir_name}_path": Path(data_dir[dir_name][split_name])
   (...)
    133         for split_name in ["train", "val", "test"]
    134     }
    136     for split_name in ["train", "val", "test"]:

File /gpfswork/rech/cnw/commun/conda/lucile-m4_3/lib/python3.8/site-packages/datasets/download/download_manager.py:431, in DownloadManager.download_and_extract(self, url_or_urls)
    415 def download_and_extract(self, url_or_urls):
    416     """Download and extract given url_or_urls.
    417 
    418     Is roughly equivalent to:
   (...)
    429         extracted_path(s): `str`, extracted paths of given URL(s).
    430     """
--> 431     return self.extract(self.download(url_or_urls))

File /gpfswork/rech/cnw/commun/conda/lucile-m4_3/lib/python3.8/site-packages/datasets/download/download_manager.py:324, in DownloadManager.download(self, url_or_urls)
    321 self.downloaded_paths.update(dict(zip(url_or_urls.flatten(), downloaded_path_or_paths.flatten())))
    323 start_time = datetime.now()
--> 324 self._record_sizes_checksums(url_or_urls, downloaded_path_or_paths)
    325 duration = datetime.now() - start_time
    326 logger.info(f"Checksum Computation took {duration.total_seconds() // 60} min")

File /gpfswork/rech/cnw/commun/conda/lucile-m4_3/lib/python3.8/site-packages/datasets/download/download_manager.py:229, in DownloadManager._record_sizes_checksums(self, url_or_urls, downloaded_path_or_paths)
    226 """Record size/checksum of downloaded files."""
    227 for url, path in zip(url_or_urls.flatten(), downloaded_path_or_paths.flatten()):
    228     # call str to support PathLike objects
--> 229     self._recorded_sizes_checksums[str(url)] = get_size_checksum_dict(
    230         path, record_checksum=self.record_checksums
    231     )

File /gpfswork/rech/cnw/commun/conda/lucile-m4_3/lib/python3.8/site-packages/datasets/utils/info_utils.py:82, in get_size_checksum_dict(path, record_checksum)
     80 if record_checksum:
     81     m = sha256()
---> 82     with open(path, "rb") as f:
     83         for chunk in iter(lambda: f.read(1 << 20), b""):
     84             m.update(chunk)

PermissionError: [Errno 13] Permission denied: '/gpfswork/rech/cnw/commun/datasets/downloads/1e6aa6d23190c30885194fabb193dce3874d902d7636b66315ee8aaa584e80d6'
```

### Steps to reproduce the bug

I think the following will reproduce the bug.

Given 2 users belonging to the same group with `umask` set to `0007`

- first run with User 1:
```python
from datasets import load_dataset
ds_name = "HuggingFaceM4/VQAv2"
ds = load_dataset(ds_name)
```

- then run with User 2:

```python
from datasets import load_dataset
ds_name = "HuggingFaceM4/TextCaps"
ds = load_dataset(ds_name)
```

### Expected behavior

No `PermissionError`

### Environment info

- `datasets` version: 2.4.0
- Platform: Linux-4.18.0-305.65.1.el8_4.x86_64-x86_64-with-glibc2.17
- Python version: 3.8.13
- PyArrow version: 7.0.0
- Pandas version: 1.4.2