Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLAT-653: ensure "try" methods of fs utilities do not throw during commo... #127

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 22 additions & 29 deletions service/fs_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,29 @@ static FsObjectInfo extractInfo(const struct stat & stats)

struct LocalUrlFsHandler : public UrlFsHandler {

virtual FsObjectInfo getInfo(const Url & url) const
virtual FsObjectInfo getInfo(const Url & url)
const
{
FsObjectInfo info;

struct stat stats;
string path = url.path();

// cerr << "fs info on path: " + path + "\n";
int res = ::stat(path.c_str(), &stats);
if (res == -1) {
if (errno == ENOENT) {
return FsObjectInfo();
if (res == 0) {
info = extractInfo(stats);
}
else {
if (errno != ENOENT) {
throw ML::Exception(errno, "stat");
}
throw ML::Exception(errno, "stat");
}

// TODO: owner ID (uid) and name (uname)

return extractInfo(stats);
return info;
}

virtual FsObjectInfo tryGetInfo(const Url & url) const
{
struct stat stats;
string path = url.path();

// cerr << "fs info on path: " + path + "\n";
int res = ::stat(path.c_str(), &stats);
if (res == -1) {
return FsObjectInfo();
}

return extractInfo(stats);
}

virtual void makeDirectory(const Url & url) const
{
boost::system::error_code ec;
Expand Down Expand Up @@ -217,9 +207,10 @@ namespace Datacratic {

/* URLFSHANDLER */

size_t
int64_t
UrlFsHandler::
getSize(const Url & url) const
getSize(const Url & url)
const
{
return getInfo(url).size;
}
Expand Down Expand Up @@ -250,20 +241,22 @@ void registerUrlFsHandler(const std::string & scheme,
}

FsObjectInfo
tryGetUriObjectInfo(const std::string & url)
getUriObjectInfo(const std::string & url)
{
Url realUrl = makeUrl(url);
return findFsHandler(realUrl.scheme())->tryGetInfo(realUrl);
FsObjectInfo info = tryGetUriObjectInfo(url);
if (!info) {
throw ML::Exception("object does not exist at: " + url);
}
}

FsObjectInfo
getUriObjectInfo(const std::string & url)
tryGetUriObjectInfo(const std::string & url)
{
Url realUrl = makeUrl(url);
return findFsHandler(realUrl.scheme())->getInfo(realUrl);
}
size_t

int64_t
getUriSize(const std::string & url)
{
Url realUrl = makeUrl(url);
Expand Down
19 changes: 9 additions & 10 deletions service/fs_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ OnUriObject;

struct UrlFsHandler {
virtual FsObjectInfo getInfo(const Url & url) const = 0;
virtual FsObjectInfo tryGetInfo(const Url & url) const = 0;

virtual size_t getSize(const Url & url) const;
virtual int64_t getSize(const Url & url) const;
virtual std::string getEtag(const Url & url) const;

virtual void makeDirectory(const Url & url) const = 0;
Expand All @@ -98,18 +97,18 @@ void registerUrlFsHandler(const std::string & scheme,
/* FREE FUNCTIONS */
/*****************************************************************************/

// Return the object info for either a file or an S3 object
FsObjectInfo getUriObjectInfo(const std::string & filename);
// Return the object info for the given url and throws if it does not exist
FsObjectInfo getUriObjectInfo(const std::string & url);

// Return the object info for either a file or an S3 object, or null if
// it doesn't exist
FsObjectInfo tryGetUriObjectInfo(const std::string & filename);
// Return the object info for either a file or an S3 object or an empty
// FsObjectInfo it doesn't exist
FsObjectInfo tryGetUriObjectInfo(const std::string & url);

// Return an URI for either a file or an s3 object
size_t getUriSize(const std::string & filename);
// Return the file size for the given url
int64_t getUriSize(const std::string & url);

// Return an etag for either a file or an s3 object
std::string getUriEtag(const std::string & filename);
std::string getUriEtag(const std::string & url);

/* Create the directories for the given path. For S3 it does nothing;
for normal directories it does mkdir -p
Expand Down
11 changes: 1 addition & 10 deletions service/s3.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,7 @@ struct S3UrlFsHandler : public UrlFsHandler {
{
string bucket = url.host();
auto api = getS3ApiForBucket(bucket);
auto bucketPath = S3Api::parseUri(url.original);
return api->getObjectInfo(bucket, bucketPath.second);
}

virtual FsObjectInfo tryGetInfo(const Url & url) const
{
string bucket = url.host();
auto api = getS3ApiForBucket(bucket);
auto bucketPath = S3Api::parseUri(url.original);
return api->tryGetObjectInfo(bucket, bucketPath.second);
return api->tryGetObjectInfo(bucket, url.path().substr(1));
}

virtual void makeDirectory(const Url & url) const
Expand Down