Esempio n. 1
0
int run_task(task_type_t type, metric_type_t metric, task_option_t *options, char *id) {
  switch (type) {
    case PROCESS:
      return handle_process(metric, options, id);
    case DIRECTORY:
      return handle_directory(options, id);
    case DISK:
      return handle_disk(metric, options, id);
    case SWAP:
      return handle_swap(id);
    case LOAD:
      return handle_load(id);
    case TOTAL:
      return handle_total(id, metric);
    default: {
      // We've been passed an incorrectly initialized task. Shouldn't happen, but handle
      // for debugging. Plus it gets GCC off my case.
      task_report_t report;
      init_task_report(&report, id, type, metric);
      sprintf(report.message, "FATAL CAUSE INVALID_TASK");
      lpush(&reports, &report);
      return NOTGIOS_GENERIC_ERROR;
    }
  }
}
Esempio n. 2
0
int handle(int newsockfd, struct sockaddr_in socket, socklen_t socklen)
{
    char buffer[256], path[PATH_MAX], *url;
    struct stat path_stat;
    int n;

    (void) socklen;

    bzero(buffer, 256);
    n = read(newsockfd, buffer, 255);
    if (n < 0)
        error("error reading");

    url = geturl(buffer);

    info("%s GET %s", inet_ntoa(socket.sin_addr), url);

    snprintf(path, PATH_MAX, "%s/%s", basedir, url);

    while (n == 255)
        n = read(newsockfd, buffer, 255);

    if (stat(path, &path_stat)) {
        handle_notfound(newsockfd);
    } else {
        if (S_ISDIR(path_stat.st_mode)) {
            if (path[strlen(path)-1] != '/') {
                size_t len;
                len = strlen(url);
                url = realloc(url, len + 2);
                url[len] = '/';
                url[len+1] = 0;
                handle_redirection(url, url, newsockfd);
            } else {
                struct stat index_stat;
                char index_path[PATH_MAX];
                snprintf(index_path, PATH_MAX, "%s/index.html", path);
                index_stat.st_mode = 0;
                stat(index_path, &index_stat);
                if (S_ISREG(index_stat.st_mode))
                    handle_file(url, index_path, newsockfd);
                else
                    handle_directory(url, path, newsockfd);
            }
        } else {
            handle_file(url, path, newsockfd);
        }
    }

    close(newsockfd);
    free(url);
    return 0;
}
Esempio n. 3
0
/* ------------------------------------------------------------------
 * Given a string to a file or path, first determines the full 'real' path.
 *
 * If the filename is a symlink, we resolve the link and call this function
 * again, with the from_link flag set.  This is used in deciding which
 * parameter (filename or fullpath) to use when calling lstat, and in
 * determining whether it is necessary to check for loops in the directory
 * hierarchy.
 *
 * If the filename is actually a directory, (and is not already open),
 * explore_dir is called to take care of its entries.  If the directory is
 * found within the visited_dir list, then we have found a loop, and the
 * directory is not explored.
 *
 * Otherwise, it's a regular ol' file and we just call read_file on it.
 *
 * list:	Linked list of visited directories.  Loops are detected
 *		by comparing absolute pathnames.
 * filename:	Name of entity being examined (directory or file).
 * fullpath:	Buffer to hold the full pathname of a file.
 * dir_d:	Maxmimum allowed depth of directory recursion to perform.
 * from_link:	Flag denoting whether this function has been called from
 *		a symbolic link or not.
 * ------------------------------------------------------------------
 */
