op_file.c revision cc2ee177dbb3befca43e36cfc56778b006c3d050
1/**
2 * @file op_file.c
3 * Useful file management helpers
4 *
5 * @remark Copyright 2002 OProfile authors
6 * @remark Read the file COPYING
7 *
8 * @author John Levon
9 * @author Philippe Elie
10 */
11
12#include <sys/stat.h>
13#include <unistd.h>
14
15#include <stdlib.h>
16#include <stdio.h>
17#include <errno.h>
18#include <string.h>
19#include <limits.h>
20
21#include "op_file.h"
22#include "op_libiberty.h"
23
24int op_file_readable(char const * file)
25{
26	struct stat st;
27	return !stat(file, &st) && S_ISREG(st.st_mode) && !access(file, R_OK);
28}
29
30
31time_t op_get_mtime(char const * file)
32{
33	struct stat st;
34
35	if (stat(file, &st))
36		return 0;
37
38	return st.st_mtime;
39}
40
41
42int create_dir(char const * dir)
43{
44	if (mkdir(dir, 0755)) {
45		/* FIXME: Does not verify existing is a dir */
46		if (errno == EEXIST)
47			return 0;
48		return errno;
49	}
50
51	return 0;
52}
53
54
55int create_path(char const * path)
56{
57	int ret = 0;
58
59	char * str = xstrdup(path);
60
61	char * pos = str[0] == '/' ? str + 1 : str;
62
63	for ( ; (pos = strchr(pos, '/')) != NULL; ++pos) {
64		*pos = '\0';
65		ret = create_dir(str);
66		*pos = '/';
67		if (ret)
68			break;
69	}
70
71	free(str);
72	return ret;
73}
74