diff --git a/service/fs_utils.cc b/service/fs_utils.cc index 22bcebae..0efa031a 100644 --- a/service/fs_utils.cc +++ b/service/fs_utils.cc @@ -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; @@ -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; } @@ -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); diff --git a/service/fs_utils.h b/service/fs_utils.h index e092d348..20226001 100644 --- a/service/fs_utils.h +++ b/service/fs_utils.h @@ -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; @@ -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 diff --git a/service/s3.cc b/service/s3.cc index ab852ed4..1519d5fc 100644 --- a/service/s3.cc +++ b/service/s3.cc @@ -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