You are not logged in.

#1 2023-02-15 12:33:07

mamograg17
Member
From: Kingdom of Morocco
Registered: 2022-02-08
Posts: 40

install a package using alpm

I am trying to install a package using alpm, I initialize handle, get pkg pointer from db... everything is ok,when i call alpm_trans_init() it exits with error, i don't know what is the reason, it outputs unexpected error when i output the error number.
this is my code:

alpm_errno_t err;
  alpm_handle_t * handle = alpm_initialize("/", "/var/lib/pacman", &err);

  //check for any errors
  if(!handle){
    std::cerr << "Error while initializing ALPM handle or regestring databases." << std::endl;
    return {};
  }
  //set the call back function to show progress
    alpm_option_set_dlcb(handle,(alpm_cb_download)progressCallBack,NULL);

    alpm_db_t* extra_repo = alpm_register_syncdb(handle, "extra", ALPM_DB_USAGE_INSTALL)
    if(community_repo== NULL)
    {
      std::cerr << "Error while registring sync database." << std::endl;
      alpm_release(handle);
      return 1;
    }

    alpm_pkg_t * pkg = alpm_db_get_pkg(extra_repo,"vlc");
    if(pkg == NULL)
    { 
      
      std::cerr << "Error while getting pkg from database" << std::endl;
      alpm_release(handle);
      return 1;
    }

    int trans = alpm_trans_init(handle,ALPM_TRANS_FLAG_ALLEXPLICIT); //it happens here
    if(trans == -1) {
      std::cerr << "Error while initializing transaction,reason:"<< alpm_strerror(err)  << std::endl;
      alpm_trans_release(handle);
      alpm_release(handle);
      return 1;
    }
    
    trans = alpm_add_pkg(handle,pkg);
    if(trans == -1) {
      std::cerr << "Error while adding package to transaction,reason:" << alpm_strerror(err) << std::endl;
      alpm_release(handle);
      return 1;
    }

    alpm_list_t * list = NULL;
    trans = alpm_trans_prepare(handle,&list);
    if(trans == -1) {
      std::cerr << "Error while preparing transaction" << std::endl;
      alpm_release(handle);
      return 1;
    }

    trans = alpm_trans_commit(handle,&list);
    if(trans == -1) {
      std::cerr << "Error while comming transaction" << std::endl;
      alpm_release(handle);
      return 1;
    }
  }
  

this is what if statement print:
Error while initializing transaction,reason:unexpected error

Offline

#2 2023-05-22 17:20:38

mamograg17
Member
From: Kingdom of Morocco
Registered: 2022-02-08
Posts: 40

Re: install a package using alpm

i edited the code and it looks like this now:

alpm_errno_t err;
  alpm_handle_t *handle = alpm_initialize("/", "/var/lib/pacman", &err);

  // set the call back function to show progress
  alpm_option_set_dlcb(handle, (alpm_cb_download)progressCallBack, NULL);

  // check for any errors
  if (!handle){
    std::cerr << "Error while initializing ALPM handle or regestring databases." << std::endl;
    return -1;
  }
    alpm_db_t *community_repo = alpm_register_syncdb(handle, "community", ALPM_DB_USAGE_INSTALL);
    if (community_repo == NULL){
        std::cerr << "Error while registring sync database." << std::endl;
        alpm_release(handle);
        return -1;
    }

    alpm_pkg_t *pkg = alpm_db_get_pkg(community_repo, argv[1]);
    if (pkg == NULL){
        std::cerr << "Error while getting pkg from database" << std::endl;
        alpm_release(handle);
        return -1;
    }

    if (alpm_trans_init(handle, ALPM_TRANS_FLAG_NEEDED | ALPM_PKG_REASON_EXPLICIT) != 0){
        std::cerr << "Error while initializing transaction:" << alpm_strerror(alpm_errno(handle)) << std::endl;
        alpm_trans_release(handle);
        alpm_release(handle);
        return -1;
    }

    if (alpm_add_pkg(handle, pkg) != 0){
        std::cerr << "Error while adding package to transaction:" << alpm_strerror(alpm_errno(handle)) << std::endl;
        alpm_release(handle);
        return -1;
    }

    alpm_list_t *list = NULL;
    // trans = alpm_trans_prepare(handle, &list);
    if (alpm_trans_prepare(handle, &list) != 0){
        std::cerr << "Error while preparing transaction:" << alpm_strerror(alpm_errno(handle)) << std::endl;
        alpm_release(handle);
        return -1;
    }

    if (alpm_trans_commit(handle, &list) != 0){
        std::cerr << "Error while commiting transaction:" << alpm_strerror(alpm_errno(handle)) << std::endl;
        alpm_release(handle);
        return -1;
    }
    //unregister database after usage
    alpm_db_unregister(community_repo);

    //unlock the database
    alpm_unlock(handle);
 

but now i get another error, it is somehow weird cause pacman is working without any problem:

Error while commiting transaction:no servers configured for repository

Offline

#3 2023-05-22 17:48:51

progandy
Member
Registered: 2012-05-17
Posts: 5,212

Re: install a package using alpm

