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'
63 static const char procfs[] =
"/proc/filesystems";
64 FILE *fs = fopen(procfs,
"r");
69 <<
"Cannot read '" << procfs <<
"': "
70 << strerror(nerr) <<
" (error " << nerr <<
")";
83 if ((nread = getdelim(&type, &len,
'\t', fs)) == -1 && ! feof(fs))
85 fprintf(stderr,
"%s:%d: %s (%zd; 1)\n",
86 procfs, line, strerror(errno), nread);
92 if ((nread = getdelim(&fstype, &len,
'\n', fs)) == -1 && ! feof(fs))
94 fprintf(stderr,
"%s:%d: %s (%zd; 2)\n",
95 procfs, line, strerror(errno), nread);
109 if (! strcmp(type,
"nodev\t")
110 || ! strcmp(fstype,
"lustre\n")
111 || ! strncmp(fstype,
"fuse", 4))
143 struct statfs *
m =
static_cast<struct statfs *
>(
arg);
144 size_t infolen =
sizeof(
struct FSInfo);
145 size_t fslen = strlen(m->f_mntfromname) + 1;
146 size_t dirlen = strlen(m->f_mntonname) + 1;
147 size_t typelen = strlen(m->f_fstypename) + 1;
148 size_t totlen = infolen + fslen + dirlen + typelen;
150 char *
p = (
char *) i;
151 i->
fsname = strncpy(p += infolen, m->f_mntfromname, fslen);
152 i->
type = strncpy(p += fslen, m->f_fstypename, typelen);
153 i->
dir = strncpy(p += typelen, m->f_mntonname, dirlen);
154 i->
dev = m->f_fsid.val[0];
161 i->
freespc /= 1024. * 1024. * 1024.;
169 i->
local = ((m->f_flags & MNT_LOCAL) ? 1 : 0);
174 mntent *m =
static_cast<mntent *
>(
arg);
175 size_t infolen =
sizeof(
struct FSInfo);
176 size_t fslen = strlen(m->mnt_fsname) + 1;
177 size_t dirlen = strlen(m->mnt_dir) + 1;
178 size_t typelen = strlen(m->mnt_type) + 1;
179 size_t totlen = infolen + fslen + dirlen + typelen;
181 char *p = (
char *) i;
182 i->
fsname = strncpy(p += infolen, m->mnt_fsname, fslen);
183 i->
type = strncpy(p += fslen, m->mnt_type, typelen);
184 i->
dir = strncpy(p += typelen, m->mnt_dir, dirlen);
210 struct statfs *mtab = 0;
211 if ((rc = getmntinfo(&mtab, MNT_NOWAIT)) < 0)
215 <<
"getmntinfo() failed: " << strerror(nerr)
216 <<
" (error " << nerr <<
")";
221 for (
int ix = 0; ix < rc; ++ix)
227 FILE *mtab = setmntent(_PATH_MOUNTED,
"r");
232 <<
"Cannot read '" << _PATH_MOUNTED <<
"': "
233 << strerror(nerr) <<
" (error " << nerr <<
")";
238 while ((m = getmntent(mtab)))
265 if (lstat(i->
dir, &s) < 0)
268 if (nerr != ENOENT && nerr != EACCES)
270 <<
"Cannot lstat('" << i->
dir <<
"'): "
271 << strerror(nerr) <<
" (error " << nerr <<
")";
275 if (statfs(i->
dir, &sfs) < 0)
279 <<
"Cannot statfs('" << i->
dir <<
"'): "
280 << strerror(nerr) <<
" (error " << nerr <<
")";
290 i->
freespc /= 1024. * 1024. * 1024.;
320 size_t len = strlen(path);
321 for (
size_t i = 0;
i <
fs_.size(); ++
i)
325 size_t fslen = strlen(
fs_[
i]->
dir);
326 if (! strncmp(
fs_[
i]->
dir, path, fslen)
327 && ((fslen == 1 &&
fs_[
i]->
dir[0] ==
'/')
328 || len == fslen || path[fslen] ==
'/')
329 && (! best || fslen > bestlen))
340 if (
fs_[i]->dev != s->st_dev ||
fs_[i]->fstype != sfs->f_type)
369 char *fullpath = realpath(path.c_str(), 0);
372 fullpath = strdup(path.c_str());
374 if (lstat(fullpath, &s) < 0)
378 <<
"Cannot lstat('" << fullpath <<
"' alias '"
379 << path <<
"'): " << strerror(nerr) <<
" (error "
385 if (statfs(fullpath, &sfs) < 0)
389 <<
"Cannot statfs('" << fullpath <<
"' alias '"
390 << path <<
"'): " << strerror(nerr) <<
" (error "
399 return m ? m->
local :
false;
426 std::ostringstream warningst;
427 warningst <<
"Cannot use lazy-download because:\n";
429 for (
size_t i = 0,
e = paths.size();
i <
e; ++
i)
432 const char *inpath = paths[
i].c_str();
433 const char *
path = inpath;
437 char *
p = getenv(path+1);
440 else if (! strcmp(path,
"$TMPDIR"))
444 if (! (fullpath = realpath(path, 0)))
445 fullpath = strdup(path);
449 <<
"Checking if '" << fullpath <<
"', from '"
450 << inpath <<
"' is valid cache path with "
451 << minFreeSpace <<
" free space" << std::endl;
454 if (lstat(fullpath, &s) < 0)
457 if (nerr != ENOENT && nerr != EACCES)
459 <<
"Cannot lstat('" << fullpath <<
"', from '"
460 << inpath <<
"'): " << strerror(nerr) <<
" (error "
466 if (statfs(fullpath, &sfs) < 0)
470 <<
"Cannot statfs('" << fullpath <<
"', from '"
471 << inpath <<
"'): " << strerror(nerr) <<
" (error "
480 <<
"Candidate '" << fullpath <<
"': "
481 <<
"found=" << (m ? 1 : 0)
482 <<
" local=" << (m && m->
local)
483 <<
" free=" << (m ? m->
freespc : 0)
484 <<
" access=" << access(fullpath, W_OK)
491 && access(fullpath, W_OK) == 0)
501 warningst <<
"- The mount " << fullpath <<
" is not local.\n";
503 else if (m->
freespc < minFreeSpace)
505 warningst <<
" - The mount at " << fullpath <<
" has only " << m->
freespc <<
" GB free; a minumum of " << minFreeSpace <<
" GB is required.\n";
507 else if (access(fullpath, W_OK))
509 warningst <<
" - The process has no permission to write into " << fullpath <<
"\n";
517 if (warning_str.size())
519 warning_str = warning_str.substr(0, warning_str.size()-2);
548 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_
FSInfo * findMount(const char *path, struct statfs *sfs, struct stat *s)
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_