- entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
- while (1) {
- if ((readdir_r(dp, entry, &result) != 0) || (result == NULL)) {
- dev->dev_errno = ENOENT;
- Dmsg2(29, "failed to find suitable file in dir %s (dev=%s)\n", dev->device->mount_point, dev->dev_name);
- closedir(dp);
- free(entry);
- return false;
- }
-
- ASSERT(name_max+1 > (int)sizeof(struct dirent) + (int)NAMELEN(entry));
-
- if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
- continue;
- }
-
- pm_strcpy(guessedname, dev->device->mount_point);
- if (guessedname.c_str()[strlen(guessedname.c_str())-1] != '/') {
- pm_strcat(guessedname, "/");
- }
- pm_strcat(guessedname, entry->d_name);
-
- if (stat(guessedname.c_str(), &statp) < 0) {
- berrno be;
- Dmsg3(29, "failed to stat %s (dev=%s), ERR=%s\n",
- guessedname.c_str(), dev->dev_name, be.strerror());
- continue;
- }
-
- if (!S_ISREG(statp.st_mode) || (statp.st_size < 500)) {
- Dmsg2(100, "%s is not a regular file, or less than 500 bytes (dev=%s)\n",
- guessedname.c_str(), dev->dev_name);
- continue;
- }
-
- /* Ok, we found a good file, remove the part extension if possible. */
- for (index = strlen(guessedname.c_str())-1; index >= 0; index--) {
- if ((guessedname.c_str()[index] == '/') ||
- (guessedname.c_str()[index] < '0') ||
- (guessedname.c_str()[index] > '9')) {
- break;
- }
- if (guessedname.c_str()[index] == '.') {
- guessedname.c_str()[index] = '\0';
- break;
- }
- }
-
- if ((stat(guessedname.c_str(), &statp) < 0) || (statp.st_size < 500)) {
- /* The file with extension truncated does not exists or is too small, so use it with its extension. */
- berrno be;
- Dmsg3(100, "failed to stat %s (dev=%s), using the file with its extension, ERR=%s\n",
- guessedname.c_str(), dev->dev_name, be.strerror());
- pm_strcpy(guessedname, dev->device->mount_point);
- if (guessedname.c_str()[strlen(guessedname.c_str())-1] != '/') {
- pm_strcat(guessedname, "/");
- }
- pm_strcat(guessedname, entry->d_name);
- continue;
- }
- break;
- }
- closedir(dp);
- free(entry);
-
- if (dev->fd >= 0) {
- close(dev->fd);
- }
-
- Dmsg1(100, "open(%s) read-only\n", guessedname.c_str());
- if ((dev->fd = open(guessedname.c_str(), O_RDONLY | O_BINARY)) < 0) {
- berrno be;
- dev->dev_errno = errno;
- Dmsg3(29, "failed to open %s (dev=%s), ERR=%s\n",
- guessedname.c_str(), dev->dev_name, be.strerror());
- Dmsg0(100, "Call open_first_part\n");
- if (open_first_part(dev, OPEN_READ_ONLY) < 0) {
- berrno be;
- dev->dev_errno = errno;
- Mmsg1(&dev->errmsg, _("Could not open_first_part, ERR=%s\n"), be.strerror());
- Emsg0(M_FATAL, 0, dev->errmsg);
- }
- return false;
- }
- dev->part_start = 0;
- dev->part_size = statp.st_size;
- dev->part = 0;
- dev->set_opened();
- dev->use_count = 1;
- Dmsg2(29, "Exit: %s opened (dev=%s)\n", guessedname.c_str(), dev->dev_name);
-
- return true;
-}
-#endif
-
-
-/* Update the free space on the device */
-void update_free_space_dev(DEVICE* dev)
-{
- POOL_MEM ocmd(PM_FNAME);
- POOLMEM* results;
- char* icmd;
- int timeout;
- long long int free;
- char ed1[50];
-
- icmd = dev->device->free_space_command;