// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- /* * Ceph - scalable distributed file system * * Copyright (C) 2004-2006 Sage Weil * * This is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License version 2.1, as published by the Free Software * Foundation. See file COPYING. * */ #ifndef __FILEPATH_H #define __FILEPATH_H /* * BUG: /a/b/c is equivalent to a/b/c in dentry-breakdown, but not string. * -> should it be different? how? should this[0] be "", with depth 4? * */ #include #include #include using namespace std; #include "buffer.h" class filepath { /** path * can be relative "a/b/c" or absolute "/a/b/c". */ string path; /** bits - path segemtns * this is ['a', 'b', 'c'] for both the aboslute and relative case. * * NOTE: this value is LAZILY maintained... i.e. it's a cache */ mutable vector bits; void rebuild_path() { if (absolute()) path = "/"; else path.clear(); for (unsigned i=0; i 0) parse_bits(); return bits.size(); } bool empty() const { return path.length() == 0; } // FIXME: const-edness bool absolute() { return path.length() && path[0] == '/'; } bool relative() { return !absolute(); } const string& operator[](int i) const { if (bits.empty() && path.length() > 0) parse_bits(); return bits[i]; } const string& last_dentry() const { if (bits.empty() && path.length() > 0) parse_bits(); return bits[ bits.size()-1 ]; } filepath prefixpath(int s) const { filepath t; for (int i=0; i 0) parse_bits(); bits.pop_back(); rebuild_path(); } void push_dentry(const string& s) { if (bits.empty() && path.length() > 0) parse_bits(); bits.push_back(s); if (path.length() && path[path.length()-1] != '/') path += "/"; path += s; } void append(const filepath& a) { for (unsigned i=0; i