2 # Find a directory in which a disk-simulating file is usable by parted.
3 # The problem is that on systems supporting O_DIRECT, open with O_DIRECT
4 # fails for some file system types (e.g., tmpfs on linux-2.6.21).
6 # Copyright (C) 2007-2012 Free Software Foundation, Inc.
7 # This file is free software; the Free Software Foundation
8 # gives unlimited permission to copy and/or distribute it,
9 # with or without modifications, as long as this notice is preserved.
13 # Set PARTED_USABLE_TEST_DIR to the name of the first usable directory
14 # from the list below. If none is usable, set it to the empty string.
15 # Consider $TMPDIR only if it specifies an absolute name, and that
16 # name contains no shell meta-character. Likewise for $HOME.
18 # This code is pretty picky. The chosen partition must support aligned reads
19 # and writes in blocks of size 512B and 4KB to a file descriptor opened with
20 # O_RDWR|O_DIRECT. Reiserfs doesn't support 512-byte reads. On tmpfs,
23 # The candidate directories:
24 # . $HOME $TMPDIR /tmp /var/tmp /dev/shm
25 AC_DEFUN([parted_FIND_USABLE_TEST_DIR],
27 AC_CACHE_CHECK([for a usable (O_DIRECT-supporting) temporary dir],
28 [parted_cv_func_open_O_DIRECT_temp_dir],
30 # First of all, if there is no O_DIRECT definition, use ".",
31 # and skip the run-test.
32 AC_EGREP_CPP([frobnozzle], [
37 ], pe_have_O_DIRECT=yes, pe_have_O_DIRECT=no)
38 if test $pe_have_O_DIRECT = no; then
39 # With no O_DIRECT definition, "." is fine.
43 for pe_dir in "$HOME" "$TMPDIR"; do
49 # Accept $HOME or $TMP only if the value is nice and boring.
50 *[^/a-zA-Z0-9_.-]*) ;;
51 *) pe_cand_dirs="$pe_cand_dirs $pe_dir";;
56 case $PARTED_TMPDIR in
57 *[^/a-zA-Z0-9_.-]*) ;;
58 *) pe_cand_dirs="$PARTED_TMPDIR $pe_cand_dirs";;
61 # This is the list of candidate directories.
62 pe_cand_dirs="$pe_cand_dirs /tmp /var/tmp /dev/shm"
64 PARTED_CANDIDATE_DIRS=$pe_cand_dirs
65 export PARTED_CANDIDATE_DIRS
70 #include <sys/types.h>
77 #define MAX_LOGICAL_BLOCK_SIZE 4096
78 static char g_buf[2 * MAX_LOGICAL_BLOCK_SIZE];
81 ptr_align (void const *ptr, size_t alignment)
84 char const *p1 = p0 + alignment - 1;
85 return (void *) (p1 - (size_t) p1 % alignment);
89 create_input_file (char const *file, char const *buf, size_t n_bytes)
91 int fd = open (file, O_CREAT | O_WRONLY, 0600);
94 if (write (fd, buf, n_bytes) != n_bytes)
103 try_o_direct (char const *file, size_t block_size)
105 char *p = ptr_align (g_buf, MAX_LOGICAL_BLOCK_SIZE);
108 if (!(p + block_size < g_buf + sizeof g_buf))
111 fd = open (file, O_RDWR | O_DIRECT);
115 if (write (fd, p, block_size) != block_size)
118 if (lseek (fd, 0, SEEK_SET) != 0)
121 if (read (fd, p, block_size) != block_size)
124 return !! close (fd);
128 #define stpcpy(a, b) my_stpcpy (a, b)
130 my_stpcpy (char *dest, const char *src)
134 do *d++ = *s; while (*s++ != '\0');
138 /* The base name of the file we'll create in the mkdtemp-returned
139 temporary directory. */
142 /* Return 0 upon failure, else the 1-based index of the first
143 useful directory name from PARTED_CANDIDATE_DIRS. */
147 char const *env_dirs;
154 if ((env_dirs = getenv ("PARTED_CANDIDATE_DIRS")) == NULL)
157 dirs_len = strlen (env_dirs);
158 if ((dirs = strndup (env_dirs, dirs_len)) == NULL)
167 /* Skip any leading spaces. */
171 space = strchr (dir, ' ');
175 dirname_len = space - dir;
179 dirname_len = strlen (dir);
182 if (dirname_len != 0)
184 /* Create an mkdtemp template starting with dir. */
187 char const *base = "partedOD.XXXXXX";
188 /* Allocate enough space not just for the dir name, but
189 also for the name of the file to create within it. */
190 char *template = malloc (dirname_len + 1 + strlen (base)
191 + 1 + strlen (BASENAME) + 1);
193 && (endp = stpcpy (stpcpy (stpcpy (template, dir), "/"), base))
194 && (tmp = mkdtemp (template)) != NULL)
196 /* Append "/BASENAME" to create the file name. */
197 stpcpy (stpcpy (endp, "/"), BASENAME);
199 if (create_input_file (tmp, g_buf, sizeof g_buf) == 0
200 && try_o_direct (tmp, 512) == 0
201 && try_o_direct (tmp, MAX_LOGICAL_BLOCK_SIZE) == 0)
204 unlink (tmp); /* ignore failure */
206 rmdir (tmp); /* ignore failure */
214 dir += dirname_len + 1;
215 if (dirs + dirs_len < dir)
226 # If the above program exits with status 0, then
227 # there it found no useful directory. Use ".".
228 [parted_cv_func_open_O_DIRECT_temp_dir=.],
230 # It found one. The exit status is an index into the list.
231 # We also run this code when the program fails to compile or
232 # to link, as will happen on systems without a mkdtemp function.
233 [pe_err=$?; set _ $pe_cand_dirs; shift
234 eval parted_cv_func_open_O_DIRECT_temp_dir='$'$pe_err],
236 # When cross-compiling, use ".".
237 [parted_cv_func_open_O_DIRECT_temp_dir=.]
241 PARTED_USABLE_TEST_DIR=$parted_cv_func_open_O_DIRECT_temp_dir
242 AC_SUBST([PARTED_USABLE_TEST_DIR])
244 # If the result is ".", don't cache it. The next user of
245 # the cache may well be running from a different file system.
246 dnl Here, I'm using "$as_unset", which is a non-published (i.e., internal)
247 dnl part of autoconf, but we don't expect its name to change any time soon.
248 dnl and by then, it'll probably be ok to use "unset" all by itself.
249 if test "$parted_cv_func_open_O_DIRECT_temp_dir" = .; then
250 $as_unset parted_cv_func_open_O_DIRECT_temp_dir