paraslash Paraslash Audio Streaming
About   News   Download   Documentation   Development

Macros | Enumerations | Functions | Variables
aft.c File Reference

Audio file table functions. More...

#include <regex.h>
#include <sys/mman.h>
#include <fnmatch.h>
#include <sys/shm.h>
#include <dirent.h>
#include <osl.h>
#include <lopsub.h>
#include "server_cmd.lsg.h"
#include "para.h"
#include "error.h"
#include "crypt.h"
#include "string.h"
#include "afh.h"
#include "afs.h"
#include "fd.h"
#include "ipc.h"
#include "portable_io.h"
#include "sideband.h"
#include "command.h"

Macros

#define GET_NUM_DIGITS(x, num)
 Compute the number of (decimal) digits of a number. More...
 

Enumerations

enum  ls_sorting_method {
  LS_SORT_BY_PATH, LS_SORT_BY_SCORE, LS_SORT_BY_LAST_PLAYED, LS_SORT_BY_NUM_PLAYED,
  LS_SORT_BY_FREQUENCY, LS_SORT_BY_CHANNELS, LS_SORT_BY_IMAGE_ID, LS_SORT_BY_LYRICS_ID,
  LS_SORT_BY_BITRATE, LS_SORT_BY_DURATION, LS_SORT_BY_AUDIO_FORMAT, LS_SORT_BY_HASH
}
 The different sorting methods of the ls command. More...
 
enum  ls_listing_mode {
  LS_MODE_SHORT, LS_MODE_LONG, LS_MODE_VERBOSE, LS_MODE_MBOX,
  LS_MODE_CHUNKS, LS_MODE_PARSER
}
 The different listing modes of the ls command. More...
 
enum  afsi_offsets {
  AFSI_LAST_PLAYED_OFFSET = 0, AFSI_ATTRIBUTES_OFFSET = 8, AFSI_NUM_PLAYED_OFFSET = 16, AFSI_IMAGE_ID_OFFSET = 20,
  AFSI_LYRICS_ID_OFFSET = 24, AFSI_AUDIO_FORMAT_ID_OFFSET = 28, AFSI_AMP_OFFSET = 29, AFSI_AUDIO_FORMAT_UNUSED_OFFSET = 30,
  AFSI_SIZE = 32
}
 Describes the layout of the mmapped-afs info struct. More...
 
enum  audio_file_table_columns {
  AFTCOL_HASH, AFTCOL_PATH, AFTCOL_AFSI, AFTCOL_AFHI,
  AFTCOL_CHUNKS, NUM_AFT_COLUMNS, AFTCOL_HASH, AFTCOL_PATH,
  AFTCOL_AFSI, AFTCOL_AFHI, AFTCOL_CHUNKS, NUM_AFT_COLUMNS
}
 The columns of the audio file table. More...
 
enum  afhi_offsets {
  AFHI_SECONDS_TOTAL_OFFSET = 0, AFHI_BITRATE_OFFSET = 4, AFHI_FREQUENCY_OFFSET = 8, AFHI_UNUSED1_OFFSET = 12,
  AFHI_HEADER_LEN_OFFSET = 16, CHUNKS_TOTAL_OFFSET = 20, HEADER_LEN_OFFSET = 24, AFHI_MAX_CHUNK_SIZE_OFFSET = 28,
  CHUNK_TV_TV_SEC_OFFSET = 32, CHUNK_TV_TV_USEC_OFFSET = 36, AFHI_CHANNELS_OFFSET = 40, AFHI_INFO_STRING_OFFSET = 41,
  MIN_AFHI_SIZE = 47
}
 The on-disk layout of a afhi struct. More...
 
enum  com_add_buffer_offsets {
  CAB_AFHI_OFFSET_POS = 0, CAB_CHUNKS_OFFSET_POS = 4, CAB_LPR_OFFSET = 8, CAB_AUDIO_FORMAT_ID_OFFSET = 12,
  CAB_HASH_OFFSET = 13, CAB_PATH_OFFSET = (CAB_HASH_OFFSET + 32 )
}
 The format of the data stored by save_audio_file_data(). More...
 
enum  touch_flags { TOUCH_FLAG_FNM_PATHNAME = 1, TOUCH_FLAG_VERBOSE = 2 }
 Flags used by the touch command. More...
 

Functions

int aft_get_row_of_path (const char *path, struct osl_row **row)
 Get the row of the audio file table corresponding to the given path. More...
 
