Beispiel #1
0
static BOOL add_directory( HFCI fci, WCHAR *dir )
{
    static const WCHAR wildcardW[] = {'*',0};
    WCHAR *p, *buffer;
    HANDLE handle;
    WIN32_FIND_DATAW data;
    BOOL ret = TRUE;

    if (!(buffer = cab_alloc( (strlenW(dir) + MAX_PATH + 2) * sizeof(WCHAR) ))) return FALSE;
    strcpyW( buffer, dir );
    p = buffer + strlenW( buffer );
    if (p > buffer && p[-1] != '\\') *p++ = '\\';
    strcpyW( p, wildcardW );

    if ((handle = FindFirstFileW( buffer, &data )) != INVALID_HANDLE_VALUE)
    {
        do
        {
            if (data.cFileName[0] == '.' && !data.cFileName[1]) continue;
            if (data.cFileName[0] == '.' && data.cFileName[1] == '.' && !data.cFileName[2]) continue;
            if (data.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) continue;

            strcpyW( p, data.cFileName );
            if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                ret = add_directory( fci, buffer );
            else
                ret = add_file( fci, buffer );
            if (!ret) break;
        } while (FindNextFileW( handle, &data ));
        FindClose( handle );
    }
    cab_free( buffer );
    return TRUE;
}
Beispiel #2
0
/* add version resources to the dll by copying them from the source module */
static BOOL add_version_resource( HMODULE module, struct dll_info *dll_info )
{
    BOOL ret = FALSE;
    DWORD rva_start = ~0U, rva_end = 0, dir_size, total_size;
    const IMAGE_RESOURCE_DIRECTORY *basedir;
    const BYTE *data_start, *root;
    BYTE *buffer;

    if (!module) return TRUE;
    if (LdrFindResourceDirectory_U( module, NULL, 0, &basedir ) != STATUS_SUCCESS) return TRUE;
    root = (const BYTE *)basedir;
    get_resource_data( basedir, root, &rva_start, &rva_end );
    data_start = (const BYTE *)module + rva_start;
    if (data_start <= root) return FALSE;
    dir_size = data_start - root;
    if (!(buffer = HeapAlloc( GetProcessHeap(), 0, dir_size ))) return FALSE;
    memcpy( buffer, root, dir_size );
    fixup_resources( (IMAGE_RESOURCE_DIRECTORY *)buffer, buffer, dll_info->mem_pos + dir_size - rva_start );
    if (!xwrite( dll_info, buffer, dir_size, dll_info->file_pos )) goto done;
    if (!xwrite( dll_info, data_start, rva_end - rva_start, dll_info->file_pos + dir_size )) goto done;
    total_size = dir_size + rva_end - rva_start;
    add_directory( dll_info, IMAGE_DIRECTORY_ENTRY_RESOURCE, dll_info->mem_pos, total_size );
    add_section( dll_info, ".rsrc", total_size, IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ );
    ret = TRUE;
done:
    HeapFree( GetProcessHeap(), 0, buffer );
    return ret;
}
int main(int argc, char **argv) {

    if(argc == 2) {
        std::vector<std::string> files;
    	add_directory(argv[1], ".", files);
        output_Makefile(files);
        
    }
    
    
    return 0;
}
 void check_sub_directory(const std::string &dirname, bool add_sub_directory)
 {
     boost::filesystem::directory_iterator end;
     for (boost::filesystem::directory_iterator iter(dirname); iter != end; ++iter) {
         if (boost::filesystem::is_directory(*iter)) {
             if (add_sub_directory) {
                 try {
                     add_directory((*iter).path().string());
                 } catch (const std::exception&) {
                     continue;
                 }
             } else {
                 remove_directory((*iter).path().string());
             }
         }
     }
 }
