[asterisk-commits] dlee: branch dlee/record r394823 - in /team/dlee/record: include/asterisk/ re...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Fri Jul 19 17:04:28 CDT 2013
Author: dlee
Date: Fri Jul 19 17:04:27 2013
New Revision: 394823
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=394823
Log:
cleanup
Modified:
team/dlee/record/include/asterisk/stasis_app_recording.h
team/dlee/record/res/stasis_recording/stored.c
Modified: team/dlee/record/include/asterisk/stasis_app_recording.h
URL: http://svnview.digium.com/svn/asterisk/team/dlee/record/include/asterisk/stasis_app_recording.h?view=diff&rev=394823&r1=394822&r2=394823
==============================================================================
--- team/dlee/record/include/asterisk/stasis_app_recording.h (original)
+++ team/dlee/record/include/asterisk/stasis_app_recording.h Fri Jul 19 17:04:27 2013
@@ -40,6 +40,16 @@
struct ao2_container *stasis_app_stored_recording_find_all(void);
+/*!
+ * \brief Creates a stored recording object, with the given name.
+ *
+ * \param name Name of the recording.
+ * \return New recording object.
+ * \return \c NULL if recording is not found. \c errno is set to indicate why
+ * - \c ENOMEM - out of memeory
+ * - \c EACCES - file permissions (or recording is outside the config dir)
+ * - Any of the error codes for stat(), opendir(), readdir()
+ */
struct stasis_app_stored_recording *stasis_app_stored_recording_find_by_name(
const char *name);
Modified: team/dlee/record/res/stasis_recording/stored.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/record/res/stasis_recording/stored.c?view=diff&rev=394823&r1=394822&r2=394823
==============================================================================
--- team/dlee/record/res/stasis_recording/stored.c (original)
+++ team/dlee/record/res/stasis_recording/stored.c Fri Jul 19 17:04:27 2013
@@ -40,7 +40,7 @@
AST_DECLARE_STRING_FIELDS(
AST_STRING_FIELD(name); /*!< Recording's name */
AST_STRING_FIELD(file); /*!< Absolute filename, without extension; for use with streamfile */
- AST_STRING_FIELD(file_with_ext); /*!< Absolute filename, with extension; for use with everything */
+ AST_STRING_FIELD(file_with_ext); /*!< Absolute filename, with extension; for use with everything else */
);
char *format; /*!< Format name (i.e. filename extension) */
@@ -104,6 +104,17 @@
return 0;
}
+/*!
+ * \brief Finds a recording in the given directory.
+ *
+ * This function searchs for a file with the given file name, with a registered
+ * format that matches its extension.
+ *
+ * \param dir_name Directory to search (absolute path).
+ * \param file File name, without extension.
+ * \return Absolute path of the recording file.
+ * \return \c NULL if recording is not found.
+ */
static char *find_recording(const char *dir_name, const char *file)
{
RAII_VAR(DIR *, dir, NULL, closedir);
@@ -148,72 +159,27 @@
return file_with_ext;
}
-static int recording_set_name(struct stasis_app_stored_recording *recording,
- const char *name)
-{
- RAII_VAR(char *, dir, NULL, ast_free);
- RAII_VAR(char *, file, NULL, ast_free);
- RAII_VAR(char *, file_with_ext, NULL, ast_free);
+/*!
+ * \brief Allocate a recording object.
+ */
+static struct stasis_app_stored_recording *recording_alloc(void)
+{
+ RAII_VAR(struct stasis_app_stored_recording *, recording, NULL,
+ ao2_cleanup);
int res;
- struct stat file_stat;
-
- ast_string_field_init(recording, 255);
-
- res = split_path(name, &dir, &file);
+
+ recording = ao2_alloc(sizeof(*recording), stored_recording_dtor);
+ if (!recording) {
+ return NULL;
+ }
+
+ res = ast_string_field_init(recording, 255);
if (res != 0) {
- return -1;
- }
- ast_string_field_build(recording, file, "%s/%s", dir, file);
-
- if (!ast_begins_with(dir, ast_config_AST_RECORDING_DIR)) {
- /* Attempt to escape the recording directory */
- ast_log(LOG_WARNING, "Attempt to access invalid recording %s\n",
- name);
- errno = EACCES;
- return -1;
- }
-
- /* The actual name of the recording is file with the config dir
- * prefix removed.
- */
- ast_string_field_set(recording, name,
- recording->file + strlen(ast_config_AST_RECORDING_DIR) + 1);
-
- file_with_ext = find_recording(dir, file);
- if (!file_with_ext) {
- return -1;
- }
- ast_string_field_set(recording, file_with_ext, file_with_ext);
- recording->format = strrchr(recording->file_with_ext, '.');
- if (!recording->format) {
- return -1;
- }
- ++(recording->format);
-
- res = stat(file_with_ext, &file_stat);
- if (res != 0) {
- return -1;
- }
-
- if (!S_ISREG(file_stat.st_mode)) {
- /* Let's not play if it's not a regular file */
- errno = EACCES;
- return -1;
- }
-
- return 0;
-}
-
-struct ast_json *stasis_app_stored_recording_to_json(
- struct stasis_app_stored_recording *recording)
-{
- if (!recording) {
- return NULL;
- }
-
- return ast_json_pack("{ s: s, s: s }",
- "name", recording->name,
- "format", recording->format);
+ return NULL;
+ }
+
+ ao2_ref(recording, +1);
+ return recording;
}
static int recording_sort(const void *obj_left, const void *obj_right, int flags)
@@ -304,6 +270,19 @@
/* process subdirectory */
RAII_VAR(char *, sub_dir, NULL, ast_free);
+ /* symlinks to directories cause weirdness; ignore */
+ res = lstat(file_with_ext, &file_stat);
+ if (res != 0) {
+ ast_log(LOG_WARNING, "Error getting file info: %s\n", file_with_ext);
+ continue;
+ }
+
+ if (S_ISLNK(file_stat.st_mode)) {
+ ast_log(LOG_WARNING, "Ignoring symlink %s\n",
+ result->d_name);
+ continue;
+ }
+
if (ast_strlen_zero(dir_name)) {
sub_dir = ast_strdup(result->d_name);
} else {
@@ -340,13 +319,12 @@
continue;
}
- recording = ao2_alloc(sizeof(*recording), stored_recording_dtor);
+ recording = recording_alloc();
if (!recording) {
ast_log(LOG_WARNING, "Error allocating recording object for %s\n", file_with_ext);
continue;
}
- ast_string_field_init(recording, 255);
ast_string_field_set(recording, file_with_ext, file_with_ext);
/* Build file and format from file_with_ext */
@@ -388,7 +366,11 @@
{
RAII_VAR(struct stasis_app_stored_recording *, recording, NULL,
ao2_cleanup);
+ RAII_VAR(char *, dir, NULL, ast_free);
+ RAII_VAR(char *, file, NULL, ast_free);
+ RAII_VAR(char *, file_with_ext, NULL, ast_free);
int res;
+ struct stat file_stat;
errno = 0;
@@ -397,13 +379,50 @@
return NULL;
}
- recording = ao2_alloc(sizeof(*recording), stored_recording_dtor);
+ recording = recording_alloc();
if (!recording) {
return NULL;
}
- res = recording_set_name(recording, name);
+ res = split_path(name, &dir, &file);
if (res != 0) {
+ return NULL;
+ }
+ ast_string_field_build(recording, file, "%s/%s", dir, file);
+
+ if (!ast_begins_with(dir, ast_config_AST_RECORDING_DIR)) {
+ /* Attempt to escape the recording directory */
+ ast_log(LOG_WARNING, "Attempt to access invalid recording %s\n",
+ name);
+ errno = EACCES;
+ return NULL;
+ }
+
+ /* The actual name of the recording is file with the config dir
+ * prefix removed.
+ */
+ ast_string_field_set(recording, name,
+ recording->file + strlen(ast_config_AST_RECORDING_DIR) + 1);
+
+ file_with_ext = find_recording(dir, file);
+ if (!file_with_ext) {
+ return NULL;
+ }
+ ast_string_field_set(recording, file_with_ext, file_with_ext);
+ recording->format = strrchr(recording->file_with_ext, '.');
+ if (!recording->format) {
+ return NULL;
+ }
+ ++(recording->format);
+
+ res = stat(file_with_ext, &file_stat);
+ if (res != 0) {
+ return NULL;
+ }
+
+ if (!S_ISREG(file_stat.st_mode)) {
+ /* Let's not play if it's not a regular file */
+ errno = EACCES;
return NULL;
}
@@ -417,3 +436,15 @@
/* Path was validated when the recording object was created */
return unlink(recording->file_with_ext);
}
+
+struct ast_json *stasis_app_stored_recording_to_json(
+ struct stasis_app_stored_recording *recording)
+{
+ if (!recording) {
+ return NULL;
+ }
+
+ return ast_json_pack("{ s: s, s: s }",
+ "name", recording->name,
+ "format", recording->format);
+}
More information about the asterisk-commits
mailing list