int get_afsi_of_row (const struct osl_row *row, struct afs_info *afsi)
 Get the audio file selector info, given a row of the audio file table. More...
 
int get_audio_file_path_of_row (const struct osl_row *row, char **path)
 Get the path of an audio file, given a row of the audio file table. More...
 
int get_afhi_of_row (const struct osl_row *row, struct afh_info *afhi)
 Get the audio format handler info, given a row of the audio file table. More...
 
int load_afd (int shmid, struct audio_file_data *afd)
 Extract a afd stored in a shared memory area. More...
 
void free_status_items (void)
 Deallocate and invalidate the status item strings. More...
 
int open_and_update_audio_file (int *fd)
 Open the audio file with highest score and set up an afd structure. More...
 
int audio_file_loop (void *private_data, osl_rbtree_loop_func *func)
 Call the given function for each file in the audio file table. More...
 
int send_afs_status (struct command_context *cc, int parser_friendly)
 Get the current afs status items from the afs process and send it. More...
 
int aft_check_callback (struct afs_callback_arg *aca)
 Check the audio file table for inconsistencies. More...
 
int aft_check_attributes (uint64_t att_mask, struct para_buffer *pb)
 Iterate over all audio files and check the attribute bit mask. More...
 

Variables

const struct server_cmd_user_data lsg_server_cmd_com_ls_user_data = { .handler = com_ls }
 
const struct server_cmd_user_data lsg_server_cmd_com_add_user_data = { .handler = com_add }
 
const struct server_cmd_user_data lsg_server_cmd_com_touch_user_data = { .handler = com_touch }
 
const struct server_cmd_user_data lsg_server_cmd_com_rm_user_data = { .handler = com_rm }
 
const struct server_cmd_user_data lsg_server_cmd_com_cpsi_user_data = { .handler = com_cpsi }
 
const struct server_cmd_user_data lsg_server_cmd_com_setatt_user_data = { .handler = com_setatt }
 
const struct afs_table_operations aft_ops
 The audio file table contains information about known audio files. More...
 

Detailed Description

Audio file table functions.

Macro Definition Documentation

◆ GET_NUM_DIGITS

#define GET_NUM_DIGITS (   x,
  num 
)
Value:
{ \
typeof((x)) _tmp = PARA_ABS(x); \
*num = 1; \
if ((_tmp)) \
while ((_tmp) > 9) { \
(_tmp) /= 10; \
(*num)++; \
} \
}

Compute the number of (decimal) digits of a number.

Enumeration Type Documentation

◆ ls_sorting_method

The different sorting methods of the ls command.

Enumerator
LS_SORT_BY_PATH 

-s=p (default)

LS_SORT_BY_SCORE 

-s=s

LS_SORT_BY_LAST_PLAYED 

-s=l

LS_SORT_BY_NUM_PLAYED 

-s=n

LS_SORT_BY_FREQUENCY 

-s=f

LS_SORT_BY_CHANNELS 

-s=c

LS_SORT_BY_IMAGE_ID 

-s=i

LS_SORT_BY_LYRICS_ID 

-s=y

LS_SORT_BY_BITRATE 

-s=b

LS_SORT_BY_DURATION 

-s=d

LS_SORT_BY_AUDIO_FORMAT 

-s=a

LS_SORT_BY_HASH 

-s=h

◆ ls_listing_mode

The different listing modes of the ls command.

Enumerator
LS_MODE_SHORT 

Default listing mode.

LS_MODE_LONG 

-l or -l=l

LS_MODE_VERBOSE 
LS_MODE_MBOX 

-l=v

LS_MODE_CHUNKS 

-l=m

LS_MODE_PARSER 

-l=c

◆ afsi_offsets

Describes the layout of the mmapped-afs info struct.

See also
struct afs_info.
Enumerator
AFSI_LAST_PLAYED_OFFSET 

Where .last_played is stored.

AFSI_ATTRIBUTES_OFFSET 

Storage position of the attributes bitmap.

AFSI_NUM_PLAYED_OFFSET 

Storage position of the .num_played field.

AFSI_IMAGE_ID_OFFSET 

Storage position of the .image_id field.

AFSI_LYRICS_ID_OFFSET 

Storage position of the .lyrics_id field.

AFSI_AUDIO_FORMAT_ID_OFFSET 

Storage position of the .audio_format_id field.

AFSI_AMP_OFFSET 

Storage position of the amplification field.

AFSI_AUDIO_FORMAT_UNUSED_OFFSET 

2 bytes reserved space for future usage.