Beispiel #5
0
void LASwriteOpener::set_file_name(const CHAR* file_name)
{
  if (this->file_name) free(this->file_name);
  if (file_name)
  {
    if (!specified)
    {
      int len = strlen(file_name);
      const CHAR* format = file_name + len - 4;
      if (strstr(format, ".laz") || strstr(format, ".LAZ"))
      {
        this->format = LAS_TOOLS_FORMAT_LAZ;
      }
      else if (strstr(format, ".las") || strstr(format, ".LAS"))
      {
        this->format = LAS_TOOLS_FORMAT_LAS;
      }
      else if (strstr(format, ".bin") || strstr(format, ".BIN")) // terrasolid
      {
        this->format = LAS_TOOLS_FORMAT_BIN;
      }
      else if (strstr(format, ".qi") || strstr(format, ".QI")) // QFIT
      {
        this->format = LAS_TOOLS_FORMAT_QFIT;
      }
      else if (strstr(format, ".wrl") || strstr(format, ".WRL")) // VRML
      {
        this->format = LAS_TOOLS_FORMAT_VRML;
      }
      else // assume ascii output
      {
        this->format = LAS_TOOLS_FORMAT_TXT;
      }
    }
    this->file_name = strdup(file_name);

    if (directory) add_directory();
    if (cut) cut_characters();
    if (appendix) add_appendix();
  }
  else
  {
    this->file_name = 0;
  }
}
Beispiel #6
0
static BOOL add_file_or_directory( HFCI fci, WCHAR *name )
{
    DWORD attr = GetFileAttributesW( name );

    if (attr == INVALID_FILE_ATTRIBUTES)
    {
        WINE_MESSAGE( "cannot open %s\n", wine_dbgstr_w(name) );
        return FALSE;
    }
    if (attr & FILE_ATTRIBUTE_DIRECTORY)
    {
        if (opt_recurse) return add_directory( fci, name );
        WINE_MESSAGE( "cabarc: Cannot add %s, it's a directory (use -r for recursive add)\n",
                      wine_dbgstr_w(name) );
        return FALSE;
    }
    return add_file( fci, name );
}
Beispiel #7
0
void LASwriteOpener::set_directory(const CHAR* directory)
{
  if (this->directory) free(this->directory);
  if (directory)
  {
    this->directory = strdup(directory);
    int len = strlen(this->directory);
    if (len && (this->directory[len-1] == DIRECTORY_SLASH))
    {
      this->directory[len-1] = '\0';
    }
    if (file_name) add_directory();
  }
  else
  {
    this->directory = 0;
  }
}
Beispiel #8
0
void
cvs_add_remote(struct cvs_file *cf)
{
	char path[MAXPATHLEN];

	cvs_log(LP_TRACE, "cvs_add_remote(%s)", cf->file_path);

	cvs_file_classify(cf, cvs_directory_tag);

	if (cf->file_type == CVS_DIR) {
		cvs_get_repository_path(cf->file_wd, path, MAXPATHLEN);
		if (strlcat(path, "/", sizeof(path)) >= sizeof(path))
			fatal("cvs_add_remote: truncation");
		if (strlcat(path, cf->file_path, sizeof(path)) >= sizeof(path))
			fatal("cvs_add_remote: truncation");
		cvs_client_send_request("Directory %s\n%s", cf->file_path,
		    path);

		add_directory(cf);
	} else {
		cvs_client_sendfile(cf);
	}
}
Beispiel #9
0
// worker requests items from the queue which are either file or
// directory paths. If item is
// 1) a filepath: computes and prints the hash of the file
// 2) a directory path: adds contained files and directories contained to
//    the queue
void worker(StringQueue& fileQueue, const Printer& printer, RefData& rd,
  Stats& stats, CmdLineOpts& opts) {

  // if we receive a non-empty refMap we compare against it
  bool compare = false;
  if (!rd.refMap.empty()) {
    compare = true;
  }

  while (!fileQueue.done()) {
    auto path = fileQueue.try_and_wait();
    if (path.empty()) {
      break;
    }

    // check if path is a directory or a file
    struct stat info;
    if (lstat(path.c_str(), &info) < 0) {
      printer.cerr("lstat failed on " + path);
      continue;
    }
    if (S_ISREG(info.st_mode)) {
      std::string hash = hasher(opts.hashMethod, path);
      if (opts.collectStats) {
        stats.add(info.st_size);
      }
      if (compare) {
        compare_to_reference(path, hash, printer, rd);
      } else {
        printer.cout(opts.hashMethod + " , " + path + " , " + hash);
      }
    } else if (S_ISDIR(info.st_mode)) {
      add_directory(fileQueue, path, printer);
    }
  }
}
Beispiel #10
0
void
cvs_add_local(struct cvs_file *cf)
{
	cvs_log(LP_TRACE, "cvs_add_local(%s)", cf->file_path);

	if (cvs_cmdop != CVS_OP_CHECKOUT && cvs_cmdop != CVS_OP_UPDATE)
		cvs_file_classify(cf, cvs_directory_tag);

	/* dont use `cvs add *' */
	if (strcmp(cf->file_name, ".") == 0 ||
	    strcmp(cf->file_name, "..") == 0 ||
	    strcmp(cf->file_name, CVS_PATH_CVSDIR) == 0) {
		if (verbosity > 1)
			cvs_log(LP_ERR,
			    "cannot add special file `%s'; skipping",
			    cf->file_name);
		return;
	}

	if (cf->file_type == CVS_DIR)
		add_directory(cf);
	else
		add_file(cf);
}
Beispiel #11
0
/* build a complete fake dll from scratch */
static BOOL build_fake_dll( HANDLE file, const WCHAR *name )
{
    static const WCHAR dotexeW[] = { '.','e','x','e',0 };
    IMAGE_DOS_HEADER *dos;
    IMAGE_NT_HEADERS *nt;
    struct dll_info info;
    const WCHAR *ext;
    BYTE *buffer;
    BOOL ret = FALSE;
    DWORD lfanew = (sizeof(*dos) + sizeof(fakedll_signature) + 15) & ~15;
    DWORD size, header_size = lfanew + sizeof(*nt);

    info.handle = file;
    buffer = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, header_size + 8 * sizeof(IMAGE_SECTION_HEADER) );

    dos = (IMAGE_DOS_HEADER *)buffer;
    dos->e_magic    = IMAGE_DOS_SIGNATURE;
    dos->e_cblp     = sizeof(*dos);
    dos->e_cp       = 1;
    dos->e_cparhdr  = lfanew / 16;
    dos->e_minalloc = 0;
    dos->e_maxalloc = 0xffff;
    dos->e_ss       = 0x0000;
    dos->e_sp       = 0x00b8;
    dos->e_lfarlc   = lfanew;
    dos->e_lfanew   = lfanew;
    memcpy( dos + 1, fakedll_signature, sizeof(fakedll_signature) );

    nt = info.nt = (IMAGE_NT_HEADERS *)(buffer + lfanew);
    /* some fields are copied from the source dll */
#if defined __x86_64__
    nt->FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64;
#elif defined __aarch64__
    nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARM64;
#elif defined __arm__
    nt->FileHeader.Machine = IMAGE_FILE_MACHINE_ARMNT;
#elif defined __powerpc__
    nt->FileHeader.Machine = IMAGE_FILE_MACHINE_POWERPC;
