rem
stringlengths
0
83.5k
add
stringlengths
0
223k
context
stringlengths
10
471k
meta
stringlengths
120
236
if (isastream(slave) && (ioctl(slave, I_PUSH, "ptem") < 0 || ioctl(slave, I_PUSH, "ldterm") < 0)) goto close_slave;
if (isastream(slave) && (ioctl(slave, I_PUSH, "ptem") < 0 || ioctl(slave, I_PUSH, "ldterm") < 0)) goto close_slave;
static int open_pty_pair(int fd[2]){#if defined(HAVE_TERMIOS_H) && defined(HAVE_GRANTPT)/** with kind regards to The GNU C LibraryReference Manual for Version 2.2.x of the GNU C Library */ int master, slave; char *name; struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1;#ifdef HAVE_GETPT master = getpt();#else master = open("/dev/ptmx", O_RDWR);#endif if (master < 0) return 0; if (grantpt(master) < 0 || unlockpt(master) < 0) goto close_master; name = ptsname(master); if (name == NULL) goto close_master; slave = open(name, O_RDWR); if (slave == -1) goto close_master;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH) if (isastream(slave) && (ioctl(slave, I_PUSH, "ptem") < 0 || ioctl(slave, I_PUSH, "ldterm") < 0)) goto close_slave;#endif tcsetattr(slave, TCSANOW, &ti); fd[0] = master; fd[1] = slave; return 0;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH)close_slave:#endif close(slave);close_master: close(master); return -1;#else#ifdef HAVE_OPENPTY struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1; return openpty(fd,fd+1,NULL,&ti,NULL);#else#warning "No tty support available. Password dialog won't work." return socketpair(PF_UNIX,SOCK_STREAM,0,fd);#endif#endif}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/11afbb378913175f7b3f19457b921df3efab59dc/fish.cpp/clean/kioslave/fish/fish.cpp
tcsetattr(slave, TCSANOW, &ti); fd[0] = master; fd[1] = slave; return 0;
tcsetattr(slave, TCSANOW, &ti); fd[0] = master; fd[1] = slave; return 0;
static int open_pty_pair(int fd[2]){#if defined(HAVE_TERMIOS_H) && defined(HAVE_GRANTPT)/** with kind regards to The GNU C LibraryReference Manual for Version 2.2.x of the GNU C Library */ int master, slave; char *name; struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1;#ifdef HAVE_GETPT master = getpt();#else master = open("/dev/ptmx", O_RDWR);#endif if (master < 0) return 0; if (grantpt(master) < 0 || unlockpt(master) < 0) goto close_master; name = ptsname(master); if (name == NULL) goto close_master; slave = open(name, O_RDWR); if (slave == -1) goto close_master;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH) if (isastream(slave) && (ioctl(slave, I_PUSH, "ptem") < 0 || ioctl(slave, I_PUSH, "ldterm") < 0)) goto close_slave;#endif tcsetattr(slave, TCSANOW, &ti); fd[0] = master; fd[1] = slave; return 0;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH)close_slave:#endif close(slave);close_master: close(master); return -1;#else#ifdef HAVE_OPENPTY struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1; return openpty(fd,fd+1,NULL,&ti,NULL);#else#warning "No tty support available. Password dialog won't work." return socketpair(PF_UNIX,SOCK_STREAM,0,fd);#endif#endif}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/11afbb378913175f7b3f19457b921df3efab59dc/fish.cpp/clean/kioslave/fish/fish.cpp
close(slave);
close(slave);
static int open_pty_pair(int fd[2]){#if defined(HAVE_TERMIOS_H) && defined(HAVE_GRANTPT)/** with kind regards to The GNU C LibraryReference Manual for Version 2.2.x of the GNU C Library */ int master, slave; char *name; struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1;#ifdef HAVE_GETPT master = getpt();#else master = open("/dev/ptmx", O_RDWR);#endif if (master < 0) return 0; if (grantpt(master) < 0 || unlockpt(master) < 0) goto close_master; name = ptsname(master); if (name == NULL) goto close_master; slave = open(name, O_RDWR); if (slave == -1) goto close_master;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH) if (isastream(slave) && (ioctl(slave, I_PUSH, "ptem") < 0 || ioctl(slave, I_PUSH, "ldterm") < 0)) goto close_slave;#endif tcsetattr(slave, TCSANOW, &ti); fd[0] = master; fd[1] = slave; return 0;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH)close_slave:#endif close(slave);close_master: close(master); return -1;#else#ifdef HAVE_OPENPTY struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1; return openpty(fd,fd+1,NULL,&ti,NULL);#else#warning "No tty support available. Password dialog won't work." return socketpair(PF_UNIX,SOCK_STREAM,0,fd);#endif#endif}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/11afbb378913175f7b3f19457b921df3efab59dc/fish.cpp/clean/kioslave/fish/fish.cpp
close(master); return -1;
close(master); return -1;
static int open_pty_pair(int fd[2]){#if defined(HAVE_TERMIOS_H) && defined(HAVE_GRANTPT)/** with kind regards to The GNU C LibraryReference Manual for Version 2.2.x of the GNU C Library */ int master, slave; char *name; struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1;#ifdef HAVE_GETPT master = getpt();#else master = open("/dev/ptmx", O_RDWR);#endif if (master < 0) return 0; if (grantpt(master) < 0 || unlockpt(master) < 0) goto close_master; name = ptsname(master); if (name == NULL) goto close_master; slave = open(name, O_RDWR); if (slave == -1) goto close_master;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH) if (isastream(slave) && (ioctl(slave, I_PUSH, "ptem") < 0 || ioctl(slave, I_PUSH, "ldterm") < 0)) goto close_slave;#endif tcsetattr(slave, TCSANOW, &ti); fd[0] = master; fd[1] = slave; return 0;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH)close_slave:#endif close(slave);close_master: close(master); return -1;#else#ifdef HAVE_OPENPTY struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1; return openpty(fd,fd+1,NULL,&ti,NULL);#else#warning "No tty support available. Password dialog won't work." return socketpair(PF_UNIX,SOCK_STREAM,0,fd);#endif#endif}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/11afbb378913175f7b3f19457b921df3efab59dc/fish.cpp/clean/kioslave/fish/fish.cpp
struct ::termios ti; memset(&ti,0,sizeof(ti));
struct ::termios ti; memset(&ti,0,sizeof(ti));
static int open_pty_pair(int fd[2]){#if defined(HAVE_TERMIOS_H) && defined(HAVE_GRANTPT)/** with kind regards to The GNU C LibraryReference Manual for Version 2.2.x of the GNU C Library */ int master, slave; char *name; struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1;#ifdef HAVE_GETPT master = getpt();#else master = open("/dev/ptmx", O_RDWR);#endif if (master < 0) return 0; if (grantpt(master) < 0 || unlockpt(master) < 0) goto close_master; name = ptsname(master); if (name == NULL) goto close_master; slave = open(name, O_RDWR); if (slave == -1) goto close_master;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH) if (isastream(slave) && (ioctl(slave, I_PUSH, "ptem") < 0 || ioctl(slave, I_PUSH, "ldterm") < 0)) goto close_slave;#endif tcsetattr(slave, TCSANOW, &ti); fd[0] = master; fd[1] = slave; return 0;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH)close_slave:#endif close(slave);close_master: close(master); return -1;#else#ifdef HAVE_OPENPTY struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1; return openpty(fd,fd+1,NULL,&ti,NULL);#else#warning "No tty support available. Password dialog won't work." return socketpair(PF_UNIX,SOCK_STREAM,0,fd);#endif#endif}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/11afbb378913175f7b3f19457b921df3efab59dc/fish.cpp/clean/kioslave/fish/fish.cpp
ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1;
ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1;
static int open_pty_pair(int fd[2]){#if defined(HAVE_TERMIOS_H) && defined(HAVE_GRANTPT)/** with kind regards to The GNU C LibraryReference Manual for Version 2.2.x of the GNU C Library */ int master, slave; char *name; struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1;#ifdef HAVE_GETPT master = getpt();#else master = open("/dev/ptmx", O_RDWR);#endif if (master < 0) return 0; if (grantpt(master) < 0 || unlockpt(master) < 0) goto close_master; name = ptsname(master); if (name == NULL) goto close_master; slave = open(name, O_RDWR); if (slave == -1) goto close_master;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH) if (isastream(slave) && (ioctl(slave, I_PUSH, "ptem") < 0 || ioctl(slave, I_PUSH, "ldterm") < 0)) goto close_slave;#endif tcsetattr(slave, TCSANOW, &ti); fd[0] = master; fd[1] = slave; return 0;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH)close_slave:#endif close(slave);close_master: close(master); return -1;#else#ifdef HAVE_OPENPTY struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1; return openpty(fd,fd+1,NULL,&ti,NULL);#else#warning "No tty support available. Password dialog won't work." return socketpair(PF_UNIX,SOCK_STREAM,0,fd);#endif#endif}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/11afbb378913175f7b3f19457b921df3efab59dc/fish.cpp/clean/kioslave/fish/fish.cpp
return openpty(fd,fd+1,NULL,&ti,NULL);
return openpty(fd,fd+1,NULL,&ti,NULL);
static int open_pty_pair(int fd[2]){#if defined(HAVE_TERMIOS_H) && defined(HAVE_GRANTPT)/** with kind regards to The GNU C LibraryReference Manual for Version 2.2.x of the GNU C Library */ int master, slave; char *name; struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1;#ifdef HAVE_GETPT master = getpt();#else master = open("/dev/ptmx", O_RDWR);#endif if (master < 0) return 0; if (grantpt(master) < 0 || unlockpt(master) < 0) goto close_master; name = ptsname(master); if (name == NULL) goto close_master; slave = open(name, O_RDWR); if (slave == -1) goto close_master;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH) if (isastream(slave) && (ioctl(slave, I_PUSH, "ptem") < 0 || ioctl(slave, I_PUSH, "ldterm") < 0)) goto close_slave;#endif tcsetattr(slave, TCSANOW, &ti); fd[0] = master; fd[1] = slave; return 0;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH)close_slave:#endif close(slave);close_master: close(master); return -1;#else#ifdef HAVE_OPENPTY struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1; return openpty(fd,fd+1,NULL,&ti,NULL);#else#warning "No tty support available. Password dialog won't work." return socketpair(PF_UNIX,SOCK_STREAM,0,fd);#endif#endif}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/11afbb378913175f7b3f19457b921df3efab59dc/fish.cpp/clean/kioslave/fish/fish.cpp
return socketpair(PF_UNIX,SOCK_STREAM,0,fd);
return socketpair(PF_UNIX,SOCK_STREAM,0,fd);
static int open_pty_pair(int fd[2]){#if defined(HAVE_TERMIOS_H) && defined(HAVE_GRANTPT)/** with kind regards to The GNU C LibraryReference Manual for Version 2.2.x of the GNU C Library */ int master, slave; char *name; struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1;#ifdef HAVE_GETPT master = getpt();#else master = open("/dev/ptmx", O_RDWR);#endif if (master < 0) return 0; if (grantpt(master) < 0 || unlockpt(master) < 0) goto close_master; name = ptsname(master); if (name == NULL) goto close_master; slave = open(name, O_RDWR); if (slave == -1) goto close_master;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH) if (isastream(slave) && (ioctl(slave, I_PUSH, "ptem") < 0 || ioctl(slave, I_PUSH, "ldterm") < 0)) goto close_slave;#endif tcsetattr(slave, TCSANOW, &ti); fd[0] = master; fd[1] = slave; return 0;#if (defined(HAVE_ISASTREAM) || defined(isastream)) && defined(I_PUSH)close_slave:#endif close(slave);close_master: close(master); return -1;#else#ifdef HAVE_OPENPTY struct ::termios ti; memset(&ti,0,sizeof(ti)); ti.c_cflag = CLOCAL|CREAD|CS8; ti.c_cc[VMIN] = 1; return openpty(fd,fd+1,NULL,&ti,NULL);#else#warning "No tty support available. Password dialog won't work." return socketpair(PF_UNIX,SOCK_STREAM,0,fd);#endif#endif}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/11afbb378913175f7b3f19457b921df3efab59dc/fish.cpp/clean/kioslave/fish/fish.cpp
const KAboutData *aboutData() const { return m_aboutData; };
const KAboutData *aboutData() const { return m_aboutData; }
const KAboutData *aboutData() const { return m_aboutData; };
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/af35753ffe08a0aa0b4d126b38523d9154a5425e/krashconf.h/clean/drkonqi/krashconf.h
unlink(sock.c_str());
unlink(sock);
void cleanup(void){ unlink(sock.c_str());}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/5572da27796be4b14fe2b1122a9b1aa8a9a7a754/kdesud.cpp/clean/kdesu/kdesud/kdesud.cpp
if (sgf_dump) { if (komaster != EMPTY) sgffile_write_line("\n(;%c[tt]C[tenuki (ko threat)]\n\ ;W[tt]C[tenuki (answers ko threat)]\n;B[%c%c]C[%s (variation %d, hash %lx, komaster %s:%c%d)\n]", (color == BLACK) ? 'B' : 'W', 'a'+j, 'a'+i, message, count_variations, hashdata.hashval, color_to_string(komaster), kom_i + 'A' + (kom_i >= 8), board_size - kom_j); else sgffile_write_line("\n(;%c[tt]C[tenuki (ko threat)]\n\ ;B[tt]C[tenuki (answers ko threat)]\n;W[%c%c]C[%s (variation %d, hash %lx)\n]", (color == BLACK) ? 'B' : 'W', 'a'+j, 'a'+i, message, count_variations, hashdata.hashval); } else if (sgf_dumptree) {
if (sgf_dumptree) {
tryko(int i, int j, int color, const char *message, int komaster, int kom_i, int kom_j){ /* Do the real work elsewhere. */ if (!do_trymove(i, j, color, 1)) return 0; if (sgf_dump) { if (komaster != EMPTY) sgffile_write_line("\n(;%c[tt]C[tenuki (ko threat)]\n\;W[tt]C[tenuki (answers ko threat)]\n;B[%c%c]C[%s (variation %d, hash %lx, komaster %s:%c%d)\n]", (color == BLACK) ? 'B' : 'W', 'a'+j, 'a'+i, message, count_variations, hashdata.hashval, color_to_string(komaster), kom_i + 'A' + (kom_i >= 8), board_size - kom_j); else sgffile_write_line("\n(;%c[tt]C[tenuki (ko threat)]\n\;B[tt]C[tenuki (answers ko threat)]\n;W[%c%c]C[%s (variation %d, hash %lx)\n]", (color == BLACK) ? 'B' : 'W', 'a'+j, 'a'+i, message, count_variations, hashdata.hashval); } else if (sgf_dumptree) { char buf[100]; if (!message) message = "???"; if (komaster != EMPTY) gg_snprintf(buf, 100, "tryko: %s (variation %d, %lx, komaster %s:%c%d)", message, count_variations, hashdata.hashval, color_to_string(komaster), kom_i + 'A' + (kom_i >= 8), board_size - kom_j); else gg_snprintf(buf, 100, "tryko: %s (variation %d, %lx)", message, count_variations, hashdata.hashval); sgftreeAddPlayLast(sgf_dumptree, NULL, color, -1, -1); sgftreeAddComment(sgf_dumptree, NULL, "tenuki (ko threat)"); sgftreeAddPlayLast(sgf_dumptree, NULL, OTHER_COLOR(color), -1, -1); sgftreeAddComment(sgf_dumptree, NULL, "tenuki (answers ko threat)"); sgftreeAddPlayLast(sgf_dumptree, NULL, color, i, j); sgftreeAddComment(sgf_dumptree, NULL, buf); } if (count_variations) count_variations++; stats.nodes++; return 1;}
6496 /local/tlutelli/issta_data/temp/c/2005_temp/2005/6496/2a5a6dbb2a697083e7683965c1293f04fe090706/board.c/buggy/engine/board.c
for (int i=0; i<st.num_fh; i++) { CFile *f = new CFile; bl.copy(off, sizeof(CFile), (char*)f); off += sizeof(CFile); fh_list.push_back(f);
for (int i=0; i<st.num_caps; i++) { int c; bl.copy(off, sizeof(c), (char*)&c); off += sizeof(c); cap_map[c]._decode(bl, off);
int _decode(bufferlist& bl, int off = 0) { bl.copy(off, sizeof(st), (char*)&st); off += sizeof(st); for (int i=0; i<st.ncached_by; i++) { int m,n; bl.copy(off, sizeof(int), (char*)&m); off += sizeof(int); bl.copy(off, sizeof(int), (char*)&n); off += sizeof(int); cached_by.insert(m); cached_by_nonce.insert(pair<int,int>(m,n)); } hardlock.decode_state(bl, off); softlock.decode_state(bl, off); // fh for (int i=0; i<st.num_fh; i++) { CFile *f = new CFile; bl.copy(off, sizeof(CFile), (char*)f); //cout << "f out client " << f->client << " fh " << f->fh << endl; off += sizeof(CFile); fh_list.push_back(f); } return off; }
2690 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2690/8c7283a709cbd1031e4adc5c129a44d81b7a0f8d/CInode.h/buggy/ceph/mds/CInode.h
get_moveXY(prop, &i, &j, gameinfo->position.boardsize);
get_moveXY(prop, &i, &j, board_size);
gameinfo_play_sgftree(Gameinfo *gameinfo, SGFNode *head, const char *untilstr){ int bs, handicap; float komi; int next = BLACK; int untilm = -1, untiln = -1; int until = 9999; int addstone = 0; /* handicap stone detector */ SGFNode *node; if (!sgfGetFloatProperty(head, "KM", &komi)) { if (gameinfo->handicap == 0) komi = 5.5; else komi = 0.5; } if (!sgfGetIntProperty(head, "SZ", &bs)) bs = 19; gnugo_clear_position(&gameinfo->position, bs, komi); /* Now we can safely parse the until string (which depends on board size). */ if (untilstr) { if (*untilstr > '0' && *untilstr <= '9') { until = atoi(untilstr); DEBUG(DEBUG_LOADSGF, "Loading until move %d\n", until); } else { untiln = *untilstr - 'A'; if (*untilstr >= 'I') --untiln; untilm = gameinfo->position.boardsize - atoi(untilstr+1); DEBUG(DEBUG_LOADSGF, "Loading until move at %d,%d (%m)\n", untilm, untiln, untilm, untiln); } } if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 1) { gameinfo->handicap = handicap; next = WHITE; } /* Finally, we iterate over all the properties of all the * nodes, actioning them. We follow only the 'child' pointers, * as we have no interest in variations. * * The sgf routines map AB[aa][bb][cc] into AB[aa]AB[bb]AB[cc] */ for (node = head; node; node = node->child) { SGFProperty *prop; int i, j; for (prop=node->props; prop; prop=prop->next) { DEBUG(DEBUG_LOADSGF, "%c%c[%s]\n", prop->name & 0xff, (prop->name >> 8), prop->value); switch(prop->name) { case SGFAB: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); /* Generally the last move is unknown when the AB or AW * properties are encountered. These are used to set up * a board position (diagram) or to place handicap stones * without reference to the order in which the stones are * placed on the board. */ last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, BLACK); sgffile_put_stone(i, j, BLACK); addstone = 1; break; case SGFAW: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, WHITE); sgffile_put_stone(i, j, WHITE); addstone = 1; break; case SGFPL: /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } /* Due to a bad comment in the SGF FF3 definition (in the * "Alphabetical list of properties" section) some * applications encode the colors with 1 for black and 2 for * white. */ if (prop->value[0] == 'w' || prop->value[0] == 'W' || prop->value[0] == '2') next = WHITE; else next = BLACK; break; case SGFW: case SGFB: next = prop->name == SGFW ? WHITE : BLACK; /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } gameinfo->move_number++; if (gameinfo->move_number == until) return next; if (get_moveXY(prop, &i, &j, gameinfo->position.boardsize)) if (i == untilm && j == untiln) return next; gnugo_play_move(&gameinfo->position, i, j, next); sgffile_move_made(i, j, next, 0); next = OTHER_COLOR(next); break; case SGFIL: /* The IL property is not a standard SGF property but * is used by GNU Go to mark illegal moves. If a move * is found marked with the IL property which is a ko * capture then that ko capture is deemed illegal and * (board_ko_i, board_ko_j) is set to the location of * the ko. */ get_moveXY(prop, &i, &j, gameinfo->position.boardsize); { int move_color; if (!ON_BOARD(i, j)) break; if (i > 0) move_color = OTHER_COLOR(p[i-1][j]); else move_color = OTHER_COLOR(p[1][j]); if (is_ko(i, j, move_color, NULL, NULL)) { board_ko_i = i; board_ko_j = j; } } break; } } } gameinfo->to_move = next; return next;}
6496 /local/tlutelli/issta_data/temp/c/2005_temp/2005/6496/2a5a6dbb2a697083e7683965c1293f04fe090706/interface.c/buggy/engine/interface.c
gnugo_add_stone(&gameinfo->position, i, j, BLACK);
add_stone(i, j, BLACK);
gameinfo_play_sgftree(Gameinfo *gameinfo, SGFNode *head, const char *untilstr){ int bs, handicap; float komi; int next = BLACK; int untilm = -1, untiln = -1; int until = 9999; int addstone = 0; /* handicap stone detector */ SGFNode *node; if (!sgfGetFloatProperty(head, "KM", &komi)) { if (gameinfo->handicap == 0) komi = 5.5; else komi = 0.5; } if (!sgfGetIntProperty(head, "SZ", &bs)) bs = 19; gnugo_clear_position(&gameinfo->position, bs, komi); /* Now we can safely parse the until string (which depends on board size). */ if (untilstr) { if (*untilstr > '0' && *untilstr <= '9') { until = atoi(untilstr); DEBUG(DEBUG_LOADSGF, "Loading until move %d\n", until); } else { untiln = *untilstr - 'A'; if (*untilstr >= 'I') --untiln; untilm = gameinfo->position.boardsize - atoi(untilstr+1); DEBUG(DEBUG_LOADSGF, "Loading until move at %d,%d (%m)\n", untilm, untiln, untilm, untiln); } } if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 1) { gameinfo->handicap = handicap; next = WHITE; } /* Finally, we iterate over all the properties of all the * nodes, actioning them. We follow only the 'child' pointers, * as we have no interest in variations. * * The sgf routines map AB[aa][bb][cc] into AB[aa]AB[bb]AB[cc] */ for (node = head; node; node = node->child) { SGFProperty *prop; int i, j; for (prop=node->props; prop; prop=prop->next) { DEBUG(DEBUG_LOADSGF, "%c%c[%s]\n", prop->name & 0xff, (prop->name >> 8), prop->value); switch(prop->name) { case SGFAB: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); /* Generally the last move is unknown when the AB or AW * properties are encountered. These are used to set up * a board position (diagram) or to place handicap stones * without reference to the order in which the stones are * placed on the board. */ last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, BLACK); sgffile_put_stone(i, j, BLACK); addstone = 1; break; case SGFAW: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, WHITE); sgffile_put_stone(i, j, WHITE); addstone = 1; break; case SGFPL: /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } /* Due to a bad comment in the SGF FF3 definition (in the * "Alphabetical list of properties" section) some * applications encode the colors with 1 for black and 2 for * white. */ if (prop->value[0] == 'w' || prop->value[0] == 'W' || prop->value[0] == '2') next = WHITE; else next = BLACK; break; case SGFW: case SGFB: next = prop->name == SGFW ? WHITE : BLACK; /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } gameinfo->move_number++; if (gameinfo->move_number == until) return next; if (get_moveXY(prop, &i, &j, gameinfo->position.boardsize)) if (i == untilm && j == untiln) return next; gnugo_play_move(&gameinfo->position, i, j, next); sgffile_move_made(i, j, next, 0); next = OTHER_COLOR(next); break; case SGFIL: /* The IL property is not a standard SGF property but * is used by GNU Go to mark illegal moves. If a move * is found marked with the IL property which is a ko * capture then that ko capture is deemed illegal and * (board_ko_i, board_ko_j) is set to the location of * the ko. */ get_moveXY(prop, &i, &j, gameinfo->position.boardsize); { int move_color; if (!ON_BOARD(i, j)) break; if (i > 0) move_color = OTHER_COLOR(p[i-1][j]); else move_color = OTHER_COLOR(p[1][j]); if (is_ko(i, j, move_color, NULL, NULL)) { board_ko_i = i; board_ko_j = j; } } break; } } } gameinfo->to_move = next; return next;}
6496 /local/tlutelli/issta_data/temp/c/2005_temp/2005/6496/2a5a6dbb2a697083e7683965c1293f04fe090706/interface.c/buggy/engine/interface.c
get_moveXY(prop, &i, &j, gameinfo->position.boardsize);
get_moveXY(prop, &i, &j, board_size);
gameinfo_play_sgftree(Gameinfo *gameinfo, SGFNode *head, const char *untilstr){ int bs, handicap; float komi; int next = BLACK; int untilm = -1, untiln = -1; int until = 9999; int addstone = 0; /* handicap stone detector */ SGFNode *node; if (!sgfGetFloatProperty(head, "KM", &komi)) { if (gameinfo->handicap == 0) komi = 5.5; else komi = 0.5; } if (!sgfGetIntProperty(head, "SZ", &bs)) bs = 19; gnugo_clear_position(&gameinfo->position, bs, komi); /* Now we can safely parse the until string (which depends on board size). */ if (untilstr) { if (*untilstr > '0' && *untilstr <= '9') { until = atoi(untilstr); DEBUG(DEBUG_LOADSGF, "Loading until move %d\n", until); } else { untiln = *untilstr - 'A'; if (*untilstr >= 'I') --untiln; untilm = gameinfo->position.boardsize - atoi(untilstr+1); DEBUG(DEBUG_LOADSGF, "Loading until move at %d,%d (%m)\n", untilm, untiln, untilm, untiln); } } if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 1) { gameinfo->handicap = handicap; next = WHITE; } /* Finally, we iterate over all the properties of all the * nodes, actioning them. We follow only the 'child' pointers, * as we have no interest in variations. * * The sgf routines map AB[aa][bb][cc] into AB[aa]AB[bb]AB[cc] */ for (node = head; node; node = node->child) { SGFProperty *prop; int i, j; for (prop=node->props; prop; prop=prop->next) { DEBUG(DEBUG_LOADSGF, "%c%c[%s]\n", prop->name & 0xff, (prop->name >> 8), prop->value); switch(prop->name) { case SGFAB: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); /* Generally the last move is unknown when the AB or AW * properties are encountered. These are used to set up * a board position (diagram) or to place handicap stones * without reference to the order in which the stones are * placed on the board. */ last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, BLACK); sgffile_put_stone(i, j, BLACK); addstone = 1; break; case SGFAW: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, WHITE); sgffile_put_stone(i, j, WHITE); addstone = 1; break; case SGFPL: /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } /* Due to a bad comment in the SGF FF3 definition (in the * "Alphabetical list of properties" section) some * applications encode the colors with 1 for black and 2 for * white. */ if (prop->value[0] == 'w' || prop->value[0] == 'W' || prop->value[0] == '2') next = WHITE; else next = BLACK; break; case SGFW: case SGFB: next = prop->name == SGFW ? WHITE : BLACK; /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } gameinfo->move_number++; if (gameinfo->move_number == until) return next; if (get_moveXY(prop, &i, &j, gameinfo->position.boardsize)) if (i == untilm && j == untiln) return next; gnugo_play_move(&gameinfo->position, i, j, next); sgffile_move_made(i, j, next, 0); next = OTHER_COLOR(next); break; case SGFIL: /* The IL property is not a standard SGF property but * is used by GNU Go to mark illegal moves. If a move * is found marked with the IL property which is a ko * capture then that ko capture is deemed illegal and * (board_ko_i, board_ko_j) is set to the location of * the ko. */ get_moveXY(prop, &i, &j, gameinfo->position.boardsize); { int move_color; if (!ON_BOARD(i, j)) break; if (i > 0) move_color = OTHER_COLOR(p[i-1][j]); else move_color = OTHER_COLOR(p[1][j]); if (is_ko(i, j, move_color, NULL, NULL)) { board_ko_i = i; board_ko_j = j; } } break; } } } gameinfo->to_move = next; return next;}
6496 /local/tlutelli/issta_data/temp/c/2005_temp/2005/6496/2a5a6dbb2a697083e7683965c1293f04fe090706/interface.c/buggy/engine/interface.c
gnugo_add_stone(&gameinfo->position, i, j, WHITE);
add_stone(i, j, WHITE);
gameinfo_play_sgftree(Gameinfo *gameinfo, SGFNode *head, const char *untilstr){ int bs, handicap; float komi; int next = BLACK; int untilm = -1, untiln = -1; int until = 9999; int addstone = 0; /* handicap stone detector */ SGFNode *node; if (!sgfGetFloatProperty(head, "KM", &komi)) { if (gameinfo->handicap == 0) komi = 5.5; else komi = 0.5; } if (!sgfGetIntProperty(head, "SZ", &bs)) bs = 19; gnugo_clear_position(&gameinfo->position, bs, komi); /* Now we can safely parse the until string (which depends on board size). */ if (untilstr) { if (*untilstr > '0' && *untilstr <= '9') { until = atoi(untilstr); DEBUG(DEBUG_LOADSGF, "Loading until move %d\n", until); } else { untiln = *untilstr - 'A'; if (*untilstr >= 'I') --untiln; untilm = gameinfo->position.boardsize - atoi(untilstr+1); DEBUG(DEBUG_LOADSGF, "Loading until move at %d,%d (%m)\n", untilm, untiln, untilm, untiln); } } if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 1) { gameinfo->handicap = handicap; next = WHITE; } /* Finally, we iterate over all the properties of all the * nodes, actioning them. We follow only the 'child' pointers, * as we have no interest in variations. * * The sgf routines map AB[aa][bb][cc] into AB[aa]AB[bb]AB[cc] */ for (node = head; node; node = node->child) { SGFProperty *prop; int i, j; for (prop=node->props; prop; prop=prop->next) { DEBUG(DEBUG_LOADSGF, "%c%c[%s]\n", prop->name & 0xff, (prop->name >> 8), prop->value); switch(prop->name) { case SGFAB: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); /* Generally the last move is unknown when the AB or AW * properties are encountered. These are used to set up * a board position (diagram) or to place handicap stones * without reference to the order in which the stones are * placed on the board. */ last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, BLACK); sgffile_put_stone(i, j, BLACK); addstone = 1; break; case SGFAW: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, WHITE); sgffile_put_stone(i, j, WHITE); addstone = 1; break; case SGFPL: /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } /* Due to a bad comment in the SGF FF3 definition (in the * "Alphabetical list of properties" section) some * applications encode the colors with 1 for black and 2 for * white. */ if (prop->value[0] == 'w' || prop->value[0] == 'W' || prop->value[0] == '2') next = WHITE; else next = BLACK; break; case SGFW: case SGFB: next = prop->name == SGFW ? WHITE : BLACK; /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } gameinfo->move_number++; if (gameinfo->move_number == until) return next; if (get_moveXY(prop, &i, &j, gameinfo->position.boardsize)) if (i == untilm && j == untiln) return next; gnugo_play_move(&gameinfo->position, i, j, next); sgffile_move_made(i, j, next, 0); next = OTHER_COLOR(next); break; case SGFIL: /* The IL property is not a standard SGF property but * is used by GNU Go to mark illegal moves. If a move * is found marked with the IL property which is a ko * capture then that ko capture is deemed illegal and * (board_ko_i, board_ko_j) is set to the location of * the ko. */ get_moveXY(prop, &i, &j, gameinfo->position.boardsize); { int move_color; if (!ON_BOARD(i, j)) break; if (i > 0) move_color = OTHER_COLOR(p[i-1][j]); else move_color = OTHER_COLOR(p[1][j]); if (is_ko(i, j, move_color, NULL, NULL)) { board_ko_i = i; board_ko_j = j; } } break; } } } gameinfo->to_move = next; return next;}
6496 /local/tlutelli/issta_data/temp/c/2005_temp/2005/6496/2a5a6dbb2a697083e7683965c1293f04fe090706/interface.c/buggy/engine/interface.c
gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0);
placehand(gameinfo->handicap);
gameinfo_play_sgftree(Gameinfo *gameinfo, SGFNode *head, const char *untilstr){ int bs, handicap; float komi; int next = BLACK; int untilm = -1, untiln = -1; int until = 9999; int addstone = 0; /* handicap stone detector */ SGFNode *node; if (!sgfGetFloatProperty(head, "KM", &komi)) { if (gameinfo->handicap == 0) komi = 5.5; else komi = 0.5; } if (!sgfGetIntProperty(head, "SZ", &bs)) bs = 19; gnugo_clear_position(&gameinfo->position, bs, komi); /* Now we can safely parse the until string (which depends on board size). */ if (untilstr) { if (*untilstr > '0' && *untilstr <= '9') { until = atoi(untilstr); DEBUG(DEBUG_LOADSGF, "Loading until move %d\n", until); } else { untiln = *untilstr - 'A'; if (*untilstr >= 'I') --untiln; untilm = gameinfo->position.boardsize - atoi(untilstr+1); DEBUG(DEBUG_LOADSGF, "Loading until move at %d,%d (%m)\n", untilm, untiln, untilm, untiln); } } if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 1) { gameinfo->handicap = handicap; next = WHITE; } /* Finally, we iterate over all the properties of all the * nodes, actioning them. We follow only the 'child' pointers, * as we have no interest in variations. * * The sgf routines map AB[aa][bb][cc] into AB[aa]AB[bb]AB[cc] */ for (node = head; node; node = node->child) { SGFProperty *prop; int i, j; for (prop=node->props; prop; prop=prop->next) { DEBUG(DEBUG_LOADSGF, "%c%c[%s]\n", prop->name & 0xff, (prop->name >> 8), prop->value); switch(prop->name) { case SGFAB: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); /* Generally the last move is unknown when the AB or AW * properties are encountered. These are used to set up * a board position (diagram) or to place handicap stones * without reference to the order in which the stones are * placed on the board. */ last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, BLACK); sgffile_put_stone(i, j, BLACK); addstone = 1; break; case SGFAW: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, WHITE); sgffile_put_stone(i, j, WHITE); addstone = 1; break; case SGFPL: /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } /* Due to a bad comment in the SGF FF3 definition (in the * "Alphabetical list of properties" section) some * applications encode the colors with 1 for black and 2 for * white. */ if (prop->value[0] == 'w' || prop->value[0] == 'W' || prop->value[0] == '2') next = WHITE; else next = BLACK; break; case SGFW: case SGFB: next = prop->name == SGFW ? WHITE : BLACK; /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } gameinfo->move_number++; if (gameinfo->move_number == until) return next; if (get_moveXY(prop, &i, &j, gameinfo->position.boardsize)) if (i == untilm && j == untiln) return next; gnugo_play_move(&gameinfo->position, i, j, next); sgffile_move_made(i, j, next, 0); next = OTHER_COLOR(next); break; case SGFIL: /* The IL property is not a standard SGF property but * is used by GNU Go to mark illegal moves. If a move * is found marked with the IL property which is a ko * capture then that ko capture is deemed illegal and * (board_ko_i, board_ko_j) is set to the location of * the ko. */ get_moveXY(prop, &i, &j, gameinfo->position.boardsize); { int move_color; if (!ON_BOARD(i, j)) break; if (i > 0) move_color = OTHER_COLOR(p[i-1][j]); else move_color = OTHER_COLOR(p[1][j]); if (is_ko(i, j, move_color, NULL, NULL)) { board_ko_i = i; board_ko_j = j; } } break; } } } gameinfo->to_move = next; return next;}
6496 /local/tlutelli/issta_data/temp/c/2005_temp/2005/6496/2a5a6dbb2a697083e7683965c1293f04fe090706/interface.c/buggy/engine/interface.c
gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0);
placehand(gameinfo->handicap);
gameinfo_play_sgftree(Gameinfo *gameinfo, SGFNode *head, const char *untilstr){ int bs, handicap; float komi; int next = BLACK; int untilm = -1, untiln = -1; int until = 9999; int addstone = 0; /* handicap stone detector */ SGFNode *node; if (!sgfGetFloatProperty(head, "KM", &komi)) { if (gameinfo->handicap == 0) komi = 5.5; else komi = 0.5; } if (!sgfGetIntProperty(head, "SZ", &bs)) bs = 19; gnugo_clear_position(&gameinfo->position, bs, komi); /* Now we can safely parse the until string (which depends on board size). */ if (untilstr) { if (*untilstr > '0' && *untilstr <= '9') { until = atoi(untilstr); DEBUG(DEBUG_LOADSGF, "Loading until move %d\n", until); } else { untiln = *untilstr - 'A'; if (*untilstr >= 'I') --untiln; untilm = gameinfo->position.boardsize - atoi(untilstr+1); DEBUG(DEBUG_LOADSGF, "Loading until move at %d,%d (%m)\n", untilm, untiln, untilm, untiln); } } if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 1) { gameinfo->handicap = handicap; next = WHITE; } /* Finally, we iterate over all the properties of all the * nodes, actioning them. We follow only the 'child' pointers, * as we have no interest in variations. * * The sgf routines map AB[aa][bb][cc] into AB[aa]AB[bb]AB[cc] */ for (node = head; node; node = node->child) { SGFProperty *prop; int i, j; for (prop=node->props; prop; prop=prop->next) { DEBUG(DEBUG_LOADSGF, "%c%c[%s]\n", prop->name & 0xff, (prop->name >> 8), prop->value); switch(prop->name) { case SGFAB: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); /* Generally the last move is unknown when the AB or AW * properties are encountered. These are used to set up * a board position (diagram) or to place handicap stones * without reference to the order in which the stones are * placed on the board. */ last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, BLACK); sgffile_put_stone(i, j, BLACK); addstone = 1; break; case SGFAW: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, WHITE); sgffile_put_stone(i, j, WHITE); addstone = 1; break; case SGFPL: /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } /* Due to a bad comment in the SGF FF3 definition (in the * "Alphabetical list of properties" section) some * applications encode the colors with 1 for black and 2 for * white. */ if (prop->value[0] == 'w' || prop->value[0] == 'W' || prop->value[0] == '2') next = WHITE; else next = BLACK; break; case SGFW: case SGFB: next = prop->name == SGFW ? WHITE : BLACK; /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } gameinfo->move_number++; if (gameinfo->move_number == until) return next; if (get_moveXY(prop, &i, &j, gameinfo->position.boardsize)) if (i == untilm && j == untiln) return next; gnugo_play_move(&gameinfo->position, i, j, next); sgffile_move_made(i, j, next, 0); next = OTHER_COLOR(next); break; case SGFIL: /* The IL property is not a standard SGF property but * is used by GNU Go to mark illegal moves. If a move * is found marked with the IL property which is a ko * capture then that ko capture is deemed illegal and * (board_ko_i, board_ko_j) is set to the location of * the ko. */ get_moveXY(prop, &i, &j, gameinfo->position.boardsize); { int move_color; if (!ON_BOARD(i, j)) break; if (i > 0) move_color = OTHER_COLOR(p[i-1][j]); else move_color = OTHER_COLOR(p[1][j]); if (is_ko(i, j, move_color, NULL, NULL)) { board_ko_i = i; board_ko_j = j; } } break; } } } gameinfo->to_move = next; return next;}
6496 /local/tlutelli/issta_data/temp/c/2005_temp/2005/6496/2a5a6dbb2a697083e7683965c1293f04fe090706/interface.c/buggy/engine/interface.c
if (gameinfo->move_number == until)
if (gameinfo->move_number == until) { gameinfo->to_move = next; globals_to_position(&gameinfo->position);
gameinfo_play_sgftree(Gameinfo *gameinfo, SGFNode *head, const char *untilstr){ int bs, handicap; float komi; int next = BLACK; int untilm = -1, untiln = -1; int until = 9999; int addstone = 0; /* handicap stone detector */ SGFNode *node; if (!sgfGetFloatProperty(head, "KM", &komi)) { if (gameinfo->handicap == 0) komi = 5.5; else komi = 0.5; } if (!sgfGetIntProperty(head, "SZ", &bs)) bs = 19; gnugo_clear_position(&gameinfo->position, bs, komi); /* Now we can safely parse the until string (which depends on board size). */ if (untilstr) { if (*untilstr > '0' && *untilstr <= '9') { until = atoi(untilstr); DEBUG(DEBUG_LOADSGF, "Loading until move %d\n", until); } else { untiln = *untilstr - 'A'; if (*untilstr >= 'I') --untiln; untilm = gameinfo->position.boardsize - atoi(untilstr+1); DEBUG(DEBUG_LOADSGF, "Loading until move at %d,%d (%m)\n", untilm, untiln, untilm, untiln); } } if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 1) { gameinfo->handicap = handicap; next = WHITE; } /* Finally, we iterate over all the properties of all the * nodes, actioning them. We follow only the 'child' pointers, * as we have no interest in variations. * * The sgf routines map AB[aa][bb][cc] into AB[aa]AB[bb]AB[cc] */ for (node = head; node; node = node->child) { SGFProperty *prop; int i, j; for (prop=node->props; prop; prop=prop->next) { DEBUG(DEBUG_LOADSGF, "%c%c[%s]\n", prop->name & 0xff, (prop->name >> 8), prop->value); switch(prop->name) { case SGFAB: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); /* Generally the last move is unknown when the AB or AW * properties are encountered. These are used to set up * a board position (diagram) or to place handicap stones * without reference to the order in which the stones are * placed on the board. */ last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, BLACK); sgffile_put_stone(i, j, BLACK); addstone = 1; break; case SGFAW: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, WHITE); sgffile_put_stone(i, j, WHITE); addstone = 1; break; case SGFPL: /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } /* Due to a bad comment in the SGF FF3 definition (in the * "Alphabetical list of properties" section) some * applications encode the colors with 1 for black and 2 for * white. */ if (prop->value[0] == 'w' || prop->value[0] == 'W' || prop->value[0] == '2') next = WHITE; else next = BLACK; break; case SGFW: case SGFB: next = prop->name == SGFW ? WHITE : BLACK; /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } gameinfo->move_number++; if (gameinfo->move_number == until) return next; if (get_moveXY(prop, &i, &j, gameinfo->position.boardsize)) if (i == untilm && j == untiln) return next; gnugo_play_move(&gameinfo->position, i, j, next); sgffile_move_made(i, j, next, 0); next = OTHER_COLOR(next); break; case SGFIL: /* The IL property is not a standard SGF property but * is used by GNU Go to mark illegal moves. If a move * is found marked with the IL property which is a ko * capture then that ko capture is deemed illegal and * (board_ko_i, board_ko_j) is set to the location of * the ko. */ get_moveXY(prop, &i, &j, gameinfo->position.boardsize); { int move_color; if (!ON_BOARD(i, j)) break; if (i > 0) move_color = OTHER_COLOR(p[i-1][j]); else move_color = OTHER_COLOR(p[1][j]); if (is_ko(i, j, move_color, NULL, NULL)) { board_ko_i = i; board_ko_j = j; } } break; } } } gameinfo->to_move = next; return next;}
6496 /local/tlutelli/issta_data/temp/c/2005_temp/2005/6496/2a5a6dbb2a697083e7683965c1293f04fe090706/interface.c/buggy/engine/interface.c
if (get_moveXY(prop, &i, &j, gameinfo->position.boardsize)) if (i == untilm && j == untiln)
if (get_moveXY(prop, &i, &j, board_size)) if (i == untilm && j == untiln) { gameinfo->to_move = next; globals_to_position(&gameinfo->position);
gameinfo_play_sgftree(Gameinfo *gameinfo, SGFNode *head, const char *untilstr){ int bs, handicap; float komi; int next = BLACK; int untilm = -1, untiln = -1; int until = 9999; int addstone = 0; /* handicap stone detector */ SGFNode *node; if (!sgfGetFloatProperty(head, "KM", &komi)) { if (gameinfo->handicap == 0) komi = 5.5; else komi = 0.5; } if (!sgfGetIntProperty(head, "SZ", &bs)) bs = 19; gnugo_clear_position(&gameinfo->position, bs, komi); /* Now we can safely parse the until string (which depends on board size). */ if (untilstr) { if (*untilstr > '0' && *untilstr <= '9') { until = atoi(untilstr); DEBUG(DEBUG_LOADSGF, "Loading until move %d\n", until); } else { untiln = *untilstr - 'A'; if (*untilstr >= 'I') --untiln; untilm = gameinfo->position.boardsize - atoi(untilstr+1); DEBUG(DEBUG_LOADSGF, "Loading until move at %d,%d (%m)\n", untilm, untiln, untilm, untiln); } } if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 1) { gameinfo->handicap = handicap; next = WHITE; } /* Finally, we iterate over all the properties of all the * nodes, actioning them. We follow only the 'child' pointers, * as we have no interest in variations. * * The sgf routines map AB[aa][bb][cc] into AB[aa]AB[bb]AB[cc] */ for (node = head; node; node = node->child) { SGFProperty *prop; int i, j; for (prop=node->props; prop; prop=prop->next) { DEBUG(DEBUG_LOADSGF, "%c%c[%s]\n", prop->name & 0xff, (prop->name >> 8), prop->value); switch(prop->name) { case SGFAB: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); /* Generally the last move is unknown when the AB or AW * properties are encountered. These are used to set up * a board position (diagram) or to place handicap stones * without reference to the order in which the stones are * placed on the board. */ last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, BLACK); sgffile_put_stone(i, j, BLACK); addstone = 1; break; case SGFAW: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, WHITE); sgffile_put_stone(i, j, WHITE); addstone = 1; break; case SGFPL: /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } /* Due to a bad comment in the SGF FF3 definition (in the * "Alphabetical list of properties" section) some * applications encode the colors with 1 for black and 2 for * white. */ if (prop->value[0] == 'w' || prop->value[0] == 'W' || prop->value[0] == '2') next = WHITE; else next = BLACK; break; case SGFW: case SGFB: next = prop->name == SGFW ? WHITE : BLACK; /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } gameinfo->move_number++; if (gameinfo->move_number == until) return next; if (get_moveXY(prop, &i, &j, gameinfo->position.boardsize)) if (i == untilm && j == untiln) return next; gnugo_play_move(&gameinfo->position, i, j, next); sgffile_move_made(i, j, next, 0); next = OTHER_COLOR(next); break; case SGFIL: /* The IL property is not a standard SGF property but * is used by GNU Go to mark illegal moves. If a move * is found marked with the IL property which is a ko * capture then that ko capture is deemed illegal and * (board_ko_i, board_ko_j) is set to the location of * the ko. */ get_moveXY(prop, &i, &j, gameinfo->position.boardsize); { int move_color; if (!ON_BOARD(i, j)) break; if (i > 0) move_color = OTHER_COLOR(p[i-1][j]); else move_color = OTHER_COLOR(p[1][j]); if (is_ko(i, j, move_color, NULL, NULL)) { board_ko_i = i; board_ko_j = j; } } break; } } } gameinfo->to_move = next; return next;}
6496 /local/tlutelli/issta_data/temp/c/2005_temp/2005/6496/2a5a6dbb2a697083e7683965c1293f04fe090706/interface.c/buggy/engine/interface.c
gnugo_play_move(&gameinfo->position, i, j, next);
play_move(i, j, next);
gameinfo_play_sgftree(Gameinfo *gameinfo, SGFNode *head, const char *untilstr){ int bs, handicap; float komi; int next = BLACK; int untilm = -1, untiln = -1; int until = 9999; int addstone = 0; /* handicap stone detector */ SGFNode *node; if (!sgfGetFloatProperty(head, "KM", &komi)) { if (gameinfo->handicap == 0) komi = 5.5; else komi = 0.5; } if (!sgfGetIntProperty(head, "SZ", &bs)) bs = 19; gnugo_clear_position(&gameinfo->position, bs, komi); /* Now we can safely parse the until string (which depends on board size). */ if (untilstr) { if (*untilstr > '0' && *untilstr <= '9') { until = atoi(untilstr); DEBUG(DEBUG_LOADSGF, "Loading until move %d\n", until); } else { untiln = *untilstr - 'A'; if (*untilstr >= 'I') --untiln; untilm = gameinfo->position.boardsize - atoi(untilstr+1); DEBUG(DEBUG_LOADSGF, "Loading until move at %d,%d (%m)\n", untilm, untiln, untilm, untiln); } } if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 1) { gameinfo->handicap = handicap; next = WHITE; } /* Finally, we iterate over all the properties of all the * nodes, actioning them. We follow only the 'child' pointers, * as we have no interest in variations. * * The sgf routines map AB[aa][bb][cc] into AB[aa]AB[bb]AB[cc] */ for (node = head; node; node = node->child) { SGFProperty *prop; int i, j; for (prop=node->props; prop; prop=prop->next) { DEBUG(DEBUG_LOADSGF, "%c%c[%s]\n", prop->name & 0xff, (prop->name >> 8), prop->value); switch(prop->name) { case SGFAB: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); /* Generally the last move is unknown when the AB or AW * properties are encountered. These are used to set up * a board position (diagram) or to place handicap stones * without reference to the order in which the stones are * placed on the board. */ last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, BLACK); sgffile_put_stone(i, j, BLACK); addstone = 1; break; case SGFAW: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, WHITE); sgffile_put_stone(i, j, WHITE); addstone = 1; break; case SGFPL: /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } /* Due to a bad comment in the SGF FF3 definition (in the * "Alphabetical list of properties" section) some * applications encode the colors with 1 for black and 2 for * white. */ if (prop->value[0] == 'w' || prop->value[0] == 'W' || prop->value[0] == '2') next = WHITE; else next = BLACK; break; case SGFW: case SGFB: next = prop->name == SGFW ? WHITE : BLACK; /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } gameinfo->move_number++; if (gameinfo->move_number == until) return next; if (get_moveXY(prop, &i, &j, gameinfo->position.boardsize)) if (i == untilm && j == untiln) return next; gnugo_play_move(&gameinfo->position, i, j, next); sgffile_move_made(i, j, next, 0); next = OTHER_COLOR(next); break; case SGFIL: /* The IL property is not a standard SGF property but * is used by GNU Go to mark illegal moves. If a move * is found marked with the IL property which is a ko * capture then that ko capture is deemed illegal and * (board_ko_i, board_ko_j) is set to the location of * the ko. */ get_moveXY(prop, &i, &j, gameinfo->position.boardsize); { int move_color; if (!ON_BOARD(i, j)) break; if (i > 0) move_color = OTHER_COLOR(p[i-1][j]); else move_color = OTHER_COLOR(p[1][j]); if (is_ko(i, j, move_color, NULL, NULL)) { board_ko_i = i; board_ko_j = j; } } break; } } } gameinfo->to_move = next; return next;}
6496 /local/tlutelli/issta_data/temp/c/2005_temp/2005/6496/2a5a6dbb2a697083e7683965c1293f04fe090706/interface.c/buggy/engine/interface.c
get_moveXY(prop, &i, &j, gameinfo->position.boardsize);
get_moveXY(prop, &i, &j, board_size);
gameinfo_play_sgftree(Gameinfo *gameinfo, SGFNode *head, const char *untilstr){ int bs, handicap; float komi; int next = BLACK; int untilm = -1, untiln = -1; int until = 9999; int addstone = 0; /* handicap stone detector */ SGFNode *node; if (!sgfGetFloatProperty(head, "KM", &komi)) { if (gameinfo->handicap == 0) komi = 5.5; else komi = 0.5; } if (!sgfGetIntProperty(head, "SZ", &bs)) bs = 19; gnugo_clear_position(&gameinfo->position, bs, komi); /* Now we can safely parse the until string (which depends on board size). */ if (untilstr) { if (*untilstr > '0' && *untilstr <= '9') { until = atoi(untilstr); DEBUG(DEBUG_LOADSGF, "Loading until move %d\n", until); } else { untiln = *untilstr - 'A'; if (*untilstr >= 'I') --untiln; untilm = gameinfo->position.boardsize - atoi(untilstr+1); DEBUG(DEBUG_LOADSGF, "Loading until move at %d,%d (%m)\n", untilm, untiln, untilm, untiln); } } if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 1) { gameinfo->handicap = handicap; next = WHITE; } /* Finally, we iterate over all the properties of all the * nodes, actioning them. We follow only the 'child' pointers, * as we have no interest in variations. * * The sgf routines map AB[aa][bb][cc] into AB[aa]AB[bb]AB[cc] */ for (node = head; node; node = node->child) { SGFProperty *prop; int i, j; for (prop=node->props; prop; prop=prop->next) { DEBUG(DEBUG_LOADSGF, "%c%c[%s]\n", prop->name & 0xff, (prop->name >> 8), prop->value); switch(prop->name) { case SGFAB: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); /* Generally the last move is unknown when the AB or AW * properties are encountered. These are used to set up * a board position (diagram) or to place handicap stones * without reference to the order in which the stones are * placed on the board. */ last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, BLACK); sgffile_put_stone(i, j, BLACK); addstone = 1; break; case SGFAW: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, WHITE); sgffile_put_stone(i, j, WHITE); addstone = 1; break; case SGFPL: /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } /* Due to a bad comment in the SGF FF3 definition (in the * "Alphabetical list of properties" section) some * applications encode the colors with 1 for black and 2 for * white. */ if (prop->value[0] == 'w' || prop->value[0] == 'W' || prop->value[0] == '2') next = WHITE; else next = BLACK; break; case SGFW: case SGFB: next = prop->name == SGFW ? WHITE : BLACK; /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } gameinfo->move_number++; if (gameinfo->move_number == until) return next; if (get_moveXY(prop, &i, &j, gameinfo->position.boardsize)) if (i == untilm && j == untiln) return next; gnugo_play_move(&gameinfo->position, i, j, next); sgffile_move_made(i, j, next, 0); next = OTHER_COLOR(next); break; case SGFIL: /* The IL property is not a standard SGF property but * is used by GNU Go to mark illegal moves. If a move * is found marked with the IL property which is a ko * capture then that ko capture is deemed illegal and * (board_ko_i, board_ko_j) is set to the location of * the ko. */ get_moveXY(prop, &i, &j, gameinfo->position.boardsize); { int move_color; if (!ON_BOARD(i, j)) break; if (i > 0) move_color = OTHER_COLOR(p[i-1][j]); else move_color = OTHER_COLOR(p[1][j]); if (is_ko(i, j, move_color, NULL, NULL)) { board_ko_i = i; board_ko_j = j; } } break; } } } gameinfo->to_move = next; return next;}
6496 /local/tlutelli/issta_data/temp/c/2005_temp/2005/6496/2a5a6dbb2a697083e7683965c1293f04fe090706/interface.c/buggy/engine/interface.c
globals_to_position(&gameinfo->position);
gameinfo_play_sgftree(Gameinfo *gameinfo, SGFNode *head, const char *untilstr){ int bs, handicap; float komi; int next = BLACK; int untilm = -1, untiln = -1; int until = 9999; int addstone = 0; /* handicap stone detector */ SGFNode *node; if (!sgfGetFloatProperty(head, "KM", &komi)) { if (gameinfo->handicap == 0) komi = 5.5; else komi = 0.5; } if (!sgfGetIntProperty(head, "SZ", &bs)) bs = 19; gnugo_clear_position(&gameinfo->position, bs, komi); /* Now we can safely parse the until string (which depends on board size). */ if (untilstr) { if (*untilstr > '0' && *untilstr <= '9') { until = atoi(untilstr); DEBUG(DEBUG_LOADSGF, "Loading until move %d\n", until); } else { untiln = *untilstr - 'A'; if (*untilstr >= 'I') --untiln; untilm = gameinfo->position.boardsize - atoi(untilstr+1); DEBUG(DEBUG_LOADSGF, "Loading until move at %d,%d (%m)\n", untilm, untiln, untilm, untiln); } } if (sgfGetIntProperty(head, "HA", &handicap) && handicap > 1) { gameinfo->handicap = handicap; next = WHITE; } /* Finally, we iterate over all the properties of all the * nodes, actioning them. We follow only the 'child' pointers, * as we have no interest in variations. * * The sgf routines map AB[aa][bb][cc] into AB[aa]AB[bb]AB[cc] */ for (node = head; node; node = node->child) { SGFProperty *prop; int i, j; for (prop=node->props; prop; prop=prop->next) { DEBUG(DEBUG_LOADSGF, "%c%c[%s]\n", prop->name & 0xff, (prop->name >> 8), prop->value); switch(prop->name) { case SGFAB: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); /* Generally the last move is unknown when the AB or AW * properties are encountered. These are used to set up * a board position (diagram) or to place handicap stones * without reference to the order in which the stones are * placed on the board. */ last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, BLACK); sgffile_put_stone(i, j, BLACK); addstone = 1; break; case SGFAW: get_moveXY(prop, &i, &j, gameinfo->position.boardsize); last_moves_i[0] = -1; last_moves_j[0] = -1; last_moves_i[1] = -1; last_moves_j[1] = -1; gnugo_add_stone(&gameinfo->position, i, j, WHITE); sgffile_put_stone(i, j, WHITE); addstone = 1; break; case SGFPL: /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } /* Due to a bad comment in the SGF FF3 definition (in the * "Alphabetical list of properties" section) some * applications encode the colors with 1 for black and 2 for * white. */ if (prop->value[0] == 'w' || prop->value[0] == 'W' || prop->value[0] == '2') next = WHITE; else next = BLACK; break; case SGFW: case SGFB: next = prop->name == SGFW ? WHITE : BLACK; /* following really should not be needed for proper sgf file */ if (!gameinfo->move_number && !addstone) { gnugo_sethand(&gameinfo->position, gameinfo->handicap, 0); sgfOverwritePropertyInt(head, "HA", handicap); } gameinfo->move_number++; if (gameinfo->move_number == until) return next; if (get_moveXY(prop, &i, &j, gameinfo->position.boardsize)) if (i == untilm && j == untiln) return next; gnugo_play_move(&gameinfo->position, i, j, next); sgffile_move_made(i, j, next, 0); next = OTHER_COLOR(next); break; case SGFIL: /* The IL property is not a standard SGF property but * is used by GNU Go to mark illegal moves. If a move * is found marked with the IL property which is a ko * capture then that ko capture is deemed illegal and * (board_ko_i, board_ko_j) is set to the location of * the ko. */ get_moveXY(prop, &i, &j, gameinfo->position.boardsize); { int move_color; if (!ON_BOARD(i, j)) break; if (i > 0) move_color = OTHER_COLOR(p[i-1][j]); else move_color = OTHER_COLOR(p[1][j]); if (is_ko(i, j, move_color, NULL, NULL)) { board_ko_i = i; board_ko_j = j; } } break; } } } gameinfo->to_move = next; return next;}
6496 /local/tlutelli/issta_data/temp/c/2005_temp/2005/6496/2a5a6dbb2a697083e7683965c1293f04fe090706/interface.c/buggy/engine/interface.c
lpStruct->iItem = (*env)->GetIntField(env, lpObject, LVHITTESTINFOFc.iItem); lpStruct->flags = (*env)->GetIntField(env, lpObject, LVHITTESTINFOFc.flags); lpStruct->pt.y = (*env)->GetIntField(env, lpObject, LVHITTESTINFOFc.y); lpStruct->pt.x = (*env)->GetIntField(env, lpObject, LVHITTESTINFOFc.x);
LVHITTESTINFO *getLVHITTESTINFOFields(JNIEnv *env, jobject lpObject, LVHITTESTINFO *lpStruct){ if (!LVHITTESTINFOFc.cached) cacheLVHITTESTINFOFields(env, lpObject); lpStruct->iSubItem = (*env)->GetIntField(env, lpObject, LVHITTESTINFOFc.iSubItem); lpStruct->iItem = (*env)->GetIntField(env, lpObject, LVHITTESTINFOFc.iItem); lpStruct->flags = (*env)->GetIntField(env, lpObject, LVHITTESTINFOFc.flags); lpStruct->pt.y = (*env)->GetIntField(env, lpObject, LVHITTESTINFOFc.y); lpStruct->pt.x = (*env)->GetIntField(env, lpObject, LVHITTESTINFOFc.x); return lpStruct;}
11867 /local/tlutelli/issta_data/temp/c/2005_temp/2005/11867/87f953c8eda7bc7ceddf6227acc0065974196814/structs.c/buggy/bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/structs.c
if (dragon[m][n].status == DEAD)
if (dragon[m][n].matcher_status == DEAD)
atari_atari_try_combination(int color, int ai, int aj, int bi, int bj){ int other = OTHER_COLOR(color); int aa_val = 0; int m, n; int save_verbose = verbose; if (aa_depth < 2) return 0; if (verbose > 0) verbose--; memset(forbidden, 0, sizeof(forbidden)); for (m = 0; m < board_size; m++) for (n = 0; n < board_size; n++) { if (p[m][n] == other) { if (dragon[m][n].status == DEAD) aa_status[m][n] = DEAD; else if (worm[m][n].attack_code != 0) { if (worm[m][n].defend_code != 0) aa_status[m][n] = CRITICAL; else aa_status[m][n] = DEAD; } else aa_status[m][n] = ALIVE; } else aa_status[m][n] = UNKNOWN; } /* reclassify a worm with 2 liberties as INSUBSTANTIAL if capturing * it does not result in a live group. */ for (m = 0; m < board_size; m++) for (n = 0; n < board_size; n++) if (p[m][n] == other && worm[m][n].origini == m && worm[m][n].originj == n && worm[m][n].liberties == 2 && aa_status[m][n] != DEAD && !owl_substantial(m, n)) { int ti, tj; for (ti = 0; ti < board_size; ti++) for (tj = 0; tj < board_size; tj++) if (is_worm_origin(ti, tj, m, n)) aa_status[ti][tj] = INSUBSTANTIAL; } if (trymove(ai, aj, color, NULL, -1, -1, EMPTY, -1, -1)) { if (trymove(bi, bj, other, NULL, -1, -1, EMPTY, -1, -1)) { aa_val = do_atari_atari(color, NULL, NULL, NULL, NULL, ai, aj, 0, 0); popgo(); } popgo(); } verbose = save_verbose; return aa_val;}
6496 /local/tlutelli/issta_data/temp/c/2005_temp/2005/6496/2a5a6dbb2a697083e7683965c1293f04fe090706/reading.c/clean/engine/reading.c
return 0;
void *fakemessenger_thread(void *ptr) { dout(1) << "thread start, setting timer kicker" << endl; g_timer.set_messenger_kicker(new C_FakeKicker()); lock.Lock(); while (1) { dout(20) << "thread waiting" << endl; if (shutdown) break; awake = false; cond.Wait(lock); awake = true; dout(20) << "thread woke up" << endl; if (shutdown) break; fakemessenger_do_loop_2(); } lock.Unlock(); cout << "unsetting messenger kicker" << endl; g_timer.unset_messenger_kicker(); dout(1) << "thread finish (i woke up but no messages, bye)" << endl;}
2690 /local/tlutelli/issta_data/temp/c/2005_temp/2005/2690/8c7283a709cbd1031e4adc5c129a44d81b7a0f8d/FakeMessenger.cc/clean/ceph/msg/FakeMessenger.cc
QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog(lastUsedDir, filters, 0, QFileDialog::tr("open files dialog"));
QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog(lastUsedDir, filters, 0, QFileDialog::tr("open files dialog"), lastUsedEncoding);
static void openFilesRememberingFilter_(QString const &filterName, QString const &filters, QStringList & selectedFiles, QString& enc, QString &title){ bool haveLastUsedFilter = false; // by default, there is no last // used filter QSettings settings; // where we keep last used filter in // persistant state QString lastUsedFilter = settings.readEntry("/qgis/UI/" + filterName, QString::null, &haveLastUsedFilter); QString lastUsedDir = settings.readEntry("/qgis/UI/" + filterName + "Dir", ".");#ifdef QGISDEBUG std::cerr << "Opening vector file dialog with filters: " << filters << std::endl;#endif QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog(lastUsedDir, filters, 0, QFileDialog::tr("open files dialog")); // allow for selection of more than one file openFileDialog->setMode(QFileDialog::ExistingFiles); openFileDialog->setCaption(title); if (haveLastUsedFilter) // set the filter to the last one used { openFileDialog->setSelectedFilter(lastUsedFilter); } if (openFileDialog->exec() == QDialog::Accepted) { selectedFiles = openFileDialog->selectedFiles(); enc = openFileDialog->encoding(); } settings.writeEntry("/qgis/UI/" + filterName, openFileDialog->selectedFilter()); settings.writeEntry("/qgis/UI/" + filterName + "Dir", openFileDialog->dirPath()); delete openFileDialog;} // openFilesRememberingFilter_
1753 /local/tlutelli/issta_data/temp/c/2005_temp/2005/1753/8198feb36fbf70d3dcefe312b86ebc1d9941e844/qgisapp.cpp/clean/src/qgisapp.cpp
settings.writeEntry("/qgis/UI/encoding", openFileDialog->encoding());
static void openFilesRememberingFilter_(QString const &filterName, QString const &filters, QStringList & selectedFiles, QString& enc, QString &title){ bool haveLastUsedFilter = false; // by default, there is no last // used filter QSettings settings; // where we keep last used filter in // persistant state QString lastUsedFilter = settings.readEntry("/qgis/UI/" + filterName, QString::null, &haveLastUsedFilter); QString lastUsedDir = settings.readEntry("/qgis/UI/" + filterName + "Dir", ".");#ifdef QGISDEBUG std::cerr << "Opening vector file dialog with filters: " << filters << std::endl;#endif QgsEncodingFileDialog* openFileDialog = new QgsEncodingFileDialog(lastUsedDir, filters, 0, QFileDialog::tr("open files dialog")); // allow for selection of more than one file openFileDialog->setMode(QFileDialog::ExistingFiles); openFileDialog->setCaption(title); if (haveLastUsedFilter) // set the filter to the last one used { openFileDialog->setSelectedFilter(lastUsedFilter); } if (openFileDialog->exec() == QDialog::Accepted) { selectedFiles = openFileDialog->selectedFiles(); enc = openFileDialog->encoding(); } settings.writeEntry("/qgis/UI/" + filterName, openFileDialog->selectedFilter()); settings.writeEntry("/qgis/UI/" + filterName + "Dir", openFileDialog->dirPath()); delete openFileDialog;} // openFilesRememberingFilter_
1753 /local/tlutelli/issta_data/temp/c/2005_temp/2005/1753/8198feb36fbf70d3dcefe312b86ebc1d9941e844/qgisapp.cpp/clean/src/qgisapp.cpp
"(c) 1999-2004, The KDE Developers");
I18N_NOOP("(c) 1999-2004, The KDE Developers") );
extern "C" KDE_EXPORT int kdemain(int _argc, char *_argv[]){ KAboutData aboutData( "kcmshell", I18N_NOOP("KDE Control Module"), 0, I18N_NOOP("A tool to start single KDE control modules"), KAboutData::License_GPL, "(c) 1999-2004, The KDE Developers"); aboutData.addAuthor("Frans Englich", "Maintainer", "[email protected]"); aboutData.addAuthor("Daniel Molkentin", 0, "[email protected]"); aboutData.addAuthor("Matthias Hoelzer-Kluepfel",0, "[email protected]"); aboutData.addAuthor("Matthias Elter",0, "[email protected]"); aboutData.addAuthor("Matthias Ettrich",0, "[email protected]"); aboutData.addAuthor("Waldo Bastian",0, "[email protected]"); KCmdLineArgs::init(_argc, _argv, &aboutData); KCmdLineArgs::addCmdLineOptions( options ); // Add our own options. KCMShell app; const KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); const QCString lang = args->getOption("lang"); if( !lang.isNull() ) KGlobal::locale()->setLanguage(lang); if (args->isSet("list")) { cout << i18n("The following modules are available:").local8Bit() << endl; listModules( "Settings/" ); int maxLen=0; for( KService::List::ConstIterator it = m_modules.begin(); it != m_modules.end(); it++) { int len = (*it)->desktopEntryName().length(); if (len > maxLen) maxLen = len; } for( KService::List::ConstIterator it = m_modules.begin(); it != m_modules.end(); it++) { QString entry("%1 - %2"); entry = entry.arg((*it)->desktopEntryName().leftJustify(maxLen, ' ')) .arg(!(*it)->comment().isEmpty() ? (*it)->comment() : i18n("No description available")); cout << entry.local8Bit() << endl; } return 0; } if (args->count() < 1) { args->usage(); return -1; } QCString dcopName; KService::List modules; for (int i = 0; i < args->count(); i++) { KService::Ptr service = locateModule(args->arg(i)); if( service ) { modules.append(service); if( !dcopName.isEmpty() ) dcopName += "_"; dcopName += args->arg(i); } } /* Check if this particular module combination is already running, but * allow the same module to run when embedding(root mode) */ app.setDCOPName(dcopName, ( args->isSet( "embed-proxy" ) || args->isSet( "embed" ))); if( app.isRunning() ) { app.waitForExit(); return 0; } KDialogBase::DialogType dtype = KDialogBase::Plain; if ( modules.count() < 1 ) return 0; else if( modules.count() > 1 ) dtype = KDialogBase::IconList; bool idValid; int id; if ( args->isSet( "embed-proxy" )) { id = args->getOption( "embed-proxy" ).toInt(&idValid); if( idValid ) { KCModuleProxy *module = new KCModuleProxy( modules.first()->desktopEntryName() ); module->realModule(); QXEmbed::embedClientIntoWindow( module, id); app.exec(); delete module; } else kdDebug(780) << "Supplied id '" << id << "' is not valid." << endl; return 0; } KCMShellMultiDialog *dlg = new KCMShellMultiDialog( dtype, i18n("Configure - %1").arg(kapp->caption()), 0, "", true ); for (KService::List::ConstIterator it = modules.begin(); it != modules.end(); ++it) dlg->addModule(KCModuleInfo(*it)); if ( args->isSet( "embed" )) { id = args->getOption( "embed" ).toInt(&idValid); if( idValid ) { QXEmbed::embedClientIntoWindow( dlg, id ); dlg->exec(); delete dlg; } else kdDebug(780) << "Supplied id '" << id << "' is not valid." << endl; } else { if (kapp->iconName() != kapp->name()) setIcon(dlg, kapp->iconName()); else if ( modules.count() == 1 ) setIcon(dlg, KCModuleInfo( modules.first()).icon()); dlg->exec(); delete dlg; } return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/95c2844814fb57d55cc784b324e60579f65c026d/main.cpp/buggy/kcmshell/main.cpp
aboutData.addAuthor("Frans Englich", "Maintainer", "[email protected]");
aboutData.addAuthor("Frans Englich", I18N_NOOP("Maintainer"), "[email protected]");
extern "C" KDE_EXPORT int kdemain(int _argc, char *_argv[]){ KAboutData aboutData( "kcmshell", I18N_NOOP("KDE Control Module"), 0, I18N_NOOP("A tool to start single KDE control modules"), KAboutData::License_GPL, "(c) 1999-2004, The KDE Developers"); aboutData.addAuthor("Frans Englich", "Maintainer", "[email protected]"); aboutData.addAuthor("Daniel Molkentin", 0, "[email protected]"); aboutData.addAuthor("Matthias Hoelzer-Kluepfel",0, "[email protected]"); aboutData.addAuthor("Matthias Elter",0, "[email protected]"); aboutData.addAuthor("Matthias Ettrich",0, "[email protected]"); aboutData.addAuthor("Waldo Bastian",0, "[email protected]"); KCmdLineArgs::init(_argc, _argv, &aboutData); KCmdLineArgs::addCmdLineOptions( options ); // Add our own options. KCMShell app; const KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); const QCString lang = args->getOption("lang"); if( !lang.isNull() ) KGlobal::locale()->setLanguage(lang); if (args->isSet("list")) { cout << i18n("The following modules are available:").local8Bit() << endl; listModules( "Settings/" ); int maxLen=0; for( KService::List::ConstIterator it = m_modules.begin(); it != m_modules.end(); it++) { int len = (*it)->desktopEntryName().length(); if (len > maxLen) maxLen = len; } for( KService::List::ConstIterator it = m_modules.begin(); it != m_modules.end(); it++) { QString entry("%1 - %2"); entry = entry.arg((*it)->desktopEntryName().leftJustify(maxLen, ' ')) .arg(!(*it)->comment().isEmpty() ? (*it)->comment() : i18n("No description available")); cout << entry.local8Bit() << endl; } return 0; } if (args->count() < 1) { args->usage(); return -1; } QCString dcopName; KService::List modules; for (int i = 0; i < args->count(); i++) { KService::Ptr service = locateModule(args->arg(i)); if( service ) { modules.append(service); if( !dcopName.isEmpty() ) dcopName += "_"; dcopName += args->arg(i); } } /* Check if this particular module combination is already running, but * allow the same module to run when embedding(root mode) */ app.setDCOPName(dcopName, ( args->isSet( "embed-proxy" ) || args->isSet( "embed" ))); if( app.isRunning() ) { app.waitForExit(); return 0; } KDialogBase::DialogType dtype = KDialogBase::Plain; if ( modules.count() < 1 ) return 0; else if( modules.count() > 1 ) dtype = KDialogBase::IconList; bool idValid; int id; if ( args->isSet( "embed-proxy" )) { id = args->getOption( "embed-proxy" ).toInt(&idValid); if( idValid ) { KCModuleProxy *module = new KCModuleProxy( modules.first()->desktopEntryName() ); module->realModule(); QXEmbed::embedClientIntoWindow( module, id); app.exec(); delete module; } else kdDebug(780) << "Supplied id '" << id << "' is not valid." << endl; return 0; } KCMShellMultiDialog *dlg = new KCMShellMultiDialog( dtype, i18n("Configure - %1").arg(kapp->caption()), 0, "", true ); for (KService::List::ConstIterator it = modules.begin(); it != modules.end(); ++it) dlg->addModule(KCModuleInfo(*it)); if ( args->isSet( "embed" )) { id = args->getOption( "embed" ).toInt(&idValid); if( idValid ) { QXEmbed::embedClientIntoWindow( dlg, id ); dlg->exec(); delete dlg; } else kdDebug(780) << "Supplied id '" << id << "' is not valid." << endl; } else { if (kapp->iconName() != kapp->name()) setIcon(dlg, kapp->iconName()); else if ( modules.count() == 1 ) setIcon(dlg, KCModuleInfo( modules.first()).icon()); dlg->exec(); delete dlg; } return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/95c2844814fb57d55cc784b324e60579f65c026d/main.cpp/buggy/kcmshell/main.cpp
KGlobal::locale()->setMainCatalogue("kcmshell");
extern "C" KDE_EXPORT int kdemain(int _argc, char *_argv[]){ KAboutData aboutData( "kcmshell", I18N_NOOP("KDE Control Module"), 0, I18N_NOOP("A tool to start single KDE control modules"), KAboutData::License_GPL, "(c) 1999-2004, The KDE Developers"); aboutData.addAuthor("Frans Englich", "Maintainer", "[email protected]"); aboutData.addAuthor("Daniel Molkentin", 0, "[email protected]"); aboutData.addAuthor("Matthias Hoelzer-Kluepfel",0, "[email protected]"); aboutData.addAuthor("Matthias Elter",0, "[email protected]"); aboutData.addAuthor("Matthias Ettrich",0, "[email protected]"); aboutData.addAuthor("Waldo Bastian",0, "[email protected]"); KCmdLineArgs::init(_argc, _argv, &aboutData); KCmdLineArgs::addCmdLineOptions( options ); // Add our own options. KCMShell app; const KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); const QCString lang = args->getOption("lang"); if( !lang.isNull() ) KGlobal::locale()->setLanguage(lang); if (args->isSet("list")) { cout << i18n("The following modules are available:").local8Bit() << endl; listModules( "Settings/" ); int maxLen=0; for( KService::List::ConstIterator it = m_modules.begin(); it != m_modules.end(); it++) { int len = (*it)->desktopEntryName().length(); if (len > maxLen) maxLen = len; } for( KService::List::ConstIterator it = m_modules.begin(); it != m_modules.end(); it++) { QString entry("%1 - %2"); entry = entry.arg((*it)->desktopEntryName().leftJustify(maxLen, ' ')) .arg(!(*it)->comment().isEmpty() ? (*it)->comment() : i18n("No description available")); cout << entry.local8Bit() << endl; } return 0; } if (args->count() < 1) { args->usage(); return -1; } QCString dcopName; KService::List modules; for (int i = 0; i < args->count(); i++) { KService::Ptr service = locateModule(args->arg(i)); if( service ) { modules.append(service); if( !dcopName.isEmpty() ) dcopName += "_"; dcopName += args->arg(i); } } /* Check if this particular module combination is already running, but * allow the same module to run when embedding(root mode) */ app.setDCOPName(dcopName, ( args->isSet( "embed-proxy" ) || args->isSet( "embed" ))); if( app.isRunning() ) { app.waitForExit(); return 0; } KDialogBase::DialogType dtype = KDialogBase::Plain; if ( modules.count() < 1 ) return 0; else if( modules.count() > 1 ) dtype = KDialogBase::IconList; bool idValid; int id; if ( args->isSet( "embed-proxy" )) { id = args->getOption( "embed-proxy" ).toInt(&idValid); if( idValid ) { KCModuleProxy *module = new KCModuleProxy( modules.first()->desktopEntryName() ); module->realModule(); QXEmbed::embedClientIntoWindow( module, id); app.exec(); delete module; } else kdDebug(780) << "Supplied id '" << id << "' is not valid." << endl; return 0; } KCMShellMultiDialog *dlg = new KCMShellMultiDialog( dtype, i18n("Configure - %1").arg(kapp->caption()), 0, "", true ); for (KService::List::ConstIterator it = modules.begin(); it != modules.end(); ++it) dlg->addModule(KCModuleInfo(*it)); if ( args->isSet( "embed" )) { id = args->getOption( "embed" ).toInt(&idValid); if( idValid ) { QXEmbed::embedClientIntoWindow( dlg, id ); dlg->exec(); delete dlg; } else kdDebug(780) << "Supplied id '" << id << "' is not valid." << endl; } else { if (kapp->iconName() != kapp->name()) setIcon(dlg, kapp->iconName()); else if ( modules.count() == 1 ) setIcon(dlg, KCModuleInfo( modules.first()).icon()); dlg->exec(); delete dlg; } return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/95c2844814fb57d55cc784b324e60579f65c026d/main.cpp/buggy/kcmshell/main.cpp
#ifdef VGO_netbsdelf2
#if defined(VGO_netbsdelf2)
void foreach_map(int (*fn)(char *start, char *end, const char *perm, off_t offset, int maj, int min, int ino, void* extra), void* extra){ static char buf[10240]; char *bufptr = buf; int ret, fd;#ifdef VGO_netbsdelf2 fd = open("/proc/curproc/maps", O_RDONLY);#else fd = open("/proc/self/maps", O_RDONLY);#endif if (fd == -1) { perror("open /proc/self/maps"); return; } ret = read(fd, buf, sizeof(buf)); if (ret == -1) { perror("read /proc/self/maps"); close(fd); return; } close(fd); if (ret == sizeof(buf)) { fprintf(stderr, "buf too small\n"); return; } while(bufptr && bufptr < buf+ret) { char perm[5]; ULong offset; int maj, min; int ino; void *segstart, *segend; sscanf(bufptr, "%p-%p %s %llx %x:%x %d", &segstart, &segend, perm, &offset, &maj, &min, &ino); bufptr = strchr(bufptr, '\n'); if (bufptr != NULL) bufptr++; /* skip \n */ if (!(*fn)(segstart, segend, perm, offset, maj, min, ino, extra)) break; }}
12192 /local/tlutelli/issta_data/temp/c/2005_temp/2005/12192/3a7f4596c341b0c70016cdf7f27373537dbacc28/ume.c/clean/trunk/valgrind/coregrind/ume.c
gopher_close();
gopher_close();
GopherProtocol::~GopherProtocol(){ gopher_close();}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/39a40b82a2fc55ed7e977945ed4c332480807109/gopher.cc/buggy/kioslave/gopher/gopher.cc
Char* VG_(build_child_exename)( void )
Char* VG_(build_child_VALGRINDCLO)( Char* exename )
Char* VG_(build_child_exename)( void ){ Char* exename = VG_(arena_malloc)(VG_AR_CORE, 64); vg_assert(NULL != exename);#ifdef VGO_netbsdelf2 VG_(sprintf)(exename, "/proc/curproc/fd/%d", vgexecfd);#else VG_(sprintf)(exename, "/proc/self/fd/%d", vgexecfd);#endif return exename;}
12192 /local/tlutelli/issta_data/temp/c/2005_temp/2005/12192/18af383b003f8ef1188c475ac4d3f992ca2c89b4/m_main.c/buggy/branches/aspacem/valgrind/coregrind/m_main.c
Char* exename = VG_(arena_malloc)(VG_AR_CORE, 64); vg_assert(NULL != exename); #ifdef VGO_netbsdelf2 VG_(sprintf)(exename, "/proc/curproc/fd/%d", vgexecfd); #else VG_(sprintf)(exename, "/proc/self/fd/%d", vgexecfd); #endif return exename;
/* If we're tracing the children, then we need to start it with our starter+arguments, which are copied into VALGRINDCLO, except the --exec= option is changed if present. */ Int i; Char *exec; Char *cp; Char *optvar; Int optlen, execlen; exec = VG_(arena_malloc)(VG_AR_CORE, VG_(strlen)( exename ) + 7/*--exec=*/ + 1/*\0*/); vg_assert(NULL != exec); VG_(sprintf)(exec, "--exec=%s", exename); optlen = 1; for (i = 0; i < vg_argc; i++) optlen += VG_(strlen)(vg_argv[i]) + 1; optlen += VG_(strlen)(exec)+1; optvar = VG_(arena_malloc)(VG_AR_CORE, optlen); cp = optvar; for (i = 1; i < vg_argc; i++) { Char *arg = vg_argv[i]; if (VG_CLO_STREQN(7, arg, "--exec=")) { } else if (VG_CLO_STREQ(arg, "--")) { break; } else { Int len = VG_(strlen)(arg); VG_(memcpy)(cp, arg, len); cp += len; *cp++ = VG_CLO_SEP; } } execlen = VG_(strlen)(exec); VG_(memcpy)(cp, exec, execlen); cp += execlen; *cp++ = VG_CLO_SEP; *cp = '\0'; return optvar;
Char* VG_(build_child_exename)( void ){ Char* exename = VG_(arena_malloc)(VG_AR_CORE, 64); vg_assert(NULL != exename);#ifdef VGO_netbsdelf2 VG_(sprintf)(exename, "/proc/curproc/fd/%d", vgexecfd);#else VG_(sprintf)(exename, "/proc/self/fd/%d", vgexecfd);#endif return exename;}
12192 /local/tlutelli/issta_data/temp/c/2005_temp/2005/12192/18af383b003f8ef1188c475ac4d3f992ca2c89b4/m_main.c/buggy/branches/aspacem/valgrind/coregrind/m_main.c
{ QMessageBox::warning(0,"OGR Driver Manger","unable to get OGRDriverManager"); return; }
{ QMessageBox::warning(0,"OGR Driver Manger","unable to get OGRDriverManager"); return; }
static void buildSupportedVectorFileFilter_(QString & fileFilters){ // first get the GDAL driver manager OGRSFDriverRegistrar *driverRegistrar = OGRSFDriverRegistrar::GetRegistrar(); if (!driverRegistrar) { QMessageBox::warning(0,"OGR Driver Manger","unable to get OGRDriverManager"); return; // XXX good place to throw exception if we } // XXX decide to do exceptions // then iterate through all of the supported drivers, adding the // corresponding file filter OGRSFDriver *driver; // current driver QString driverName; // current driver name // Grind through all the drivers and their respective metadata. // We'll add a file filter for those drivers that have a file // extension defined for them; the others, welll, even though // theoreticaly we can open those files because there exists a // driver for them, the user will have to use the "All Files" to // open datasets with no explicitly defined file name extension.#ifdef QGISDEBUG std::cerr << "Driver count: " << driverRegistrar->GetDriverCount() << std::endl; #endif for (int i = 0; i < driverRegistrar->GetDriverCount(); ++i) { driver = driverRegistrar->GetDriver(i); Q_CHECK_PTR(driver); if (!driver) { qWarning("unable to get driver %d", i); continue; } driverName = driver->GetName();#ifdef QGISDEBUG qDebug("got driver string %s", driver->GetName()); std::cerr << "Filter string at start of build filter is: " << fileFilters << std::endl; #endif if (driverName.startsWith("ESRI")) { fileFilters += createFileFilter_("ESRI Shapefiles", "*.shp"); } else if (driverName.startsWith("UK")) { // XXX needs file filter extension } else if (driverName.startsWith("SDTS")) { fileFilters += createFileFilter_( "Spatial Data Transfer Standard", "*catd.ddf" ); } else if (driverName.startsWith("TIGER")) { // XXX needs file filter extension } else if (driverName.startsWith("S57")) { // XXX needs file filter extension } else if (driverName.startsWith("MapInfo")) { fileFilters += createFileFilter_("MapInfo", "*.mif *.mid *.tab"); // XXX needs file filter extension } else if (driverName.startsWith("DGN")) { // XXX needs file filter extension } else if (driverName.startsWith("VRT")) { // XXX needs file filter extension } else if (driverName.startsWith("AVCBin")) { // XXX needs file filter extension } else if (driverName.startsWith("REC")) { // XXX needs file filter extension } else if (driverName.startsWith("Memory")) { // XXX needs file filter extension } else if (driverName.startsWith("Jis")) { // XXX needs file filter extension } else if (driverName.startsWith("GML")) {// XXX not yet supported; post 0.1 release task// fileFilters += createFileFilter_( "Geography Markup Language", // "*.gml" ); } else { // NOP, we don't know anything about the current driver // with regards to a proper file filter string } } // each loaded GDAL driver // can't forget the default case fileFilters += "All files (*.*)";} // buildSupportedVectorFileFilter_()
1753 /local/tlutelli/issta_data/temp/c/2005_temp/2005/1753/010d0b31402c95e0212fe5b11da9040bb7e8bb88/qgisapp.cpp/clean/src/qgisapp.cpp
driver = driverRegistrar->GetDriver(i);
qWarning("unable to get driver %d", i); continue; }
static void buildSupportedVectorFileFilter_(QString & fileFilters){ // first get the GDAL driver manager OGRSFDriverRegistrar *driverRegistrar = OGRSFDriverRegistrar::GetRegistrar(); if (!driverRegistrar) { QMessageBox::warning(0,"OGR Driver Manger","unable to get OGRDriverManager"); return; // XXX good place to throw exception if we } // XXX decide to do exceptions // then iterate through all of the supported drivers, adding the // corresponding file filter OGRSFDriver *driver; // current driver QString driverName; // current driver name // Grind through all the drivers and their respective metadata. // We'll add a file filter for those drivers that have a file // extension defined for them; the others, welll, even though // theoreticaly we can open those files because there exists a // driver for them, the user will have to use the "All Files" to // open datasets with no explicitly defined file name extension.#ifdef QGISDEBUG std::cerr << "Driver count: " << driverRegistrar->GetDriverCount() << std::endl; #endif for (int i = 0; i < driverRegistrar->GetDriverCount(); ++i) { driver = driverRegistrar->GetDriver(i); Q_CHECK_PTR(driver); if (!driver) { qWarning("unable to get driver %d", i); continue; } driverName = driver->GetName();#ifdef QGISDEBUG qDebug("got driver string %s", driver->GetName()); std::cerr << "Filter string at start of build filter is: " << fileFilters << std::endl; #endif if (driverName.startsWith("ESRI")) { fileFilters += createFileFilter_("ESRI Shapefiles", "*.shp"); } else if (driverName.startsWith("UK")) { // XXX needs file filter extension } else if (driverName.startsWith("SDTS")) { fileFilters += createFileFilter_( "Spatial Data Transfer Standard", "*catd.ddf" ); } else if (driverName.startsWith("TIGER")) { // XXX needs file filter extension } else if (driverName.startsWith("S57")) { // XXX needs file filter extension } else if (driverName.startsWith("MapInfo")) { fileFilters += createFileFilter_("MapInfo", "*.mif *.mid *.tab"); // XXX needs file filter extension } else if (driverName.startsWith("DGN")) { // XXX needs file filter extension } else if (driverName.startsWith("VRT")) { // XXX needs file filter extension } else if (driverName.startsWith("AVCBin")) { // XXX needs file filter extension } else if (driverName.startsWith("REC")) { // XXX needs file filter extension } else if (driverName.startsWith("Memory")) { // XXX needs file filter extension } else if (driverName.startsWith("Jis")) { // XXX needs file filter extension } else if (driverName.startsWith("GML")) {// XXX not yet supported; post 0.1 release task// fileFilters += createFileFilter_( "Geography Markup Language", // "*.gml" ); } else { // NOP, we don't know anything about the current driver // with regards to a proper file filter string } } // each loaded GDAL driver // can't forget the default case fileFilters += "All files (*.*)";} // buildSupportedVectorFileFilter_()
1753 /local/tlutelli/issta_data/temp/c/2005_temp/2005/1753/010d0b31402c95e0212fe5b11da9040bb7e8bb88/qgisapp.cpp/clean/src/qgisapp.cpp
Q_CHECK_PTR(driver);
driverName = driver->GetName();
static void buildSupportedVectorFileFilter_(QString & fileFilters){ // first get the GDAL driver manager OGRSFDriverRegistrar *driverRegistrar = OGRSFDriverRegistrar::GetRegistrar(); if (!driverRegistrar) { QMessageBox::warning(0,"OGR Driver Manger","unable to get OGRDriverManager"); return; // XXX good place to throw exception if we } // XXX decide to do exceptions // then iterate through all of the supported drivers, adding the // corresponding file filter OGRSFDriver *driver; // current driver QString driverName; // current driver name // Grind through all the drivers and their respective metadata. // We'll add a file filter for those drivers that have a file // extension defined for them; the others, welll, even though // theoreticaly we can open those files because there exists a // driver for them, the user will have to use the "All Files" to // open datasets with no explicitly defined file name extension.#ifdef QGISDEBUG std::cerr << "Driver count: " << driverRegistrar->GetDriverCount() << std::endl; #endif for (int i = 0; i < driverRegistrar->GetDriverCount(); ++i) { driver = driverRegistrar->GetDriver(i); Q_CHECK_PTR(driver); if (!driver) { qWarning("unable to get driver %d", i); continue; } driverName = driver->GetName();#ifdef QGISDEBUG qDebug("got driver string %s", driver->GetName()); std::cerr << "Filter string at start of build filter is: " << fileFilters << std::endl; #endif if (driverName.startsWith("ESRI")) { fileFilters += createFileFilter_("ESRI Shapefiles", "*.shp"); } else if (driverName.startsWith("UK")) { // XXX needs file filter extension } else if (driverName.startsWith("SDTS")) { fileFilters += createFileFilter_( "Spatial Data Transfer Standard", "*catd.ddf" ); } else if (driverName.startsWith("TIGER")) { // XXX needs file filter extension } else if (driverName.startsWith("S57")) { // XXX needs file filter extension } else if (driverName.startsWith("MapInfo")) { fileFilters += createFileFilter_("MapInfo", "*.mif *.mid *.tab"); // XXX needs file filter extension } else if (driverName.startsWith("DGN")) { // XXX needs file filter extension } else if (driverName.startsWith("VRT")) { // XXX needs file filter extension } else if (driverName.startsWith("AVCBin")) { // XXX needs file filter extension } else if (driverName.startsWith("REC")) { // XXX needs file filter extension } else if (driverName.startsWith("Memory")) { // XXX needs file filter extension } else if (driverName.startsWith("Jis")) { // XXX needs file filter extension } else if (driverName.startsWith("GML")) {// XXX not yet supported; post 0.1 release task// fileFilters += createFileFilter_( "Geography Markup Language", // "*.gml" ); } else { // NOP, we don't know anything about the current driver // with regards to a proper file filter string } } // each loaded GDAL driver // can't forget the default case fileFilters += "All files (*.*)";} // buildSupportedVectorFileFilter_()
1753 /local/tlutelli/issta_data/temp/c/2005_temp/2005/1753/010d0b31402c95e0212fe5b11da9040bb7e8bb88/qgisapp.cpp/clean/src/qgisapp.cpp
if (!driver) { qWarning("unable to get driver %d", i); continue; }
static void buildSupportedVectorFileFilter_(QString & fileFilters){ // first get the GDAL driver manager OGRSFDriverRegistrar *driverRegistrar = OGRSFDriverRegistrar::GetRegistrar(); if (!driverRegistrar) { QMessageBox::warning(0,"OGR Driver Manger","unable to get OGRDriverManager"); return; // XXX good place to throw exception if we } // XXX decide to do exceptions // then iterate through all of the supported drivers, adding the // corresponding file filter OGRSFDriver *driver; // current driver QString driverName; // current driver name // Grind through all the drivers and their respective metadata. // We'll add a file filter for those drivers that have a file // extension defined for them; the others, welll, even though // theoreticaly we can open those files because there exists a // driver for them, the user will have to use the "All Files" to // open datasets with no explicitly defined file name extension.#ifdef QGISDEBUG std::cerr << "Driver count: " << driverRegistrar->GetDriverCount() << std::endl; #endif for (int i = 0; i < driverRegistrar->GetDriverCount(); ++i) { driver = driverRegistrar->GetDriver(i); Q_CHECK_PTR(driver); if (!driver) { qWarning("unable to get driver %d", i); continue; } driverName = driver->GetName();#ifdef QGISDEBUG qDebug("got driver string %s", driver->GetName()); std::cerr << "Filter string at start of build filter is: " << fileFilters << std::endl; #endif if (driverName.startsWith("ESRI")) { fileFilters += createFileFilter_("ESRI Shapefiles", "*.shp"); } else if (driverName.startsWith("UK")) { // XXX needs file filter extension } else if (driverName.startsWith("SDTS")) { fileFilters += createFileFilter_( "Spatial Data Transfer Standard", "*catd.ddf" ); } else if (driverName.startsWith("TIGER")) { // XXX needs file filter extension } else if (driverName.startsWith("S57")) { // XXX needs file filter extension } else if (driverName.startsWith("MapInfo")) { fileFilters += createFileFilter_("MapInfo", "*.mif *.mid *.tab"); // XXX needs file filter extension } else if (driverName.startsWith("DGN")) { // XXX needs file filter extension } else if (driverName.startsWith("VRT")) { // XXX needs file filter extension } else if (driverName.startsWith("AVCBin")) { // XXX needs file filter extension } else if (driverName.startsWith("REC")) { // XXX needs file filter extension } else if (driverName.startsWith("Memory")) { // XXX needs file filter extension } else if (driverName.startsWith("Jis")) { // XXX needs file filter extension } else if (driverName.startsWith("GML")) {// XXX not yet supported; post 0.1 release task// fileFilters += createFileFilter_( "Geography Markup Language", // "*.gml" ); } else { // NOP, we don't know anything about the current driver // with regards to a proper file filter string } } // each loaded GDAL driver // can't forget the default case fileFilters += "All files (*.*)";} // buildSupportedVectorFileFilter_()
1753 /local/tlutelli/issta_data/temp/c/2005_temp/2005/1753/010d0b31402c95e0212fe5b11da9040bb7e8bb88/qgisapp.cpp/clean/src/qgisapp.cpp
driverName = driver->GetName();
if (driverName.startsWith("ESRI")) { myFileFilters += createFileFilter_("ESRI Shapefiles", "*.shp"); } else if (driverName.startsWith("UK")) { } else if (driverName.startsWith("SDTS")) { myFileFilters += createFileFilter_( "Spatial Data Transfer Standard", "*catd.ddf" ); } else if (driverName.startsWith("TIGER")) { } else if (driverName.startsWith("S57")) { } else if (driverName.startsWith("MapInfo")) { myFileFilters += createFileFilter_("MapInfo", "*.mif *.mid *.tab"); } else if (driverName.startsWith("DGN")) { } else if (driverName.startsWith("VRT")) { } else if (driverName.startsWith("AVCBin")) { } else if (driverName.startsWith("REC")) { } else if (driverName.startsWith("Memory")) { } else if (driverName.startsWith("Jis")) { } else if (driverName.startsWith("GML")) { } else { }
static void buildSupportedVectorFileFilter_(QString & fileFilters){ // first get the GDAL driver manager OGRSFDriverRegistrar *driverRegistrar = OGRSFDriverRegistrar::GetRegistrar(); if (!driverRegistrar) { QMessageBox::warning(0,"OGR Driver Manger","unable to get OGRDriverManager"); return; // XXX good place to throw exception if we } // XXX decide to do exceptions // then iterate through all of the supported drivers, adding the // corresponding file filter OGRSFDriver *driver; // current driver QString driverName; // current driver name // Grind through all the drivers and their respective metadata. // We'll add a file filter for those drivers that have a file // extension defined for them; the others, welll, even though // theoreticaly we can open those files because there exists a // driver for them, the user will have to use the "All Files" to // open datasets with no explicitly defined file name extension.#ifdef QGISDEBUG std::cerr << "Driver count: " << driverRegistrar->GetDriverCount() << std::endl; #endif for (int i = 0; i < driverRegistrar->GetDriverCount(); ++i) { driver = driverRegistrar->GetDriver(i); Q_CHECK_PTR(driver); if (!driver) { qWarning("unable to get driver %d", i); continue; } driverName = driver->GetName();#ifdef QGISDEBUG qDebug("got driver string %s", driver->GetName()); std::cerr << "Filter string at start of build filter is: " << fileFilters << std::endl; #endif if (driverName.startsWith("ESRI")) { fileFilters += createFileFilter_("ESRI Shapefiles", "*.shp"); } else if (driverName.startsWith("UK")) { // XXX needs file filter extension } else if (driverName.startsWith("SDTS")) { fileFilters += createFileFilter_( "Spatial Data Transfer Standard", "*catd.ddf" ); } else if (driverName.startsWith("TIGER")) { // XXX needs file filter extension } else if (driverName.startsWith("S57")) { // XXX needs file filter extension } else if (driverName.startsWith("MapInfo")) { fileFilters += createFileFilter_("MapInfo", "*.mif *.mid *.tab"); // XXX needs file filter extension } else if (driverName.startsWith("DGN")) { // XXX needs file filter extension } else if (driverName.startsWith("VRT")) { // XXX needs file filter extension } else if (driverName.startsWith("AVCBin")) { // XXX needs file filter extension } else if (driverName.startsWith("REC")) { // XXX needs file filter extension } else if (driverName.startsWith("Memory")) { // XXX needs file filter extension } else if (driverName.startsWith("Jis")) { // XXX needs file filter extension } else if (driverName.startsWith("GML")) {// XXX not yet supported; post 0.1 release task// fileFilters += createFileFilter_( "Geography Markup Language", // "*.gml" ); } else { // NOP, we don't know anything about the current driver // with regards to a proper file filter string } } // each loaded GDAL driver // can't forget the default case fileFilters += "All files (*.*)";} // buildSupportedVectorFileFilter_()
1753 /local/tlutelli/issta_data/temp/c/2005_temp/2005/1753/010d0b31402c95e0212fe5b11da9040bb7e8bb88/qgisapp.cpp/clean/src/qgisapp.cpp
#ifdef QGISDEBUG qDebug("got driver string %s", driver->GetName()); std::cerr << "Filter string at start of build filter is: " << fileFilters << std::endl; #endif if (driverName.startsWith("ESRI")) { fileFilters += createFileFilter_("ESRI Shapefiles", "*.shp"); } else if (driverName.startsWith("UK")) { } else if (driverName.startsWith("SDTS")) { fileFilters += createFileFilter_( "Spatial Data Transfer Standard", "*catd.ddf" ); } else if (driverName.startsWith("TIGER")) { } else if (driverName.startsWith("S57")) { } else if (driverName.startsWith("MapInfo")) { fileFilters += createFileFilter_("MapInfo", "*.mif *.mid *.tab"); } else if (driverName.startsWith("DGN")) { } else if (driverName.startsWith("VRT")) { } else if (driverName.startsWith("AVCBin")) { } else if (driverName.startsWith("REC")) { } else if (driverName.startsWith("Memory")) { } else if (driverName.startsWith("Jis")) { } else if (driverName.startsWith("GML")) { } else { } }
std::cout << myFileFilters << std::endl; }
static void buildSupportedVectorFileFilter_(QString & fileFilters){ // first get the GDAL driver manager OGRSFDriverRegistrar *driverRegistrar = OGRSFDriverRegistrar::GetRegistrar(); if (!driverRegistrar) { QMessageBox::warning(0,"OGR Driver Manger","unable to get OGRDriverManager"); return; // XXX good place to throw exception if we } // XXX decide to do exceptions // then iterate through all of the supported drivers, adding the // corresponding file filter OGRSFDriver *driver; // current driver QString driverName; // current driver name // Grind through all the drivers and their respective metadata. // We'll add a file filter for those drivers that have a file // extension defined for them; the others, welll, even though // theoreticaly we can open those files because there exists a // driver for them, the user will have to use the "All Files" to // open datasets with no explicitly defined file name extension.#ifdef QGISDEBUG std::cerr << "Driver count: " << driverRegistrar->GetDriverCount() << std::endl; #endif for (int i = 0; i < driverRegistrar->GetDriverCount(); ++i) { driver = driverRegistrar->GetDriver(i); Q_CHECK_PTR(driver); if (!driver) { qWarning("unable to get driver %d", i); continue; } driverName = driver->GetName();#ifdef QGISDEBUG qDebug("got driver string %s", driver->GetName()); std::cerr << "Filter string at start of build filter is: " << fileFilters << std::endl; #endif if (driverName.startsWith("ESRI")) { fileFilters += createFileFilter_("ESRI Shapefiles", "*.shp"); } else if (driverName.startsWith("UK")) { // XXX needs file filter extension } else if (driverName.startsWith("SDTS")) { fileFilters += createFileFilter_( "Spatial Data Transfer Standard", "*catd.ddf" ); } else if (driverName.startsWith("TIGER")) { // XXX needs file filter extension } else if (driverName.startsWith("S57")) { // XXX needs file filter extension } else if (driverName.startsWith("MapInfo")) { fileFilters += createFileFilter_("MapInfo", "*.mif *.mid *.tab"); // XXX needs file filter extension } else if (driverName.startsWith("DGN")) { // XXX needs file filter extension } else if (driverName.startsWith("VRT")) { // XXX needs file filter extension } else if (driverName.startsWith("AVCBin")) { // XXX needs file filter extension } else if (driverName.startsWith("REC")) { // XXX needs file filter extension } else if (driverName.startsWith("Memory")) { // XXX needs file filter extension } else if (driverName.startsWith("Jis")) { // XXX needs file filter extension } else if (driverName.startsWith("GML")) {// XXX not yet supported; post 0.1 release task// fileFilters += createFileFilter_( "Geography Markup Language", // "*.gml" ); } else { // NOP, we don't know anything about the current driver // with regards to a proper file filter string } } // each loaded GDAL driver // can't forget the default case fileFilters += "All files (*.*)";} // buildSupportedVectorFileFilter_()
1753 /local/tlutelli/issta_data/temp/c/2005_temp/2005/1753/010d0b31402c95e0212fe5b11da9040bb7e8bb88/qgisapp.cpp/clean/src/qgisapp.cpp
fileFilters += "All files (*.*)";
myFileFilters += "All files (*.*)"; fileFilters = myFileFilters;
static void buildSupportedVectorFileFilter_(QString & fileFilters){ // first get the GDAL driver manager OGRSFDriverRegistrar *driverRegistrar = OGRSFDriverRegistrar::GetRegistrar(); if (!driverRegistrar) { QMessageBox::warning(0,"OGR Driver Manger","unable to get OGRDriverManager"); return; // XXX good place to throw exception if we } // XXX decide to do exceptions // then iterate through all of the supported drivers, adding the // corresponding file filter OGRSFDriver *driver; // current driver QString driverName; // current driver name // Grind through all the drivers and their respective metadata. // We'll add a file filter for those drivers that have a file // extension defined for them; the others, welll, even though // theoreticaly we can open those files because there exists a // driver for them, the user will have to use the "All Files" to // open datasets with no explicitly defined file name extension.#ifdef QGISDEBUG std::cerr << "Driver count: " << driverRegistrar->GetDriverCount() << std::endl; #endif for (int i = 0; i < driverRegistrar->GetDriverCount(); ++i) { driver = driverRegistrar->GetDriver(i); Q_CHECK_PTR(driver); if (!driver) { qWarning("unable to get driver %d", i); continue; } driverName = driver->GetName();#ifdef QGISDEBUG qDebug("got driver string %s", driver->GetName()); std::cerr << "Filter string at start of build filter is: " << fileFilters << std::endl; #endif if (driverName.startsWith("ESRI")) { fileFilters += createFileFilter_("ESRI Shapefiles", "*.shp"); } else if (driverName.startsWith("UK")) { // XXX needs file filter extension } else if (driverName.startsWith("SDTS")) { fileFilters += createFileFilter_( "Spatial Data Transfer Standard", "*catd.ddf" ); } else if (driverName.startsWith("TIGER")) { // XXX needs file filter extension } else if (driverName.startsWith("S57")) { // XXX needs file filter extension } else if (driverName.startsWith("MapInfo")) { fileFilters += createFileFilter_("MapInfo", "*.mif *.mid *.tab"); // XXX needs file filter extension } else if (driverName.startsWith("DGN")) { // XXX needs file filter extension } else if (driverName.startsWith("VRT")) { // XXX needs file filter extension } else if (driverName.startsWith("AVCBin")) { // XXX needs file filter extension } else if (driverName.startsWith("REC")) { // XXX needs file filter extension } else if (driverName.startsWith("Memory")) { // XXX needs file filter extension } else if (driverName.startsWith("Jis")) { // XXX needs file filter extension } else if (driverName.startsWith("GML")) {// XXX not yet supported; post 0.1 release task// fileFilters += createFileFilter_( "Geography Markup Language", // "*.gml" ); } else { // NOP, we don't know anything about the current driver // with regards to a proper file filter string } } // each loaded GDAL driver // can't forget the default case fileFilters += "All files (*.*)";} // buildSupportedVectorFileFilter_()
1753 /local/tlutelli/issta_data/temp/c/2005_temp/2005/1753/010d0b31402c95e0212fe5b11da9040bb7e8bb88/qgisapp.cpp/clean/src/qgisapp.cpp
qDebug( "kio_ftp : Starting");
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Done" );
kdebug( KDEBUG_INFO, 0, "Done");
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Making copy to %s", _dest );
kdebug( KDEBUG_INFO, 0, "Making copy to %s", _dest );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Checking %s", (*soit).ascii() );
kdebug( KDEBUG_INFO, 0, "Checking %s", (*soit).ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : All URLs ok %s", _dest );
kdebug( KDEBUG_INFO, 0, "All URLs ok %s", _dest );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Dest ok %s", dest.ascii() );
kdebug( KDEBUG_INFO, 0, "Dest ok %s", dest.ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : IO server ok %s", dest.ascii() );
kdebug( KDEBUG_INFO, 0, "IO server ok %s", dest.ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : connected to a server" );
kdebug( KDEBUG_INFO, 0, "connected to a server" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Iterating" );
kdebug( KDEBUG_INFO, 0, "Iterating" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Looping" );
kdebug( KDEBUG_INFO, 0, "Looping" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Executing %s", (*soit).ascii() );
kdebug( KDEBUG_INFO, 0, "Executing %s", (*soit).ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Parsed URL" );
kdebug( KDEBUG_INFO, 0, "Parsed URL" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() );
kdebug( KDEBUG_INFO, 0, "Recursive 1 %s", dest.ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Recursive ok %s", dest.ascii() );
kdebug( KDEBUG_INFO, 0, "Recursive ok %s", dest.ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Job started ok %s", dest.ascii() );
kdebug( KDEBUG_INFO, 0, "Job started ok %s", dest.ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Destinations ok %s", dest.data() );
kdebug( KDEBUG_INFO, 0, "Destinations ok %s", dest.data() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Created directories %s", dest.data() );
kdebug( KDEBUG_INFO, 0, "Created directories %s", dest.data() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug("################# COULD NOT PUT %d",currentError);
kdebug( KDEBUG_ERROR, 0, "################# COULD NOT PUT %d",currentError);
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Offset = %ld", offset );
kdebug( KDEBUG_INFO, 0, "Offset = %ld", offset );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() );
kdebug( KDEBUG_INFO, 0, "Opening %s", (*fit).m_strAbsSource.ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Copied files %s", dest.data() );
kdebug( KDEBUG_INFO, 0, "Copied files %s", dest.data() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : URL is ok " );
kdebug( KDEBUG_INFO, 0, "URL is ok " );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Executing %s", _url );
kdebug( KDEBUG_INFO, 0, "Executing %s", _url );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() );
kdebug( KDEBUG_INFO, 0, "Adding .part extension to %s", udest_orig.path().ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug("Write Access denied for '%s' %d",udest.path(), errno );
kdebug( KDEBUG_ERROR, 0, "Write Access denied for '%s' %d",udest.path(), errno );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Offset = %ld", offset );
kdebug( KDEBUG_INFO, 0, "Offset = %ld", offset );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() );
kdebug( KDEBUG_ERROR, 0, "########## COULD NOT WRITE %s", udest.path().ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Checking %s", (*soit).ascii() );
kdebug( KDEBUG_INFO, 0, "Checking %s", (*soit).ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : All URLs ok" );
kdebug( KDEBUG_INFO, 0, "All URLs ok" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Iterating" );
kdebug( KDEBUG_INFO, 0, "Iterating" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Looping" );
kdebug( KDEBUG_INFO, 0, "Looping" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Executing %s", (*soit).ascii() );
kdebug( KDEBUG_INFO, 0, "Executing %s", (*soit).ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Parsed URL" );
kdebug( KDEBUG_INFO, 0, "Parsed URL" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Recursive ok" );
kdebug( KDEBUG_INFO, 0, "Recursive ok" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Deleting file %s", filename.ascii() );
kdebug( KDEBUG_INFO, 0, "Deleting file %s", filename.ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() );
kdebug( KDEBUG_INFO, 0, "Deleting directory %s", dirname.ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() );
kdebug( KDEBUG_INFO, 0, "########## RECURSIVE LISTING %s", p.ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : not a dir" );
kdebug( KDEBUG_INFO, 0, "not a dir" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() );
kdebug( KDEBUG_INFO, 0, "########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : ##Listing" );
kdebug( KDEBUG_INFO, 0, "##Listing" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : #%s", e->name.ascii() );
kdebug( KDEBUG_INFO, 0, "#%s", e->name.ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() );
kdebug( KDEBUG_INFO, 0, "ppending '%s' '%s'", p2.ascii(), tmp.ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : =============== LIST %s ===============", _url );
kdebug( KDEBUG_INFO, 0, "=============== LIST %s ===============", _url );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : Listing %s", e->name.ascii() );
kdebug( KDEBUG_INFO, 0, "Listing %s", e->name.ascii() );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : ============= COMPLETED LIST ============" );
kdebug( KDEBUG_INFO, 0, "============= COMPLETED LIST ============" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" );
kdebug( KDEBUG_INFO, 0, "============= COMPLETED LIST 2 ============" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : =============== BYE ===========" );
kdebug( KDEBUG_INFO, 0, "=============== BYE ===========" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : =============== slotTestDir ==============" );
kdebug( KDEBUG_INFO, 0, "=============== slotTestDir ==============" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : =========== ERROR ========" );
kdebug( KDEBUG_INFO, 0, " =========== ERROR ========" );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url );
kdebug( KDEBUG_INFO, 0, "========== DONE DIR ========= %s", _url );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url );
kdebug( KDEBUG_INFO, 0, "========== DONE FILE ========= %s", _url );
int check( KIOConnection *_con );extern "C" { void sigalrm_handler(int);}int main( int, char ** ){ signal(SIGCHLD, KIOProtocol::sigchld_handler); signal(SIGSEGV, KIOProtocol::sigsegv_handler); // KProtocolManager manager; qDebug( "kio_ftp : Starting"); KInstance instance( "kio_ftp" ); KIOConnection parent( 0, 1 ); FtpProtocol ftp( &parent ); ftp.dispatchLoop(); qDebug( "kio_ftp : Done" );}/* * We'll use an alarm that will set this flag when transfer has timed out */char sigbreak = 0;void sigalrm_handler(int){ sigbreak = 1;} void setup_alarm(unsigned int timeout){ sigbreak = 0; alarm(timeout); signal(SIGALRM, sigalrm_handler);}FtpProtocol::FtpProtocol( KIOConnection *_conn ) : KIOProtocol( _conn ){ m_cmd = CMD_NONE; m_bIgnoreJobErrors = false; m_bCanResume = true; // most of ftp servers support resuming ?}void FtpProtocol::slotMkdir( const char *_url, int _mode ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in mkdir command" ); m_cmd = CMD_NONE; return; } // Connect to the ftp server if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry* e = ftp.ftpStat( usrc ); if ( !e ) { if ( !ftp.ftpMkdir( usrc.path() ) ) { error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else { // set the desired attributes for dir if ( _mode != -1 ) if ( !ftp.ftpChmod( usrc.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } finished(); ftp.ftpDisconnect(); return; } } if ( S_ISDIR( e->type ) ) { // !!! ok ? error( ERR_DOES_ALREADY_EXIST, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } error( ERR_COULD_NOT_MKDIR, strdup(_url) ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return;}void FtpProtocol::slotCopy( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false );}void FtpProtocol::slotCopy( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true );}void FtpProtocol::slotMove( QStringList& _source, const char *_dest ){ doCopy( _source, _dest, false, true );}void FtpProtocol::slotMove( const char* _source, const char *_dest ){ QStringList lst; lst.append( _source ); doCopy( lst, _dest, true, true );}void FtpProtocol::doCopy( QStringList& _source, const char *_dest, bool _rename, bool _move ){ if ( _rename ) assert( _source.count() == 1 ); qDebug( "kio_ftp : Making copy to %s", _dest ); // Check whether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, (*soit) ); m_cmd = CMD_NONE; return; } if ( usrc.host().isEmpty() ) { error( ERR_MALFORMED_URL, (*soit) ); // !!! create a new error type ? m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok %s", _dest ); // Make a copy of the parameter. if we do IPC calls from here, then we overwrite // our argument. This is tricky! ( but saves memory and speeds things up ) QString dest = _dest; // Check wellformedness of the destination KURL udest( dest ); if ( udest.isMalformed() ) { error( ERR_MALFORMED_URL, dest ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : Dest ok %s", dest.ascii() ); // Find IO server for destination QString exec = KProtocolManager::self().executable( udest.protocol() ); if ( exec.isEmpty() ) { error( ERR_UNSUPPORTED_PROTOCOL, udest.protocol() ); m_cmd = CMD_NONE; return; } // Is the right most protocol a filesystem protocol ? if ( KProtocolManager::self().outputType( udest.protocol() ) != KProtocolManager::T_FILESYSTEM ) { error( ERR_PROTOCOL_IS_NOT_A_FILESYSTEM, udest.protocol() ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : IO server ok %s", dest.ascii() ); // Connect to the ftp server KURL usrc( _source.first() ); if ( ! ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : connected to a server" ); // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( usrc.hasUser() ) { b_user = true; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc( *soit ); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, _rename ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to copy size += s; } qDebug( "kio_ftp : Recursive 1 %s", dest.ascii() ); /* // Check wether we do not copy a directory in itself or one of its subdirectories struct stat buff2; if ( udest.isLocalFile() && stat( udest.path(), &buff2 ) == 0 ) { bool b_error = false; for( soit = _source.begin(); soit != _source.end(); ++soit ) { KURL usrc( *soit ); struct stat buff1; // Can we stat both the source, too ? ( Should always be the case ) if ( stat( usrc.path(), &buff1 ) == 0 ) { bool b_error = false; // Are source and dest equal ? => error if ( buff1.st_ino == buff2.st_ino ) b_error = true; } } if ( !b_error ) { // Iterate over all subdirectories QValueList<CopyDir>::iterator it = dirs.begin(); for( ; it != dirs.end() && !b_error; it++ ) if ( buff2.st_ino == (*it).m_ino ) b_error = true; } // Do we have a cylic copy now ? => error if ( b_error ) { error( ERR_CYCLIC_COPY, *soit ); m_cmd = CMD_NONE; return; } } */ qDebug( "kio_ftp : Recursive ok %s", dest.ascii() ); m_cmd = CMD_GET; // Start a server for the destination protocol KIOSlave slave( exec ); if ( slave.pid() == -1 ) { error( ERR_CANNOT_LAUNCH_PROCESS, exec ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Put a protocol on top of the job FtpIOJob job( &slave, this ); qDebug( "kio_ftp : Job started ok %s", dest.ascii() ); // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( files.count() ); totalDirs( dirs.count() ); int processed_files = 0; int processed_dirs = 0; int processed_size = 0; // Replace the relative destinations with absolut destinations // by prepending the destinations path QString tmp1 = udest.path( 1 ); // Strip '/' QString tmp1_stripped = udest.path( -1 ); QValueList<CopyDir>::Iterator dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { QString tmp2 = (*dit).m_strRelDest; if ( _rename ) (*dit).m_strRelDest = tmp1_stripped; else (*dit).m_strRelDest = tmp1; (*dit).m_strRelDest += tmp2; } QValueList<Copy>::Iterator fit = files.begin(); for( ; fit != files.end(); fit++ ) { QString tmp2 = (*fit).m_strRelDest; if ( _rename ) // !!! && (*fit).m_strRelDest == "" ) (*fit).m_strRelDest = tmp1_stripped; else (*fit).m_strRelDest = tmp1; (*fit).m_strRelDest += tmp2; } qDebug( "kio_ftp : Destinations ok %s", dest.data() ); /***** * Make directories *****/ m_bIgnoreJobErrors = true; bool overwrite_all = false; bool auto_skip = false; bool resume_all = false; QStringList skip_list, overwrite_list; // Create all directories dit = dirs.begin(); for( ; dit != dirs.end(); dit++ ) { // Repeat until we got no error do { job.clearError(); KURL ud( udest ); ud.setPath( (*dit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is d a subdirectory of *sit ? if ( *sit==d ) skip = true; if ( skip ) continue; // Is this URL on the overwrite list ? bool overwrite = false; QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if ( *oit == d ) overwrite = true; if ( overwrite ) continue; // Tell what we are doing makingDir( d.data() ); // qDebug( "kio_ftp : Making remote dir %s", d ); // Create the directory job.mkdir( d.data(), (*dit).m_access ); while( !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { // Can we prompt the user and ask for a solution ? if ( /* m_bGUI && */ job.errorId() == ERR_DOES_ALREADY_EXIST ) { QString old_path = udest.path( 1 ); QString old_url = udest.url( 1 ); // Should we skip automatically ? if ( auto_skip ) { job.clearError(); // We dont want to copy files in this directory, so we put it on the skip list. skip_list.append( old_url ); continue; } else if ( overwrite_all ) { job.clearError(); continue; } /* RenameDlg_Mode m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE ); if ( dirs.count() > 1 ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE ); */ RenameDlg_Mode m = (RenameDlg_Mode)( M_MULTI | M_SKIP | M_OVERWRITE ); QString tmp2 = udest.url(), n; RenameDlg_Result r = open_RenameDlg( (*dit).m_strAbsSource, tmp2, m, n ); if ( r == R_CANCEL ) { ftp.ftpDisconnect(); error( ERR_USER_CANCELED, "" ); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); // The new path with trailing '/' QString tmp3 = u.path( 1 ); /////// // Replace old path with tmp3 /////// QValueList<CopyDir>::Iterator dit2 = dit; // Change the current one and strip the trailing '/' (*dit2).m_strRelDest = u.path( -1 ); // Change the name of all subdirectories dit2++; for( ; dit2 != dirs.end(); dit2++ ) if ( strncmp((*dit2).m_strRelDest, old_path, old_path.length())==0 ) (*dit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Change all filenames QValueList<Copy>::Iterator fit2 = files.begin(); for( ; fit2 != files.end(); fit2++ ) if ( strncmp( (*fit2).m_strRelDest, old_path, old_path.length() ) == 0 ) (*fit2).m_strRelDest.replace( 0, old_path.length(), tmp3 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Skip all files and directories that start with 'old_url' skip_list.append( old_url ); // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { // Dont bother for subdirectories overwrite_list.append( old_url ); // Clear the error => The current command is not repeated => we will // overwrite every file in this directory or any of its subdirectories job.clearError(); } else if ( r == R_OVERWRITE_ALL ) { job.clearError(); overwrite_all = true; } else assert( 0 ); } // No need to ask the user, so raise an error else { error( job.errorId(), job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); processedDirs( ++processed_dirs ); } qDebug( "kio_ftp : Created directories %s", dest.data() ); /***** * Server check - supports resuming ? *****/ if ( !ftp.ftpResume( 0 ) ) m_bCanResume = false; /***** * Copy files *****/ time_t t_start = time( 0L ); time_t t_last = t_start; fit = files.begin(); for( ; fit != files.end(); fit++ ) { bool overwrite = false; bool skip_copying = false; bool resume = false; unsigned long offset = 0; // Repeat until we got no error do { job.clearError(); KURL ud( dest ); ud.setPath( (*fit).m_strRelDest); QString d = ud.url(); // Is this URL on the skip list ? bool skip = false; QStringList::Iterator sit = skip_list.begin(); for( ; sit != skip_list.end() && !skip; sit++ ) // Is 'd' a file in directory '*sit' or one of its subdirectories ? if ( *sit==d ) skip = true; if ( skip ) continue; // emit sigCanResume( m_bCanResume ) canResume( m_bCanResume ); QString realpath = "ftp:"; realpath += (*fit).m_strAbsSource; copyingFile( realpath.ascii(), d.ascii() ); // qDebug( "kio_ftp : Writing to %s", d ); // Is this URL on the overwrite list ? QStringList::Iterator oit = overwrite_list.begin(); for( ; oit != overwrite_list.end() && !overwrite; oit++ ) if (*oit==d) overwrite = true; // implicitly set permissions rw-r--r-- for anonymous ftp int md = -1; // but when it's not anonymous ftp, set permissions as in original source if ( b_user ) md = (*fit).m_access; job.put( d, md, overwrite_all || overwrite, resume_all || resume, (*fit).m_size ); while( !job.isReady() && !job.hasFinished() ) job.dispatch(); // Did we have an error ? if ( job.hasError() ) { int currentError = job.errorId(); qDebug("################# COULD NOT PUT %d",currentError); // if ( /* m_bGUI && */ job.errorId() == ERR_WRITE_ACCESS_DENIED ) if ( /* m_bGUI && */ currentError != ERR_DOES_ALREADY_EXIST && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); skip_copying = true; continue; } QString tmp2 = ud.url(); SkipDlg_Result r; r = open_SkipDlg( tmp2, ( files.count() > 1 ) ); if ( r == S_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == S_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else if ( r == S_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); skip_copying = true; continue; } else assert( 0 ); } // Can we prompt the user and ask for a solution ? else if ( /* m_bGUI && */ currentError == ERR_DOES_ALREADY_EXIST || currentError == ERR_DOES_ALREADY_EXIST_FULL ) { // Should we skip automatically ? if ( auto_skip ) { job.clearError(); continue; } RenameDlg_Result r; QString n; if ( KProtocolManager::self().autoResume() && m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) { r = R_RESUME_ALL; } else { RenameDlg_Mode m; // ask for resume only if transfer can be resumed and if it is not // already fully downloaded if ( files.count() > 1 ){ if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)(M_MULTI | M_SKIP | M_OVERWRITE); } else { if ( m_bCanResume && currentError != ERR_DOES_ALREADY_EXIST_FULL ) m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE | M_RESUME); else m = (RenameDlg_Mode)( M_SINGLE | M_OVERWRITE); } QString tmp2 = ud.url(); r = open_RenameDlg( (*fit).m_strAbsSource, tmp2, m, n ); } if ( r == R_CANCEL ) { error( ERR_USER_CANCELED, "" ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } else if ( r == R_RENAME ) { KURL u( n ); // The Dialog should have checked this. if ( u.isMalformed() ) assert( 0 ); renamed( u.path( -1 ) ); // emit signal // Change the destination name of the current file (*fit).m_strRelDest = u.path( -1 ); // Dont clear error => we will repeat the current command } else if ( r == R_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); } else if ( r == R_AUTO_SKIP ) { // Clear the error => The current command is not repeated => skipped job.clearError(); auto_skip = true; } else if ( r == R_OVERWRITE ) { overwrite = true; // Dont clear error => we will repeat the current command } else if ( r == R_OVERWRITE_ALL ) { overwrite_all = true; // Dont clear error => we will repeat the current command } else if ( r == R_RESUME ) { resume = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else if ( r == R_RESUME_ALL ) { resume_all = true; offset = getOffset( ud.url() ); // Dont clear error => we will repeat the current command } else assert( 0 ); } // No need to ask the user, so raise an error else { error( currentError, job.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } } while( job.hasError() ); if ( skip_copying ) continue; // add the offset to processed size if ( offset > 0 ) { processed_size += offset; qDebug( "kio_ftp : Offset = %ld", offset ); } KURL tmpurl( "ftp:/" ); tmpurl.setPath( (*fit).m_strAbsSource ); qDebug( "kio_ftp : Opening %s", (*fit).m_strAbsSource.ascii() ); if ( !ftp.ftpOpen( tmpurl, Ftp::READ, offset ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // we want to know the processed size even before we read something // especially in case we overwrite file => this cleans size to zero processedSize( processed_size ); char buffer[ 4096 ]; int n; int read_size = 0; do { setup_alarm( KProtocolManager::self().readTimeout() ); // start timeout n = ftp.read( buffer, 2048 ); // !!! slow down loop for local testing // for ( int tmpi = 0; tmpi < 1000000; tmpi++ ) ; job.data( buffer, n ); processed_size += n; read_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( read_size / ( t - t_start ) ); t_last = t; } // Check parent while ( check( connection() ) ) dispatch(); // Check for error messages from slave while ( check( &slave ) ) job.dispatch(); // An error ? if ( job.hasFinished() ) { ftp.ftpDisconnect(); ftp.ftpClose(); finished(); m_cmd = CMD_NONE; return; } } while ( n > 0 ); job.dataEnd(); ftp.ftpClose(); while( !job.hasFinished() ) job.dispatch(); time_t t = time( 0L ); processedSize( processed_size ); if ( t - t_start >= 1 ) { speed( read_size / ( t - t_start ) ); t_last = t; } processedFiles( ++processed_files ); } qDebug( "kio_ftp : Copied files %s", dest.data() ); // slotDel() handles disconnecting by itself if ( _move ) { slotDel( _source ); } else ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGet( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in get command" ); m_cmd = CMD_NONE; return; } if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } FtpEntry *e = ftp.ftpStat( usrc ); if ( !e ) { error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } /* if ( S_ISDIR( e->type ) ) { error( ERR_IS_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; }*/ m_cmd = CMD_GET; if ( !ftp.open( usrc, Ftp::READ ) ) { error( ERR_CANNOT_OPEN_FOR_READING, strdup(_url) ); m_cmd = CMD_NONE; return; } ready(); gettingFile( _url ); totalSize( e->size ); int processed_size = 0; time_t t_start = time( 0L ); time_t t_last = t_start; char buffer[ 2048 ]; while( !ftp.atEOF() ) { int n = ftp.read( buffer, 2048 ); data( buffer, n ); processed_size += n; time_t t = time( 0L ); if ( t - t_last >= 1 ) { processedSize( processed_size ); speed( processed_size / ( t - t_start ) ); t_last = t; } } dataEnd(); ftp.ftpClose(); processedSize( e->size ); time_t t = time( 0L ); if ( t - t_start >= 1 ) speed( processed_size / ( t - t_start ) ); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotGetSize( const char* _url ) { m_cmd = CMD_GET_SIZE; // Check wether URL is wellformed KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, _url ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in getSize command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : URL is ok " ); if ( !ftp.ftpConnect( usrc ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; return; } // Get a list of all source files and directories QValueList<Copy> files; QValueList<CopyDir> dirs; qDebug( "kio_ftp : Executing %s", _url ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), files, dirs, false ) ) == -1 ) { ftp.ftpDisconnect(); // Error message is already sent m_cmd = CMD_NONE; return; } // Tell our client what we 'r' gonna do totalSize( s ); ftp.ftpDisconnect(); finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotPut( const char *_url, int _mode, bool _overwrite, bool _resume, int _size ){ QString url_orig = _url; QString url_part = url_orig + ".part"; KURL udest_orig( url_orig ); KURL udest_part( url_part ); bool m_bMarkPartial = KProtocolManager::self().markPartial(); if ( udest_orig.isMalformed() ) { error( ERR_MALFORMED_URL, url_orig ); m_cmd = CMD_NONE; finished(); return; } if ( strcmp( udest_orig.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file as as destination in put command" ); m_cmd = CMD_NONE; finished(); return; } // Connect to the ftp server, only if we are not connected // this prevents connecting twice in recursive copying ( mkdir connects first time ) if ( !ftp.isConnected() ) if ( !ftp.ftpConnect( udest_orig ) ) { error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } m_cmd = CMD_PUT; // Find out, whether we are logged anonymously or not // Authorization has been already checked with ftp.ftpConnect( usrc ) bool b_user = false; if ( udest_orig.hasUser() ) b_user = true; FtpEntry* e; if ( ( e = ftp.ftpStat( udest_orig ) ) ) { // if original file exists, but we are using mark partial -> rename it to XXX.part if ( m_bMarkPartial ) ftp.ftpRename( udest_orig.path(), udest_part.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } else if ( ( e = ftp.ftpStat( udest_part ) ) ) { // if file with extension .part exists but we are not using mark partial // -> rename XXX.part to original name if ( ! m_bMarkPartial ) ftp.ftpRename( udest_part.path(), udest_orig.path() ); if ( !_overwrite && !_resume ) { if ( e->size == _size ) error( ERR_DOES_ALREADY_EXIST_FULL, udest_orig.path() ); else error( ERR_DOES_ALREADY_EXIST, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } KURL udest; // if we are using marking of partial downloads -> add .part extension if ( m_bMarkPartial ) { qDebug( "kio_ftp : Adding .part extension to %s", udest_orig.path().ascii() ); udest = udest_part; } else udest = udest_orig; /* if ( access( udest.path(), W_OK ) == -1 ) { qDebug("Write Access denied for '%s' %d",udest.path(), errno ); error( ERR_WRITE_ACCESS_DENIED, url ); m_cmd = CMD_NONE; finished(); return; } */ unsigned long offset = 0; // set the mode according to offset if ( _resume ) { offset = e->size; qDebug( "kio_ftp : Offset = %ld", offset ); } if ( !ftp.ftpOpen( udest, Ftp::WRITE, offset ) ) { qDebug( "kio_ftp : ####################### COULD NOT WRITE %s", udest.path().ascii() ); error( ftp.error(), ftp.errorText() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } // We are ready for receiving data ready(); // Loop until we got 'dataEnd' while ( m_cmd == CMD_PUT && dispatch() ); ftp.ftpClose(); // check order !!! ftp.ftpDisconnect( true ); if ( (e = ftp.ftpStat( udest )) ) { if ( e->size == _size ) { // after full download rename the file back to original name if ( m_bMarkPartial ) { cerr << "kio_ftp renaming\n"; if ( !ftp.ftpRename( udest.path(), udest_orig.path() ) ) { error( ERR_CANNOT_RENAME, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } // do chmod only after full download if ( _mode != -1 ) { cerr << "kio_ftp chmoding\n"; // set the desired attributes for dir !!! how ? if ( !ftp.ftpChmod( udest_orig.path(), _mode ) ) { error( ERR_CANNOT_CHMOD, udest_orig.path() ); ftp.ftpDisconnect( true ); m_cmd = CMD_NONE; finished(); return; } } } // if the size is less then minimum -> delete the file else if ( e->size < KProtocolManager::self().minimumKeepSize() ) { ftp.ftpDelete( udest.path() ); } } ftp.ftpDisconnect( true ); // We have done our job => finish finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotDel( QStringList& _source ){ // Check wether the URLs are wellformed QStringList::Iterator soit = _source.begin(); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Checking %s", (*soit).ascii() ); KURL usrc( *soit ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, *soit); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error(ERR_INTERNAL,"kio_ftp got non ftp file in delete command" ); m_cmd = CMD_NONE; return; } } qDebug( "kio_ftp : All URLs ok" ); // Get a list of all source files and directories QValueList<Copy> fs; QValueList<CopyDir> ds; int size = 0; qDebug( "kio_ftp : Iterating" ); soit = _source.begin(); qDebug( "kio_ftp : Looping" ); for( ; soit != _source.end(); ++soit ) { qDebug( "kio_ftp : Executing %s", (*soit).ascii() ); KURL usrc(*soit); qDebug( "kio_ftp : Parsed URL" ); // Did an error occur ? int s; if ( ( s = listRecursive( usrc.path(), fs, ds, false ) ) == -1 ) { // Error message is already sent ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } // Sum up the total amount of bytes we have to delete size += s; } qDebug( "kio_ftp : Recursive ok" ); if ( fs.count() == 1 ) { m_cmd = CMD_DEL; } else { m_cmd = CMD_MDEL; } // Tell our client what we 'r' gonna do totalSize( size ); totalFiles( fs.count() ); totalDirs( ds.count() ); /***** * Delete files *****/ QValueList<Copy>::Iterator fit = fs.begin(); for( ; fit != fs.end(); fit++ ) { QString filename = (*fit).m_strAbsSource; qDebug( "kio_ftp : Deleting file %s", filename.ascii() ); deletingFile( filename ); if ( !ftp.ftpDelete( filename ) ) // !!! use unlink ? { error( ERR_CANNOT_DELETE, filename ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } /***** * Delete empty directories *****/ QValueList<CopyDir>::Iterator dit = ds.begin(); for( ; dit != ds.end(); dit++ ) { QString dirname = (*dit).m_strAbsSource; qDebug( "kio_ftp : Deleting directory %s", dirname.ascii() ); deletingFile( dirname ); if ( !ftp.ftpRmdir( dirname ) ) { error( ERR_COULD_NOT_RMDIR, dirname ); ftp.ftpDisconnect(); m_cmd = CMD_NONE; return; } } finished(); m_cmd = CMD_NONE;}void FtpProtocol::slotData( void *_p, int _len ){ switch( m_cmd ) { case CMD_PUT: ftp.write( _p, _len ); break; }}void FtpProtocol::slotDataEnd(){ switch( m_cmd ) { case CMD_PUT: m_cmd = CMD_NONE; }}long FtpProtocol::listRecursive( const char *_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs, bool _rename ){ m_bAutoSkip = false; // Check wether we have to copy the complete directory tree beginning by its root. int len = strlen( _path ); while( len >= 1 && _path[ len - 1 ] == '/' ) len--; if ( len == 0 ) { CopyDir c; c.m_strAbsSource = _path; if ( _rename ) c.m_strRelDest = ""; else c.m_strRelDest = ""; c.m_access = S_IRWXU | S_IRWXO | S_IRWXG; c.m_type = S_IFDIR; _dirs.append( c ); return listRecursive2( "/", c.m_strRelDest, _files, _dirs ); } QString p=_path; qDebug( "kio_ftp : ########## RECURSIVE LISTING %s", p.ascii() ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); FtpEntry* e = ftp.ftpStat( tmpurl ); if ( !e ) { error( ERR_DOES_NOT_EXIST, p); return -1; } KURL u( p ); // Should be checked before, but who knows if ( u.isMalformed() ) assert( 0 ); // Is the source not a directory ? => so just copy it and we are done. if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : not a dir" ); QString fname; if ( _rename ) fname = ""; else { fname = u.filename(); // Should be impossible, but who knows ... if ( fname.isEmpty() ) assert( 0 ); } Copy c; c.m_strAbsSource = p; c.m_strRelDest = fname; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); return e->size; } // The source is a directory. So we have to go into recursion here. QString tmp1; if ( _rename ) tmp1 = u.path( 0 ); else { tmp1 = u.directory( true ); tmp1 += "/"; } QString tmp2; if ( _rename ) tmp2 = ""; else tmp2 = u.filename(); CopyDir c; c.m_strAbsSource = p; c.m_strRelDest = tmp2; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); qDebug( "kio_ftp : ########### STARTING RECURSION with %s and %s",tmp1.ascii(), tmp2.ascii() ); return listRecursive2( tmp1, tmp2, _files, _dirs );}long FtpProtocol::listRecursive2( const char *_abs_path, const char *_rel_path, QValueList<Copy>& _files, QValueList<CopyDir>& _dirs ){ long size = 0; cerr << "listRecursive2 " << _abs_path << " " << _rel_path << endl; QString p = _abs_path; p += _rel_path; scanningDir( p ); KURL tmpurl( "ftp:/" ); tmpurl.setPath( p ); if ( !ftp.ftpOpenDir( tmpurl ) ) { if ( m_bAutoSkip ) return 0; SkipDlg_Result result = open_SkipDlg( p, true ); if ( result == S_CANCEL ) { // error( ERR_CANNOT_ENTER_DIRECTORY, p ); return -1; } else if ( result == S_AUTO_SKIP ) m_bAutoSkip = true; return 0; } QStringList recursion; qDebug( "kio_ftp : ##Listing" ); FtpEntry *e; while ( ( e = ftp.readdir() ) != 0L ) { qDebug( "kio_ftp : #%s", e->name.ascii() ); if ( e->name == "." || e->name == ".." ) continue; QString p2 = p; p2 += "/"; p2 += e->name; QString tmp = _rel_path; tmp += "/"; tmp += e->name; if ( !S_ISDIR( e->type ) ) { qDebug( "kio_ftp : Appending '%s' '%s'", p2.ascii(), tmp.ascii() ); Copy c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; c.m_size = e->size; _files.append( c ); size += e->size; } else { CopyDir c; c.m_strAbsSource = p2; c.m_strRelDest = tmp; c.m_access = e->access; c.m_type = e->type; _dirs.append( c ); recursion.append( tmp ); } } if ( !ftp.ftpCloseDir() ) { // error( ERR_COULD_NOT_CLOSEDIR, p ); return -1; } QStringList::Iterator it = recursion.begin(); for( ; it != recursion.end(); ++it ) { long s; if ( ( s = listRecursive2( _abs_path, (*it).ascii(), _files, _dirs ) ) == -1 ) return -1; size += s; } return size;}void FtpProtocol::slotListDir( const char *_url ){ qDebug( "kio_ftp : =============== LIST %s ===============", _url ); KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file in list command" ); m_cmd = CMD_NONE; return; } /* struct stat buff; if ( stat( usrc.path(), &buff ) == -1 ) { error( ERR_DOES_NOT_EXIST, url); m_cmd = CMD_NONE; return; } if ( !S_ISDIR( buff.st_mode ) ) { error( ERR_IS_FILE, url ); m_cmd = CMD_NONE; return; } */ m_cmd = CMD_LIST; if ( !ftp.opendir( usrc ) ) { error( ERR_CANNOT_ENTER_DIRECTORY, strdup(_url) ); m_cmd = CMD_NONE; return; } FtpEntry* e; while ( ( e = ftp.readdir() ) ) { if ( e->name == "." || e->name == ".." ) continue; qDebug( "kio_ftp : Listing %s", e->name.ascii() ); KUDSEntry entry; KUDSAtom atom; atom.m_uds = UDS_NAME; atom.m_str = e->name; entry.append( atom ); atom.m_uds = UDS_FILE_TYPE; atom.m_long = e->type; entry.append( atom ); atom.m_uds = UDS_SIZE; atom.m_long = e->size; entry.append( atom ); /* atom.m_uds = UDS_MODIFICATION_TIME; atom.m_long = buff.st_mtime; entry.append( atom ); */ atom.m_uds = UDS_ACCESS; atom.m_long = e->access; entry.append( atom ); atom.m_uds = UDS_USER; atom.m_str = e->owner; entry.append( atom ); atom.m_uds = UDS_GROUP; atom.m_str = e->group; entry.append( atom ); atom.m_uds = UDS_LINK_DEST; atom.m_str = e->link; entry.append( atom ); /* atom.m_uds = UDS_ACCESS_TIME; atom.m_long = buff.st_atime; entry.append( atom ); atom.m_uds = UDS_CREATION_TIME; atom.m_long = buff.st_ctime; entry.append( atom ); */ listEntry( entry ); } qDebug( "kio_ftp : ============= COMPLETED LIST ============" ); ftp.closedir(); qDebug( "kio_ftp : ============= COMPLETED LIST 2 ============" ); m_cmd = CMD_NONE; finished(); qDebug( "kio_ftp : =============== BYE ===========" );}void FtpProtocol::slotTestDir( const char *_url ){ KURL usrc( _url ); if ( usrc.isMalformed() ) { error( ERR_MALFORMED_URL, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( strcmp( usrc.protocol(), "ftp" ) != 0L ) { error( ERR_INTERNAL, "kio_ftp got non ftp file as source in copy command" ); m_cmd = CMD_NONE; return; } qDebug( "kio_ftp : =============== slotTestDir ==============" ); FtpEntry* e; if ( !( e = ftp.stat( usrc ) ) ) { qDebug( "kio_ftp : =========== ERROR ========" ); error( ERR_DOES_NOT_EXIST, strdup(_url) ); m_cmd = CMD_NONE; return; } if ( S_ISDIR( e->type ) ) { qDebug( "kio_ftp : ========== DONE DIR ========= %s", _url ); isDirectory(); } else { qDebug( "kio_ftp : ========== DONE FILE ========= %s", _url ); isFile(); } finished();}void FtpProtocol::jobError( int _errid, const char *_txt ){ if ( !m_bIgnoreJobErrors ) error( _errid, _txt );}/************************************* * * FtpIOJob * *************************************/FtpIOJob::FtpIOJob( KIOConnection *_conn, FtpProtocol *_Ftp ) : KIOJobBase( _conn ){ m_pFtp = _Ftp;} void FtpIOJob::slotError( int _errid, const char *_txt ){ KIOJobBase::slotError( _errid, _txt ); m_pFtp->jobError( _errid, _txt );}/************************************* * * Utilities * *************************************/int check( KIOConnection *_con ){ int err; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 0; fd_set rfds; FD_ZERO( &rfds ); FD_SET( _con->inFD(), &rfds ); again: if ( ( err = select( _con->inFD(), &rfds, 0L, 0L, &tv ) ) == -1 && errno == EINTR ) goto again; // No error and something to read ? if ( err != -1 && err != 0 ) return 1; return 0;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/62a34169840bd1768b62f75500dfb95480b14c0f/main.cc/buggy/kioslave/ftp/main.cc
oldURL.path().remove(0,1), newURL.path().remove(0,1) );
oldPath, newPath );
void DevicesProtocol::rename(KURL const &oldURL, KURL const &newURL, bool) { kdDebug(7126)<<"DevicesProtocol::rename(): old="<<oldURL<<" new="<<newURL<<endl; DCOPRef mountwatcher("kded", "mountwatcher"); DCOPReply reply = mountwatcher.call( "setDisplayName", oldURL.path().remove(0,1), newURL.path().remove(0,1) ); bool ok = false; if ( reply.isValid() ) { ok = reply; } if(ok) { finished(); } else { error(KIO::ERR_DIR_ALREADY_EXIST, i18n("This device name already exists")); }}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/0b55159d0326211e1288577ce0768c6b7fa15fc2/kio_devices.cc/buggy/kioslave/devices/kio_devices.cc
QString path = url.encodedPathAndQuery();
QString path = url.path();
void DevicesProtocol::mountAndRedirect(const KURL& url){ QString device; QString path = url.encodedPathAndQuery(); int i = path.find('/', 1); if (i > 0) { device = path.mid(1, i-1); path = path.mid(i+1); } else { device = path.mid(1); path = QString::null; } QStringList info = deviceInfo(KURL::decode_string(device)); if (info.empty()) { error(KIO::ERR_SLAVE_DEFINED,i18n("Unknown device %1").arg(url.fileName())); return; } QStringList::Iterator it=info.begin(); if (it!=info.end()) { QString device=*it; ++it; if (it!=info.end()) { ++it; ++it; if (it!=info.end()) { QString mp=*it; ++it;++it; if (it!=info.end()) { bool mounted=((*it)=="true"); if (!mounted) { if (!mp.startsWith("file:/")) { error(KIO::ERR_SLAVE_DEFINED,i18n("Device not accessible") +mp); return; } KProcess *proc = new KProcess; *proc << "kio_devices_mounthelper"; *proc << "-m" << url.url(); proc->start(KProcess::Block); int ec = 0; if (proc->normalExit()) ec = proc->exitStatus(); delete proc; if (ec) { error(KIO::ERR_SLAVE_DEFINED,i18n("Device not mounted")); return; } } KURL newUrl(mp); if (!path.isEmpty()) newUrl.cd(path); redirection(newUrl); finished(); return; } } } } error(KIO::ERR_SLAVE_DEFINED,i18n("Illegal data received")); return;}
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/0b55159d0326211e1288577ce0768c6b7fa15fc2/kio_devices.cc/buggy/kioslave/devices/kio_devices.cc
int kdemain( int, char **argv )
int kdemain( int argc, char **argv )
int kdemain( int, char **argv ) { kdDebug(7126)<<"kdemain for devices"<<endl; KInstance instance( "kio_devices" ); DevicesProtocol slave(argv[1],argv[2], argv[3]); slave.dispatchLoop(); return 0; }
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/ede8231f59006c4c061d0b9514327a6b8b76564a/kio_devices.cc/buggy/kioslave/devices/kio_devices.cc
KInstance instance( "kio_devices" );
KApplication::disableAutoDcopRegistration(); KApplication app(argc, argv, "kio_devices", false, true);
int kdemain( int, char **argv ) { kdDebug(7126)<<"kdemain for devices"<<endl; KInstance instance( "kio_devices" ); DevicesProtocol slave(argv[1],argv[2], argv[3]); slave.dispatchLoop(); return 0; }
3322 /local/tlutelli/issta_data/temp/c/2005_temp/2005/3322/ede8231f59006c4c061d0b9514327a6b8b76564a/kio_devices.cc/buggy/kioslave/devices/kio_devices.cc
QGISEXTERN QgsGPXProvider * classFactory(const char *uri) { return new QgsGPXProvider(uri);
QGISEXTERN QgsGPXProvider * classFactory(const QString *uri) { return new QgsGPXProvider(*uri);
QGISEXTERN QgsGPXProvider * classFactory(const char *uri) { return new QgsGPXProvider(uri);}
1753 /local/tlutelli/issta_data/temp/c/2005_temp/2005/1753/3a5382196dd89bb9c2f16d979424811f53d11860/qgsgpxprovider.cpp/clean/providers/gpx/qgsgpxprovider.cpp