# example: check if write access to path

**URL:** https://discourse.cmake.org/t/example-check-if-write-access-to-path/6535
**Category:** Code
**Created:** [September 20, 2022, 6:54pm UTC](https://discourse.cmake.org/t/example-check-if-write-access-to-path/6535 "2022-09-20T18:54:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [September 20, 2022, 6:54pm UTC](https://discourse.cmake.org/t/example-check-if-write-access-to-path/6535/1 "2022-09-20T18:54:42Z")

</div>

At CMake configure time I often need to test if a directory or file is writable. One example is to fatal\_error if CMAKE\_INSTALL\_PREFIX is not in a writable location for users not so experienced with CMake or building programs in general.

`file(TOUCH)` and `file(MAKE_DIRECTORY)` don’t halt the configure step till erroring at the end of configure.

What I do instead is like this snippet to generate a fatal\_error with text telling the user what to try:

```cmake
set(test_path /path/to/test/.ignore)
# could be done with random string filename in the desired path

execute_process(COMMAND ${CMAKE_COMMAND} -E touch ${test_path}
RESULT_VARIABLE ret
)
if(NOT ret EQUAL "0")
  message(FATAL_ERROR "No write access to ${test_path}
  <text for user to resolve issue>")
endif()

```

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [September 20, 2022, 11:16pm UTC](https://discourse.cmake.org/t/example-check-if-write-access-to-path/6535/2 "2022-09-20T23:16:38Z")

</div>

While still not so elegant, since CMake 3.21 you can use `file(COPY_FILE)` and include the `RESULT` keyword to see if that succeeded. You could try copying a trivial empty file into the destination and removing it again. Basically the same approach as yours, just without having to shell out to a separate process.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [September 21, 2022, 1:32am UTC](https://discourse.cmake.org/t/example-check-if-write-access-to-path/6535/3 "2022-09-21T01:32:04Z")

</div>

yes that would nice if `file(TOUCH)` and `file(MAKE_DIRECTORY)` could have `RESULT` added

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 27, 2022, 7:10pm UTC](https://discourse.cmake.org/t/example-check-if-write-access-to-path/6535/4 "2022-09-27T19:10:07Z")

</div>

Please file an issue for this. It’ll get lost here 🙂 .
