- if (mount_dev(dev, 1) < 0) {
- /* If the device cannot be mounted, check if it is writable */
- if (dev->free_space_errno >= 0) {
- Dmsg1(100, "open_guess_name_dev: device cannot be mounted, but it seems to be writable, returning 0. dev=%s\n", dev->dev_name);
- return 0;
- } else {
- Dmsg1(100, "open_guess_name_dev: device cannot be mounted, and is not writable, returning -1. dev=%s\n", dev->dev_name);
- return -1;
- }
- }
-
- name_max = pathconf(".", _PC_NAME_MAX);
- if (name_max < 1024) {
- name_max = 1024;
- }
-
- if (!(dp = opendir(dev->device->mount_point))) {
- berrno be;
- dev->dev_errno = errno;
- Dmsg3(29, "open_guess_name_dev: failed to open dir %s (dev=%s), ERR=%s\n", dev->device->mount_point, dev->dev_name, be.strerror());
- return -1;
- }
-
- 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, "open_guess_name_dev: failed to find suitable file in dir %s (dev=%s)\n", dev->device->mount_point, dev->dev_name);
- closedir(dp);
- return -1;
- }
-
- 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, "open_guess_name_dev: 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, "open_guess_name_dev: %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, "open_guess_name_dev: 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);
-
- if (dev->fd >= 0) {
- close(dev->fd);
- }
-
- if ((dev->fd = open(guessedname.c_str(), O_RDONLY | O_BINARY)) < 0) {
- berrno be;
- dev->dev_errno = errno;
- Dmsg3(29, "open_guess_name_dev: failed to open %s (dev=%s), ERR=%s\n",
- guessedname.c_str(), dev->dev_name, be.strerror());
- if (open_first_part(dev) < 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 -1;