int process_entry(params *P, visited_dir *list, char *filename,
		  char *path, int dir_d, int from_link)
{
	struct stat statbuf;
	dev_t dev;
	ino_t ino;
	char *absolute_filename;
	int myerrno;
	if ((absolute_filename = malloc(path_size)) == NULL)
	{
		fprintf(stderr, "Malloc failed in process_entry.\n");
		return -1;
	}
	pthread_mutex_lock(&chdir_mutex);
	chdir(path);
	if (lstat(filename, &statbuf) < 0)
	{
		myerrno = errno;
		pthread_mutex_unlock(&chdir_mutex);
		if (filename[0] == '/')
		{
			snprintf(absolute_filename,path_size, "%s", filename);
		}
		else
		{
			snprintf(absolute_filename,path_size, "%s/%s", 
				path, filename);
		}
		if (!enabled(P, NO_SYMLINKS) || (myerrno != ELOOP))
			print_file_error(P, myerrno, absolute_filename);
		free(absolute_filename);
		return 0;
	}

	if (realpath(filename, absolute_filename) == 0)
	{
		myerrno = errno;
		pthread_mutex_unlock(&chdir_mutex);
		if (filename[0] == '/')
		{
			snprintf(absolute_filename,path_size, "%s", filename);
		}
		else
		{
			snprintf(absolute_filename,path_size, "%s/%s", 
				path, filename);
		}

		if (!enabled(P, NO_SYMLINKS) || (myerrno != ELOOP))
			print_file_error(P, myerrno, absolute_filename);
		free(absolute_filename);
		return 0;
	}
	if ( S_ISLNK(statbuf.st_mode))
	{ /* symbolic link: could be either file or dir, so resolve & process */
		if (!enabled(P, NO_SYMLINKS))
		{
			if (lstat(absolute_filename, &statbuf) < 0)
			{
				myerrno = errno;
				pthread_mutex_unlock(&chdir_mutex);
				/*snprintf(absolute_filename,path_size, "%s/%s", 
						path, filename);*/
				print_file_error(P, myerrno, absolute_filename);

				free(absolute_filename);
				return 0;
			}
		}
		else
		{
			free(absolute_filename);
			return 0;
		}
	}
	pthread_mutex_unlock(&chdir_mutex);
	dev = statbuf.st_dev;
	ino = statbuf.st_ino;
	myerrno = 0;
	if (S_ISREG(statbuf.st_mode) || S_ISCHR(statbuf.st_mode))
	{ /* regular, readable file */
		read_file(P, absolute_filename);
		inc_stat(P, t_infiles, 1);
		free(absolute_filename);
	}
	else if (!S_ISDIR(statbuf.st_mode))
	{ /* if not link, file, or dir, error (if appropriate) */
		print_file_error(P, 0, absolute_filename);
		free(absolute_filename);
		return 0;
	}
		
	else if (S_ISDIR(statbuf.st_mode) && dir_d != 0)
	{ /* directory, and dir_depth allows us to explore it */
		if(have_visited(list, dev, ino))
		{ /* if loop found, print it, but don't explore */
			inc_stat(P, t_dloops, 1);
			if (!enabled(P, QUIET_FILENAME))
			{
				print_loop(P, filename, path, 
					absolute_filename, list, dev, ino);
			}
			free(absolute_filename);
		}
		else
		{ /* add a list node, explore the dir, then free the node */
			handle_directory(P, list, absolute_filename, 
				dev, ino, path, (dir_d - 1));
		}
	}
	else if (S_ISDIR(statbuf.st_mode) && dir_d == 0)
	{
		inc_stat(P, t_dskips, 1);
		free(absolute_filename);
	}
	return 0;
}
Esempio n. 4
0
std::string
System::normalize_path(const std::string& path)
{
  if (path.empty())
  {
    return std::string();
  }
  else
  {
    bool absolute = false;
    if (path[0] == '/')
    {
      absolute = true;
    }

    std::string result;
    result.reserve(path.size());

    std::string::const_reverse_iterator last_slash = path.rbegin();
    int skip = 0;

    for(std::string::const_reverse_iterator i = path.rbegin(); i != path.rend(); ++i)
    {
      if (*i == '/')
      {
        std::string dir(last_slash, i);

        handle_directory(dir, skip, result);

        last_slash = i+1;
      }
    }

    // relative path name
    if (last_slash != path.rend())
    {
      std::string dir(last_slash, path.rend());
      handle_directory(dir, skip, result);
    }

    if (!absolute)
    {
      for(int i = 0; i < skip; ++i)
      {
        if (i == skip - 1)
        {
          if (result.empty())
          {
            result.append("..");
          }
          else
          {
            result.append("/..");
          }
        }
        else
        {
          result.append("../");
        }
      }
    }
    else // if (absolute)
    {
      result += "/";
    }

    std::reverse(result.begin(), result.end());
    return result;
  }
}
Esempio n. 5
0
int handle_directory(const std::string& path, const std::string& filekey, AudioFileRecordStore& record_store)
{
    return handle_directory(path.c_str(), filekey.c_str(), record_store);
}