Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6b87f8e9 authored by Shunkai Yao's avatar Shunkai Yao
Browse files

Refine dumpEffectConfigFile with getopt

replace strcpy with strlcpy

Bug: 282778856
Test: Push to Pixel phone and test with different params
Change-Id: Iad1063d970743090317fca62d6bcc5b3a6ba1713
parent a545e7c0
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -137,7 +137,7 @@ int checkLibraryPath(const char *lib_path_in, char *lib_path_out) {
                 kLibraryPathRoot[i],
                 lib_name);
        if (F_OK == access(path, 0)) {
            strcpy(lib_path_out, path);
            strlcpy(lib_path_out, path, PATH_MAX);
            ALOGW_IF(strncmp(lib_path_out, lib_path_in, PATH_MAX) != 0,
                "checkLibraryPath() corrected library path %s to %s", lib_path_in, lib_path_out);
            return 0;
+35 −40
Original line number Diff line number Diff line
@@ -14,54 +14,49 @@
 * limitations under the License.
 */

#include <getopt.h>

#include <media/EffectsFactoryApi.h>
#include <unistd.h>
#include "EffectsXmlConfigLoader.h"
#include "EffectsConfigLoader.h"

int main(int argc, char* argv[]) {
    const char* path = nullptr;
    bool legacyFormat;
    const char* const short_opts = "lx:h";
    const option long_opts[] = {{"legacy", no_argument, nullptr, 'l'},
                                {"xml", optional_argument, nullptr, 'x'},
                                {"help", no_argument, nullptr, 'h'}};

    if (argc == 2 && strcmp(argv[1], "--legacy") == 0) {
        legacyFormat = true;
        fprintf(stderr, "Dumping legacy effect config file\n");
    } else if ((argc == 2 || argc == 3) && strcmp(argv[1], "--xml") == 0) {
        legacyFormat = false;
        if (argc == 3) {
            fprintf(stderr, "Dumping XML effect config file: %s\n", path);
        } else {
            fprintf(stderr, "Dumping default XML effect config file.\n");
        }
    } else {
        fprintf(stderr, "Invalid arguments.\n"
                        "Usage: %s [--legacy|--xml [FILE]]\n", argv[0]);
    const auto opt = getopt_long(argc, argv, short_opts, long_opts, nullptr);
    switch (opt) {
        case 'l': { // -l or --legacy
            printf("Dumping legacy effect config file\n");
            if (EffectLoadEffectConfig() < 0) {
                fprintf(stderr, "loadEffectConfig failed, see logcat for detail.\n");
                return 1;
            }

    if (!legacyFormat) {
        ssize_t ret = EffectLoadXmlEffectConfig(path);
            return EffectDumpEffects(STDOUT_FILENO);
        }
        case 'x': { // -x or --xml
            printf("Dumping effect config file: %s\n", (optarg == NULL) ? "default" : optarg);
            ssize_t ret = EffectLoadXmlEffectConfig(optarg);
            if (ret < 0) {
                fprintf(stderr, "loadXmlEffectConfig failed, see logcat for detail.\n");
            return 2;
                return 1;
            }
            if (ret > 0) {
            fprintf(stderr, "Partially failed to load config. Skipped %zu elements, "
                    "see logcat for detail.\n", (size_t)ret);
                printf("Partially failed to load config. Skipped %zu elements.\n",
                        (size_t)ret);
            }
            return EffectDumpEffects(STDOUT_FILENO);
        }

    if (legacyFormat) {
        auto ret = EffectLoadEffectConfig();
        if (ret < 0) {
            fprintf(stderr, "loadEffectConfig failed, see logcat for detail.\n");
            return 3;
        case 'h': // -h or --help
        default: {
            printf("Usage: %s\n"
                   "--legacy (or -l):        Legacy audio effect config file to load\n"
                   "--xml (or -x) <FILE>:    Audio effect config file to load\n"
                   "--help (or -h):          Show this help\n",
                   argv[0]);
            return 0;
        }
        fprintf(stderr, "legacy loadEffectConfig has probably succeed, see logcat to make sure.\n");
    }

    if (EffectDumpEffects(STDOUT_FILENO) != 0) {
        fprintf(stderr, "Effect dump failed, see logcat for detail.\n");
        return 4;
    }
}