Home | History | Annotate | Download | only in lib
      1 /*
      2  * Copyright (c) 2017 Cyril Hrubis <chrubis (at) suse.cz>
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation, either version 2 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 #include <errno.h>
     19 #include <stdio.h>
     20 #include <stdlib.h>
     21 
     22 #define TST_NO_DEFAULT_MAIN
     23 #include "tst_test.h"
     24 #include "tst_fs.h"
     25 
     26 void tst_fill_fs(const char *path, int verbose)
     27 {
     28 	int i = 0;
     29 	char file[PATH_MAX];
     30 	char buf[4096];
     31 	size_t len;
     32 	ssize_t ret;
     33 	int fd;
     34 
     35 	for (;;) {
     36 		len = random() % (1024 * 102400);
     37 
     38 		snprintf(file, sizeof(file), "%s/file%i", path, i++);
     39 
     40 		if (verbose)
     41 			tst_res(TINFO, "Creating file %s size %zu", file, len);
     42 
     43 		fd = open(file, O_WRONLY | O_CREAT, 0700);
     44 		if (fd == -1) {
     45 			if (errno != ENOSPC)
     46 				tst_brk(TBROK | TERRNO, "open()");
     47 
     48 			tst_res(TINFO | TERRNO, "open()");
     49 			return;
     50 		}
     51 
     52 		while (len) {
     53 			ret = write(fd, buf, MIN(len, sizeof(buf)));
     54 
     55 			if (ret < 0) {
     56 				SAFE_CLOSE(fd);
     57 
     58 				if (errno != ENOSPC)
     59 					tst_brk(TBROK | TERRNO, "write()");
     60 
     61 				tst_res(TINFO | TERRNO, "write()");
     62 				return;
     63 			}
     64 
     65 			len -= ret;
     66 		}
     67 
     68 		SAFE_CLOSE(fd);
     69 	}
     70 }
     71