AFSI_SIZE 

On-disk storage space needed.

◆ audio_file_table_columns

The columns of the audio file table.

Enumerator
AFTCOL_HASH 

The hash on the content of the audio file.

AFTCOL_PATH 

The full path in the filesystem.

AFTCOL_AFSI 

The audio file selector info.

AFTCOL_AFHI 

The audio format handler info.

AFTCOL_CHUNKS 

The chunk table info and the chunk table of the audio file.

NUM_AFT_COLUMNS 

The number of columns of this table.

AFTCOL_HASH 

The hash on the content of the audio file.

AFTCOL_PATH 

The full path in the filesystem.

AFTCOL_AFSI 

The audio file selector info.

AFTCOL_AFHI 

The audio format handler info.

AFTCOL_CHUNKS 

The chunk table info and the chunk table of the audio file.

NUM_AFT_COLUMNS 

The number of columns of this table.

◆ afhi_offsets

The on-disk layout of a afhi struct.

Enumerator
AFHI_SECONDS_TOTAL_OFFSET 

Where the number of seconds is stored.

AFHI_BITRATE_OFFSET 

Position of the bitrate.

AFHI_FREQUENCY_OFFSET 

Position of the frequency.

AFHI_UNUSED1_OFFSET 

Was: Location of the audio file header.

AFHI_HEADER_LEN_OFFSET 
CHUNKS_TOTAL_OFFSET 

The total number of chunks (4 bytes).

HEADER_LEN_OFFSET 

The length of the audio file header (4 bytes).

AFHI_MAX_CHUNK_SIZE_OFFSET 

Size of the largest chunk in bytes.

(4 bytes).

CHUNK_TV_TV_SEC_OFFSET 

The seconds part of the chunk time (4 bytes).

CHUNK_TV_TV_USEC_OFFSET 

The microseconds part of the chunk time (4 bytes).

AFHI_CHANNELS_OFFSET 

Number of channels is stored here.

(1 byte)

AFHI_INFO_STRING_OFFSET 

The tag info position.

MIN_AFHI_SIZE 

Minimal on-disk size of a valid afhi struct.

◆ com_add_buffer_offsets

The format of the data stored by save_audio_file_data().

Enumerator
CAB_AFHI_OFFSET_POS 
CAB_CHUNKS_OFFSET_POS 

Start of the chunk table (if present).

CAB_LPR_OFFSET 

Start of the (serialized) lopsub parse result.

CAB_AUDIO_FORMAT_ID_OFFSET 

Audio format id.

CAB_HASH_OFFSET 

The hash of the audio file being added.

CAB_PATH_OFFSET 

Start of the path of the audio file.

◆ touch_flags

Flags used by the touch command.

Enumerator
TOUCH_FLAG_FNM_PATHNAME 

Whether the FNM_PATHNAME flag should be passed to fnmatch().

TOUCH_FLAG_VERBOSE 

Activates verbose mode.

Function Documentation

◆ aft_get_row_of_path()

int aft_get_row_of_path ( const char *  path,
struct osl_row **  row 
)

Get the row of the audio file table corresponding to the given path.

Parameters
pathThe full path of the audio file.
rowResult pointer.
Returns
Standard.

References osl().

◆ get_afsi_of_row()

int get_afsi_of_row ( const struct osl_row *  row,
struct afs_info afsi 
)

Get the audio file selector info, given a row of the audio file table.

Parameters
rowPointer to a row in the audio file table.
afsiResult pointer.
Returns
Positive on success, negative on errors.

◆ get_audio_file_path_of_row()

int get_audio_file_path_of_row ( const struct osl_row *  row,
char **  path 
)

Get the path of an audio file, given a row of the audio file table.

Parameters
rowPointer to a row in the audio file table.
pathResult pointer.

The result is a pointer to memory-mapped data. The caller must not attempt to free it.

Returns
Standard.

References osl().

Referenced by mp_path().

◆ get_afhi_of_row()

int get_afhi_of_row ( const struct osl_row *  row,
struct afh_info afhi 
)

Get the audio format handler info, given a row of the audio file table.

Parameters
rowPointer to a row of the audio file table.
afhiResult pointer.
Returns
The return value of the underlying call to osl_get_object().

After the call the members of the afhi structure point to mapped memory which is owned by the osl table, Hence the caller must not attempt to free this memory by calling clear_afhi().

References osl().

◆ load_afd()

int load_afd ( int  shmid,
struct audio_file_data afd 
)

Extract a afd stored in a shared memory area.

