int main(int argc, char *argv[]) { if (argc < 5) { printf("Need: <ip1> <port1> <ip2> <port2> \n"); exit(1); } const char *ip = argv[1]; const int port = atoi(argv[2]); const char *ip2 = argv[3]; const int port2 = atoi(argv[4]); printf("fsMount1:\n"); if (fsMount(ip, port, "foo") < 0) { perror("fsMount"); exit(1); } printf("fsMount2:\n"); if (fsMount(ip, port, "foo") >= 0) { printf("ERROR: Should not be able to mount the same folder twice\n"); exit(1); } printf("fsMount3:\n"); if (fsMount(ip2, port2, "foo2") < 0) { perror("fsMount"); exit(1); } printf("open1:\n"); int fd; if ((fd = fsOpen("foo/file1.txt", 0)) < 0) { perror("fsOpen"); exit(1); } printf("fd: %d\n", fd); printf("open2:\n"); int fd2; if ((fd2 = fsOpen("foo2/file2.txt", 0)) < 0) { perror("fsOpen"); exit(1); } printf("fd2: %d\n", fd2); printf("open3:\n"); if (fsOpen("foo/file2.txt", 0) >= 0) { printf("ERROR: There should be no file2.txt in server#1\n"); exit(1); } printf("open4:\n"); if (fsOpen("foo2/file1.txt", 0) >= 0) { printf("ERROR: There should be no file1.txt in server#2\n"); exit(1); } printf("open5:\n"); if (fsOpen("foo3/file1.txt", 0) >= 0) { printf("ERROR: There is no mounted folder called foo3\n"); exit(1); } printf("read1:\n"); printf("fd: %d\n", fd); printf("fd2: %d\n", fd2); char buf[3001]; int bytesread; int i = 0; bytesread = fsRead(fd, (void *)buf, 3000); perror("fsRead"); *((char *) buf + bytesread) = '\0'; printf("Bytes=%d, String='%s'\n", bytesread, buf); if (strncmp(buf, "Hello World1", 12) != 0) { printf("ERROR: Read on file1.txt should return 'Hello World1' \n"); exit(1); } printf("read2:\n"); bytesread = fsRead(fd2, (void *)buf, 3000); *((char *) buf + bytesread) = '\0'; if (strncmp(buf, "Hello World2", 12) != 0) { printf("ERROR: Read on file2.txt should return 'Hello World2' \n"); exit(1); } printf("read3:\n"); while ((bytesread = fsRead(fd2, (void *)buf, 3000)) > 0) { *((char *) buf + bytesread) = '\0'; printf("%s", (char *) buf); i += 1; } printf("\n"); printf("close:\n"); if (fsClose(fd) < 0) { perror("fsClose"); exit(1); } if ((fd = fsOpen("foo/file1.txt", 1)) < 0) { perror("fsOpen"); exit(1); } printf("write 1:\n"); //Write to "../server1/file1.txt" if(fsWrite(fd,(void *)"Hello Hello", 8)<0) { printf("write failed:\n"); } printf("write 2:\n"); //Write to "../server1/file1.txt" if(fsWrite(fd,(void *)"Hola Hola", 4)<0) { printf("write2 failed:\n"); } printf("close:\n"); if (fsClose(fd) < 0) { perror("fsClose"); exit(1); } printf("close2:\n"); if (fsClose(fd2) < 0) { perror("fsClose"); exit(1); } // printf("remove:\n"); // if (fsRemove("foo/file1.txt") < 0) { // printf("ERROR: remove file 1 failed\n"); exit(1); // } // printf("remove2:\n"); // if (fsRemove("foo") == 0 || fsRemove("foo2") == 0) { // printf("ERROR: Should not be able to delete mounted folder\n"); exit(1); // } printf("openDir:\n"); FSDIR* tempFsDir=fsOpenDir("foo"); if(tempFsDir==NULL) { printf("FSDir is NULL\n"); } printf("readDir:\n"); struct fsDirent* tempfsDirent=NULL; do { tempfsDirent=fsReadDir(tempFsDir); if(tempfsDirent!=NULL) { printf("fsDirent: entName: %s\n", tempfsDirent->entName); } }while(tempfsDirent!=NULL); //printf("fsDirent: entName: %s\n", tempfsDirent->entName); printf("closeDir:\n"); if(fsCloseDir(tempFsDir)<0) { printf("closeDir failed\n"); } printf("unmount:\n"); if (fsUnmount("foo") < 0) { perror("fsUnmount"); exit(1); } printf("unmount2:\n"); if (fsUnmount("foo2") < 0) { perror("fsUnmount"); exit(1); } printf("All tests passed!\n"); return 0; }
void ftpServerSendData(FtpServerContext *context, FtpClientConnection *connection) { error_t error; size_t n; //Any data waiting for transmission? if(connection->bufferLength > 0) { //Send more data error = socketSend(connection->dataSocket, connection->buffer + connection->bufferPos, connection->bufferLength, &n, 0); //Failed to send data? if(error != NO_ERROR && error != ERROR_TIMEOUT) { //Close the data connection ftpServerCloseDataConnection(connection); //Release previously allocated resources if(connection->file != NULL) { fsCloseFile(connection->file); connection->file = NULL; } if(connection->dir != NULL) { fsCloseDir(connection->dir); connection->dir = NULL; } //Back to idle state connection->controlState = FTP_CONTROL_STATE_IDLE; //Transfer status strcpy(connection->response, "451 Transfer aborted\r\n"); //Debug message TRACE_DEBUG("FTP server: %s", connection->response); //Number of bytes in the response buffer connection->responseLength = strlen(connection->response); connection->responsePos = 0; //Exit immediately return; } //Advance data pointer connection->bufferPos += n; //Number of bytes still available in the buffer connection->bufferLength -= n; } //Empty transmission buffer? if(connection->bufferLength == 0) { //File transfer in progress? if(connection->controlState == FTP_CONTROL_STATE_RETR) { //Read more data error = fsReadFile(connection->file, connection->buffer, FTP_SERVER_BUFFER_SIZE, &n); //End of stream? if(error) { //Close file fsCloseFile(connection->file); connection->file = NULL; //Wait for all the data to be transmitted and acknowledged connection->dataState = FTP_DATA_STATE_WAIT_ACK; //Exit immediately return; } } //Directory listing in progress? else if(connection->controlState == FTP_CONTROL_STATE_LIST) { uint_t perm; time_t currentTime; time_t modified; char_t *path; FsDirEntry dirEntry; //Read a new entry in the directory error = fsReadDir(connection->dir, &dirEntry); //End of stream? if(error) { //Close directory fsCloseDir(connection->dir); connection->dir = NULL; //Wait for all the data to be transmitted and acknowledged connection->dataState = FTP_DATA_STATE_WAIT_ACK; //Exit immediately return; } //Point to the scratch buffer path = connection->buffer; //Get the pathname of the directory being listed strcpy(path, connection->path); //Retrieve the full pathname pathCombine(path, dirEntry.name, FTP_SERVER_MAX_PATH_LEN); pathCanonicalize(path); //Get permissions for the specified file perm = ftpServerGetFilePermissions(context, connection, path); //Enforce access rights if(perm & FTP_FILE_PERM_LIST) { //Format links, owner, group and size fields n = sprintf(connection->buffer, "---------- 1 owner group %10" PRIu32, dirEntry.size); //Check whether the current entry is a directory if(dirEntry.attributes & FS_FILE_ATTR_DIRECTORY) connection->buffer[0] = 'd'; //Read access? if(perm & FTP_FILE_PERM_READ) { connection->buffer[1] = 'r'; connection->buffer[4] = 'r'; connection->buffer[7] = 'r'; } //Write access if(perm & FTP_FILE_PERM_WRITE) { connection->buffer[2] = 'w'; connection->buffer[5] = 'w'; connection->buffer[8] = 'w'; } //Get current time currentTime = getCurrentUnixTime(); //Get modification time modified = convertDateToUnixTime(&dirEntry.modified); //Check whether the modification time is within the previous 180 days if(currentTime > modified && currentTime < (modified + FTP_SERVER_180_DAYS)) { //The format of the date/time field is Mmm dd hh:mm n += sprintf(connection->buffer + n, " %s %02" PRIu8 " %02" PRIu8 ":%02" PRIu8, months[MIN(dirEntry.modified.month, 12)], dirEntry.modified.day, dirEntry.modified.hours, dirEntry.modified.minutes); } else { //The format of the date/time field is Mmm dd yyyy n += sprintf(connection->buffer + n, " %s %02" PRIu8 " %04" PRIu16, months[MIN(dirEntry.modified.month, 12)], dirEntry.modified.day, dirEntry.modified.year); } //Append filename n += sprintf(connection->buffer + n, " %s\r\n", dirEntry.name); //Debug message TRACE_DEBUG("FTP server: %s", connection->buffer); } else { //Insufficient access rights n = 0; } } //Invalid state? else { //The FTP server has encountered a critical error ftpServerCloseConnection(context, connection); //Exit immediately return; } //Number of bytes in the buffer connection->bufferPos = 0; connection->bufferLength = n; } }
int main(int argc, char *argv[]) { if(argc < 4) { fprintf(stderr, "usage: %s <srv-ip/name> <srv-port> <local dir name>\n", argv[0]); exit(1); } printf ("\n\n\n\nenter main fs_client \n. ***********\n test case involves mounting 3 server alias, opendir 3 aliases and unmount 3 server alias \n"); char *dirname = argv[3]; printf("fsMount(): %d\n", fsMount(argv[1], atoi(argv[2]), dirname)); printf("fsMount(): - folderAlsia %d\n", fsMount(argv[1],atoi(argv[2]), "folderAlias")); printf("fsMount(): - ../exFolder %d\n", fsMount(argv[1],atoi(argv[2]), "../exFolder")); FSDIR *fd = fsOpenDir(dirname); //printf("client app, returned fd-> num is: %i\n",fd->num) ; if(fd == NULL) { perror("fsOpenDir"); exit(1); } printf ("successfully opened dir with id of FSDIR: %i \n",fd->num); FSDIR *fd2 = fsOpenDir("folderAlias/apples/"); //printf("client app, -- testin second open returned fd-> num is: %i\n",fd2->num) ; if(fd2 == NULL) { perror("fsOpenDir For second failed"); exit(1); } printf ("successfully opened dir with id of FSDIR: %i \n",fd2->num); FSDIR *fd3 = fsOpenDir("../exFolder/apples/bananas/"); if(fd3 == NULL) { perror("fsOpenDir exFolder/apples/bananas failed \n");exit(1); } printf ("successfully oepened bananas dir with id of FSDIR : %i \n",fd3->num); printf ("\t now checking fsDirent part\n"); struct fsDirent *fdent = NULL; printf("client app, fd->num is : %i\n",fd->num); for(fdent = fsReadDir(fd); fdent != NULL; fdent = fsReadDir(fd)) { printf("\t %s, %d\n", fdent->entName, (int)(fdent->entType)); } if(errno != 0) { perror("fsReadDir"); } printf("fsCloseDir(): %d\n", fsCloseDir(fd)); struct fsDirent *fdent2 = NULL; printf("client app, fd2->num is : %i\n",fd2->num); for(fdent2 = fsReadDir(fd2); fdent2 != NULL; fdent2 = fsReadDir(fd2)) { printf("\t %s, %d\n", fdent2->entName, (int)(fdent2->entType)); } if(errno != 0) { perror("fsReadDir"); } printf("fsCloseDir(): %d\n", fsCloseDir(fd2)); struct fsDirent *fdent3 = NULL; printf("client app, fd3->num is : %i\n",fd3->num); for(fdent3 = fsReadDir(fd3); fdent3 != NULL; fdent3 = fsReadDir(fd3)) { printf("\t %s, %d\n", fdent3->entName, (int)(fdent3->entType)); } if(errno != 0) { perror("fsReadDir"); } // part to test open close and write int ff = open("/dev/urandom", 0); if(ff < 0) { perror("open(/dev/urandom)"); exit(1); } else printf("open(): %d\n", ff); //return 0; char fname[256]; sprintf(fname, "%s/", dirname); if(read(ff, (void *)(fname+strlen(dirname)+1), 10) < 0) { perror("read(/dev/urandom)"); exit(1); } int i; for(i = 0; i < 10; i++) { //printf("%d\n", ((unsigned char)(fname[i]))%26); fname[i+strlen(dirname)+1] = ((unsigned char)(fname[i+strlen(dirname)+1]))%26 + 'a'; } fname[10+strlen(dirname)+1] = (char)0; printf("Filename to write: %s\n", (char *)fname); char buf[256]; if(read(ff, (void *)buf, 256) < 0) { perror("read(2)"); exit(1); } printBuf(buf, 256); printf("close(): %d\n", close(ff)); int ctr; // for (ctr =0; ctr < 100;ctr++){ ff = fsOpen("../exFolder/apples/01.txt", 1); if(ff < 0) { perror("fsOpen(write)"); exit(1); } if(fsWrite(ff, buf, 256) < 256) { fprintf(stderr, "fsWrite() wrote fewer than 256\n"); } sleep(60); //return 0; if(fsClose(ff) < 0) { perror("fsClose"); exit(1); } sleep(60); //return 0; char readbuf[256]; if((ff = fsOpen("../exFolder/apples/01.txt", 0)) < 0) { perror("fsOpen(read)"); exit(1); } int readcount = -1; if((readcount = fsRead(ff, readbuf, 256)) < 256) { fprintf(stderr, "fsRead() read fewer than 256\n"); } if(memcmp(readbuf, buf, readcount)) { fprintf(stderr, "return buf from fsRead() differs from data written!\n"); } else { printf("fsread(): return buf identical to data written upto %d bytes.\n", readcount); } if(fsClose(ff) < 0) { perror("fsClose return is < 0"); exit(1); } ff = fsOpen("sample/apples/value_write_01",1); if (ff < 0){ perror("fsOpen(value_write) probably failed");exit(1); } char *testBuf= "line 01 to be added to the file\n"; if (fsWrite(ff,testBuf,strlen(testBuf)) < strlen(testBuf)){ perror ("fsWrite line 01 on value_write failed");exit(1); } char *testBuf2= "line 02"; if (fsWrite(ff,testBuf2,strlen(testBuf2)) < strlen(testBuf2)){ perror ("fsWrite line 02 on value_write failed");exit(1); } if (fsClose(ff) < 0) { perror("fsClose with value_write file failed");exit(1);} printf("fsCloseDir(): %d\n", fsCloseDir(fd3)); printf("fsUnmount(): folderAlias %i\n",fsUnmount("folderAlias")); printf("fsUnmount(): ../exFolder/ %i\n",fsUnmount("../exFolder")); printf("fsUnmount(): dirFolder entered %i \n",fsUnmount(dirname)); //printf("fsUnmount(): %s\n",dirname,fsUnmount(dirname)); //return 0; //*/ printf ("\n\n successfully exit fs_client test 0_10 \n. ***********\n test case involves mounting 3 server alias, opendir 3 aliases and unmount 3 server alias\n \t\t subsequent writes are appended, new writes are overridden\n \n open 01.txt in apples folder without sleep. write to it and then read from it after\n"); return 0; }