Skip to content

Commit bdc8aae

Browse files
client: trim path before logging it
Path can be virtually infinitely long and logging a long long path (imagine around 2000 path components) is un-useful as well as lowers readability of the log. Therefore, trim before logging. Fixes: https://tracker.ceph.com/issues/72993 Signed-off-by: Rishabh Dave <[email protected]>
1 parent 11de1e5 commit bdc8aae

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

src/client/Client.cc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7689,7 +7689,10 @@ int Client::path_walk(InodeRef dirinode, const filepath& origpath, InodeRef *end
76897689
return rc;
76907690
}
76917691

7692-
int Client::path_walk(InodeRef dirinode, const filepath& origpath, walk_dentry_result* result, const UserPerm& perms, const PathWalk_ExtraOptions& extra_options)
7692+
int Client::path_walk(InodeRef dirinode, const filepath& origpath,
7693+
walk_dentry_result* result, const UserPerm& perms,
7694+
const PathWalk_ExtraOptions& extra_options,
7695+
std::string trimmed_path)
76937696
{
76947697
int rc = 0;
76957698
filepath path = origpath;
@@ -7713,7 +7716,11 @@ int Client::path_walk(InodeRef dirinode, const filepath& origpath, walk_dentry_r
77137716
int symlinks = 0;
77147717
unsigned i = 0;
77157718

7716-
ldout(cct, 10) << __func__ << ": cur=" << *diri << " path=" << path << dendl;
7719+
if (trimmed_path == "") {
7720+
std::string trimmed_path = path.get_trimmed_path();
7721+
}
7722+
7723+
ldout(cct, 10) << __func__ << ": cur=" << *diri << " path=" << trimmed_path << dendl;
77177724

77187725
if (path.depth() == 0) {
77197726
/* diri/dname can also be used as a filepath; or target */
@@ -7727,7 +7734,7 @@ int Client::path_walk(InodeRef dirinode, const filepath& origpath, walk_dentry_r
77277734
int caps = 0;
77287735
dname = path[i];
77297736
ldout(cct, 10) << " " << i << " " << *diri << " " << dname << dendl;
7730-
ldout(cct, 20) << " (path is " << path << ")" << dendl;
7737+
ldout(cct, 20) << " (path is " << trimmed_path << ")" << dendl;
77317738
InodeRef next;
77327739
if (!diri.get()->is_dir()) {
77337740
ldout(cct, 20) << diri.get() << " is not a dir inode, name " << dname.c_str() << dendl;
@@ -15420,11 +15427,12 @@ int Client::ll_unlink(Inode *in, const char *name, const UserPerm& perm)
1542015427

1542115428
int Client::_rmdir(Inode *dir, const char *name, const UserPerm& perms, bool check_perms)
1542215429
{
15423-
ldout(cct, 8) << "_rmdir(" << dir->ino << " " << name << " uid "
15430+
std::string trimmed_path = filepath(name).get_trimmed_path();
15431+
ldout(cct, 8) << "_rmdir(" << dir->ino << " " << trimmed_path << " uid "
1542415432
<< perms.uid() << " gid " << perms.gid() << ")" << dendl;
1542515433

1542615434
walk_dentry_result wdr;
15427-
if (int rc = path_walk(dir, filepath(name), &wdr, perms, {.followsym = false}); rc < 0) {
15435+
if (int rc = path_walk(dir, filepath(name), &wdr, perms, {.followsym = false}, trimmed_path); rc < 0) {
1542815436
return rc;
1542915437
}
1543015438

src/client/Client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ class Client : public Dispatcher, public md_config_obs_t {
10731073
bool is_rename = false;
10741074
bool require_target = true;
10751075
};
1076-
int path_walk(InodeRef dirinode, const filepath& fp, struct walk_dentry_result* result, const UserPerm& perms, const PathWalk_ExtraOptions& extra_options);
1076+
int path_walk(InodeRef dirinode, const filepath& fp, struct walk_dentry_result* result, const UserPerm& perms, const PathWalk_ExtraOptions& extra_options, std::string trimmed_path=std::string());
10771077
int path_walk(InodeRef dirinode, const filepath& fp, InodeRef *end, const UserPerm& perms, const PathWalk_ExtraOptions& extra_options);
10781078

10791079
// fake inode number for 32-bits ino_t

src/include/filepath.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@
1717

1818
#include <ostream>
1919

20+
/* Trim given path to final 10 components and return it by prefixing it with
21+
* "..." to indicate that the path has been trimmed. */
22+
std::string filepath::get_trimmed_path() const
23+
{
24+
std::size_t n = 0;
25+
for (int i = 1; i <= 10; ++i) {
26+
n = path.rfind("/", n - 1);
27+
if (n == std::string::npos) {
28+
return std::string(path);
29+
}
30+
}
31+
32+
return std::string("..." + path.substr(n, -1));
33+
}
34+
2035
void filepath::rebuild_path() {
2136
path.clear();
2237
for (unsigned i=0; i<bits.size(); i++) {

src/include/filepath.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class filepath {
8686
inodeno_t get_ino() const { return ino; }
8787
const std::string& get_path() const { return path; }
8888
void set_trimmed();
89+
std::string get_trimmed_path() const;
8990
const char *c_str() const { return path.c_str(); }
9091

9192
int length() const { return path.length(); }

0 commit comments

Comments
 (0)