|
| 1 | +/*************************************************************** |
| 2 | + * |
| 3 | + * Copyright (C) 2025, Pelican Project, Morgridge Institute for Research |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); you |
| 6 | + * may not use this file except in compliance with the License. You may |
| 7 | + * obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + ***************************************************************/ |
| 18 | + |
| 19 | +#include "GlobusDirectory.hh" |
| 20 | +#include "GlobusFileSystem.hh" |
| 21 | +#include "HTTPCommands.hh" |
| 22 | +#include "logging.hh" |
| 23 | +#include "stl_string_utils.hh" |
| 24 | + |
| 25 | +#include <XrdOss/XrdOssWrapper.hh> |
| 26 | +#include <XrdSys/XrdSysError.hh> |
| 27 | + |
| 28 | +#include <nlohmann/json.hpp> |
| 29 | + |
| 30 | +#include <sstream> |
| 31 | + |
| 32 | +using json = nlohmann::json; |
| 33 | + |
| 34 | +time_t parseTimestamp(const std::string &last_modified) { |
| 35 | + if (!last_modified.empty()) { |
| 36 | + struct tm tm_time = {}; |
| 37 | + if (strptime(last_modified.c_str(), "%Y-%m-%d %H:%M:%S", &tm_time) != |
| 38 | + nullptr) { |
| 39 | + return mktime(&tm_time); |
| 40 | + } |
| 41 | + } |
| 42 | + return 0; |
| 43 | +} |
| 44 | + |
| 45 | +void GlobusDirectory::Reset() { |
| 46 | + m_opened = false; |
| 47 | + m_idx = 0; |
| 48 | + m_objInfo.clear(); |
| 49 | + m_directories.clear(); |
| 50 | + m_stat_buf = nullptr; |
| 51 | + m_object = ""; |
| 52 | +} |
| 53 | + |
| 54 | +int GlobusDirectory::ListGlobusDir() { |
| 55 | + m_log.Log(XrdHTTPServer::Debug, "GlobusDirectory::ListGlobusDir", |
| 56 | + "Listing directory:", m_object.c_str()); |
| 57 | + HTTPDownload listCommand(m_fs.getLsUrl(), m_object, m_log, |
| 58 | + m_fs.getTransferToken()); |
| 59 | + |
| 60 | + if (!listCommand.SendRequest(0, 0)) { |
| 61 | + return HTTPRequest::HandleHTTPError( |
| 62 | + listCommand, m_log, "Globus directory listing", m_object.c_str()); |
| 63 | + } |
| 64 | + |
| 65 | + std::string response = listCommand.getResultString(); |
| 66 | + |
| 67 | + try { |
| 68 | + auto json = json::parse(response); |
| 69 | + |
| 70 | + if (json.contains("DATA") && json["DATA"].is_array()) { |
| 71 | + const auto &data = json["DATA"]; |
| 72 | + for (const auto &item : data) { |
| 73 | + if (item.contains("name") && item.contains("size") && |
| 74 | + item.contains("type")) { |
| 75 | + GlobusObjectInfo obj; |
| 76 | + obj.m_key = item["name"].get<std::string>(); |
| 77 | + obj.m_size = item["size"].get<size_t>(); |
| 78 | + |
| 79 | + if (item.contains("last_modified")) { |
| 80 | + obj.m_last_modified = |
| 81 | + item["last_modified"].get<std::string>(); |
| 82 | + } |
| 83 | + |
| 84 | + if (item["type"].get<std::string>() == "file") { |
| 85 | + m_objInfo.push_back(obj); |
| 86 | + } else if (item["type"].get<std::string>() == "dir") { |
| 87 | + std::string dirName = obj.m_key; |
| 88 | + if (dirName.back() != '/') { |
| 89 | + dirName += "/"; |
| 90 | + } |
| 91 | + obj.m_key = dirName; |
| 92 | + m_directories.push_back(obj); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + } catch (const json::exception &e) { |
| 99 | + m_log.Log(XrdHTTPServer::Warning, "GlobusDirectory::ListGlobusDir", |
| 100 | + "Failed to parse JSON response:", e.what()); |
| 101 | + return -EIO; |
| 102 | + } |
| 103 | + |
| 104 | + m_idx = 0; |
| 105 | + m_opened = true; |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | +int GlobusDirectory::Opendir(const char *path, XrdOucEnv &env) { |
| 111 | + if (m_opened) { |
| 112 | + return -EBADF; |
| 113 | + } |
| 114 | + Reset(); |
| 115 | + |
| 116 | + std::string realPath = path; |
| 117 | + if (realPath.back() != '/') { |
| 118 | + realPath = realPath + "/"; |
| 119 | + } |
| 120 | + |
| 121 | + std::string storagePrefix = m_fs.getStoragePrefix(); |
| 122 | + std::string object; |
| 123 | + |
| 124 | + if (realPath.find(storagePrefix) == 0) { |
| 125 | + object = realPath.substr(storagePrefix.length()); |
| 126 | + } else { |
| 127 | + object = realPath; |
| 128 | + } |
| 129 | + |
| 130 | + if (!object.empty() && object[0] == '/') { |
| 131 | + object = object.substr(1); |
| 132 | + } |
| 133 | + |
| 134 | + m_object = object; |
| 135 | + |
| 136 | + return ListGlobusDir(); |
| 137 | +} |
| 138 | + |
| 139 | +int GlobusDirectory::Readdir(char *buff, int blen) { |
| 140 | + if (!m_opened) { |
| 141 | + return -EBADF; |
| 142 | + } |
| 143 | + |
| 144 | + if (m_stat_buf) { |
| 145 | + memset(m_stat_buf, '\0', sizeof(struct stat)); |
| 146 | + } |
| 147 | + |
| 148 | + // m_idx encodes the location inside the current directory. |
| 149 | + // - m_idx in [0, m_objInfo.size) means return a "file" from the object |
| 150 | + // list. |
| 151 | + // - m_idx == m_objInfo.size means return the first entry in the directories |
| 152 | + // list. |
| 153 | + // - m_idx in (m_directories.size, -1] means return an entry from the |
| 154 | + // directories list. |
| 155 | + // - m_idx == -m_directories.size means that all the path elements have been |
| 156 | + // consumed. |
| 157 | + auto idx = m_idx; |
| 158 | + if (m_objInfo.empty() && m_directories.empty()) { |
| 159 | + *buff = '\0'; |
| 160 | + return XrdOssOK; |
| 161 | + } else if (idx >= 0 && idx < static_cast<ssize_t>(m_objInfo.size())) { |
| 162 | + // Return a file entry |
| 163 | + m_idx++; |
| 164 | + std::string full_name = m_objInfo[idx].m_key; |
| 165 | + auto lastSlashIdx = full_name.rfind("/"); |
| 166 | + if (lastSlashIdx != std::string::npos) { |
| 167 | + full_name.erase(0, lastSlashIdx + 1); |
| 168 | + } |
| 169 | + |
| 170 | + strncpy(buff, full_name.c_str(), blen); |
| 171 | + if (buff[blen - 1] != '\0') { |
| 172 | + buff[blen - 1] = '\0'; |
| 173 | + return -ENOMEM; |
| 174 | + } |
| 175 | + |
| 176 | + if (m_stat_buf) { |
| 177 | + m_stat_buf->st_mode = 0x0600 | S_IFREG; |
| 178 | + m_stat_buf->st_nlink = 1; |
| 179 | + m_stat_buf->st_size = m_objInfo[idx].m_size; |
| 180 | + time_t timestamp = parseTimestamp(m_objInfo[idx].m_last_modified); |
| 181 | + if (timestamp != 0) { |
| 182 | + m_stat_buf->st_mtime = timestamp; |
| 183 | + m_stat_buf->st_atime = timestamp; |
| 184 | + m_stat_buf->st_ctime = timestamp; |
| 185 | + } |
| 186 | + } |
| 187 | + } else if (idx < 0 && -idx == static_cast<ssize_t>(m_directories.size())) { |
| 188 | + // All items have been consumed |
| 189 | + *buff = '\0'; |
| 190 | + return XrdOssOK; |
| 191 | + } else if (idx == static_cast<ssize_t>(m_objInfo.size()) || |
| 192 | + -idx < static_cast<ssize_t>(m_directories.size())) { |
| 193 | + // Handle directory entries |
| 194 | + if (m_directories.empty()) { |
| 195 | + *buff = '\0'; |
| 196 | + return XrdOssOK; |
| 197 | + } |
| 198 | + if (idx == static_cast<ssize_t>(m_objInfo.size())) { |
| 199 | + m_idx = -1; |
| 200 | + idx = 0; |
| 201 | + } else { |
| 202 | + idx = -m_idx; |
| 203 | + m_idx--; |
| 204 | + } |
| 205 | + std::string full_name = m_directories[idx].m_key; |
| 206 | + if (!full_name.empty() && full_name.back() == '/') { |
| 207 | + full_name.pop_back(); |
| 208 | + } |
| 209 | + |
| 210 | + strncpy(buff, full_name.c_str(), blen); |
| 211 | + if (buff[blen - 1] != '\0') { |
| 212 | + buff[blen - 1] = '\0'; |
| 213 | + return -ENOMEM; |
| 214 | + } |
| 215 | + |
| 216 | + if (m_stat_buf) { |
| 217 | + m_stat_buf->st_mode = 0x0700 | S_IFDIR; |
| 218 | + m_stat_buf->st_nlink = 2; |
| 219 | + m_stat_buf->st_size = 4096; |
| 220 | + time_t timestamp = |
| 221 | + parseTimestamp(m_directories[idx].m_last_modified); |
| 222 | + if (timestamp != 0) { |
| 223 | + m_stat_buf->st_mtime = timestamp; |
| 224 | + m_stat_buf->st_atime = timestamp; |
| 225 | + m_stat_buf->st_ctime = timestamp; |
| 226 | + } |
| 227 | + } |
| 228 | + } else { |
| 229 | + return -EBADF; |
| 230 | + } |
| 231 | + |
| 232 | + if (m_stat_buf) { |
| 233 | + m_stat_buf->st_uid = 1; |
| 234 | + m_stat_buf->st_gid = 1; |
| 235 | + if (m_stat_buf->st_mtime == 0) { |
| 236 | + m_stat_buf->st_mtime = m_stat_buf->st_ctime = m_stat_buf->st_atime = |
| 237 | + 0; |
| 238 | + } |
| 239 | + m_stat_buf->st_dev = 0; |
| 240 | + m_stat_buf->st_ino = 1; |
| 241 | + } |
| 242 | + return XrdOssOK; |
| 243 | +} |
| 244 | + |
| 245 | +int GlobusDirectory::StatRet(struct stat *buf) { |
| 246 | + if (!m_opened) { |
| 247 | + return -EBADF; |
| 248 | + } |
| 249 | + |
| 250 | + m_stat_buf = buf; |
| 251 | + return XrdOssOK; |
| 252 | +} |
| 253 | + |
| 254 | +int GlobusDirectory::Close(long long *retsz) { |
| 255 | + if (!m_opened) { |
| 256 | + return -EBADF; |
| 257 | + } |
| 258 | + Reset(); |
| 259 | + return XrdOssOK; |
| 260 | +} |
0 commit comments