Skip to content

Commit

Permalink
API CHANGE: StorageAPI now has GetParentPath function (#178)
Browse files Browse the repository at this point in the history
* API CHANGE: StorageAPI now has GetParentPath function
Added Longtail_GetParentPath that handles windows paths properly
Fixed up blockstorestorage to initialize api using Longtail_MakeStorageAPI()
* release notes
  • Loading branch information
DanEngelbrecht authored Jan 25, 2022
1 parent bacfad3 commit ca66bdf
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 51 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ jobs:
release_name: Release ${{ env.RELEASE_VERSION }}
body: |
# Changes in this Release
- **CHANGED API** JobAPI.CreateJobsFunc() now takes progress and cancel options. progress callback will only be called if it is same thread that made ReserveJobs
- **ADDED** Longtail_GetCurrentThreadId()
- **FIX** Smoother progress when indexing folders with many files
- **CHANGED API** Added `GetParentPath()` to `Storage_API`
- **FIX** EnsureParentPath now handles Windows path with backslashes properly
draft: false
prerelease: false
files: "*-x64.zip"
122 changes: 100 additions & 22 deletions lib/blockstorestorage/longtail_blockstorestorage.c
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,77 @@ static int BlockStoreStorageAPI_GetEntryProperties(
return 0;
}

static int BlockStoreStorageAPI_LockFile(struct Longtail_StorageAPI* storage_api, const char* path, Longtail_StorageAPI_HLockFile* out_lock_file)
{
MAKE_LOG_CONTEXT_FIELDS(ctx)
LONGTAIL_LOGFIELD(storage_api, "%p"),
LONGTAIL_LOGFIELD(path, "%s"),
LONGTAIL_LOGFIELD(out_lock_file, "%p")
MAKE_LOG_CONTEXT_WITH_FIELDS(ctx, 0, LONGTAIL_LOG_LEVEL_OFF)

LONGTAIL_VALIDATE_INPUT(ctx, storage_api != 0, return 0)
LONGTAIL_VALIDATE_INPUT(ctx, path != 0, return 0)
LONGTAIL_VALIDATE_INPUT(ctx, out_lock_file != 0, return 0)

LONGTAIL_LOG(ctx, LONGTAIL_LOG_LEVEL_ERROR, "Unsupported, failed with %d", ENOTSUP)
return ENOTSUP;
}

static int BlockStoreStorageAPI_UnlockFile(struct Longtail_StorageAPI* storage_api, Longtail_StorageAPI_HLockFile lock_file)
{
MAKE_LOG_CONTEXT_FIELDS(ctx)
LONGTAIL_LOGFIELD(storage_api, "%p"),
LONGTAIL_LOGFIELD(lock_file, "%p")
MAKE_LOG_CONTEXT_WITH_FIELDS(ctx, 0, LONGTAIL_LOG_LEVEL_OFF)

LONGTAIL_VALIDATE_INPUT(ctx, storage_api != 0, return 0)
LONGTAIL_VALIDATE_INPUT(ctx, lock_file != 0, return 0)

LONGTAIL_LOG(ctx, LONGTAIL_LOG_LEVEL_ERROR, "Unsupported, failed with %d", ENOTSUP)
return ENOTSUP;
}

static char* BlockStoreStorageAPI_GetParentPath(
struct Longtail_StorageAPI* storage_api,
const char* path)
{
MAKE_LOG_CONTEXT_FIELDS(ctx)
LONGTAIL_LOGFIELD(storage_api, "%p"),
LONGTAIL_LOGFIELD(path, "%s"),
MAKE_LOG_CONTEXT_WITH_FIELDS(ctx, 0, LONGTAIL_LOG_LEVEL_OFF)

LONGTAIL_VALIDATE_INPUT(ctx, storage_api != 0, return 0)
LONGTAIL_VALIDATE_INPUT(ctx, path != 0, return 0)

size_t delim_pos = 0;
size_t path_len = 0;
while (path[path_len] != 0)
{
if (path[path_len] == '/')
{
delim_pos = path_len;
}
++path_len;
}
if (path[delim_pos] != '/' || delim_pos == 0)
{
return 0;
}

char* result = (char*)Longtail_Alloc("BlockStoreStorageAPI_GetParentPath", delim_pos + 1);
if (!result)
{
LONGTAIL_LOG(ctx, LONGTAIL_LOG_LEVEL_ERROR, "Longtail_Alloc() failed with %d", ENOMEM)
return 0;
}
result[delim_pos] = 0;
while (delim_pos--)
{
result[delim_pos] = path[delim_pos];
}
return result;
}

static void BlockStoreStorageAPI_Dispose(struct Longtail_API* api)
{
struct BlockStoreStorageAPI* block_store_fs = (struct BlockStoreStorageAPI*)api;
Expand Down Expand Up @@ -1247,31 +1318,38 @@ static int BlockStoreStorageAPI_Init(
LONGTAIL_VALIDATE_INPUT(ctx, version_index != 0, return 0)
LONGTAIL_VALIDATE_INPUT(ctx, out_storage_api != 0, return 0)

struct BlockStoreStorageAPI* block_store_fs = (struct BlockStoreStorageAPI*)mem;
struct Longtail_StorageAPI* api = Longtail_MakeStorageAPI(
mem,
BlockStoreStorageAPI_Dispose,
BlockStoreStorageAPI_OpenReadFile,
BlockStoreStorageAPI_GetSize,
BlockStoreStorageAPI_Read,
BlockStoreStorageAPI_OpenWriteFile,
BlockStoreStorageAPI_Write,
BlockStoreStorageAPI_SetSize,
BlockStoreStorageAPI_SetPermissions,
BlockStoreStorageAPI_GetPermissions,
BlockStoreStorageAPI_CloseFile,
BlockStoreStorageAPI_CreateDir,
BlockStoreStorageAPI_RenameFile,
BlockStoreStorageAPI_ConcatPath,
BlockStoreStorageAPI_IsDir,
BlockStoreStorageAPI_IsFile,
BlockStoreStorageAPI_RemoveDir,
BlockStoreStorageAPI_RemoveFile,
BlockStoreStorageAPI_StartFind,
BlockStoreStorageAPI_FindNext,
BlockStoreStorageAPI_CloseFind,
BlockStoreStorageAPI_GetEntryProperties,
BlockStoreStorageAPI_LockFile,
BlockStoreStorageAPI_UnlockFile,
BlockStoreStorageAPI_GetParentPath);

struct BlockStoreStorageAPI* block_store_fs = (struct BlockStoreStorageAPI*)api;

uint64_t store_index_chunk_count = *store_index->m_ChunkCount;
uint32_t version_index_asset_count = *version_index->m_AssetCount;

block_store_fs->m_API.m_API.Dispose = BlockStoreStorageAPI_Dispose;
block_store_fs->m_API.OpenReadFile = BlockStoreStorageAPI_OpenReadFile;
block_store_fs->m_API.GetSize = BlockStoreStorageAPI_GetSize;
block_store_fs->m_API.Read = BlockStoreStorageAPI_Read;
block_store_fs->m_API.OpenWriteFile = BlockStoreStorageAPI_OpenWriteFile;
block_store_fs->m_API.Write = BlockStoreStorageAPI_Write;
block_store_fs->m_API.SetSize = BlockStoreStorageAPI_SetSize;
block_store_fs->m_API.SetPermissions = BlockStoreStorageAPI_SetPermissions;
block_store_fs->m_API.GetPermissions = BlockStoreStorageAPI_GetPermissions;
block_store_fs->m_API.CloseFile = BlockStoreStorageAPI_CloseFile;
block_store_fs->m_API.CreateDir = BlockStoreStorageAPI_CreateDir;
block_store_fs->m_API.RenameFile = BlockStoreStorageAPI_RenameFile;
block_store_fs->m_API.ConcatPath = BlockStoreStorageAPI_ConcatPath;
block_store_fs->m_API.IsDir = BlockStoreStorageAPI_IsDir;
block_store_fs->m_API.IsFile = BlockStoreStorageAPI_IsFile;
block_store_fs->m_API.RemoveDir = BlockStoreStorageAPI_RemoveDir;
block_store_fs->m_API.RemoveFile = BlockStoreStorageAPI_RemoveFile;
block_store_fs->m_API.StartFind = BlockStoreStorageAPI_StartFind;
block_store_fs->m_API.FindNext = BlockStoreStorageAPI_FindNext;
block_store_fs->m_API.CloseFind = BlockStoreStorageAPI_CloseFind;
block_store_fs->m_API.GetEntryProperties = BlockStoreStorageAPI_GetEntryProperties;
block_store_fs->m_HashAPI = hash_api;
block_store_fs->m_JobAPI = job_api;
block_store_fs->m_BlockStore = block_store;
Expand Down
18 changes: 17 additions & 1 deletion lib/filestorage/longtail_filestorage.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,21 @@ static int FSStorageAPI_UnlockFile(struct Longtail_StorageAPI* storage_api, Long
return 0;
}

static char* FSStorageAPI_GetParentPath(
struct Longtail_StorageAPI* storage_api,
const char* path)
{
MAKE_LOG_CONTEXT_FIELDS(ctx)
LONGTAIL_LOGFIELD(storage_api, "%p"),
LONGTAIL_LOGFIELD(path, "%s"),
MAKE_LOG_CONTEXT_WITH_FIELDS(ctx, 0, LONGTAIL_LOG_LEVEL_OFF)

LONGTAIL_VALIDATE_INPUT(ctx, storage_api != 0, return 0)
LONGTAIL_VALIDATE_INPUT(ctx, path != 0, return 0)

return Longtail_GetParentPath(path);
}

static int FSStorageAPI_Init(
void* mem,
struct Longtail_StorageAPI** out_storage_api)
Expand Down Expand Up @@ -601,7 +616,8 @@ static int FSStorageAPI_Init(
FSStorageAPI_CloseFind,
FSStorageAPI_GetEntryProperties,
FSStorageAPI_LockFile,
FSStorageAPI_UnlockFile);
FSStorageAPI_UnlockFile,
FSStorageAPI_GetParentPath);
*out_storage_api = api;
return 0;
}
Expand Down
72 changes: 72 additions & 0 deletions lib/longtail_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,42 @@ char* Longtail_ConcatPath(const char* folder, const char* file)
return path;
}