#else
    nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
#endif
    nt->FileHeader.TimeDateStamp = 0;
    nt->FileHeader.Characteristics = 0;
    nt->OptionalHeader.MajorLinkerVersion = 1;
    nt->OptionalHeader.MinorLinkerVersion = 0;
    nt->OptionalHeader.MajorOperatingSystemVersion = 1;
    nt->OptionalHeader.MinorOperatingSystemVersion = 0;
    nt->OptionalHeader.MajorImageVersion = 1;
    nt->OptionalHeader.MinorImageVersion = 0;
    nt->OptionalHeader.MajorSubsystemVersion = 4;
    nt->OptionalHeader.MinorSubsystemVersion = 0;
    nt->OptionalHeader.Win32VersionValue = 0;
    nt->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
    nt->OptionalHeader.DllCharacteristics = 0;
    nt->OptionalHeader.SizeOfStackReserve = 0;
    nt->OptionalHeader.SizeOfStackCommit = 0;
    nt->OptionalHeader.SizeOfHeapReserve = 0;
    nt->OptionalHeader.SizeOfHeapCommit = 0;
    /* other fields have fixed values */
    nt->Signature                              = IMAGE_NT_SIGNATURE;
    nt->FileHeader.NumberOfSections            = 0;
    nt->FileHeader.SizeOfOptionalHeader        = IMAGE_SIZEOF_NT_OPTIONAL_HEADER;
    nt->OptionalHeader.Magic                   = IMAGE_NT_OPTIONAL_HDR_MAGIC;
    nt->OptionalHeader.ImageBase               = 0x10000000;
    nt->OptionalHeader.SectionAlignment        = section_alignment;
    nt->OptionalHeader.FileAlignment           = file_alignment;
    nt->OptionalHeader.NumberOfRvaAndSizes     = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;

    header_size = (BYTE *)(nt + 1) - buffer;
    info.mem_pos  = ALIGN( header_size, section_alignment );
    info.file_pos = ALIGN( header_size, file_alignment );

    nt->OptionalHeader.AddressOfEntryPoint = info.mem_pos;
    nt->OptionalHeader.BaseOfCode          = info.mem_pos;

    ext = strrchrW( name, '.' );
    if (!ext || strcmpiW( ext, dotexeW )) nt->FileHeader.Characteristics |= IMAGE_FILE_DLL;

    if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
    {
        size = sizeof(dll_code_section);
        if (!xwrite( &info, dll_code_section, size, info.file_pos )) goto done;
    }
    else
    {
        size = sizeof(exe_code_section);
        if (!xwrite( &info, exe_code_section, size, info.file_pos )) goto done;
    }
    nt->OptionalHeader.SizeOfCode = size;
    add_section( &info, ".text", size, IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ );

    if (!xwrite( &info, &reloc_section, sizeof(reloc_section), info.file_pos )) goto done;
    add_directory( &info, IMAGE_DIRECTORY_ENTRY_BASERELOC, info.mem_pos, sizeof(reloc_section) );
    add_section( &info, ".reloc", sizeof(reloc_section),
                 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_MEM_READ );

    header_size += nt->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
    nt->OptionalHeader.SizeOfHeaders = ALIGN( header_size, file_alignment );
    nt->OptionalHeader.SizeOfImage   = ALIGN( info.mem_pos, section_alignment );
    ret = xwrite( &info, buffer, header_size, 0 );
done:
    HeapFree( GetProcessHeap(), 0, buffer );
    return ret;
}
Beispiel #12
0
/* build a complete fake dll, optionally using module as a source */
static BOOL build_fake_dll( HANDLE file, HMODULE module )
{
    IMAGE_DOS_HEADER *dos;
    IMAGE_NT_HEADERS *nt;
    const IMAGE_NT_HEADERS *src_nt = NULL;
    struct dll_info info;
    BYTE *buffer;
    BOOL ret = FALSE;
    DWORD lfanew = (sizeof(*dos) + sizeof(fakedll_signature) + 15) & ~15;
    DWORD size, header_size = lfanew + sizeof(*nt);

    info.handle = file;
    buffer = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, header_size + 8 * sizeof(IMAGE_SECTION_HEADER) );

    dos = (IMAGE_DOS_HEADER *)buffer;
    dos->e_magic    = IMAGE_DOS_SIGNATURE;
    dos->e_cblp     = sizeof(*dos);
    dos->e_cp       = 1;
    dos->e_cparhdr  = lfanew / 16;
    dos->e_minalloc = 0;
    dos->e_maxalloc = 0xffff;
    dos->e_ss       = 0x0000;
    dos->e_sp       = 0x00b8;
    dos->e_lfarlc   = lfanew;
    dos->e_lfanew   = lfanew;
    memcpy( dos + 1, fakedll_signature, sizeof(fakedll_signature) );

    nt = info.nt = (IMAGE_NT_HEADERS *)(buffer + lfanew);
    src_nt = RtlImageNtHeader( module );
    /* some fields are copied from the source dll */
