int rm_dir(string dir) { mixed *file; int count; int i; /* if (! is_root(previous_object())) return 0; */ count = 0; if (dir[strlen(dir) - 1] != '/') dir += "/"; file = get_dir(dir, -1); i = sizeof(file); while (i--) { reset_eval_cost(); if (file[i][1] != -2) if (rm(dir + file[i][0])) count++; } i = sizeof(file); while (i--) { reset_eval_cost(); if (file[i][1] == -2) count += rm_dir(dir + file[i][0]); } write (HIC "删除目录(" + dir + ")。\n" NOR); rmdir(dir); return count; }
int main() { int sock; struct sockaddr_in server; char buffer[MAXBUF]; //Создание сокета sock = socket(AF_INET , SOCK_STREAM , 0); if (sock == -1) { printf("Could not create socket"); } puts("Socket created\n"); server.sin_addr.s_addr = inet_addr("127.0.0.1"); server.sin_family = AF_INET; server.sin_port = htons( 1234 ); //Connect to remote server if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) { perror("connect failed. Error"); return 1; } bzero(buffer, MAXBUF); recv(sock , buffer , MAXBUF , 0); // прием слова puts(buffer); printf("Для получения справки введите команду help\n"); while(1){ bzero(buffer, MAXBUF); printf("Cmd: "); scanf("%s" , buffer); // команды if(strcmp(buffer, "view") == 0) view_dir(buffer, sock, MAXBUF); else if(strcmp(buffer, "view_dir") == 0) view_dir(buffer, sock, MAXBUF); else if(strcmp(buffer, "help") == 0) get_help(); else if(strcmp(buffer, "get_dir") == 0) get_dir(buffer, sock, MAXBUF); else if(strcmp(buffer, "ch_dir") == 0) ch_dir(buffer, sock, MAXBUF); else if(strcmp(buffer, "upload") == 0) upload(buffer, MAXBUF, sock); else if(strcmp(buffer, "mk_dir") == 0) mk_dir(buffer, MAXBUF, sock); else if(strcmp(buffer, "rm_dir") == 0) rm_dir(buffer, MAXBUF, sock); else printf("unknown comand\n"); printf("Для получения справки введите команду help\n"); } close(sock); return 0; }
static int inotify_delete_self_helper( const char *name, const char *path, const void *dummy) { (void)dummy; return rm_dir(name, path); }
static void inotify_delete_self(const char *name, const char *path) { char filepath[PATH_MAX]; mk_filename(filepath, PATH_MAX, path, "inotify_dir"); if (mk_dir(name, filepath) < 0) return; inotify_exercise(name, filepath, filepath, "inotify_dir", inotify_delete_self_helper, IN_DELETE_SELF, NULL); /* We remove (again) in case the test failed */ (void)rm_dir(name, filepath); }
int file_rm(const char *fpath) { struct stat st; if (stat(fpath, &st)) return -1; if (!S_ISDIR(st.st_mode)) return unlink(fpath); return rm_dir(fpath); }
int syscall_rmdir(const char *pathname) { int i, j, drive, res; int curdir_handle; char name_comp[13], conv_name[11], dir_path[501]; if (strlen(pathname) > 500) return ELONGPATH; parse_path(pathname, &drive, dir_path, name_comp); if (name_comp[0] == 0 && strlen(dir_path) > 0) { // remove the last component if (dir_path[strlen(dir_path)-1] == '/' || dir_path[strlen(dir_path)-1] == '\\') dir_path[strlen(dir_path)-1] = 0; j = 13; for (i=strlen(dir_path); (j>=0 && i>=0 && dir_path[i] != '/' && dir_path[i] != '\\'); i--) name_comp[--j] = dir_path[i]; if (j<0) j = 0; // Long name. Incorrect results if (i == 0) // special case dir_path[1] = 0; // only root dir else dir_path[i] = 0; // replace last / with null char for (i=0; i<=12; i++) name_comp[i] = name_comp[j++]; } if (dir_path[0] != 0) { curdir_handle = open_path(drive, dir_path); if (curdir_handle < 0) return curdir_handle; // Error } else { curdir_handle = get_curdir_handle(drive); increment_ref_count(curdir_handle); } // Last new dir name component. if (convert_name(name_comp, conv_name) < 0) { close_dir(curdir_handle); return EINVALIDNAME; // Error } res = rm_dir(curdir_handle, conv_name); close_dir(curdir_handle); if (res == 1) return 0; // Success else return res; // failure }
/* Create a new external memory list. */ static int em_list_create(em_list_t *self) { mapped_file_t *mf; em_list_index_hdr_t index_hdr; em_list_values_hdr_t values_hdr; size_t size; char *filename; const char *dirname = self->dirname; /* Compute initial external memory list index size. */ size = EM_LIST_E2S(0); /* Create directory to hold external memory list files. */ if(mk_dir(dirname) != 0) goto _err1; /* Create "index.bin" and write file header. */ filename = path_combine(dirname, "index.bin"); if((mf = mapped_file_create(filename, size)) == NULL) goto _err2; index_hdr.magic = MAGIC; index_hdr.used = 0; index_hdr.capacity = 0; mapped_file_write(mf, &index_hdr, sizeof(em_list_index_hdr_t)); self->index = mf; /* Create "values.bin" and write file header. */ filename = path_combine(dirname, "values.bin"); if((mf = mapped_file_create(filename, size)) == NULL) goto _err3; values_hdr.magic = MAGIC; mapped_file_write(mf, &values_hdr, sizeof(em_list_values_hdr_t)); self->values = mf; return 0; _err3: mapped_file_unlink(self->index); mapped_file_close(self->index); _err2: rm_dir(dirname); _err1: PyErr_SetString(PyExc_RuntimeError, "Cannot open EMList"); return -1; }
bool test_code_generation(Language lang, IDL idl, Format format) { bool result = true; #ifdef _WIN32 std::string codegen_bash("bin/dsn.cg.bat"); #else std::string codegen_bash("bin/dsn.cg.sh"); #endif std::string codegen_cmd = combine(DSN_ROOT, codegen_bash)\ + std::string(" counter.")\ + (idl == idl_protobuf ? "proto" : "thrift")\ + (lang == lang_cpp ? " cpp" : " csharp")\ + " src "\ + (format == format_binary ? "binary" : "json")\ + " single"; create_dir("src", result); execute(codegen_cmd, result); std::vector<std::string> src_files; std::string src_root(lang == lang_cpp ? "repo/cpp" : "repo/csharp"); if (lang == lang_cpp) { src_files.push_back("counter.main.cpp"); } else { src_files.push_back("counter.main.cs"); } for (auto i : src_files) { copy_file(combine(combine(RESOURCE_ROOT, src_root), i), file("src"), result); } cmake(lang, result); rm_dir("data", result); rm_dir("builder", result); rm_dir("src", result); return result; }
/* ----------------------------------------------------- * rmdir_driver: * allows rmdir to act just like Linux by calling * rm_dir() on each argument * * i.e., rmdir dir1 dir2 dir3 will remove all of directories --------------------------------------------------------*/ void rmdir_driver(char** args) { int i=0; if(args[0] == NULL) { printf("Error: rmdir requires arguments\n"); return; } for(i=0; args[i] != NULL; i++) { rm_dir(args[i]); } }
static bool rm_dir(const char *pathname, bool top) { DIR *dp; struct dirent *d; char file[PATH_MAX]; struct stat s; bool retval = true; if ((dp = opendir(pathname)) == NULL) return false; errno = 0; while (((d = readdir(dp)) != NULL) && errno == 0) { if (strcmp(d->d_name, ".") != 0 && strcmp(d->d_name, "..") != 0) { snprintf(file, sizeof(file), "%s/%s", pathname, d->d_name); if (stat(file, &s) != 0) { retval = false; break; } if (S_ISDIR(s.st_mode)) { if (!rm_dir(file, true)) { retval = false; break; } } else { if (unlink(file)) { retval = false; break; } } } } closedir(dp); if (!retval) return false; if (top && rmdir(pathname) != 0) return false; return true; }
/* Function `rm_dir` * * Behavs: Remove directory `path` * Params: * `path`: directory to be removed * Return: * 0: Success * Other: Failed * */ static int rm_dir(const char *path) { DIR *dp; struct dirent *de; struct stat st; char buf[512],*p,*q,*r,*s; if(!(dp=opendir(path))) return -1; if(!(s=(p=strdup(path)))) { closedir(dp); return -2; } q=buf; if(*path=='/') *q++='/'; while(!!(r=strsep(&p,"/"))) { if(!*r) continue; while(*r) *q++=*r++; *q++='/'; } free(s); while(!!(de=readdir(dp))) { if(!*(de->d_name)) continue; if(!strcmp(de->d_name,".")) continue; if(!strcmp(de->d_name,"..")) continue; sprintf(q,"%s",de->d_name); if(lstat(buf,&st)) continue; if(!S_ISDIR(st.st_mode)) unlink(buf); else rm_dir(buf); } closedir(dp); *q=0; return rmdir(buf); }
/** * Handle a rmdir request * * @param msg client message data * @return Response data, NULL on allocation error */ resp_t *do_rmdir(msg_t *msg) { resp_t *resp = calloc(sizeof(resp_t)); debug("Attempting to remove dir...\n"); if (!resp) { debug("Failed to allocate response.\n"); return NULL; } if (!rm_dir(msg->buf)) { resp->type = RMDIR_OK; return resp; } else { debug("Failed to rm dir.\n"); resp->type = RMDIR_FAIL; return resp; } cgc_memcpy(resp, msg, sizeof(resp_t)); return resp; }
int ca_delete(struct ca *ca) { return (rm_dir(ca->sslpath)); }
int ca_export(struct ca *ca, char *keyname, char *myname, char *password) { DIR *dexp; struct dirent *de; struct stat st; char *pass; char prev[_PASSWORD_LEN + 1]; char cmd[PATH_MAX * 2]; char oname[PATH_MAX]; char src[PATH_MAX]; char dst[PATH_MAX]; char *p; char tpl[] = "/tmp/ikectl.XXXXXXXXXX"; u_int i; int fd; if (keyname != NULL) { if (strlcpy(oname, keyname, sizeof(oname)) >= sizeof(oname)) err(1, "name too long"); } else { strlcpy(oname, "ca", sizeof(oname)); } /* colons are not valid characters in windows filenames... */ while ((p = strchr(oname, ':')) != NULL) *p = '_'; if (password != NULL) pass = password; else { pass = getpass("Export passphrase:"); if (pass == NULL || *pass == '\0') err(1, "password not set"); strlcpy(prev, pass, sizeof(prev)); pass = getpass("Retype export passphrase:"); if (pass == NULL || strcmp(prev, pass) != 0) errx(1, "passphrase does not match!"); } if (keyname != NULL) { snprintf(cmd, sizeof(cmd), "env EXPASS=%s %s pkcs12 -export" " -name %s -CAfile %s/ca.crt -inkey %s/private/%s.key" " -in %s/%s.crt -out %s/private/%s.pfx -passout env:EXPASS" " -passin file:%s", pass, PATH_OPENSSL, keyname, ca->sslpath, ca->sslpath, keyname, ca->sslpath, keyname, ca->sslpath, oname, ca->passfile); system(cmd); } snprintf(cmd, sizeof(cmd), "env EXPASS=%s %s pkcs12 -export" " -caname '%s' -name '%s' -cacerts -nokeys" " -in %s/ca.crt -out %s/ca.pfx -passout env:EXPASS -passin file:%s", pass, PATH_OPENSSL, ca->caname, ca->caname, ca->sslpath, ca->sslpath, ca->passfile); system(cmd); if ((p = mkdtemp(tpl)) == NULL) err(1, "could not create temp dir"); chmod(p, 0755); for (i = 0; i < nitems(hier); i++) { strlcpy(dst, p, sizeof(dst)); strlcat(dst, hier[i].dir, sizeof(dst)); if (stat(dst, &st) != 0 && errno == ENOENT && mkdir(dst, hier[i].mode) != 0) err(1, "failed to create dir %s", dst); } /* create a file with the address of the peer to connect to */ if (myname != NULL) { snprintf(dst, sizeof(dst), "%s/export/peer.txt", p); if ((fd = open(dst, O_WRONLY|O_CREAT, 0644)) == -1) err(1, "open %s", dst); write(fd, myname, strlen(myname)); close(fd); } snprintf(src, sizeof(src), "%s/ca.pfx", ca->sslpath); snprintf(dst, sizeof(dst), "%s/export/ca.pfx", p); fcopy(src, dst, 0644); snprintf(src, sizeof(src), "%s/ca.crt", ca->sslpath); snprintf(dst, sizeof(dst), "%s/ca/ca.crt", p); fcopy(src, dst, 0644); snprintf(src, sizeof(src), "%s/ca.crl", ca->sslpath); if (stat(src, &st) == 0) { snprintf(dst, sizeof(dst), "%s/crls/ca.crl", p); fcopy(src, dst, 0644); } if (keyname != NULL) { snprintf(src, sizeof(src), "%s/private/%s.pfx", ca->sslpath, oname); snprintf(dst, sizeof(dst), "%s/export/%s.pfx", p, oname); fcopy(src, dst, 0644); snprintf(src, sizeof(src), "%s/private/%s.key", ca->sslpath, keyname); snprintf(dst, sizeof(dst), "%s/private/%s.key", p, keyname); fcopy(src, dst, 0600); snprintf(dst, sizeof(dst), "%s/private/local.key", p); fcopy(src, dst, 0600); snprintf(src, sizeof(src), "%s/%s.crt", ca->sslpath, keyname); snprintf(dst, sizeof(dst), "%s/certs/%s.crt", p, keyname); fcopy(src, dst, 0644); snprintf(cmd, sizeof(cmd), "%s rsa -out %s/local.pub" " -in %s/private/%s.key -pubout", PATH_OPENSSL, p, ca->sslpath, keyname); system(cmd); } if (stat(PATH_TAR, &st) == 0) { if (keyname == NULL) snprintf(cmd, sizeof(cmd), "%s -zcf %s.tgz -C %s .", PATH_TAR, oname, ca->sslpath); else snprintf(cmd, sizeof(cmd), "%s -zcf %s.tgz -C %s .", PATH_TAR, oname, p); system(cmd); snprintf(src, sizeof(src), "%s.tgz", oname); if (realpath(src, dst) != NULL) printf("exported files in %s\n", dst); } if (stat(PATH_ZIP, &st) == 0) { dexp = opendir(EXPDIR); if (dexp) { while ((de = readdir(dexp)) != NULL) { if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue; snprintf(src, sizeof(src), "%s/%s", EXPDIR, de->d_name); snprintf(dst, sizeof(dst), "%s/export/%s", p, de->d_name); fcopy(src, dst, 644); } closedir(dexp); } snprintf(dst, sizeof(dst), "%s/export", p); if (getcwd(src, sizeof(src)) == NULL) err(1, "could not get cwd"); if (chdir(dst) == -1) err(1, "could not change %s", dst); snprintf(dst, sizeof(dst), "%s/%s.zip", src, oname); snprintf(cmd, sizeof(cmd), "%s -qr %s .", PATH_ZIP, dst); system(cmd); printf("exported files in %s\n", dst); if (chdir(src) == -1) err(1, "could not change %s", dst); } rm_dir(p); return (0); }
/* Function `f_rm` * * Behavs: Remove file or directory `path` * Params: * `path`: File or directory to be removed * Return: * 0: Success * Other: Failed * */ int f_rm(const char *path) { struct stat st; if(lstat(path,&st)) return -1; return (!S_ISDIR(st.st_mode)?unlink(path):rm_dir(path)); }
int main() { setlocale(LC_ALL, "Russian"); char buffer[MAXBUF]; //инициализация библиотеки Winsock if (WSAStartup(0x202,(WSADATA *)&buffer[0])) { printf("WSAStart error %d\n",WSAGetLastError()); _getch(); return -1; } //создание сокета SOCKET sock; sock=socket(AF_INET,SOCK_STREAM,0); if (sock < 0) { printf("Socket() error %d\n",WSAGetLastError()); _getch(); return -1; } //установка соединения // заполнение структуры sockaddr_in // указание адреса и порта сервера sockaddr_in dest_addr; dest_addr.sin_family=AF_INET; dest_addr.sin_port=htons(PORT); HOSTENT *hst; // преобразование IP адреса из символьного в // сетевой формат if (inet_addr(SERVERADDR)!=INADDR_NONE) dest_addr.sin_addr.s_addr=inet_addr(SERVERADDR); else // попытка получить IP адрес по доменному // имени сервера if (hst=gethostbyname(SERVERADDR)) // hst->h_addr_list содержит не массив адресов, // а массив указателей на адреса ((unsigned long *)&dest_addr.sin_addr)[0]= ((unsigned long **)hst->h_addr_list)[0][0]; else { printf("Invalid address %s\n",SERVERADDR); closesocket(sock); WSACleanup(); _getch(); return -1; } if (connect(sock,(sockaddr *)&dest_addr, sizeof(dest_addr))) { printf("Connect error %d\n",WSAGetLastError()); _getch(); return -1; } //----------------------------------------------- memset(buffer, 0, MAXBUF); recv(sock , buffer , MAXBUF , 0); // прием слова puts(buffer); printf("Для получения справки введите команду help\n"); while(1){ memset(buffer, 0, MAXBUF); printf("Cmd: "); scanf("%s" , buffer); // команды if(strcmp(buffer, "view") == 0) view_dir(buffer, sock, MAXBUF); else if(strcmp(buffer, "view_dir") == 0) view_dir(buffer, sock, MAXBUF); else if(strcmp(buffer, "help") == 0) get_help(); else if(strcmp(buffer, "get_dir") == 0) get_dir(buffer, sock, MAXBUF); else if(strcmp(buffer, "ch_dir") == 0) ch_dir(buffer, sock, MAXBUF); else if(strcmp(buffer, "upload") == 0) upload(buffer, MAXBUF, sock); else if(strcmp(buffer, "download") == 0) download(buffer, MAXBUF, sock); else if(strcmp(buffer, "mk_dir") == 0) mk_dir(buffer, MAXBUF, sock); else if(strcmp(buffer, "rm_dir") == 0) rm_dir(buffer, MAXBUF, sock); else if(strcmp(buffer, "echo") == 0) echo(buffer, sock, MAXBUF); else if(strcmp(buffer, "disconnect") == 0){ send(sock, "disconnect", 10, 0); break; } else printf("unknown comand\n"); printf("Для получения справки введите команду help\n"); } //----------------------------------------------- printf("\nExit\n"); closesocket(sock); WSACleanup(); _getch(); return 0; }