static int is_path_delimiter(char c)
{
return (c == '/') || (c == '\\');
}

char* Longtail_GetParentPath(const char* path)
{
size_t delim_pos = 0;
size_t path_len = 0;
while (path[path_len] != 0)
{
if (is_path_delimiter(path[path_len]))
{
delim_pos = path_len;
}
++path_len;
}
if ((!is_path_delimiter(path[delim_pos])) || delim_pos == 0)
{
return 0;
}

char* result = (char*)Longtail_Alloc("Longtail_GetParentPath", delim_pos + 1);
if (!result)
{
LONGTAIL_LOG(0, LONGTAIL_LOG_LEVEL_ERROR, "Longtail_Alloc() failed with %d", ENOMEM)
return 0;
}
result[delim_pos] = 0;
while (delim_pos--)
{
result[delim_pos] = path[delim_pos];
}
return result;
}

char* Longtail_GetTempFolder()
{
char tmp[MAX_PATH + 1];
Expand Down Expand Up @@ -1859,6 +1895,42 @@ char* Longtail_ConcatPath(const char* folder, const char* file)
return path;
}

static int is_path_delimiter(char c)
{
return (c == '/');
}

char* Longtail_GetParentPath(const char* path)
{
size_t delim_pos = 0;
size_t path_len = 0;
while (path[path_len] != 0)
{
if (is_path_delimiter(path[path_len]))
{
delim_pos = path_len;
}
++path_len;
}
if ((!is_path_delimiter(path[delim_pos])) || delim_pos == 0)
{
return 0;
}

char* result = (char*)Longtail_Alloc("Longtail_GetParentPath", delim_pos + 1);
if (!result)
{
LONGTAIL_LOG(0, LONGTAIL_LOG_LEVEL_ERROR, "Longtail_Alloc() failed with %d", ENOMEM)
return 0;
}
result[delim_pos] = 0;
while (delim_pos--)
{
result[delim_pos] = path[delim_pos];
}
return result;
}