#define SET(field,def) nt->field = src_nt ? src_nt->field : def
    SET( FileHeader.Machine, IMAGE_FILE_MACHINE_I386 );
    SET( FileHeader.TimeDateStamp, 0 );
    SET( FileHeader.Characteristics, IMAGE_FILE_DLL );
    SET( OptionalHeader.MajorLinkerVersion, 1 );
    SET( OptionalHeader.MinorLinkerVersion, 0 );
    SET( OptionalHeader.MajorOperatingSystemVersion, 1 );
    SET( OptionalHeader.MinorOperatingSystemVersion, 0 );
    SET( OptionalHeader.MajorImageVersion, 1 );
    SET( OptionalHeader.MinorImageVersion, 0 );
    SET( OptionalHeader.MajorSubsystemVersion, 4 );
    SET( OptionalHeader.MinorSubsystemVersion, 0 );
    SET( OptionalHeader.Win32VersionValue, 0 );
    SET( OptionalHeader.Subsystem, IMAGE_SUBSYSTEM_WINDOWS_GUI );
    SET( OptionalHeader.DllCharacteristics, 0 );
    SET( OptionalHeader.SizeOfStackReserve, 0 );
    SET( OptionalHeader.SizeOfStackCommit, 0 );
    SET( OptionalHeader.SizeOfHeapReserve, 0 );
    SET( OptionalHeader.SizeOfHeapCommit, 0 );
#undef SET
    /* other fields have fixed values */
    nt->Signature                              = IMAGE_NT_SIGNATURE;
    nt->FileHeader.NumberOfSections            = 0;
    nt->FileHeader.SizeOfOptionalHeader        = IMAGE_SIZEOF_NT_OPTIONAL_HEADER;
    nt->OptionalHeader.Magic                   = IMAGE_NT_OPTIONAL_HDR_MAGIC;
    nt->OptionalHeader.ImageBase               = 0x10000000;
    nt->OptionalHeader.SectionAlignment        = section_alignment;
    nt->OptionalHeader.FileAlignment           = file_alignment;
    nt->OptionalHeader.NumberOfRvaAndSizes     = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;

    header_size = (BYTE *)(nt + 1) - buffer;
    info.mem_pos  = ALIGN( header_size, section_alignment );
    info.file_pos = ALIGN( header_size, file_alignment );

    nt->OptionalHeader.AddressOfEntryPoint = info.mem_pos;
    nt->OptionalHeader.BaseOfCode          = info.mem_pos;

    if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
    {
        size = sizeof(dll_code_section);
        if (!xwrite( &info, dll_code_section, size, info.file_pos )) goto done;
    }
    else
    {
        size = sizeof(exe_code_section);
        if (!xwrite( &info, exe_code_section, size, info.file_pos )) goto done;
    }
    nt->OptionalHeader.SizeOfCode = size;
    add_section( &info, ".text", size, IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ );

    if (!xwrite( &info, &reloc_section, sizeof(reloc_section), info.file_pos )) goto done;
    add_directory( &info, IMAGE_DIRECTORY_ENTRY_BASERELOC, info.mem_pos, sizeof(reloc_section) );
    add_section( &info, ".reloc", sizeof(reloc_section),
                 IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_MEM_READ );

    if (!add_version_resource( module, &info )) goto done;

    header_size += nt->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
    nt->OptionalHeader.SizeOfHeaders = ALIGN( header_size, file_alignment );
    nt->OptionalHeader.SizeOfImage   = ALIGN( info.mem_pos, section_alignment );
    ret = xwrite( &info, buffer, header_size, 0 );
