2 #define _FILE_OFFSET_BITS 64
10 #include <sys/param.h>
12 # include <sys/statvfs.h>
13 # include <sys/ucred.h>
14 # include <sys/mount.h>
23 #pragma GCC diagnostic ignored "-Wformat" // shut warning on '%z'
65 static const char procfs[] =
"/proc/filesystems";
66 FILE *fs = fopen(procfs,
"r");
71 <<
"Cannot read '" << procfs <<
"': "
72 << strerror(nerr) <<
" (error " << nerr <<
")";
85 if ((nread = getdelim(&type, &len,
'\t', fs)) == -1 && ! feof(fs))
87 fprintf(stderr,
"%s:%d: %s (%zd; 1)\n",
88 procfs, line, strerror(errno), nread);
94 if ((nread = getdelim(&fstype, &len,
'\n', fs)) == -1 && ! feof(fs))
96 fprintf(stderr,
"%s:%d: %s (%zd; 2)\n",
97 procfs, line, strerror(errno), nread);
111 if (! strcmp(type,
"nodev\t")
112 || ! strcmp(fstype,
"lustre\n")
113 || ! strncmp(fstype,
"fuse", 4))
145 struct statfs *
m =
static_cast<struct statfs *
>(
arg);
146 size_t infolen =
sizeof(
struct FSInfo);
147 size_t fslen = strlen(m->f_mntfromname) + 1;
148 size_t dirlen = strlen(m->f_mntonname) + 1;
149 size_t typelen = strlen(m->f_fstypename) + 1;
150 size_t totlen = infolen + fslen + dirlen + typelen;
152 char *
p = (
char *) i;
153 i->
fsname = strncpy(p += infolen, m->f_mntfromname, fslen);
154 i->
type = strncpy(p += fslen, m->f_fstypename, typelen);
155 i->
dir = strncpy(p += typelen, m->f_mntonname, dirlen);
156 i->
dev = m->f_fsid.val[0];
165 i->
freespc /= 1024. * 1024. * 1024.;
173 i->
local = ((m->f_flags & MNT_LOCAL) ? 1 : 0);
178 mntent *m =
static_cast<mntent *
>(
arg);
179 size_t infolen =
sizeof(
struct FSInfo);
180 size_t fslen = strlen(m->mnt_fsname) + 1;
181 size_t dirlen = strlen(m->mnt_dir) + 1;
182 size_t typelen = strlen(m->mnt_type) + 1;
183 size_t originlen = strlen(m->mnt_fsname) + 1;
184 size_t totlen = infolen + fslen + dirlen + typelen + originlen;
186 char *p = (
char *) i;
187 i->
fsname = strncpy(p += infolen, m->mnt_fsname, fslen);
188 i->
type = strncpy(p += fslen, m->mnt_type, typelen);
189 i->
dir = strncpy(p += typelen, m->mnt_dir, dirlen);
190 i->
origin = strncpy(p += dirlen, m->mnt_fsname, originlen);
196 i->
bind = strstr(m->mnt_opts,
"bind") !=
nullptr;
217 struct statfs *mtab = 0;
218 if ((rc = getmntinfo(&mtab, MNT_NOWAIT)) < 0)
222 <<
"getmntinfo() failed: " << strerror(nerr)
223 <<
" (error " << nerr <<
")";
228 for (
int ix = 0; ix < rc; ++ix)
234 FILE *mtab = setmntent(_PATH_MOUNTED,
"r");
239 <<
"Cannot read '" << _PATH_MOUNTED <<
"': "
240 << strerror(nerr) <<
" (error " << nerr <<
")";
245 while ((m = getmntent(mtab)))
272 if (lstat(i->
dir, &s) < 0)
275 if (nerr != ENOENT && nerr != EACCES)
277 <<
"Cannot lstat('" << i->
dir <<
"'): "
278 << strerror(nerr) <<
" (error " << nerr <<
")";
282 if (statfs(i->
dir, &sfs) < 0)
286 <<
"Cannot statfs('" << i->
dir <<
"'): "
287 << strerror(nerr) <<
" (error " << nerr <<
")";
297 i->
freespc /= 1024. * 1024. * 1024.;
325 for (
const auto & old_path : prev_paths)
327 if (!strcmp(old_path.c_str(),
path))
330 <<
"Found a loop in bind mounts; stopping evaluation.";
337 size_t len = strlen(path);
338 for (
size_t i = 0;
i <
fs_.size(); ++
i)
342 size_t fslen = strlen(
fs_[
i]->
dir);
343 if (! strncmp(
fs_[
i]->
dir, path, fslen)
344 && ((fslen == 1 &&
fs_[
i]->
dir[0] ==
'/')
345 || len == fslen || path[fslen] ==
'/')
346 && (! best || fslen > bestlen))
357 if (
fs_[i]->dev != s->st_dev ||
fs_[i]->fstype != sfs->f_type)
373 char *fullpath = realpath(best->
origin, 0);
376 fullpath = strdup(best->
origin);
378 if (lstat(fullpath, &s2) < 0)
382 <<
"Cannot lstat('" << fullpath <<
"' alias '"
383 << path <<
"'): " << strerror(nerr) <<
" (error "
389 if (statfs(fullpath, &sfs2) < 0)
393 <<
"Cannot statfs('" << fullpath <<
"' alias '"
394 << path <<
"'): " << strerror(nerr) <<
" (error "
400 prev_paths.push_back(path);
402 return new_best ? new_best : best;
422 char *fullpath = realpath(path.c_str(), 0);
425 fullpath = strdup(path.c_str());
427 if (lstat(fullpath, &s) < 0)
431 <<
"Cannot lstat('" << fullpath <<
"' alias '"
432 << path <<
"'): " << strerror(nerr) <<
" (error "
438 if (statfs(fullpath, &sfs) < 0)
442 <<
"Cannot statfs('" << fullpath <<
"' alias '"
443 << path <<
"'): " << strerror(nerr) <<
" (error "
449 std::vector<std::string> prev_paths;
453 return m ? m->
local :
false;
480 std::ostringstream warningst;
481 warningst <<
"Cannot use lazy-download because:\n";
483 for (
size_t i = 0,
e = paths.size();
i <
e; ++
i)
486 const char *inpath = paths[
i].c_str();
487 const char *
path = inpath;
491 char *
p = getenv(path+1);
494 else if (! strcmp(path,
"$TMPDIR"))
498 if (! (fullpath = realpath(path, 0)))
499 fullpath = strdup(path);
503 <<
"Checking if '" << fullpath <<
"', from '"
504 << inpath <<
"' is valid cache path with "
505 << minFreeSpace <<
" free space" << std::endl;
508 if (lstat(fullpath, &s) < 0)
511 if (nerr != ENOENT && nerr != EACCES)
513 <<
"Cannot lstat('" << fullpath <<
"', from '"
514 << inpath <<
"'): " << strerror(nerr) <<
" (error "
520 if (statfs(fullpath, &sfs) < 0)
524 <<
"Cannot statfs('" << fullpath <<
"', from '"
525 << inpath <<
"'): " << strerror(nerr) <<
" (error "
531 std::vector<std::string> prev_paths;
535 <<
"Candidate '" << fullpath <<
"': "
536 <<
"found=" << (m ? 1 : 0)
537 <<
" local=" << (m && m->
local)
538 <<
" free=" << (m ? m->
freespc : 0)
539 <<
" access=" << access(fullpath, W_OK)
546 && access(fullpath, W_OK) == 0)
556 warningst <<
"- The mount " << fullpath <<
" is not local.\n";
558 else if (m->
freespc < minFreeSpace)
560 warningst <<
" - The mount at " << fullpath <<
" has only " << m->
freespc <<
" GB free; a minumum of " << minFreeSpace <<
" GB is required.\n";
562 else if (access(fullpath, W_OK))
564 warningst <<
" - The process has no permission to write into " << fullpath <<
"\n";
572 if (warning_str.size())
574 warning_str = warning_str.substr(0, warning_str.size()-2);
603 for (
size_t i = 0,
e =
fs_.size();
i <
e; ++
i)
int statFSInfo(FSInfo *i)
Information about file systems on this node.
std::string unusable_dir_warnings_
std::string findCachePath(const std::vector< std::string > &paths, double minFreeSpace)
bool isLocalPath(const std::string &path)
tuple path
else: Piece not in the list, fine.
std::vector< std::string > fstypes_
FSInfo * initFSInfo(void *p)
std::vector< FSInfo * > fs_
FSInfo * findMount(const char *path, struct statfs *sfs, struct stat *s, std::vector< std::string > &)