- locales = set_new(string_hash_func, string_compare_func);
- if (!locales)
- return log_oom();
-
- fd = open("/usr/lib/locale/locale-archive", O_RDONLY|O_NOCTTY|O_CLOEXEC);
- if (fd < 0) {
- log_error("Failed to open locale archive: %m");
- r = -errno;
- goto finish;
- }
-
- if (fstat(fd, &st) < 0) {
- log_error("fstat() failed: %m");
- r = -errno;
- goto finish;
- }
-
- if (!S_ISREG(st.st_mode)) {
- log_error("Archive file is not regular");
- r = -EBADMSG;
- goto finish;
- }
-
- if (st.st_size < (off_t) sizeof(struct locarhead)) {
- log_error("Archive has invalid size");
- r = -EBADMSG;
- goto finish;
- }
-
- p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
- if (p == MAP_FAILED) {
- log_error("Failed to map archive: %m");
- r = -errno;
- goto finish;
- }
-
- h = (const struct locarhead *) p;
- if (h->magic != 0xde020109 ||
- h->namehash_offset + h->namehash_size > st.st_size ||
- h->string_offset + h->string_size > st.st_size ||
- h->locrectab_offset + h->locrectab_size > st.st_size ||
- h->sumhash_offset + h->sumhash_size > st.st_size) {
- log_error("Invalid archive file.");
- r = -EBADMSG;
- goto finish;
- }
-
- e = (const struct namehashent*) ((const uint8_t*) p + h->namehash_offset);
- for (i = 0; i < h->namehash_size; i++) {
- char *z;
-
- if (e[i].locrec_offset == 0)
- continue;
-
- z = strdup((char*) p + e[i].name_offset);
- if (!z) {
- r = log_oom();
- goto finish;
- }
-
- r = set_put(locales, z);
- if (r < 0) {
- free(z);
- log_error("Failed to add locale: %s", strerror(-r));
- goto finish;
- }
- }