done:
    HeapFree( GetProcessHeap(), 0, buffer );
    return ret;
}
Beispiel #13
0
void LASwriteOpener::make_file_name(const CHAR* file_name, I32 file_number)
{
  int len;

  if (cut) cut_characters();

  if (file_number > -1)
  {
    if (file_name)
    {
      if (this->file_name) free(this->file_name);
      len = strlen(file_name);
      this->file_name = (CHAR*)malloc(len+10);
      strcpy(this->file_name, file_name);
    }
    else
    {
      if (this->file_name == 0)
      {
        this->file_name = strdup("output.000000.xxx");
      }
      len = strlen(this->file_name);
    }
    while (len > 0 && this->file_name[len] != '.') len--;
    len++;
    int num = len - 2;
    while (num > 0 && this->file_name[num] >= '0' && this->file_name[num] <= '9')
    {
      this->file_name[num] = '0' + (file_number%10);
      file_number = file_number/10;
      num--;
    }
  }
  else
  {
    if (this->file_name) free(this->file_name);
    if (file_name)
    {
      len = strlen(file_name);
      this->file_name = (CHAR*)malloc(len + (appendix ? strlen(appendix) + 5 : 10));
      strcpy(this->file_name, file_name);
      while (len > 0 && this->file_name[len] != '.') len--;
      if (appendix != 0)
      {
        strcpy(&(this->file_name[len]), appendix);
        len += strlen(appendix);
      }
      else if ((directory == 0) && (cut == 0) && (file_number == -1))
      {
        this->file_name[len] = '_';
        this->file_name[len+1] = '1';
        len += 2;
      }
      this->file_name[len] = '.';
      len++;
    }
    else
    {
      len = 7;
      this->file_name = strdup("output.xxx");
    }
  }
  if (format <= LAS_TOOLS_FORMAT_LAS)
  {
    this->file_name[len] = 'l';
    this->file_name[len+1] = 'a';
    this->file_name[len+2] = 's';
  }
  else if (format == LAS_TOOLS_FORMAT_LAZ)
  {
    this->file_name[len] = 'l';
    this->file_name[len+1] = 'a';
    this->file_name[len+2] = 'z';
  }
  else if (format == LAS_TOOLS_FORMAT_BIN)
  {
    this->file_name[len] = 'b';
    this->file_name[len+1] = 'i';
    this->file_name[len+2] = 'n';
  }
  else if (format == LAS_TOOLS_FORMAT_QFIT)
  {
    this->file_name[len] = 'q';
    this->file_name[len+1] = 'i';
    this->file_name[len+2] = '\0';
  }
  else // if (format == LAS_TOOLS_FORMAT_TXT)
  {
    this->file_name[len] = 't';
    this->file_name[len+1] = 'x';
    this->file_name[len+2] = 't';
  }
  this->file_name[len+3] = '\0';

  if (directory) add_directory();

  if (file_name && (strcmp(this->file_name, file_name) == 0))
  {
    if (!force)
    {
      free(this->file_name);
      if (format <= LAS_TOOLS_FORMAT_LAS)
      {
        this->file_name = strdup("temp.las");
      }
      else if (format == LAS_TOOLS_FORMAT_LAZ)
      {
        this->file_name = strdup("temp.laz");
      }
      else if (format == LAS_TOOLS_FORMAT_BIN)
      {
        this->file_name = strdup("temp.bin");
      }
      else if (format == LAS_TOOLS_FORMAT_QFIT)
      {
        this->file_name = strdup("temp.qi");
      }
      else if (format == LAS_TOOLS_FORMAT_VRML)
      {
        this->file_name = strdup("temp.wrl");
      }
      else // if (format == LAS_TOOLS_FORMAT_TXT)
      {
        this->file_name = strdup("temp.txt");
      }
      fprintf(stderr,"WARNING: generated output name '%s'\n", file_name);
      fprintf(stderr,"         identical to input name. changed to '%s'.\n", this->file_name);
      fprintf(stderr,"         you can override this safety measure with '-oforce'.\n");
    }
  }
}
Beispiel #14
0
// This function adds a directory marker to the header file
void add_directory( char * dirname)
{
	char path[256];
	char *pathptr = path;
	char *tmpptr;

	strcpy(path, dirname);
	fwrite(&Total_size, 1, 4, fp_out_hdr);
	int i = 0;
	fwrite(&i, 1, 4, fp_out_hdr);
	// strip out any directories that this dir is a subdir of
#ifndef PLAT_UNIX
	while ((tmpptr = strchr(pathptr, '\\')) != NULL) {
#else
	while ((tmpptr = strchr(pathptr, '/')) != NULL) {
#endif
		pathptr = tmpptr+1;
	}
	fwrite(pathptr, 1, 32, fp_out_hdr);
	fwrite(&i, 1, 4, fp_out_hdr); // timestamp = 0
	Num_files++;
}

void pack_directory( char * filespec)
{
#ifndef PLAT_UNIX
	int find_handle;
	_finddata_t find;
#endif
	char tmp[512];
	char tmp1[512];

/*
	char dir_name[512];
	char *last_slash;

	last_slash = strrchr(filespec, '\\');
	if ( last_slash ) {
		strcpy(dir_name, last_slash+1);
	} else {
		strcpy(dir_name, filespec);
	}

	if ( !stricmp(dir_name, "voice") ) {
		return;
	}
*/

#ifdef PLAT_UNIX
	char *ts;

	// strip trailing '/'
	ts = filespec+(strlen(filespec)-1);
	while(*ts == '/' && ts > filespec)
		*ts = '\0';

	strcpy( tmp1, filespec );

	add_directory(filespec);
	strcat( tmp1, "/*.*" );
#else
	strcpy( tmp1, filespec );

	add_directory(filespec);
	strcat( tmp1, "\\*.*" );
#endif
	
	printf( "In dir '%s'\n", tmp1 );

#ifndef PLAT_UNIX
	find_handle = _findfirst( tmp1, &find );
	if( find_handle != -1 )	{
		if ( find.attrib & _A_SUBDIR )	{
			if (strcmp( "..", find.name) && strcmp( ".", find.name))	{
				strcpy( tmp, filespec );
				strcat( tmp, "\\" );
				strcat( tmp, find.name );
				pack_directory(tmp);
			}
		} else {
			pack_file( filespec, find.name, find.size, (fs_time_t)find.time_write );
		}

		while( !_findnext( find_handle, &find ) )	{
			if ( find.attrib & _A_SUBDIR )	{
				if (strcmp( "..", find.name) && strcmp( ".", find.name))	{
					strcpy( tmp, filespec );
					strcat( tmp, "\\" );
					strcat( tmp, find.name );
					pack_directory(tmp);

				}
			} else {
				pack_file( filespec, find.name, find.size, find.time_write );
			}
		}
	}
#else
	DIR *dirp;
	struct dirent *dir;

	dirp = opendir (filespec);
	if ( dirp ) {
		while ((dir = readdir(dirp)) != NULL) {

			char fn[MAX_PATH];
			snprintf(fn, MAX_PATH-1, "%s/%s", filespec, dir->d_name);
			fn[MAX_PATH-1] = 0;
			
			struct stat buf;
			if (stat(fn, &buf) == -1) {
				continue;
			}

			if ( (strcmp(dir->d_name, ".") == 0) || (strcmp(dir->d_name, "..") == 0) ) {
				continue;
			}

			if (S_ISDIR(buf.st_mode)) {
				strcpy( tmp, filespec );
				strcat( tmp, "/" );
				strcat( tmp, dir->d_name );
				pack_directory(tmp);
			} else {
				pack_file( filespec, dir->d_name, buf.st_size, buf.st_mtime );
			}
		}
		closedir(dirp);
	} else {
		printf("Error: Source directory does not exist!\n");
		no_dir = 1;
	}
#endif
	add_directory("..");
}
Beispiel #15
0
void main_loop(pDataStruct workingData){
	char *commandList[] = {"set", "get", "add", "delete","exit","help","ls","cat","cd"};
	char *setList[] = {"date", "user", "group","help"};
	char *getList[] = {"date", "user", "group", "workingDir","help", "userlist", "grouplist", "usersingroup"};
	char *addList[] = {"user", "group", "file", "directory", "usertogroup","perm", "appendfile","help"};
	char *deleteList[] = {"user", "group", "file", "directory", "usertogroup","perm", "filebytes","help"};
	char arg[16][MAXCOMMAND];
	char command[MAXCOMMAND];
	int argnum, i, j, tempNum, cmd, size;
	char *c;
	char temp[MAXPATH];
	char *tempBuf;
	unsigned char *rcvBuf;
	pNode tempNode = NULL;
	pUser tempUser = NULL;
	pGroup tempGroup = NULL;
	pFile tempFile = NULL;
	pPerms tempPerms = NULL;
	pFileChunk tempFileChunk = NULL;
	pGroupUserList tempGroupUserList = NULL;

	while(1){
		argnum = i = j = tempNum = cmd = 0;
		c = 0;
		bzero(temp, sizeof(temp));
		tempBuf = NULL;
		tempNode = NULL;
		tempUser = NULL;
		tempGroup = NULL;
		tempFile = NULL;
		tempPerms = NULL;
		tempFileChunk = NULL;
		tempGroupUserList = NULL;		
		bzero(command, sizeof(command));
		print_prompt(workingData);
		get_string(command, MAXCOMMAND-1, "");
		for (j = 0;j<16;j++){
			bzero(arg[j],MAXCOMMAND);
		}
		argnum = parse_arg(arg, command);
		tempNum = 0;
		cmd = 100;

		for (i=0;i<sizeof(commandList)/4;i++){
			if (strcmp(commandList[i],arg[0]) == 0 ){
				cmd = i;
			}
		}
		switch(cmd)
		{
			case 0x0 : //set
				cmd = 100;
				for (i=0;i<sizeof(setList)/4;i++){
					if (strcmp(setList[i],arg[1]) == 0){
						cmd = i;
					}
				}
				switch(cmd)
				{

					case 0 : //set date
						workingData->date = get_time();
						break;//end set date

					case 1 : //set user

						if ( strcmp(arg[2],"") == 0){
							puts("missing user name or number");
							break;
						}
						tempUser = find_user(arg[2],workingData);
						if ( tempUser != NULL ){
							workingData->currentUser = tempUser;
						} else { 
							printf("unknown user: @s\n",arg[2]);
						}
						break;//end set user

					case 2 : //set group

						if ( strcmp(arg[2], "") == 0){
							puts("missing group name or number");
							break;
						}
						tempGroup = find_group(arg[2], workingData);
						if ( tempGroup != NULL){
							workingData->currentGroup = tempGroup;
						} else {
							printf("unknown group: @s\n",arg[2]);
						}
						break;

					case 3 : //set help
						print_help(setList,(sizeof(setList)/4));
						break;//end set help

					default :
						printf("Invalid command: @s\n",arg[1]);

				}

				break;//end set 

			case 1 ://get
				cmd = 100;
				for (i=0;i<sizeof(getList)/4;i++){
					if (strcmp(getList[i],arg[1]) == 0){
						cmd = i;
					}
				}
				switch(cmd)
				{

					case 0 : //get date
						strofdate( workingData->date);
						break;//end get date

					case 1 : //get user
						printf("@s\n",workingData->currentUser->name);
						break;//end get user

					case 2 : //get group
						printf("@s\n",workingData->currentGroup->name);
						break;//end get group

					case 3 : //get workingDir
						bzero(temp,MAXPATH);
						str_of_path( temp, workingData, workingData->workingDir);
						printf("@s/\n",temp);
						break;//end get workingDir

					case 4 : //get help
						print_help(getList,(sizeof(getList)/4));
						break;//end get help

					case 5 : //get userlist
						print_user_list(workingData);

						break;//end get userlist

					case 6 : //get grouplist
						print_group_list(workingData);
						break;//end get grouplist

					case 7 : //get usersingroup
						if ( strcmp(arg[2], "") == 0){
							puts("missing group name or number");
							break;
						}
						tempGroup = find_group(arg[2], workingData);
						if ( tempGroup != NULL ){
							print_users_in_group(tempGroup);
							break;
						}
						printf("unknown group: @s\n",arg[2]);
						break;//end get useringroup						

					default :
						printf("Invalid command: @s\n",arg[1]);
				}

				break;//end get

			case 2 ://add
				cmd = 100;
				for (i=0;i<sizeof(addList)/4;i++){
					if ( strcmp(addList[i],arg[1]) == 0 ){
						cmd = i;
					}
				}
				switch(cmd)
				{
					case 0 : //add user

						if ( strcmp(arg[2],"") == 0 ){
							bzero(temp,MAXPATH);
							get_string(temp, 128, "UserName");
							strcpy(arg[2],temp);
						}
						tempUser = find_user( arg[2], workingData);
						if ( tempUser == NULL ){
							add_user( arg[2], workingData );
						} else {
							puts("Username already in use");
						}
						break;//end add user

					case 1 : //add group
						if ( strcmp(arg[2],"") == 0 ){
							bzero(temp,MAXPATH);
							get_string(temp, 128, "GroupName");
							strcpy(arg[2], temp);
						}
						tempGroup = find_group( arg[2], workingData);
						if ( tempGroup == NULL ){
							add_group( arg[2], workingData );
						} else {
							puts("Groupname already in use");
						}
						break;//end add group

					case 2 : //add file
						//add file name size
						tempNum = atoi(arg[3]);
						if (strcmp(arg[2],"") == 0){
							puts("Filename required");
							break;
						}
						if ( tempNum > MAXUPLOAD ){
							puts("Too big");
							break;
						}
						if ( find_node_by_name(arg[2],workingData->workingDir->directoryHeadNode) != NULL ){
							puts("There is another file or directory with that name");
							break;
						}
						if ( tempNum > 0){
							rcvBuf = mallocOrDie(tempNum,"Failed to allocate tempBuf");
							puts("-----Begin File-----");//five - 
							if ( tempNum != receive_bytes(rcvBuf,tempNum) ){
								die("Failed to reveive file");
								break;
							}
						} else {rcvBuf = NULL;}
						tempNode = add_file( workingData->workingDir, workingData->date, tempNum, arg[2], (char *)rcvBuf, workingData->currentUser );
						break;//end add file

					case 3 : //add directory

						if (strcmp(arg[2],"") == 0){
							puts("Directory name required");
							break;
						}
						if ( find_node_by_name(arg[2],workingData->workingDir->directoryHeadNode) != NULL ){
							puts("There is another file or directory with that name");
							break;
						}
						tempNode = add_directory( workingData->date, arg[2], workingData->workingDir, workingData->currentUser );
						break;//end add directory

					case 4 : //add useringroup
						if (strcmp(arg[2],"") == 0){
							puts("username or number required");
							break;
						}
						tempUser = find_user( arg[2], workingData);
						if ( tempUser != NULL ){//user exists
							tempGroupUserList = is_user_in_group( tempUser, workingData->currentGroup );
							if ( tempGroupUserList == NULL ){//user is not already in group	
								add_user_to_group( tempUser, workingData->currentGroup );
							} else {printf("@s is already in @s\n",arg[2], workingData->currentGroup->name);}
						} else {
							puts("User does not exist, add user first");
						}

						break;//end add useringroup
						
					case 5 : //add perm
						if (  ( strcmp(arg[2],"") == 0 )||( strcmp(arg[2]," ") == 0 )  ){//arg[2] name of file or dir
							puts("name of file or directory required");
							break;
						}
						if (!(  (strcmp(arg[3],"user") == 0) ^ (strcmp(arg[3],"group") == 0) )) {//arg[3] user, group
							puts("'user' or 'group'");
							break;
						}
						if (strcmp(arg[4],"") == 0){
							puts("user name, group name, or number required");//arg[4] name or number of user or group
							break;
						}
						tempNode = find_node_by_name(arg[2],workingData->workingDir->directoryHeadNode );//validate file or dir
						if (tempNode == NULL){
							puts("Invalid file or directory");
							break;
						}
						if (tempNode->type == LINK){
							tempNode = tempNode->directoryHeadNode;
							break;
						}
						if (strcmp(arg[3],"user") == 0){
							tempUser = find_user(arg[4],workingData);
							tempGroup = NULL;
							if (tempUser == NULL){
								printf("user @s not found\n",arg[4]);
								break;
							}
						} else {
							tempGroup = find_group(arg[4],workingData);
							tempUser = NULL;
							if (tempGroup == NULL){
								printf("group @s not found\n",arg[4]);
								break;
							}
						}

						validate_current_perms(tempNode, workingData);
						tempPerms = add_perm(tempUser, tempGroup, tempNode ); 
						break;//end add perm

					case 6 : //add appendfile
						tempNum = atoi(arg[3]);
						if (strcmp(arg[2],"") == 0){
							puts("Filename required");
							break;
						}
						if ( tempNum > MAXUPLOAD ){
							puts("Too big");
							break;
						}
						tempFile = find_file_by_name(arg[2],workingData->workingDir);
						if (  tempFile == NULL ){
							puts("No file in working directory by that name");
							break;
						}
						if ( tempNum > 0){
							tempBuf = mallocOrDie(tempNum,"Failed to allocate tempBuf");
							puts("-----Begin File-----");//five - 
							if ( tempNum != receive_bytes((unsigned char *)tempBuf,tempNum) ){
								die("Failed to reveive file");
								break;
							}
						} else {
							tempBuf = NULL;
							puts("Can not add 0 bytes to file");
							break;
						}
						tempFileChunk = add_file_chunk( tempBuf, tempFile, tempNum );
						break;//end add appendfile

					case 7 : //add help
						print_help(addList, (sizeof(addList)/4));	
						break;//end add help

					default :
						printf("Invalid command: @s\n",arg[1]);
				}

				break;//end add
			case 3 ://delete 	
				cmd = 100;
				for (i=0;i<sizeof(deleteList)/4;i++){
					if ( strcmp(deleteList[i],arg[1]) == 0 ){
						cmd = i;
					}
				}
				switch(cmd)
				{

					case 0 : //delete user
						bzero(temp,MAXPATH);
						if ( strcmp(arg[2],"") == 0 ){
							get_string(temp, 128, "User Name or number");
							strcpy(arg[2],temp);
						}
						tempUser = find_user( arg[2], workingData);
						if ( tempUser != NULL ){
							remove_user(tempUser , workingData );
						} else {
							puts("No such user found");
						}
						break;//end delete user

					case 1 : //delete group
						bzero(temp,MAXPATH);
						if ( strcmp(arg[2],"") == 0 ){
							get_string(temp, 128, "Group Name or number");
							strcpy(arg[2],temp);
						}
						tempGroup = find_group( arg[2], workingData);
						if ( tempGroup != NULL ){
							remove_group(tempGroup , workingData );
						} else {
							puts("no such group found");
						}
						break;//end delete group 

					case 2 : //delete file 
						if (strcmp(arg[2],"") == 0){
							puts("Filename required");
							break;
						}
						tempNode = find_file_node_by_name(arg[2],workingData->workingDir);
						if (tempNode == NULL){
							puts("No such file");
							break;
						}
						delete_node(tempNode, workingData);
						break;//end delete file 

					case 3 : //delete directory
						if (strcmp(arg[2],"") == 0){
							puts("Directory name required");
							break;
						}
						tempNode = find_directory_by_name(arg[2],workingData->workingDir);
						if (tempNode == NULL){
							puts("No such directory");
							break;
						}
						delete_node(tempNode, workingData);
						break;//end delete directory

					case 4 : //delete usertogroup
						if (strcmp(arg[2],"") == 0){
							puts("User name required");
							break;
						}
						if (strcmp(arg[3],"") == 0){
							puts("group name required");
							break;
						}
						tempUser = find_user(arg[2],workingData);
						if (tempUser == NULL){
							puts("No such user");
							break;
						}
						tempGroup = find_group(arg[3],workingData);
						if (tempGroup == NULL){
							puts("No such group");
							break;
						}
						tempGroupUserList = is_user_in_group(tempUser, tempGroup);
						if (tempGroupUserList == NULL){
							puts("User is not in group");
							break;
						}
						remove_user_from_group(tempUser, tempGroup);
						break;//end delete usertogroup

					case 5 : //delete perm
						if (strcmp(arg[3],"") == 0){
							puts("User or group name required");
							break;
						}
						if (strcmp(arg[2],"") == 0){
							puts("file name required");
							break;
						}
						tempNode = find_node_by_name(arg[2],workingData->workingDir->directoryHeadNode);
						if (tempNode == NULL){
							puts("No such file");
							break;
						}
						tempPerms = find_perm_by_name(arg[3],tempNode,workingData);
						if (tempPerms != NULL){
							delete_perms(tempNode, tempPerms);
						} else {
							puts("No such user permission on file");
						}
						break;//end delete perm

					case 6 : //delete filebytes [file] [numbytes]
						if (strcmp(arg[2],"") == 0){
							puts("Filename required");
							break;
						}
						size = strict_atoi(arg[3]);
						if (size == 0){
							puts("zero bytes deleted");
							break;
						}
						//validate file						
						tempNode = find_file_node_by_name(arg[2],workingData->workingDir);
						tempFile = tempNode->file;
						if (tempNode == NULL){
							puts("No such file");
							printf("@s\n",arg[2]);
							break;
						}
						tempNum = get_file_size(tempFile);
						if (size > tempNum){
							puts("Too many bytes");
						} 
						//tempNum is new file size
						tempNum = tempNum - size;
						delete_file_bytes(tempFile, tempNum);
						break;//end delete file 

					case 7 : //delete help
						print_help(deleteList, (sizeof(deleteList)/4));	
						break;//end delete help

					default:
						print_help(deleteList, (sizeof(deleteList)/4));	
						//break;//end delete help

				}
				break;//end delete
				

			case 4 ://cgc_exit
				puts("exiting");
				goto cgc_exit;
				break;

			case 5 ://help
				print_help(commandList, (sizeof(commandList)/4));
				break;
			case 6 ://ls
				print_working_dir(workingData);
				break;
			case 7 ://cat
				if (strcmp(arg[1],"") == 0){
					puts("Filename required");
					break;
				}
				tempFile = find_file_by_name(arg[1], workingData->workingDir);							
				if ( tempFile != NULL ){
					tempFileChunk = tempFile->head;
					puts("-----Begin File-----");//five - 
					while ( tempFileChunk != NULL ){
						if (tempFileChunk->chunkSize != cgc_write(tempFileChunk,tempFileChunk->chunkSize)){
							puts("file write failed");
						}
/*
						c = tempFileChunk->chunk;
						for ( i = 0; i < tempFileChunk->chunkSize; i++ ){//putc each byte of every chunk
							putc( *c);
							c++;
						}
*/						
						tempFileChunk = tempFileChunk->next;	
					}
					puts("-----END File-----");//five - 
				}
				break;
			case 8 ://cd
				if (strcmp(arg[1],"") == 0){
					puts("directory required");
					break;
				}
				if (strcmp(arg[1],"..") == 0){
					if (workingData->workingDir->parent != NULL){
						workingData->workingDir = workingData->workingDir->parent;
					}
					break;
				}
				tempNode = find_directory_by_name(arg[1],workingData->workingDir);
				if ( tempNode != NULL ){
					workingData->workingDir = tempNode;
					break;
				}
				puts("No such directory in working directory");
				break;//end cd

			default :
				printf("Invalid command @s\n",arg[0]);
				print_help(commandList, (sizeof(commandList)/4));
				
		}
	}
	cgc_exit:
	return;
}
Beispiel #16
0
/*
 * sets the location of the store
 * and the temp directory based on the location of our filesystem.
 */
void set_store_location(char* loc)
{
  storeLocation = prefix_path(loc, STORE_NAME);
  add_directory(TEMP_NAME);
}
Beispiel #17
0
std::string Dir_Cache_Manager::add_directory_ldap(std::string url, std::string subTree, const std::string setKey)
{
    return add_directory(SRef<Directory_Set_Item*>(new Directory_Set_Item(url, subTree)), setKey != "" ? setKey : get_new_directory_key());
}