Motr  M0
fs.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2014-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_LIB
24 #include "lib/trace.h"
25 
26 #include <unistd.h> /* close(2) */
27 #include <dirent.h> /* opendir(3) */
28 #include <fcntl.h> /* open(2) */
29 #include <errno.h>
30 #include "lib/memory.h" /* M0_ALLOC_ARR */
31 
32 M0_INTERNAL int m0_cleandir(const char *dir)
33 {
34  struct dirent *de;
35  DIR *d;
36  int rc;
37  int fd;
38 
39  fd = open(dir, O_RDONLY|O_DIRECTORY);
40  if (fd == -1) {
41  if (errno == ENOENT)
42  return M0_RC(0);
43  rc = -errno;
44  M0_LOG(M0_NOTICE, "open(%s) failed: rc=%d", dir, rc);
45  return M0_ERR(rc);
46  }
47  d = opendir(dir);
48  if (d != NULL) {
49  while ((de = readdir(d)) != NULL)
50  unlinkat(fd, de->d_name, 0);
51  closedir(d);
52  }
53  close(fd);
54 
55  rc = rmdir(dir) == 0 ? 0 : -errno;
56  if (rc != 0)
57  M0_LOG(M0_ERROR, "rmdir(%s) failed: rc=%d", dir, rc);
58  return M0_RC(rc);
59 }
60 
61 M0_INTERNAL int m0_file_read(const char *path, char **out)
62 {
63  FILE *f;
64  long size;
65  size_t n;
66  int rc = 0;
67 
68  M0_ENTRY("path=`%s'", path);
69 
70  f = fopen(path, "rb");
71  if (f == NULL)
72  return M0_ERR_INFO(-errno, "path=`%s'", path);
73 
74  rc = fseek(f, 0, SEEK_END);
75  if (rc == 0) {
76  size = ftell(f);
77  rc = fseek(f, 0, SEEK_SET);
78  }
79  if (rc != 0) {
80  fclose(f);
81  return M0_ERR_INFO(-errno, "fseek() failed: path=`%s'", path);
82  }
83  /* it should be freed by the caller */
84  M0_ALLOC_ARR(*out, size + 1);
85  if (*out != NULL) {
86  n = fread(*out, 1, size + 1, f);
87  M0_ASSERT_INFO(n == size, "n=%zu size=%ld", n, size);
88  if (ferror(f))
89  rc = -errno;
90  else if (!feof(f))
91  rc = M0_ERR(-EFBIG);
92  else
93  (*out)[n] = '\0';
94  } else {
95  rc = M0_ERR(-ENOMEM);
96  }
97 
98  fclose(f);
99  return M0_RC(rc);
100 }
101 
102 #undef M0_TRACE_SUBSYSTEM
103 
104 /*
105  * Local variables:
106  * c-indentation-style: "K&R"
107  * c-basic-offset: 8
108  * tab-width: 8
109  * fill-column: 80
110  * scroll-step: 1
111  * End:
112  */
#define M0_ALLOC_ARR(arr, nr)
Definition: memory.h:84
#define NULL
Definition: misc.h:38
static FILE * f
Definition: adieu.c:79
#define M0_LOG(level,...)
Definition: trace.h:167
M0_INTERNAL int m0_file_read(const char *path, char **out)
Definition: fs.c:61
return M0_RC(rc)
#define M0_ENTRY(...)
Definition: trace.h:170
#define M0_ERR_INFO(rc, fmt,...)
Definition: trace.h:215
return M0_ERR(-EOPNOTSUPP)
M0_INTERNAL int m0_cleandir(const char *dir)
Definition: fs.c:25
uint64_t n
Definition: fops.h:107
m0_bcount_t size
Definition: di.c:39
struct m0t1fs_filedata * fd
Definition: dir.c:1030
#define M0_ASSERT_INFO(cond, fmt,...)
struct inode * dir
Definition: dir.c:1028
#define out(...)
Definition: gen.c:41
int32_t rc
Definition: trigger_fop.h:47