rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
fm_ops.c File Reference

The file manager's jobs, one step at a time (fm_ops.h). More...

#include "fm_ops.h"
#include <string.h>
#include "log_name.h"

Enumerations

enum  {
  STEP_CHECK = 0 , STEP_DATA , STEP_SYNC , STEP_UNLINK ,
  STEP_RENAME
}
 The steps of a job, in the order they run. A job without data does all of its work in STEP_CHECK. More...

Functions

static fm_err_t refuse (fm_job_t *job, fm_err_t err)
 Hold a refusal in the job, so fm_job_state() says it too.
static void begin (fm_job_t *job, fm_job_kind_t kind, unsigned mount)
 Clear job and name what it does.
static fm_err_t run (fm_job_t *job)
 Let a checked job run.
static fm_err_t join (char *out, const char *dir, const char *name, bool is_dir)
 The path of name in folder dir (fm_path_join()).
static fm_err_t tmp_path (char *out, const char *dir, const char *name)
 Write the path of name's ".NEW" in dir into out.
fm_err_t fm_job_copy (fm_job_t *job, const fm_bench_t *bench, unsigned mount, const char *src_dir, const char *name, const char *dst_dir)
 Copy file name of folder src_dir into folder dst_dir under the same name.
static fm_err_t target_path (fm_job_t *job, const fm_bench_t *bench, const char *dir, const char *name, bool is_dir)
 The checks MOVE and RENAME share on the target: not a LOCKED or FAULT name, and a path that fits.
fm_err_t fm_job_move (fm_job_t *job, const fm_bench_t *bench, unsigned mount, const char *src_dir, const char *name, bool is_dir, const char *dst_dir)
 Move file or empty folder name of src_dir into dst_dir.
fm_err_t fm_job_rename (fm_job_t *job, const fm_bench_t *bench, unsigned mount, const char *dir, const char *name, bool is_dir, const char *new_name)
 Give entry name of folder dir the name new_name.
fm_err_t fm_job_delete (fm_job_t *job, const fm_bench_t *bench, unsigned mount, const char *dir, const char *name, bool is_dir)
 Delete file or empty folder name of folder dir.
static fm_err_t start_new (fm_job_t *job, const fm_bench_t *bench, const char *dir, const char *name)
 The checks NEW's two jobs share.
fm_err_t fm_job_mkdir (fm_job_t *job, const fm_bench_t *bench, unsigned mount, const char *dir, const char *name)
 Make folder name in folder dir.
fm_err_t fm_job_create (fm_job_t *job, const fm_bench_t *bench, unsigned mount, const char *dir, const char *name)
 Make the empty file name in folder dir.
