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

Commit 7d0a5c36 authored by Steven Moreland's avatar Steven Moreland
Browse files

EndSection returns Result<Success>

Allow it to fail. When there is an error for a section ending,
print the error pointing to the line where the section starts.

Bug: 69050941
Test: boot, init_tests
Change-Id: I1d8ed25f4b74cc9ac24d38b8075751c7d606aea8
parent b480d441
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -379,10 +379,12 @@ Result<Success> ActionParser::ParseLineSection(std::vector<std::string>&& args,
    return action_ ? action_->AddCommand(std::move(args), line) : Success();
}

void ActionParser::EndSection() {
Result<Success> ActionParser::EndSection() {
    if (action_ && action_->NumCommands() > 0) {
        action_manager_->AddAction(std::move(action_));
    }

    return Success();
}

}  // namespace init
+1 −1
Original line number Diff line number Diff line
@@ -130,7 +130,7 @@ class ActionParser : public SectionParser {
    Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename,
                                 int line) override;
    Result<Success> ParseLineSection(std::vector<std::string>&& args, int line) override;
    void EndSection() override;
    Result<Success> EndSection() override;

  private:
    ActionManager* action_manager_;
+16 −4
Original line number Diff line number Diff line
@@ -50,12 +50,24 @@ void Parser::ParseData(const std::string& filename, const std::string& data) {
    state.nexttoken = 0;

    SectionParser* section_parser = nullptr;
    int section_start_line = -1;
    std::vector<std::string> args;

    auto end_section = [&] {
        if (section_parser == nullptr) return;

        if (auto result = section_parser->EndSection(); !result) {
            LOG(ERROR) << filename << ": " << section_start_line << ": " << result.error();
        }

        section_parser = nullptr;
        section_start_line = -1;
    };

    for (;;) {
        switch (next_token(&state)) {
            case T_EOF:
                if (section_parser) section_parser->EndSection();
                end_section();
                return;
            case T_NEWLINE:
                state.line++;
@@ -65,18 +77,18 @@ void Parser::ParseData(const std::string& filename, const std::string& data) {
                // uevent.
                for (const auto& [prefix, callback] : line_callbacks_) {
                    if (android::base::StartsWith(args[0], prefix.c_str())) {
                        if (section_parser) section_parser->EndSection();
                        end_section();

                        if (auto result = callback(std::move(args)); !result) {
                            LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
                        }
                        section_parser = nullptr;
                        break;
                    }
                }
                if (section_parsers_.count(args[0])) {
                    if (section_parser) section_parser->EndSection();
                    end_section();
                    section_parser = section_parsers_[args[0]].get();
                    section_start_line = state.line;
                    if (auto result =
                            section_parser->ParseSection(std::move(args), filename, state.line);
                        !result) {
+1 −1
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ class SectionParser {
    virtual Result<Success> ParseSection(std::vector<std::string>&& args,
                                         const std::string& filename, int line) = 0;
    virtual Result<Success> ParseLineSection(std::vector<std::string>&&, int) { return Success(); };
    virtual void EndSection(){};
    virtual Result<Success> EndSection() { return Success(); };
    virtual void EndFile(){};
};

+3 −1
Original line number Diff line number Diff line
@@ -1135,10 +1135,12 @@ Result<Success> ServiceParser::ParseLineSection(std::vector<std::string>&& args,
    return service_ ? service_->ParseLine(std::move(args)) : Success();
}

void ServiceParser::EndSection() {
Result<Success> ServiceParser::EndSection() {
    if (service_) {
        service_list_->AddService(std::move(service_));
    }

    return Success();
}

bool ServiceParser::IsValidName(const std::string& name) const {
Loading