char* Longtail_GetTempFolder()
{
return Longtail_Strdup("/tmp");
Expand Down
1 change: 1 addition & 0 deletions lib/longtail_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ int Longtail_Write(HLongtail_OpenFile handle, uint64_t offset, uint64_t leng
int Longtail_GetFileSize(HLongtail_OpenFile handle, uint64_t* out_size);
void Longtail_CloseFile(HLongtail_OpenFile handle);
char* Longtail_ConcatPath(const char* folder, const char* file);
char* Longtail_GetParentPath(const char* path);

char* Longtail_GetTempFolder();

Expand Down
44 changes: 43 additions & 1 deletion lib/memstorage/longtail_memstorage.c
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,47 @@ static int InMemStorageAPI_UnlockFile(struct Longtail_StorageAPI* storage_api, L
return 0;
}

static char* InMemStorageAPI_GetParentPath(
struct Longtail_StorageAPI* storage_api,
const char* path)
{
MAKE_LOG_CONTEXT_FIELDS(ctx)
LONGTAIL_LOGFIELD(storage_api, "%p"),
LONGTAIL_LOGFIELD(path, "%s"),
MAKE_LOG_CONTEXT_WITH_FIELDS(ctx, 0, LONGTAIL_LOG_LEVEL_OFF)

LONGTAIL_VALIDATE_INPUT(ctx, storage_api != 0, return 0)
LONGTAIL_VALIDATE_INPUT(ctx, path != 0, return 0)

size_t delim_pos = 0;
size_t path_len = 0;
while (path[path_len] != 0)
{
if (path[path_len] == '/')
{
delim_pos = path_len;
}
++path_len;
}
if (path[delim_pos] != '/' || delim_pos == 0)
{
return 0;
}

char* result = (char*)Longtail_Alloc("InMemStorageAPI_GetParentPath", delim_pos + 1);
if (!result)
{
LONGTAIL_LOG(ctx, LONGTAIL_LOG_LEVEL_ERROR, "Longtail_Alloc() failed with %d", ENOMEM)
return 0;
}
result[delim_pos] = 0;
while (delim_pos--)
{
result[delim_pos] = path[delim_pos];
}
return result;
}

static int InMemStorageAPI_Init(
void* mem,
struct Longtail_StorageAPI** out_storage_api)
Expand Down Expand Up @@ -1091,7 +1132,8 @@ static int InMemStorageAPI_Init(
InMemStorageAPI_CloseFind,
InMemStorageAPI_GetEntryProperties,
InMemStorageAPI_LockFile,
InMemStorageAPI_UnlockFile);
InMemStorageAPI_UnlockFile,
InMemStorageAPI_GetParentPath);

struct InMemStorageAPI* storage_api = (struct InMemStorageAPI*)api;

Expand Down
Loading

0 comments on commit ca66bdf

Please sign in to comment.