Attach the shared memory area given by shmid, load the audio file data stored therein and detach the area afterwards. Called by vss, after receiving a positive response to the request for the next audio file.

  • Parameters
    shmidThe identifier of the shared memory area containing the afd.
    afdResult pointer.
    Returns
    Standard.

References ATTACH_RO, shm_attach(), and shm_size().

◆ free_status_items()

void free_status_items ( void  )

Deallocate and invalidate the status item strings.

This needs to be a public function so that afs.c can call it on shutdown.

References freep().

◆ open_and_update_audio_file()

int open_and_update_audio_file ( int *  fd)

Open the audio file with highest score and set up an afd structure.

This determines and opens the next audio file, verifies that it did not change by comparing the recomputed the hash value of the file contents against the value stored in the audio file table. If all goes well, it creates a shared memory area containing the serialized version of the afd structure, including the chunk table, if any. The caller can then send the ID of this area and the open fd to the server process.

Parameters
fdResult pointer for the file descriptor of the audio file.

On success, the numplayed field of the audio file selector info is increased and the lastplayed time is set to the current time. Finally, the score of the audio file is updated.

Returns
Positive shmid on success, negative on errors.

References HASH2_SIZE.

◆ audio_file_loop()

int audio_file_loop ( void *  private_data,
osl_rbtree_loop_func *  func 
)

Call the given function for each file in the audio file table.

Parameters
private_dataAn arbitrary data pointer, passed to func.
funcThe custom function to be called.
Returns
Standard.

References osl().

Referenced by aft_check_attributes(), and aft_check_callback().

◆ send_afs_status()

int send_afs_status ( struct command_context cc,
int  parser_friendly 
)

Get the current afs status items from the afs process and send it.

Parameters
ccThe command context, used e.g. for data encryption.
parser_friendlyWhether parser-friendly output format should be used.

As the contents of the afs status items change in time and the command handler only has a COW version created at fork time, it can not send up-to-date afs status items directly. Therefore the usual callback mechanism is used to pass the status items from the afs process to the command handler via a shared memory area and a pipe.

Returns
The return value of the underlying call to send_callback_request().

References send_callback_request().

◆ aft_check_callback()

int aft_check_callback ( struct afs_callback_arg aca)

Check the audio file table for inconsistencies.

Parameters
acaOnly ->pbout is used for diagnostics.
Returns
Standard. Inconsistencies are reported but not regarded as an error.

References audio_file_loop(), para_printf(), and afs_callback_arg::pbout.

◆ aft_check_attributes()

int aft_check_attributes ( uint64_t  att_mask,
struct para_buffer pb 
)

Iterate over all audio files and check the attribute bit mask.

Parameters
att_maskThe mask of all valid attributes.
pbUsed for reporting inconsistencies.

This reads the attribute bit mask of each audio file from the afs info structure stored in the audio file table and verifies that all set bits are also turned on in att_mask, i.e., correspond to an attribute of the attribute table. Audio files for which this is not the case are reported via pb.

Returns
Standard. Inconsistencies are not regarded as errors.
See also
attribute_check_callback().

References audio_file_loop(), and para_printf().

Variable Documentation

◆ lsg_server_cmd_com_ls_user_data

const struct server_cmd_user_data lsg_server_cmd_com_ls_user_data = { .handler = com_ls }

◆ lsg_server_cmd_com_add_user_data

const struct server_cmd_user_data lsg_server_cmd_com_add_user_data = { .handler = com_add }

◆ lsg_server_cmd_com_touch_user_data

const struct server_cmd_user_data lsg_server_cmd_com_touch_user_data = { .handler = com_touch }

◆ lsg_server_cmd_com_rm_user_data

const struct server_cmd_user_data lsg_server_cmd_com_rm_user_data = { .handler = com_rm }

◆ lsg_server_cmd_com_cpsi_user_data

const struct server_cmd_user_data lsg_server_cmd_com_cpsi_user_data = { .handler = com_cpsi }

◆ lsg_server_cmd_com_setatt_user_data

const struct server_cmd_user_data lsg_server_cmd_com_setatt_user_data = { .handler = com_setatt }

◆ aft_ops

const struct afs_table_operations aft_ops
Initial value:
= {
.open = aft_open,
.close = aft_close,
.create = aft_create,
.event_handler = aft_event_handler,
}

The audio file table contains information about known audio files.

PARA_ABS
#define PARA_ABS(x)
Compute the absolute value of x.
Definition: para.h:45