hark130 / sketchy-idea

Reusable, stand-alone, Linux code
GNU General Public License v3.0
0 stars 0 forks source link

13-6: Directory operations #4

Open hark130 opened 5 months ago

hark130 commented 5 months ago
  1. Create a skid_dir_operations.h
  2. Create a skid_dir_operations.c
  3. Instantiate a build system (if it doesn't already exist)
  4. Write a build rule
  5. [OPTIONAL] Write some tests

Add functions for these objectives:

Identify files in a directory matching specific criteria
Descend into a directory tree recursively, performing an operation on all files
Create a directory
Remove a directory
Read the contents of a directory
hark130 commented 3 months ago

I suspect I'll also need a skid_file_operations library as well. Odd, it hasn't popped up in the JQR yet.

int create_file(const char *filename, const char *contents);
int delete_file(const char *filename);
int empty_file(const char *filename);
hark130 commented 3 months ago

Seems like I'll be doing a lot of memory allocation so I likely need a skid_memory library as well.

/*
 *  Description:
 *      Allocate zeroized array in heap memory.
 *
 *  Args:
 *      num_elem: The number of elements in the array.
 *      size_elem: The size of each element in the array.
 *      errnum: [Out] Storage location for errno values encountered.
 *
 *  Returns:
 *      Heap-allocated memory of total size num_elem * size_elem that has been zeroized, on success.
 *      Caller is responsible for freeing the return value with free_skid_mem().
 *      NULL on error (check errnum for details).
 */
void *alloc_skid_mem(size_t num_elem, size_t size_elem, int *errnum);

/*
 *  Description:
 *      Free skid-allocated heap memory and set the original pointer to NULL.
 *
 *  Args:
 *      old_mem: Pointer to the heap-allocated memory's storage location.
 *
 *  Returns:
 *      0 on success, errno on error.
 */
int free_skid_mem(void **old_mem);
hark130 commented 3 months ago

Descend into a directory tree recursively, performing an operation on all files

Likely, the best way to approach this is to utilize nftw(), documented here, to recursively operate on files but that function pointer is a bit unwieldy for users. Maybe, write some common wrappers: "print" wrappers around skid_file_metadata_read functions, set_*_now() skid_file_metadata_write wrappers, future functions from skid_file_operations (e.g., clear, delete), etc.

hark130 commented 3 months ago

Identify files in a directory matching specific criteria

Initial research turned up a few different ways to go about doing this: glob(3), roll-your-own skid_dir_operations's read_dir_contents() manual parsing, something-something-<regex.h>.

hark130 commented 2 months ago

Just leaving this here...

TO DO LIST (in order?)

[ ] Write devops code to instantiate a hierarchy
[ ] Write destroy_dir() unit tests
    [ ] Design unit tests
    [ ] Design test framework
    [ ] Write unit tests
[ ] Define destroy_dir()
[ ] Write read_dir_contents() unit tests
[ ] Define read_dir_contents()
[ ] Write read_dir_contents() manual test
[ ] Update 13-6_export.sh w/ manual test code
[ ] Stub-out nftw()-wrapper (and public feature wrappers)
[ ] Write nftw()-wrapper unit tests (core & features)
    [ ] Design unit tests
    [ ] Design test framework
    [ ] Write unit tests
[ ] Define the nftw()-wrapper
[ ] Implement feature wrappers
    [ ] Local typeflag helper func(s?) (is_file(), is_dir())
    [ ] Local fn()-compliant funcs
    [ ] Connect local fn() funcs to nftw()-wrapper in public funcs
[ ] Write uftw()-wrapper man test (print path details)
[ ] Update 13-6_export.sh w/ manual test code
[ ] Focus on final "specific criteria" requirement
hark130 commented 2 months ago

Just leaving this here. Let's fail faster...

make -C code dist/devops_code.o && make -C code dist/test_devops_code_create_path_tree.bin && echo; ./code/dist/test_devops_code_create_path_tree.bin "/tmp/test_devops_code-c_p_t4-$(date +"%Y%m%d%H%M%S")" 1 1 1 && echo; ls -Rl ls -d /tmp/test_devops_code-c_p_t_4*/ | tail -n 1