You have added the community repository, but you did not configure a server for it. You did not set up signaure verification either, you probably want to enable that. libalpm does not parse the pacman.conf. If you want that, then you'll have to do that yourself. (Or use the helper library from pacutils similar to this)

Last edited by progandy (2023-05-22 17:50:06)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#4 2023-05-22 17:54:45

papajoke
Member
From: france
Registered: 2014-10-10
Posts: 40

Re: install a package using alpm

alpm no read pacman.conf, it's at you to parse this file and add datas in alpm options


lts - zsh - Kde - Intel Core i3 - 6Go RAM - GeForce 405 video-nouveau

Offline

#5 2023-05-22 18:08:05

mamograg17
Member
From: Kingdom of Morocco
Registered: 2022-02-08
Posts: 40

Re: install a package using alpm

progandy wrote:

You have added the community repository, but you did not configure a server for it. You did not set up signaure verification either, you probably want to enable that. libalpm does not parse the pacman.conf. If you want that, then you'll have to do that yourself. (Or use the helper library from pacutils similar to this)

i have implemented that but getting error while getting pkg pointer:

 alpm_errno_t err;
  pu_config_t *config = pu_config_new();
  pu_ui_config_load(config, "/etc/pacman.conf");
  alpm_handle_t *handle = pu_initialize_handle_from_config(config);
  alpm_list_t *sync_dbs = pu_register_syncdbs(handle, config->repos);

  // set the call back function to show progress
  alpm_option_set_dlcb(handle, (alpm_cb_download)progressCallBack, NULL);

  // check for any errors
  if (!handle){
    std::cerr << "Error while initializing ALPM handle or regestring databases." << std::endl;
    return -1;
  }

    alpm_pkg_t *pkg = NULL;
    for (alpm_list_t *db = sync_dbs; db; db = alpm_list_next(db))
    {
      pkg = alpm_db_get_pkg(db->data, argv[1]);
      if (!pkg)
      {
        std::cerr << "Error while getting pkg from database" << std::endl;
      }
    }
  ....
  

the error:

 error: invalid conversion from ‘void*’ to ‘alpm_db_t*’ [-fpermissive]
   55 |       pkg = alpm_db_get_pkg(db->data, argv[1]);
      |                             ~~~~^~~~
      |                                 |
      |                                 void*

Offline

#6 2023-05-22 18:41:33

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,758
Website

Re: install a package using alpm

That last error is clear, you need to cast the pointer to the appropriate type.  That or use -fpermissive.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#7 2023-05-22 19:22:25

mamograg17
Member
From: Kingdom of Morocco
Registered: 2022-02-08
Posts: 40

Re: install a package using alpm

Trilby wrote:

That last error is clear, you need to cast the pointer to the appropriate type.  That or use -fpermissive.

the server error has gone, but another one appeared:

Error while commiting transaction:failed to retrieve some files

this is the code now:

alpm_errno_t err;
  alpm_handle_t *handle = alpm_initialize("/", "/var/lib/pacman", &err);

  // set the call back function to show progress
  alpm_option_set_dlcb(handle, (alpm_cb_download)progressCallBack, NULL);

  // check for any errors
  if (!handle){
    std::cerr << "Error while initializing ALPM handle or regestring databases." << std::endl;
    return -1;
  }

    alpm_db_t *community_repo = alpm_register_syncdb(handle, "community", ALPM_DB_USAGE_INSTALL);
    if (community_repo == NULL){
        std::cerr << "Error while registring sync database." << std::endl;
        alpm_release(handle);
        return -1;
    }

    alpm_db_add_server(community_repo, "https://geo.mirror.pkgbuild.com/");

    alpm_pkg_t *pkg = alpm_db_get_pkg(community_repo, argv[1]);
    if (pkg == NULL){
        std::cerr << "Error while getting pkg from database" << std::endl;
        alpm_release(handle);
        return -1;
    }

    if (alpm_trans_init(handle, ALPM_TRANS_FLAG_NODEPS | ALPM_TRANS_FLAG_NOCONFLICTS) != 0)
    {
        std::cerr << "Error while initializing transaction:" << alpm_strerror(alpm_errno(handle)) << std::endl;
        alpm_trans_release(handle);
        alpm_release(handle);
        return -1;
    }

    if (alpm_add_pkg(handle, pkg) != 0){
        std::cerr << "Error while adding package to transaction:" << alpm_strerror(alpm_errno(handle)) << std::endl;
        alpm_release(handle);
        return -1;
    }

    alpm_list_t *list = NULL;
    // trans = alpm_trans_prepare(handle, &list);
    if (alpm_trans_prepare(handle, &list) != 0){
        std::cerr << "Error while preparing transaction:" << alpm_strerror(alpm_errno(handle)) << std::endl;
        alpm_release(handle);
        return -1;
    }

    if (alpm_trans_commit(handle, &list) != 0){
        std::cerr << "Error while commiting transaction:" << alpm_strerror(alpm_errno(handle)) << std::endl;
        alpm_release(handle);
        return -1;
    }

Offline

Board footer

Powered by FluxBB