/*
* Copyright (c) [2004-2015] Novell, Inc.
+ * Copyright (c) 2016 SUSE LLC
*
* All Rights Reserved.
*
getgrouplist(const char* username, gid_t gid)
{
int n = 16;
- gid_t* buf = (gid_t*) malloc(sizeof(gid_t) * n);
+ vector<gid_t> gids(n);
- if (::getgrouplist(username, gid, buf, &n) == -1)
- {
- buf = (gid_t*) realloc(buf, sizeof(gid_t) * n);
- ::getgrouplist(username, gid, buf, &n);
- }
+ while (::getgrouplist(username, gid, &gids[0], &n) == -1)
+ gids.resize(n);
- vector<gid_t> gids(&buf[0], &buf[n]);
- sort(gids.begin(), gids.end());
+ gids.resize(n);
- free(buf);
+ sort(gids.begin(), gids.end());
return gids;
}
xattrs3
xattrs4
test-btrfsutils
+ug-tests
noinst_PROGRAMS = simple1 permissions1 permissions2 permissions3 owner1 owner2 \
owner3 directory1 missing-directory1 error1 error2 error4 \
- $(TMP_XATST) test-btrfsutils
+ $(TMP_XATST) test-btrfsutils ug-tests
simple1_SOURCES = simple1.cc common.h common.cc
test_btrfsutils_SOURCES = test-btrfsutils.cc
+ug_tests_SOURCES = ug-tests.cc
+
EXTRA_DIST = $(noinst_SCRIPTS)
--- /dev/null
+
+#include <unistd.h>
+#include <sys/types.h>
+
+#include <iostream>
+
+#include <snapper/AppUtil.h>
+
+using namespace std;
+using namespace snapper;
+
+
+void
+test1()
+{
+ uid_t uid = getuid();
+ cout << "uid:" << uid << endl;
+
+ string username;
+ gid_t gid;
+ if (!get_uid_username_gid(uid, username, gid))
+ cerr << "failed to get username and gid" << endl;
+ cout << "username:" << username << endl;
+ cout << "gid:" << gid << endl;
+
+ vector<gid_t> gids = getgrouplist(username.c_str(), gid);
+ cout << "gids:";
+ for (gid_t gid : gids)
+ cout << gid << " ";
+ cout << endl;
+
+ cout << endl;
+}
+
+
+void
+test2()
+{
+ uid_t uid;
+ if (!get_user_uid("root", uid))
+ cerr << "failed to get uid" << endl;
+ cout << "uid:" << uid << endl;
+
+ gid_t gid;
+ if (!get_group_gid("audio", gid))
+ cerr << "failed to get gid" << endl;
+ cout << "gid:" << gid << endl;
+
+ cout << endl;
+}
+
+
+int
+main()
+{
+ test1();
+ test2();
+}