fm_err_t fm_job_save (fm_job_t *job, const fm_bench_t *bench, unsigned mount, const char *dir, const char *name, uint64_t len, fm_fill_fn fill, void *fill_ctx)
 Write len bytes from fill as file name of folder dir, through "<name>.NEW" (the file's comment).
fm_err_t fm_job_load (fm_job_t *job, const fm_bench_t *bench, unsigned mount, const char *dir, const char *name, uint8_t *buf, size_t cap)
 Read file name of folder dir into buf. A read: the gate does not stop it.
static fm_job_state_t finish (fm_job_t *job, const fm_io_t *io, fm_job_state_t state, fm_err_t err, bool drop)
 End the job: close what is open and, when drop, remove the ".NEW" it wrote.
static fm_err_t free_name (const fm_io_t *io, const char *path)
 Whether path is free for a new entry.
static fm_err_t there (const fm_io_t *io, const char *path, bool is_dir, fm_stat_t *st)
 Whether path is there as the kind the job expects.
static void count_entry (const fm_dirent_t *e, void *vctx)
 Count one entry of a folder that has to be empty.
static fm_err_t empty_dir (const fm_io_t *io, const char *path)
 Whether folder path holds nothing, those names included that the walk cannot list.
static fm_err_t single (fm_job_t *job, const fm_io_t *io)
 The single calls of MOVE, RENAME, DELETE, MKDIR and CREATE, with their checks.
static fm_err_t open_files (fm_job_t *job, const fm_io_t *io)
 The first step of a job with data: the names checked and the files opened.
static fm_err_t one_chunk (fm_job_t *job, const fm_io_t *io, uint8_t *chunk, size_t size)
 One chunk of a copy, a save or a load.
static fm_op_t op_of (fm_job_kind_t kind)
 The operation the rules are asked about for a job's kind.
static fm_err_t still_allowed (const fm_job_t *job, const fm_bench_t *bench)
 Whether the rules let the job go on as the bench stands now: fm_allowed() of the source of a copy, a load, a move, a rename or a delete, of the file a save writes, or of the folder NEW makes something in.
static fm_job_state_t data_step (fm_job_t *job, const fm_io_t *io, const fm_bench_t *bench, uint8_t *chunk, size_t size)
 One step of a copy, a save or a load.
fm_job_state_t fm_job_step (fm_job_t *job, const fm_io_t *io, const fm_bench_t *bench, uint8_t *chunk, size_t size)
 Run the job's next step.
void fm_job_cancel (fm_job_t *job)
 Ask the job to stop at its next chunk. A single call that has started finishes.
fm_job_state_t fm_job_state (const fm_job_t *job, uint64_t *done, uint64_t *total)
 Where the job stands.

Detailed Description

The file manager's jobs, one step at a time (fm_ops.h).

SPDX-License-Identifier: MIT

Enumeration Type Documentation

◆ anonymous enum

anonymous enum

The steps of a job, in the order they run. A job without data does all of its work in STEP_CHECK.

Enumerator
STEP_CHECK 

the card's answers on the names; the opens

STEP_DATA 

one chunk

STEP_SYNC 

the ".NEW" on the card, and closed

STEP_UNLINK 

a save's old file deleted

STEP_RENAME 

the ".NEW" given its name

Function Documentation

◆ begin()

void begin ( fm_job_t * job,
fm_job_kind_t kind,
unsigned mount )
static

Clear job and name what it does.

Parameters
jobthe job
kindwhat it does
mountthe mount its names are from

◆ count_entry()

void count_entry ( const fm_dirent_t * e,
void * vctx )
static

Count one entry of a folder that has to be empty.

Parameters
ethe entry
vctxan unsigned count

◆ data_step()

fm_job_state_t data_step ( fm_job_t * job,
const fm_io_t * io,
const fm_bench_t * bench,
uint8_t * chunk,
size_t size )
static

One step of a copy, a save or a load.

Parameters
jobthe job
iothe card, held
benchthe gate as it stands now
chunkthe chunk's room
sizeits bytes
Returns
the job's state after the step

◆ empty_dir()

fm_err_t empty_dir ( const fm_io_t * io,
const char * path )
static

Whether folder path holds nothing, those names included that the walk cannot list.

Parameters
iothe card, held
paththe folder
Returns
FM_OK; FM_ERR_NOT_EMPTY; the card's failure

◆ finish()

fm_job_state_t finish ( fm_job_t * job,
const fm_io_t * io,
fm_job_state_t state,
fm_err_t err,
bool drop )
static

End the job: close what is open and, when drop, remove the ".NEW" it wrote.

Parameters
jobthe job
iothe card, held
stateFM_JOB_DONE, FM_JOB_STOPPED or FM_JOB_FAILED
errwhy, FM_OK for DONE
dropremove job->tmp
Returns
state

◆ fm_job_cancel()

void fm_job_cancel ( fm_job_t * job)

Ask the job to stop at its next chunk. A single call that has started finishes.

Parameters
jobthe job

◆ fm_job_copy()

fm_err_t fm_job_copy ( fm_job_t * job,
const fm_bench_t * bench,
unsigned mount,
const char * src_dir,
const char * name,
const char * dst_dir )

Copy file name of folder src_dir into folder dst_dir under the same name.

Parameters
jobthe job
benchthe gate and the rules; may be NULL
mountthe mount the names were listed from
src_dirthe source's folder
namethe file's name
dst_dirthe target folder
Returns
FM_OK when the job runs; else the refusal, which the job holds as FM_JOB_FAILED: FM_ERR_ARG, FM_ERR_ARMED, FM_ERR_LOGGING, FM_ERR_BUSY, FM_ERR_LOCKED, FM_ERR_FIXED, FM_ERR_SAME for one folder, FM_ERR_NAME

◆ fm_job_create()

fm_err_t fm_job_create ( fm_job_t * job,
const fm_bench_t * bench,
unsigned mount,
const char * dir,
const char * name )

Make the empty file name in folder dir.

Parameters
jobthe job
benchthe gate and the rules; may be NULL
mountthe mount the folder was listed from
dirthe folder to make it in
namethe new file's name
Returns
FM_OK, or the refusal

◆ fm_job_delete()

fm_err_t fm_job_delete ( fm_job_t * job,
const fm_bench_t * bench,
unsigned mount,
const char * dir,
const char * name,
bool is_dir )

Delete file or empty folder name of folder dir.

Parameters
jobthe job
benchthe gate and the rules; may be NULL
mountthe mount the names were listed from
dirthe folder
namethe entry's name
is_dirthe entry is a folder
Returns
FM_OK, or the refusal

◆ fm_job_load()

fm_err_t fm_job_load ( fm_job_t * job,
const fm_bench_t * bench,
unsigned mount,
const char * dir,
const char * name,
uint8_t * buf,
size_t cap )

Read file name of folder dir into buf. A read: the gate does not stop it.

Parameters
jobthe job
benchthe rules; may be NULL
mountthe mount the file was listed from
dirthe file's folder
namethe file's name
buftakes the bytes
caproom in buf; a larger file is FM_ERR_TOO_BIG
Returns
FM_OK, or the refusal

◆ fm_job_mkdir()

fm_err_t fm_job_mkdir ( fm_job_t * job,
const fm_bench_t * bench,
unsigned mount,
const char * dir,
const char * name )

Make folder name in folder dir.

Parameters
jobthe job
benchthe gate and the rules; may be NULL
mountthe mount the folder was listed from
dirthe folder to make it in
namethe new folder's name
Returns
FM_OK, or the refusal

◆ fm_job_move()

fm_err_t fm_job_move ( fm_job_t * job,
const fm_bench_t * bench,
unsigned mount,
const char * src_dir,
const char * name,
bool is_dir,
const char * dst_dir )

Move file or empty folder name of src_dir into dst_dir.

Parameters
jobthe job
benchthe gate and the rules; may be NULL
mountthe mount the names were listed from
src_dirthe source's folder
namethe entry's name
is_dirthe entry is a folder
dst_dirthe target folder
Returns
FM_OK, or the refusal as fm_job_copy(); FM_ERR_SAME also for a folder moved into itself

◆ fm_job_rename()

fm_err_t fm_job_rename ( fm_job_t * job,
const fm_bench_t * bench,
unsigned mount,
const char * dir,
const char * name,
bool is_dir,
const char * new_name )

Give entry name of folder dir the name new_name.

A change of case alone is a rename of one entry, not an overwrite.

Parameters
jobthe job
benchthe gate and the rules; may be NULL
mountthe mount the names were listed from
dirthe folder
namethe entry's name now
is_dirthe entry is a folder
new_nameits new name
Returns
FM_OK, or the refusal; FM_ERR_ARG for the same name

◆ fm_job_save()

fm_err_t fm_job_save ( fm_job_t * job,
const fm_bench_t * bench,
unsigned mount,
const char * dir,
const char * name,
uint64_t len,
fm_fill_fn fill,
void * fill_ctx )

Write len bytes from fill as file name of folder dir, through "<name>.NEW" (the file's comment).

Parameters
jobthe job
benchthe gate and the rules; may be NULL
mountthe mount the file was read from
dirthe file's folder
namethe file's name; it need not be there yet
lenthe text's bytes
fillhands over the bytes
fill_ctxpassed to fill
Returns
FM_OK, or the refusal

◆ fm_job_state()

fm_job_state_t fm_job_state ( const fm_job_t * job,
uint64_t * done,
uint64_t * total )

Where the job stands.

Parameters
jobthe job; NULL is FM_JOB_IDLE
donetakes the bytes done; may be NULL
totaltakes the bytes to do; may be NULL
Returns
its state

◆ fm_job_step()

fm_job_state_t fm_job_step ( fm_job_t * job,
const fm_io_t * io,
const fm_bench_t * bench,
uint8_t * chunk,
size_t size )

Run the job's next step.

Parameters
jobthe job
iothe card
benchthe gate as it stands now; may be NULL
chunkroom for one chunk of a copy, a save or a load
sizebytes chunk takes; FM_CHUNK on the panel
Returns
the job's state after the step

◆ free_name()

fm_err_t free_name ( const fm_io_t * io,
const char * path )
static

Whether path is free for a new entry.

Parameters
iothe card, held
paththe path
Returns
FM_OK for no such name; FM_ERR_EXISTS; or the card's failure

◆ join()

fm_err_t join ( char * out,
const char * dir,
const char * name,
bool is_dir )
static

The path of name in folder dir (fm_path_join()).

Parameters
outroom for FM_PATH_SIZE bytes
dirthe folder
namethe entry's name
is_dirthe entry is a folder: it counts in the depth
Returns
FM_OK, or FM_ERR_NAME for a path too long or too deep

◆ one_chunk()

fm_err_t one_chunk ( fm_job_t * job,
const fm_io_t * io,
uint8_t * chunk,
size_t size )
static

One chunk of a copy, a save or a load.

Parameters
jobthe job
iothe card, held
chunkthe chunk's room
sizeits bytes
Returns
FM_OK, with job->step moved on at the data's end; or why not

◆ op_of()

fm_op_t op_of ( fm_job_kind_t kind)
static

The operation the rules are asked about for a job's kind.

Parameters
kindthe job's kind
Returns
its fm_op_t

◆ open_files()

fm_err_t open_files ( fm_job_t * job,
const fm_io_t * io )
static

The first step of a job with data: the names checked and the files opened.

Parameters
jobthe job
iothe card, held
Returns
FM_OK or why not

◆ refuse()

fm_err_t refuse ( fm_job_t * job,
fm_err_t err )
static

Hold a refusal in the job, so fm_job_state() says it too.

Parameters
jobthe job
errthe refusal
Returns
err

◆ run()

fm_err_t run ( fm_job_t * job)
static

Let a checked job run.

Parameters
jobthe job
Returns
FM_OK

◆ single()

fm_err_t single ( fm_job_t * job,
const fm_io_t * io )
static

The single calls of MOVE, RENAME, DELETE, MKDIR and CREATE, with their checks.

Parameters
jobthe job
iothe card, held
Returns
FM_OK or why not

◆ start_new()

fm_err_t start_new ( fm_job_t * job,
const fm_bench_t * bench,
const char * dir,
const char * name )
static

The checks NEW's two jobs share.

Parameters
jobthe job, which takes the new path
benchthe gate and the rules; may be NULL
dirthe folder to make it in
namethe new name
Returns
FM_OK or the refusal, which the job holds

◆ still_allowed()

fm_err_t still_allowed ( const fm_job_t * job,
const fm_bench_t * bench )
static

Whether the rules let the job go on as the bench stands now: fm_allowed() of the source of a copy, a load, a move, a rename or a delete, of the file a save writes, or of the folder NEW makes something in.

Parameters
jobthe job
benchthe bench now
Returns
FM_OK or the refusal

◆ target_path()

fm_err_t target_path ( fm_job_t * job,
const fm_bench_t * bench,
const char * dir,
const char * name,
bool is_dir )
static

The checks MOVE and RENAME share on the target: not a LOCKED or FAULT name, and a path that fits.

Parameters
jobthe job, which takes the target's path
benchthe rules; may be NULL
dirthe target folder
namethe target name
is_dirthe entry is a folder
Returns
FM_OK or the refusal

◆ there()

fm_err_t there ( const fm_io_t * io,
const char * path,
bool is_dir,
fm_stat_t * st )
static

Whether path is there as the kind the job expects.

Parameters
iothe card, held
paththe path
is_dira folder is expected
sttakes the answer
Returns
FM_OK; FM_ERR_ARG for the other kind; the card's answer

◆ tmp_path()

fm_err_t tmp_path ( char * out,
const char * dir,
const char * name )
static

Write the path of name's ".NEW" in dir into out.

Parameters
outroom for FM_PATH_SIZE bytes
dirthe folder
namethe file's name
Returns
FM_OK, or FM_ERR_NAME when it does not fit